add trust_media_from_pk2 method

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2025-03-06 17:31:09 -05:00
parent b072c93964
commit e453c742de
3 changed files with 68 additions and 10 deletions

View File

@@ -1,14 +1,13 @@
use crate::{
error::Error,
search::SearchQuery,
timeline::{Timeline, TimelineTab},
};
use crate::error::Error;
use crate::search::SearchQuery;
use crate::timeline::{Timeline, TimelineTab};
use enostr::{Filter, NoteId, Pubkey};
use nostrdb::{Ndb, Transaction};
use notedeck::{
filter::{self, default_limit},
FilterError, FilterState, NoteCache, RootIdError, RootNoteIdBuf,
};
use notedeck_ui::contacts::contacts_filter;
use serde::{Deserialize, Serialize};
use std::hash::{Hash, Hasher};
use std::{borrow::Cow, fmt::Display};
@@ -666,11 +665,7 @@ impl<'a> ColumnTitle<'a> {
}
fn contact_filter_state(txn: &Transaction, ndb: &Ndb, pk: &Pubkey) -> FilterState {
let contact_filter = Filter::new()
.authors([pk.bytes()])
.kinds([3])
.limit(1)
.build();
let contact_filter = contacts_filter(pk);
let results = ndb
.query(txn, &[contact_filter.clone()], 1)

View File

@@ -0,0 +1,62 @@
use nostrdb::{Filter, Ndb, Note, Transaction};
fn pk1_is_following_pk2(
ndb: &Ndb,
txn: &Transaction,
pk1: &[u8; 32],
pk2: &[u8; 32],
) -> Option<bool> {
let note = get_contacts_note(ndb, txn, pk1)?;
Some(note_follows(note, pk2))
}
pub fn trust_media_from_pk2(
ndb: &Ndb,
txn: &Transaction,
pk1: Option<&[u8; 32]>,
pk2: &[u8; 32],
) -> bool {
pk1.map(|pk| pk1_is_following_pk2(ndb, txn, pk, pk2).unwrap_or(false))
.unwrap_or(false)
}
fn get_contacts_note<'a>(ndb: &'a Ndb, txn: &'a Transaction, user: &[u8; 32]) -> Option<Note<'a>> {
Some(
ndb.query(txn, &[contacts_filter(user)], 1)
.ok()?
.first()?
.note
.clone(),
)
}
pub fn contacts_filter(pk: &[u8; 32]) -> Filter {
Filter::new().authors([pk]).kinds([3]).limit(1).build()
}
fn note_follows(contacts_note: Note<'_>, pk: &[u8; 32]) -> bool {
for tag in contacts_note.tags() {
if tag.count() < 2 {
continue;
}
let Some(t) = tag.get_str(0) else {
continue;
};
if t != "p" {
continue;
}
let Some(author) = tag.get_id(1) else {
continue;
};
if *pk == *author {
return true;
}
}
false
}

View File

@@ -1,6 +1,7 @@
pub mod anim;
pub mod colors;
pub mod constants;
pub mod contacts;
pub mod gif;
pub mod icons;
pub mod images;