notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit e453c742deb8d118b5c4bab7986a247e153d299f
parent b072c93964af313735bdd09680c90fc36864043e
Author: kernelkind <kernelkind@gmail.com>
Date:   Thu,  6 Mar 2025 17:31:09 -0500

add `trust_media_from_pk2` method

Signed-off-by: kernelkind <kernelkind@gmail.com>

Diffstat:
Mcrates/notedeck_columns/src/timeline/kind.rs | 15+++++----------
Acrates/notedeck_ui/src/contacts.rs | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mcrates/notedeck_ui/src/lib.rs | 1+
3 files changed, 68 insertions(+), 10 deletions(-)

diff --git a/crates/notedeck_columns/src/timeline/kind.rs b/crates/notedeck_columns/src/timeline/kind.rs @@ -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) diff --git a/crates/notedeck_ui/src/contacts.rs b/crates/notedeck_ui/src/contacts.rs @@ -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 +} diff --git a/crates/notedeck_ui/src/lib.rs b/crates/notedeck_ui/src/lib.rs @@ -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;