Skip to main content

iris_chat_protocol/protocol_engine/
engine_message_authors.rs

1use super::*;
2
3impl ProtocolEngine {
4    /// Message authors of the accepted owners, most important first: the live
5    /// ratchet keys of active sessions, then of inactive ones, then keys that
6    /// only still have missed messages from older ratchet periods. A
7    /// subscription that reaches a relay limit drops from the end.
8    pub fn message_author_pubkeys_by_priority<F>(&self, accept_owner: F) -> Vec<PublicKey>
9    where
10        F: Fn(PublicKey) -> bool,
11    {
12        let snapshot = self.session_manager.snapshot();
13        let mut active = Vec::new();
14        let mut inactive = Vec::new();
15        for user in &snapshot.users {
16            let Ok(owner) = PublicKey::parse(&user.owner_pubkey.to_string()) else {
17                continue;
18            };
19            if !accept_owner(owner) {
20                continue;
21            }
22            for device in &user.devices {
23                if !device.authorized
24                    || device.is_stale
25                    || !self.owner_device_binding_is_verified_in_roster(
26                        user.owner_pubkey,
27                        device.device_pubkey,
28                        user.roster.as_ref(),
29                    )
30                {
31                    continue;
32                }
33                active.extend(&device.active_session);
34                inactive.extend(&device.inactive_sessions);
35            }
36        }
37        let sessions = || active.iter().chain(&inactive).copied();
38        let mut ranked = nostr_double_ratchet::prioritized_message_authors(sessions());
39        // The library ranks current-format sessions. Keep every other author
40        // the unordered projection subscribes to, after them.
41        let mut all = HashSet::new();
42        for session in sessions() {
43            collect_expected_sender_pubkeys(session, &mut all);
44        }
45        for author in &ranked {
46            all.remove(author);
47        }
48        let mut rest = all.into_iter().collect::<Vec<_>>();
49        rest.sort_unstable();
50        ranked.extend(rest);
51        ranked
52    }
53}