Skip to main content

vector_core/community/
realtime.rs

1//! Realtime Community (Concord) subscription, routing, dispatch, and control-follow.
2//!
3//! The per-event cryptographic engine ([`inbound::process_incoming`]) already lives in core;
4//! this module is the realtime *plumbing* around it — the relay subscription, the pseudonym→channel
5//! route maps, the dispatch of typed [`inbound::IncomingEvent`]s to an [`InboundEventHandler`], and
6//! the control/rekey realtime-follow. It is consumed by [`crate::VectorCore::listen`] so headless
7//! clients (SDK, CLI, bots) get realtime Community delivery through the same handler as DMs.
8
9use std::collections::{HashMap, HashSet};
10use std::sync::{LazyLock, Mutex as StdMutex};
11use std::sync::Arc;
12use std::time::Duration;
13
14use nostr_sdk::prelude::*;
15use tokio::sync::Mutex;
16
17use crate::community::{derive, inbound, roster, service, Channel, CommunityId, Epoch};
18use crate::community::transport::LiveTransport;
19use crate::event_handler::InboundEventHandler;
20use crate::state::SessionGuard;
21use crate::stored_event::event_kind;
22
23/// Realtime channel-follow retry budget: a re-founding publishes the channel rekey under the NEW
24/// root right after the base rekey, so the base-3303-triggered follow can race its propagation.
25const CHANNEL_FOLLOW_MAX_ATTEMPTS: usize = 5;
26const CHANNEL_FOLLOW_BACKOFF_MS: u64 = 700;
27
28/// Current Community subscription id — single subscription scoped to the epoch pseudonyms of
29/// every channel we hold; refreshed on join/leave/rekey.
30static COMMUNITY_SUB_ID: LazyLock<Mutex<Option<SubscriptionId>>> = LazyLock::new(|| Mutex::new(None));
31
32/// Sorted pseudonym set of the CURRENTLY live Community subscription. Lets `refresh_subscription` skip
33/// a redundant unsubscribe+resubscribe when nothing changed (e.g. a metadata/avatar edit, which fires
34/// a control re-fold but doesn't alter the pseudonym set). The relay pool auto-re-applies the existing
35/// subscription on every reconnect, so leaving it in place is strictly more reliable than rebuilding it
36/// (a rebuild that lands mid-reconnect silently fails to register and kills realtime delivery).
37static COMMUNITY_SUB_SET: LazyLock<Mutex<Vec<String>>> = LazyLock::new(|| Mutex::new(Vec::new()));
38
39/// Pool-wide `subscribe` of the community filter, opened alongside the targeted `subscribe_to`. On
40/// Android the targeted sub registers but never streams; the pool-wide one does (it rides the same
41/// auto-managed path the DM sub uses). On desktop the targeted sub streams. We keep BOTH so every
42/// platform gets live delivery; `process_incoming` dedups by outer-event id, so an event seen on both
43/// folds exactly once.
44static COMMUNITY_POOLWIDE_SUB_ID: LazyLock<Mutex<Option<SubscriptionId>>> = LazyLock::new(|| Mutex::new(None));
45
46/// `z` pseudonym (hex) → the Channel it belongs to. Lets the notification loop open an arriving
47/// event against the right channel key (and read its `banned` set for the inbound drop-filter).
48static COMMUNITY_ROUTES: LazyLock<Mutex<HashMap<String, Channel>>> =
49    LazyLock::new(|| Mutex::new(HashMap::new()));
50
51/// Control-plane `z` pseudonym (hex) → community id (hex). Routes a CONTROL edition (3308) or a
52/// base/channel REKEY coordinate (3303) to a realtime control refresh of that community.
53static CONTROL_ROUTES: LazyLock<Mutex<HashMap<String, String>>> =
54    LazyLock::new(|| Mutex::new(HashMap::new()));
55
56/// Per-community in-flight guard for [`refresh_control`] — one concurrent fold per community.
57static REFRESH_CONTROL_INFLIGHT: LazyLock<StdMutex<HashSet<String>>> =
58    LazyLock::new(|| StdMutex::new(HashSet::new()));
59
60/// The subscription id of the live Community subscription, if any. Lets a notification loop test
61/// whether an arriving event belongs to the Community sub.
62pub async fn subscription_id() -> Option<SubscriptionId> {
63    COMMUNITY_SUB_ID.lock().await.clone()
64}
65
66/// The pool-wide subscription id (the path that streams on Android, where the
67/// targeted `subscribe_to` registers but never delivers). The listen loop must OR
68/// this with [`subscription_id`], else events arriving under the pool-wide sub
69/// match no branch and are silently dropped.
70pub async fn poolwide_subscription_id() -> Option<SubscriptionId> {
71    COMMUNITY_POOLWIDE_SUB_ID.lock().await.clone()
72}
73
74/// Clear all realtime route/subscription state. Call from `swap_session` so a swapped-in account
75/// can't read the prior account's channel keys / banned sets.
76pub async fn clear() {
77    *COMMUNITY_SUB_ID.lock().await = None;
78    *COMMUNITY_POOLWIDE_SUB_ID.lock().await = None;
79    COMMUNITY_SUB_SET.lock().await.clear();
80    COMMUNITY_ROUTES.lock().await.clear();
81    CONTROL_ROUTES.lock().await.clear();
82    REFRESH_CONTROL_INFLIGHT.lock().unwrap_or_else(|e| e.into_inner()).clear();
83}
84
85/// Rebuild ONLY the in-memory route maps from the persisted communities, WITHOUT touching the live
86/// relay subscription. Refreshes each cached `Channel` (incl. its `banned` set) so a received
87/// ban/unban takes live effect without a full resubscribe. Returns the pseudonyms + relays a caller
88/// also resubscribing needs.
89pub async fn rebuild_routes() -> (Vec<String>, HashSet<String>) {
90    let mut routes: HashMap<String, Channel> = HashMap::new();
91    let mut control_routes: HashMap<String, String> = HashMap::new();
92    let mut pseudonyms: Vec<String> = Vec::new();
93    let mut relays: HashSet<String> = HashSet::new();
94
95    if let Ok(ids) = crate::db::community::list_community_ids() {
96        for id in ids {
97            if let Ok(Some(community)) = crate::db::community::load_community(&id) {
98                for r in &community.relays {
99                    relays.insert(r.clone());
100                }
101                for ch in &community.channels {
102                    // Subscribe to EVERY held epoch pseudonym (not just the head) so a straggler posting
103                    // under a retained older epoch still arrives in realtime; the inbound router opens each
104                    // against the channel's full keyset.
105                    for (epoch, key) in ch.read_epoch_keys() {
106                        let pseudonym = derive::channel_pseudonym(&key, &ch.id, epoch).to_hex();
107                        pseudonyms.push(pseudonym.clone());
108                        routes.insert(pseudonym, ch.clone());
109                    }
110                    // The NEXT channel-rekey coordinate (3303) under the CURRENT server root — follow a
111                    // channel rotation event-driven the instant it lands.
112                    let next_chan = derive::rekey_pseudonym(
113                        &community.server_root_key, &ch.id, Epoch(ch.epoch.0 + 1),
114                    ).to_hex();
115                    pseudonyms.push(next_chan.clone());
116                    control_routes.insert(next_chan, community.id.to_hex());
117                }
118                // Control-plane pseudonym at the current server-root epoch (banlist/roles/metadata/invites).
119                let ctrl = roster::control_pseudonym(
120                    &community.server_root_key, &community.id, community.server_root_epoch,
121                );
122                pseudonyms.push(ctrl.clone());
123                control_routes.insert(ctrl, community.id.to_hex());
124                // The NEXT base re-founding coordinate (3303) — follow a privatize / private-ban in realtime.
125                let next_base = derive::base_rekey_pseudonym(
126                    &community.server_root_key, &community.id,
127                    Epoch(community.server_root_epoch.0 + 1),
128                ).to_hex();
129                pseudonyms.push(next_base.clone());
130                control_routes.insert(next_base, community.id.to_hex());
131            }
132        }
133    }
134
135    *COMMUNITY_ROUTES.lock().await = routes;
136    *CONTROL_ROUTES.lock().await = control_routes;
137    (pseudonyms, relays)
138}
139
140/// The coalesced control-probe coordinate set: every held v1 community's
141/// control pseudonym + next-base-rekey + next-channel-rekey `z` coordinate,
142/// mapped back to its community id. A single change-detector fetch over these
143/// (kinds 3308/3303) tells the boot sweep which communities actually have new
144/// control/rotation editions, so the rest skip the per-community catch-up chain.
145/// Returns `(coordinates, coordinate → community_id_hex, relay_union)`.
146pub async fn control_probe_coordinates() -> (Vec<String>, HashMap<String, String>, HashSet<String>) {
147    let mut coords: Vec<String> = Vec::new();
148    let mut map: HashMap<String, String> = HashMap::new();
149    let mut relays: HashSet<String> = HashSet::new();
150    let Ok(ids) = crate::db::community::list_community_ids() else {
151        return (coords, map, relays);
152    };
153    for id in ids {
154        // v1 only — v2 control/rekey planes are author-addressed giftwraps (a
155        // separate, auth-gated probe; see B1b).
156        if matches!(
157            crate::db::community::community_protocol(&id).ok().flatten(),
158            Some(crate::community::ConcordProtocol::V2)
159        ) {
160            continue;
161        }
162        let Ok(Some(community)) = crate::db::community::load_community(&id) else {
163            continue;
164        };
165        let cid = community.id.to_hex();
166        for r in &community.relays {
167            relays.insert(r.clone());
168        }
169        let mut add = |coord: String| {
170            if !map.contains_key(&coord) {
171                coords.push(coord.clone());
172                map.insert(coord, cid.clone());
173            }
174        };
175        add(roster::control_pseudonym(&community.server_root_key, &community.id, community.server_root_epoch));
176        add(derive::base_rekey_pseudonym(&community.server_root_key, &community.id, Epoch(community.server_root_epoch.0 + 1)).to_hex());
177        for ch in &community.channels {
178            add(derive::rekey_pseudonym(&community.server_root_key, &ch.id, Epoch(ch.epoch.0 + 1)).to_hex());
179        }
180    }
181    (coords, map, relays)
182}
183
184/// (Re)build the Community subscription: rebuild the route maps, then open TWO subscriptions over the
185/// same filter — a targeted `subscribe_to` (streams on desktop) and a pool-wide `subscribe` (streams on
186/// Android). `process_incoming` dedups by outer-event id, so overlap folds exactly once.
187pub async fn refresh_subscription(client: &Client) {
188    let (pseudonyms, relays) = rebuild_routes().await;
189
190    // Hold COMMUNITY_SUB_ID across the unsubscribe+subscribe ON PURPOSE: it serializes concurrent
191    // refreshers (Monitor, health-probe reconnect, refresh_control, accept_invite) so they can't
192    // race into a duplicate subscription. Narrowing this lock would reintroduce that double-sub race.
193    let mut new_set = pseudonyms.clone();
194    new_set.sort();
195
196    let mut sub_guard = COMMUNITY_SUB_ID.lock().await;
197    let mut set_guard = COMMUNITY_SUB_SET.lock().await;
198
199    // Idempotent: unchanged pseudonym set + a live sub → keep it (the pool auto-re-applies it across
200    // reconnects, verified). Rebuilding would be pure churn and a rebuild landing mid-reconnect silently
201    // fails to register. rebuild_routes() above already refreshed routes/banlist — all a no-change needs.
202    if sub_guard.is_some() && *set_guard == new_set {
203        return;
204    }
205
206    if let Some(old_id) = sub_guard.take() {
207        client.unsubscribe(&old_id).await;
208    }
209    *set_guard = new_set;
210
211    if pseudonyms.is_empty() {
212        if let Some(old_pw) = COMMUNITY_POOLWIDE_SUB_ID.lock().await.take() {
213            client.unsubscribe(&old_pw).await;
214        }
215        return;
216    }
217
218    // Community events live on the community's relays, which may differ from the user's DM relays.
219    // Add them GOSSIP|PING (warm, but excluded from pool-wide DM/profile ops) and subscribe by TARGET.
220    for r in &relays {
221        let _ = client.pool().add_relay(r.as_str(), crate::community_relay_options()).await;
222    }
223    client.connect().await;
224
225    // Wait (briefly) for at least one community relay to actually CONNECT before subscribing. A
226    // subscribe_to/subscribe issued against a still-connecting relay silently fails to register the
227    // live sub (seen right after the Android bg-sync churn / on create-with-avatar, which fires extra
228    // control re-folds → re-subscribes mid-reconnect). Polling until a socket is live makes the sub
229    // land reliably regardless of churn timing. Dead/slow relays (e.g. a timing-out one) are ignored —
230    // one connected relay is enough to stream.
231    {
232        let wanted: Vec<RelayUrl> = relays.iter().filter_map(|r| RelayUrl::parse(r).ok()).collect();
233        for _ in 0..24 {
234            let pool = client.pool().all_relays().await;
235            let any_live = wanted.iter().any(|u| {
236                pool.get(u).map(|r| r.status() == RelayStatus::Connected).unwrap_or(false)
237            });
238            if any_live {
239                break;
240            }
241            tokio::time::sleep(Duration::from_millis(250)).await;
242        }
243    }
244
245    let filter = Filter::new()
246        .kinds([
247            Kind::Custom(event_kind::COMMUNITY_MESSAGE),
248            Kind::Custom(event_kind::COMMUNITY_REACTION),
249            Kind::Custom(event_kind::COMMUNITY_EDIT),
250            Kind::Custom(event_kind::COMMUNITY_DELETE),
251            Kind::Custom(event_kind::COMMUNITY_PRESENCE),
252            Kind::Custom(event_kind::COMMUNITY_KICK),
253            Kind::Custom(event_kind::COMMUNITY_TYPING),
254            Kind::Custom(event_kind::COMMUNITY_WEBXDC),
255            Kind::Custom(event_kind::COMMUNITY_CONTROL),
256            Kind::Custom(event_kind::COMMUNITY_REKEY),
257        ])
258        .custom_tags(SingleLetterTag::lowercase(Alphabet::Z), pseudonyms)
259        .limit(0);
260
261    // Pool-wide subscribe — the path that streams on Android (replaces any prior one).
262    {
263        let mut pw = COMMUNITY_POOLWIDE_SUB_ID.lock().await;
264        if let Some(old) = pw.take() {
265            client.unsubscribe(&old).await;
266        }
267        if let Ok(out) = client.subscribe(filter.clone(), None).await {
268            *pw = Some(out.val);
269        }
270    }
271    // Targeted subscribe — the path that streams on desktop.
272    if let Ok(output) = client.subscribe_to(relays.iter().cloned(), filter, None).await {
273        *sub_guard = Some(output.val);
274    }
275}
276
277/// Route an arriving Community event: a CONTROL/REKEY edition triggers a realtime control refresh;
278/// any other (message/reaction/edit/delete/presence/typing/webxdc/kick) is opened against its
279/// channel via [`inbound::process_incoming`], persisted, and dispatched to `handler`. `session`
280/// straddles the relay I/O so a mid-flight account swap can't write into the swapped-in account.
281pub async fn dispatch_event(
282    session: &SessionGuard,
283    event: Event,
284    handler: Arc<dyn InboundEventHandler>,
285) {
286    let Some(my_pk) = crate::my_public_key() else { return; };
287    let Some(pseudonym) = event.tags.iter().find_map(|t| {
288        let s = t.as_slice();
289        (s.len() >= 2 && s[0] == "z").then(|| s[1].clone())
290    }) else { return; };
291
292    // A CONTROL edition (3308) or base/channel REKEY (3303) → follow the control plane in realtime.
293    let kind = event.kind.as_u16();
294    if kind == event_kind::COMMUNITY_CONTROL || kind == event_kind::COMMUNITY_REKEY {
295        let community_id = CONTROL_ROUTES.lock().await.get(&pseudonym).cloned();
296        if let Some(community_id) = community_id {
297            if session.is_valid() {
298                // Spawn off the loop — the refresh runs several relay fetches (seconds) and must not
299                // head-of-line-block other event consumption. It self-captures a guard + re-checks.
300                tokio::spawn(refresh_control(community_id, handler.clone()));
301            }
302        }
303        return;
304    }
305
306    let Some(channel) = COMMUNITY_ROUTES.lock().await.get(&pseudonym).cloned() else {
307        return;
308    };
309    if !session.is_valid() {
310        return;
311    }
312
313    let outcome = {
314        let mut state = crate::state::STATE.lock().await;
315        inbound::process_incoming(&mut state, &event, &channel, &my_pk)
316    };
317    let chat_id = channel.id.to_hex();
318    match outcome {
319        Some(inbound::IncomingEvent::NewMessage(mut msg)) => {
320            // Resolve the reply preview (content/npub) from the DB before emitting,
321            // mirroring the DM realtime path. The replied-to message is often an
322            // older one that's persisted but outside the in-memory window; without
323            // this the recipient's live render finds no in-memory target and the
324            // reply shows as a plain message with no context.
325            if !msg.replied_to.is_empty() {
326                let _ = crate::db::events::populate_reply_context(&mut msg).await;
327            }
328            let _ = crate::db::events::save_message(&chat_id, &msg).await;
329            // This is the LIVE stream (limit-0 subscription) — back-paged history arrives via a
330            // separate one-shot batch, never here. So these are always genuinely new; surface them.
331            handler.on_community_message(&chat_id, &msg, true);
332        }
333        Some(inbound::IncomingEvent::Updated { target_id, message, edit_event }) => {
334            // Edits are event-sourced (folded on reload); reactions re-save the message row.
335            if let Some(ev) = edit_event {
336                let mut ev = (*ev).clone();
337                if let Ok(cid) = crate::db::id_cache::get_chat_id_by_identifier(&chat_id) {
338                    ev.chat_id = cid;
339                }
340                let _ = crate::db::events::save_event(&ev).await;
341            } else {
342                let _ = crate::db::events::save_message(&chat_id, &message).await;
343            }
344            handler.on_community_update(&chat_id, &target_id, &message);
345        }
346        Some(inbound::IncomingEvent::Removed { target_id }) => {
347            let _ = crate::db::events::delete_event(&target_id).await;
348            handler.on_community_removed(&chat_id, &target_id);
349        }
350        Some(inbound::IncomingEvent::ReactionRemoved { message_id, reaction_id, message }) => {
351            // Drop the reaction's kind-7 row (save is additive) and refresh the parent's chips.
352            let _ = crate::db::events::delete_event(&reaction_id).await;
353            handler.on_community_update(&chat_id, &message_id, &message);
354        }
355        Some(inbound::IncomingEvent::Presence { npub, joined, event_id, created_at, invited_by, invited_label }) => {
356            handler.on_community_presence(
357                &chat_id, &npub, joined, &event_id, created_at,
358                invited_by.as_deref(), invited_label.as_deref(),
359            );
360        }
361        Some(inbound::IncomingEvent::WebxdcPeer { npub, topic_id, node_addr, event_id, created_at }) => {
362            handler.on_community_webxdc(
363                &chat_id, &npub, &topic_id, node_addr.as_deref(), &event_id, created_at,
364            );
365        }
366        Some(inbound::IncomingEvent::Typing { npub, until }) => {
367            handler.on_community_typing(&chat_id, &npub, until);
368        }
369        Some(inbound::IncomingEvent::Kicked { community_id })
370        | Some(inbound::IncomingEvent::SelfLeft { community_id }) => {
371            // The handler owns teardown (the GUI prunes chats/relays + republishes the list; a
372            // headless consumer can call `teardown_local`). Core only routes + notifies here.
373            handler.on_community_self_removed(&community_id);
374        }
375        None => {}
376    }
377}
378
379/// Realtime control-plane follow: walk a re-founding, fold the control plane (banlist/roles/
380/// metadata/invites), follow channel rekeys, then resubscribe at the new pseudonyms if any epoch
381/// advanced (else just refresh the route maps so the new banlist takes live effect). Mirrors the
382/// Tauri `refresh_community_control` orchestration over the already-core `catch_up_*` primitives.
383pub async fn refresh_control(community_id: String, handler: Arc<dyn InboundEventHandler>) {
384    // Claim the in-flight slot or bail (a concurrent refresh is already folding this community).
385    {
386        let mut inflight = REFRESH_CONTROL_INFLIGHT.lock().unwrap_or_else(|e| e.into_inner());
387        if !inflight.insert(community_id.clone()) {
388            return;
389        }
390    }
391    struct RefreshClaim(String);
392    impl Drop for RefreshClaim {
393        fn drop(&mut self) {
394            REFRESH_CONTROL_INFLIGHT.lock().unwrap_or_else(|e| e.into_inner()).remove(&self.0);
395        }
396    }
397    let _claim = RefreshClaim(community_id.clone());
398
399    let session = SessionGuard::capture();
400    let Some(id_bytes) = hex_to_id32(&community_id) else { return; };
401    let Some(community) = crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten() else { return; };
402    let bt = LiveTransport::with_timeout(Duration::from_secs(20));
403    let pre_server_epoch = community.server_root_epoch.0;
404    let pre_channel_epochs: Vec<(String, u64)> =
405        community.channels.iter().map(|c| (c.id.to_hex(), c.epoch.0)).collect();
406
407    // FOLLOW FIRST: a privatize / private-ban re-founds the base under a NEW epoch + re-anchors the
408    // control plane there, so walk the rotation BEFORE folding control. An AUTHORIZED rotation that
409    // excluded us is a removal → tear down locally.
410    if let Ok(c) = service::catch_up_server_root(&bt, &community).await {
411        if !session.is_valid() { return; }
412        if c.removed { handler.on_community_self_removed(&community_id); return; }
413    }
414    if !session.is_valid() { return; }
415    let community = crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten().unwrap_or(community);
416    let _ = service::fetch_and_apply_control(&bt, &community).await;
417    if !session.is_valid() { return; }
418    // Banned by the just-folded banlist → torn down, nothing more to do.
419    if let Some(c) = crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten() {
420        if service::am_i_banned(&c) {
421            handler.on_community_self_removed(&community_id);
422            return;
423        }
424    }
425    if !session.is_valid() { return; }
426
427    // Walk each channel's rekey chain. A re-founding rotates base AND every channel once; the channel
428    // rekey publishes right after the base rekey, so a single fetch can race propagation — retry with a
429    // short backoff until every channel reaches the expected epoch (next sync is the backstop).
430    let base_delta = crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten()
431        .map(|c| c.server_root_epoch.0).unwrap_or(pre_server_epoch).saturating_sub(pre_server_epoch);
432    for attempt in 0..CHANNEL_FOLLOW_MAX_ATTEMPTS {
433        if !session.is_valid() { return; }
434        let Some(cur) = crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten() else { break; };
435        for ch in &cur.channels {
436            let _ = service::catch_up_channel_rekeys(&bt, &cur, &ch.id).await;
437        }
438        let caught = base_delta == 0 || crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten()
439            .map(|c| c.channels.iter().all(|ch| {
440                let pre = pre_channel_epochs.iter().find(|(id, _)| id == &ch.id.to_hex()).map(|(_, e)| *e).unwrap_or(ch.epoch.0);
441                ch.epoch.0 >= pre.saturating_add(base_delta)
442            }))
443            .unwrap_or(true);
444        if caught { break; }
445        if attempt + 1 < CHANNEL_FOLLOW_MAX_ATTEMPTS {
446            tokio::time::sleep(Duration::from_millis(CHANNEL_FOLLOW_BACKOFF_MS)).await;
447        }
448    }
449    if !session.is_valid() { return; }
450    let community = crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten().unwrap_or(community);
451    let _ = service::retry_pending_read_cut(&bt, &community).await;
452    if !session.is_valid() { return; }
453    let community = crate::db::community::load_community(&CommunityId(id_bytes)).ok().flatten().unwrap_or(community);
454
455    // If an epoch advanced, rebuild the FULL subscription so realtime delivery resumes at the new
456    // pseudonyms; else just refresh the route maps so the inbound drop-filter sees the new banlist.
457    let advanced = community.server_root_epoch.0 != pre_server_epoch
458        || community.channels.iter().any(|c| {
459            pre_channel_epochs.iter().find(|(id, _)| id == &c.id.to_hex()).map(|(_, e)| *e != c.epoch.0).unwrap_or(true)
460        });
461    if advanced && session.is_valid() {
462        if let Some(client) = crate::state::nostr_client() {
463            refresh_subscription(&client).await;
464        }
465    } else {
466        let _ = rebuild_routes().await;
467    }
468    if !session.is_valid() { return; }
469    crate::community::list::refresh_membership_current(&community);
470    handler.on_community_refreshed(&community_id);
471}
472
473/// Tear down a community locally on a received removal (kick/leave/ban), RETAINING the held epoch
474/// keys (so a self-scrub republish/erase still works), then refresh the subscription so we stop
475/// listening on its pseudonyms. Headless consumers can call this from `on_community_self_removed`;
476/// the GUI does a richer teardown (prune chats/relays + republish the list) in its handler instead.
477pub async fn teardown_local(community_id: &str) {
478    let _ = crate::db::community::delete_community_retain_keys(community_id);
479    if let Some(client) = crate::state::nostr_client() {
480        refresh_subscription(&client).await;
481    }
482}
483
484/// hex (64 chars) → 32-byte id.
485fn hex_to_id32(hex: &str) -> Option<[u8; 32]> {
486    (hex.len() == 64).then(|| crate::simd::hex::hex_to_bytes_32(hex))
487}