Skip to main content

vector_core/
requests.rs

1//! DM requests: a GUI shields its user from someone who wrote first until they answer or
2//! accept. Off unless a GUI turns it on, so SDK clients and bots take every DM and invite
3//! as they always have.
4
5use std::sync::atomic::{AtomicBool, Ordering};
6
7static SHIELD: AtomicBool = AtomicBool::new(false);
8
9/// Called once at startup by a client with a person behind it.
10pub fn enable_shield() {
11    SHIELD.store(true, Ordering::Relaxed);
12}
13
14/// Whether a stranger's wallpaper, attachment probes and blocked invites are held back.
15pub fn shielded() -> bool {
16    SHIELD.load(Ordering::Relaxed)
17}
18
19/// Of these people, the ones a Community we hold counts as a member.
20pub fn in_our_communities<'a>(npubs: impl IntoIterator<Item = &'a str>) -> std::collections::HashSet<String> {
21    let mut wanted: std::collections::HashMap<nostr_sdk::prelude::PublicKey, &str> =
22        npubs.into_iter().filter_map(|npub| Some((nostr_sdk::prelude::PublicKey::parse(npub).ok()?, npub))).collect();
23    let mut found = std::collections::HashSet::new();
24    if wanted.is_empty() {
25        return found;
26    }
27    for community in crate::community::v2::realtime::load_held_v2_cached().communities() {
28        for member in crate::community::v2::service::stored_memberlist(community).unwrap_or_default() {
29            if let Some(npub) = wanted.remove(&member) {
30                found.insert(npub.to_string());
31            }
32        }
33        if wanted.is_empty() {
34            break;
35        }
36    }
37    found
38}
39
40/// Whether this person's pictures wait on their DM request: we never answered or accepted it,
41/// and no Community we hold counts them, where we see them anyway.
42pub async fn pictures_held(npub: &str) -> bool {
43    let request = crate::state::STATE.lock().await.sent_request(npub);
44    request && in_our_communities([npub]).is_empty()
45}
46
47/// Everyone whose pictures wait on their DM request.
48pub async fn held_pictures() -> std::collections::HashSet<String> {
49    let senders: Vec<String> = crate::state::STATE.lock().await.request_senders().into_iter().map(str::to_string).collect();
50    let mutual = in_our_communities(senders.iter().map(String::as_str));
51    senders.into_iter().filter(|npub| !mutual.contains(npub)).collect()
52}
53
54/// The DM requests whose senders show their pictures anyway: a Community we hold counts them.
55pub async fn request_mutuals() -> Vec<String> {
56    let senders: Vec<String> = crate::state::STATE.lock().await.request_senders().into_iter().map(str::to_string).collect();
57    in_our_communities(senders.iter().map(String::as_str)).into_iter().collect()
58}