relay_prefetch.rs (1968B)
1 use enostr::Pubkey; 2 use notedeck::{ 3 Accounts, RelaySelection, RemoteApi, ScopedSubApi, ScopedSubIdentity, SubConfig, SubOwnerKey, 4 }; 5 6 use crate::{ 7 cache::{ConversationCache, ConversationId}, 8 list_fetch_sub_key, list_prefetch_owner_key, 9 nip17::participant_dm_relay_list_filter, 10 }; 11 12 /// Pure builder for the scoped-sub spec used to prefetch one participant relay list. 13 fn participant_relay_prefetch_spec(participant: &Pubkey) -> SubConfig { 14 SubConfig { 15 relays: RelaySelection::AccountsRead, 16 filters: vec![participant_dm_relay_list_filter(participant)], 17 use_transparent: false, 18 } 19 } 20 21 /// Ensures remote prefetch subscriptions for one conversation's participants. 22 pub(crate) fn ensure_conversation_prefetch( 23 remote: &mut RemoteApi<'_>, 24 accounts: &Accounts, 25 cache: &ConversationCache, 26 conversation_id: ConversationId, 27 ) { 28 let Some(conversation) = cache.get(conversation_id) else { 29 return; 30 }; 31 32 ensure_participant_prefetch(remote, accounts, &conversation.metadata.participants); 33 } 34 35 /// Ensures remote prefetch subscriptions for all provided participants. 36 pub(crate) fn ensure_participant_prefetch( 37 remote: &mut RemoteApi<'_>, 38 accounts: &Accounts, 39 participants: &[Pubkey], 40 ) { 41 if participants.is_empty() { 42 return; 43 } 44 45 let account_pk = *accounts.selected_account_pubkey(); 46 let owner = list_prefetch_owner_key(account_pk); 47 let mut scoped_subs = remote.scoped_subs(accounts); 48 ensure_participant_subs(&mut scoped_subs, owner, participants); 49 } 50 51 fn ensure_participant_subs( 52 scoped_subs: &mut ScopedSubApi<'_, '_>, 53 owner: SubOwnerKey, 54 participants: &[Pubkey], 55 ) { 56 for participant in participants { 57 let key = list_fetch_sub_key(participant); 58 let spec = participant_relay_prefetch_spec(participant); 59 let identity = ScopedSubIdentity::account(owner, key); 60 let _ = scoped_subs.ensure_sub(identity, spec); 61 } 62 }