use std::collections::HashSet;
use std::sync::{Arc, LazyLock, Mutex as StdMutex};
use nostr_sdk::prelude::{Client, Event, Filter, Kind, PublicKey, RelayStatus, RelayUrl, SubscriptionId};
use tokio::sync::mpsc::UnboundedSender;
use tokio::sync::Mutex;
use super::community::CommunityV2;
use super::stream;
use super::{derive, inbound};
use crate::community::{CommunityId, ConcordProtocol, Epoch};
use crate::event_handler::InboundEventHandler;
use crate::state::SessionGuard;
static V2_SUB_ID: LazyLock<Mutex<Option<SubscriptionId>>> = LazyLock::new(|| Mutex::new(None));
static V2_POOLWIDE_SUB_ID: LazyLock<Mutex<Option<SubscriptionId>>> = LazyLock::new(|| Mutex::new(None));
static V2_SUB_SET: LazyLock<Mutex<Vec<String>>> = LazyLock::new(|| Mutex::new(Vec::new()));
static V2_SEEN_WRAPS: LazyLock<Mutex<HashSet<[u8; 32]>>> = LazyLock::new(|| Mutex::new(HashSet::new()));
const SEEN_WRAPS_CAP: usize = 8192;
static V2_FOLLOW_TX: LazyLock<StdMutex<Option<UnboundedSender<CommunityId>>>> = LazyLock::new(|| StdMutex::new(None));
static V2_FOLLOW_PENDING: LazyLock<StdMutex<HashSet<[u8; 32]>>> = LazyLock::new(|| StdMutex::new(HashSet::new()));
static V2_FOLLOW_LOCKS: LazyLock<StdMutex<std::collections::HashMap<[u8; 32], Arc<Mutex<()>>>>> =
LazyLock::new(|| StdMutex::new(std::collections::HashMap::new()));
pub(crate) fn follow_lock(id: &CommunityId) -> Arc<Mutex<()>> {
V2_FOLLOW_LOCKS.lock().unwrap().entry(id.0).or_default().clone()
}
pub async fn subscription_id() -> Option<SubscriptionId> {
V2_SUB_ID.lock().await.clone()
}
pub async fn poolwide_subscription_id() -> Option<SubscriptionId> {
V2_POOLWIDE_SUB_ID.lock().await.clone()
}
pub async fn clear() {
*V2_SUB_ID.lock().await = None;
*V2_POOLWIDE_SUB_ID.lock().await = None;
V2_SUB_SET.lock().await.clear();
V2_SEEN_WRAPS.lock().await.clear();
*V2_FOLLOW_TX.lock().unwrap() = None;
V2_FOLLOW_PENDING.lock().unwrap().clear();
V2_FOLLOW_LOCKS.lock().unwrap().clear();
super::streamauth::clear();
}
pub fn plane_authors(communities: &[CommunityV2]) -> Vec<PublicKey> {
let mut out = Vec::new();
for c in communities {
out.push(derive::guestbook_group_key(&c.community_root, c.id(), c.root_epoch).pk());
out.push(control_author(c));
out.push(super::derive::dissolved_group_key(c.id()).pk());
for ch in &c.channels {
if ch.private && ch.key.is_none() {
continue;
}
let (secret, epoch) = c.channel_secret(ch);
out.push(derive::channel_group_key(&secret, &ch.id, epoch).pk());
}
out.extend(rekey_authors(c));
}
out.sort_by_key(|p| p.to_hex());
out.dedup();
out
}
pub(crate) fn control_author(c: &CommunityV2) -> PublicKey {
derive::control_group_key(&c.community_root, c.id(), c.root_epoch).pk()
}
pub(crate) fn rekey_authors(c: &CommunityV2) -> Vec<PublicKey> {
let mut out = vec![derive::base_rekey_group_key(&c.community_root, c.id(), Epoch(c.root_epoch.0.saturating_add(1))).pk()];
for ch in &c.channels {
if ch.private {
out.push(derive::channel_rekey_group_key(&c.community_root, &ch.id, Epoch(ch.epoch.0.saturating_add(1))).pk());
}
}
out
}
pub fn load_held_v2() -> Vec<CommunityV2> {
let ids = crate::db::community::list_community_ids().unwrap_or_default();
ids.iter()
.filter(|id| matches!(crate::db::community::community_protocol(id).ok().flatten(), Some(ConcordProtocol::V2)))
.filter(|id| !crate::db::community::get_community_dissolved(&crate::simd::hex::bytes_to_hex_32(&id.0)).unwrap_or(false))
.filter_map(|id| crate::db::community::load_community_v2(id).ok().flatten())
.collect()
}
pub async fn refresh_subscription(client: &Client) {
{
let communities = load_held_v2();
let mut relays: Vec<String> = communities.iter().flat_map(|c| c.relays.iter().cloned()).collect();
relays.sort();
relays.dedup();
if !relays.is_empty() {
for r in &relays {
let _ = client.pool().add_relay(r.as_str(), crate::community_relay_options()).await;
}
client.connect().await;
let wanted: Vec<RelayUrl> = relays.iter().filter_map(|r| RelayUrl::parse(r).ok()).collect();
for _ in 0..24 {
let pool = client.pool().all_relays().await;
if wanted.iter().any(|u| pool.get(u).map(|r| r.status() == RelayStatus::Connected).unwrap_or(false)) {
break;
}
tokio::time::sleep(std::time::Duration::from_millis(250)).await;
}
for c in &communities {
super::streamauth::register_community(c);
}
super::streamauth::prime_auth(client, &relays).await;
}
}
let mut sub_guard = V2_SUB_ID.lock().await;
let mut set_guard = V2_SUB_SET.lock().await;
let communities = load_held_v2();
let authors = plane_authors(&communities);
let mut relays: Vec<String> = communities.iter().flat_map(|c| c.relays.iter().cloned()).collect();
relays.sort();
relays.dedup();
let mut new_set: Vec<String> = authors.iter().map(|p| p.to_hex()).collect();
new_set.sort();
if sub_guard.is_some() && *set_guard == new_set && (authors.is_empty() || V2_POOLWIDE_SUB_ID.lock().await.is_some()) {
return; }
if let Some(old) = sub_guard.take() {
client.unsubscribe(&old).await;
}
*set_guard = new_set;
if authors.is_empty() {
if let Some(old_pw) = V2_POOLWIDE_SUB_ID.lock().await.take() {
client.unsubscribe(&old_pw).await;
}
return;
}
let filter = Filter::new()
.kinds([Kind::Custom(stream::KIND_WRAP), Kind::Custom(stream::KIND_WRAP_EPHEMERAL)])
.authors(authors)
.limit(0);
{
let mut pw = V2_POOLWIDE_SUB_ID.lock().await;
if let Some(old) = pw.take() {
client.unsubscribe(&old).await;
}
if let Ok(out) = client.subscribe(filter.clone(), None).await {
*pw = Some(out.val);
}
}
if let Ok(out) = client.subscribe_to(relays.iter().cloned(), filter, None).await {
*sub_guard = Some(out.val);
}
}
pub(crate) async fn resubscribe_relay(client: &Client, relay: &RelayUrl) {
let targeted = V2_SUB_ID.lock().await.clone();
let poolwide = V2_POOLWIDE_SUB_ID.lock().await.clone();
if targeted.is_none() && poolwide.is_none() {
return; }
let communities = load_held_v2();
let authors = plane_authors(&communities);
if authors.is_empty() {
return;
}
let filter = Filter::new()
.kinds([Kind::Custom(stream::KIND_WRAP), Kind::Custom(stream::KIND_WRAP_EPHEMERAL)])
.authors(authors)
.limit(0);
for id in [targeted, poolwide].into_iter().flatten() {
let _ = client.subscribe_with_id_to([relay.clone()], id, filter.clone(), None).await;
}
}
pub async fn dispatch_event(session: &SessionGuard, event: Event, handler: Arc<dyn InboundEventHandler>) {
let Some(my_pk) = crate::my_public_key() else {
return;
};
if !session.is_valid() {
return;
}
{
let mut seen = V2_SEEN_WRAPS.lock().await;
if !seen.insert(event.id.to_bytes()) {
return;
}
if seen.len() > SEEN_WRAPS_CAP {
let keep = event.id.to_bytes();
seen.clear();
seen.insert(keep);
}
}
let communities = load_held_v2();
for c in &communities {
match inbound::dispatch_wrap(&event, c, &my_pk, &*handler) {
inbound::DispatchedV2::NotOurs => continue,
inbound::DispatchedV2::Control { .. } | inbound::DispatchedV2::Rekey { .. } => {
enqueue_follow(c.id());
return;
}
inbound::DispatchedV2::Dissolved { community_id } => {
if crate::db::community::set_community_dissolved(&community_id).unwrap_or(false) {
handler.on_community_dissolved(&community_id);
if let Some(client) = crate::state::nostr_client() {
refresh_subscription(&client).await;
}
}
return;
}
inbound::DispatchedV2::Chat { channel_id, event } => {
if !session.is_valid() {
return;
}
match inbound::persist_chat_event(&event, &channel_id, &my_pk, session).await {
Some(inbound::ChatPersist::New(message)) => handler.on_community_message(&channel_id, &message, true),
Some(inbound::ChatPersist::Updated { message, .. }) => handler.on_community_update(&channel_id, &message.id, &message),
Some(inbound::ChatPersist::ReactionRemoved { message, .. }) => handler.on_community_update(&channel_id, &message.id, &message),
Some(inbound::ChatPersist::Removed(target_id)) => handler.on_community_removed(&channel_id, &target_id),
None => {}
}
return;
}
inbound::DispatchedV2::Presence { .. } => {
if !session.is_valid() {
return;
}
let gb = derive::guestbook_group_key(&c.community_root, c.id(), c.root_epoch);
if let Ok(opened) = super::stream::open_wrap(&event, &gb) {
if let Ok(ev) = super::guestbook::parse_guestbook_event(&opened) {
let changed = super::service::ingest_guestbook_event(c, ev, event.created_at.as_secs()).unwrap_or(false);
if changed && session.is_valid() {
handler.on_community_refreshed(&crate::simd::hex::bytes_to_hex_32(&c.id().0));
}
}
}
return;
}
_ => return, }
}
}
pub fn follow_worker_running() -> bool {
V2_FOLLOW_TX.lock().unwrap().as_ref().map(|tx| !tx.is_closed()).unwrap_or(false)
}
pub fn enqueue_follow(id: &CommunityId) {
let mut pending = V2_FOLLOW_PENDING.lock().unwrap();
if !pending.insert(id.0) {
return; }
match V2_FOLLOW_TX.lock().unwrap().as_ref() {
Some(tx) if tx.send(*id).is_ok() => {}
_ => {
pending.remove(&id.0); }
}
}
pub fn spawn_follow_worker(handler: Arc<dyn InboundEventHandler>) {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<CommunityId>();
*V2_FOLLOW_TX.lock().unwrap() = Some(tx);
V2_FOLLOW_PENDING.lock().unwrap().clear();
let session = SessionGuard::capture();
tokio::spawn(async move {
while let Some(id) = rx.recv().await {
if !session.is_valid() {
break;
}
V2_FOLLOW_PENDING.lock().unwrap().remove(&id.0);
follow_community(&session, &id, &*handler).await;
}
});
}
async fn follow_community(session: &SessionGuard, id: &CommunityId, handler: &dyn InboundEventHandler) {
let Some(client) = crate::state::nostr_client() else {
return;
};
let lock = follow_lock(id);
let _guard = lock.lock().await;
let community_id = crate::simd::hex::bytes_to_hex_32(&id.0);
let transport = crate::community::transport::LiveTransport::with_timeout(std::time::Duration::from_secs(12));
let Ok(Some(current)) = crate::db::community::load_community_v2(id) else {
return; };
match super::service::follow_rekeys(&transport, ¤t, session).await {
Ok(follow) if follow.dissolved => {
if !session.is_valid() {
return;
}
handler.on_community_dissolved(&community_id);
return;
}
Ok(follow) if follow.self_removed => {
if !session.is_valid() {
return;
}
let _ = crate::db::community::delete_community(&community_id);
refresh_subscription(&client).await;
handler.on_community_self_removed(&community_id);
return;
}
Ok(follow) if follow.updated.is_some() => {
if !session.is_valid() {
return;
}
refresh_subscription(&client).await;
handler.on_community_refreshed(&community_id);
}
Ok(_) => {}
Err(_) => return,
}
let Ok(Some(current)) = crate::db::community::load_community_v2(id) else {
return;
};
if let Ok(Some(_)) = super::service::follow_control(&transport, ¤t, session).await {
if !session.is_valid() {
return;
}
refresh_subscription(&client).await;
handler.on_community_refreshed(&community_id);
enqueue_follow(id);
}
let Ok(Some(current)) = crate::db::community::load_community_v2(id) else {
return;
};
if let Ok(fresh) = super::service::sync_guestbook(&transport, ¤t, session).await {
if fresh.is_empty() || !session.is_valid() {
return;
}
surface_presence(¤t, &fresh, handler);
handler.on_community_refreshed(&community_id);
}
}
fn surface_presence(
community: &CommunityV2,
fresh: &[super::guestbook::GuestbookEvent],
handler: &dyn InboundEventHandler,
) {
use super::guestbook::GuestbookEntry;
use nostr_sdk::prelude::ToBech32;
let Some(primary) = community.primary_channel() else {
return;
};
let chat_id = crate::simd::hex::bytes_to_hex_32(&primary.id.0);
let cid_hex = crate::simd::hex::bytes_to_hex_32(&community.id().0);
let banned = crate::db::community::get_community_banlist(&cid_hex).unwrap_or_default();
for ev in fresh {
let (member, joined, at_ms, invited_by) = match &ev.entry {
GuestbookEntry::Join { member, at_ms, invited_by } => (member, true, *at_ms, invited_by.clone()),
GuestbookEntry::Leave { member, at_ms } => (member, false, *at_ms, None),
GuestbookEntry::Kick { .. } | GuestbookEntry::Snapshot { .. } => continue,
};
if banned.contains(&member.to_hex()) {
continue;
}
let Ok(npub) = member.to_bech32() else { continue };
let event_id = crate::simd::hex::bytes_to_hex_32(&ev.rumor_id);
let (by, label) = match &invited_by {
Some((c, l)) => (Some(c.as_str()), Some(l.as_str())),
None => (None, None),
};
handler.on_community_presence(&chat_id, &npub, joined, &event_id, at_ms / 1000, by, label);
}
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::control::{genesis, CommunityMetadata};
use crate::community::Epoch;
use nostr_sdk::prelude::Keys;
fn a_community(name: &str) -> CommunityV2 {
let owner = Keys::generate();
let g = genesis(&owner, CommunityMetadata { name: name.into(), ..Default::default() }, 1_000).unwrap();
CommunityV2::from_genesis(&g, name, None, vec!["wss://r".into()], 0)
}
#[test]
fn plane_authors_covers_the_dispatched_planes_only() {
let c = a_community("A");
let authors = plane_authors(std::slice::from_ref(&c));
let gb = derive::guestbook_group_key(&c.community_root, c.id(), c.root_epoch).pk();
let control = derive::control_group_key(&c.community_root, c.id(), c.root_epoch).pk();
let general = {
let (s, e) = c.channel_secret(&c.channels[0]);
derive::channel_group_key(&s, &c.channels[0].id, e).pk()
};
let next_base = derive::base_rekey_group_key(&c.community_root, c.id(), Epoch(1)).pk();
let dissolved = derive::dissolved_group_key(c.id()).pk();
assert!(
authors.contains(&gb)
&& authors.contains(&control)
&& authors.contains(&general)
&& authors.contains(&next_base)
&& authors.contains(&dissolved)
);
assert_eq!(authors.len(), 5, "guestbook + control + dissolved + chat + base-rekey planes are subscribed");
}
#[test]
fn plane_authors_is_deterministic_deduped_and_multi_community() {
let a = a_community("A");
let b = a_community("B");
let one = plane_authors(std::slice::from_ref(&a));
assert_eq!(plane_authors(std::slice::from_ref(&a)), one);
let two = plane_authors(&[a.clone(), b.clone()]);
assert_eq!(two.len(), one.len() * 2);
assert_eq!(plane_authors(&[b, a]), two);
}
#[tokio::test]
async fn dispatch_event_routes_a_v2_message_to_the_handler() {
use crate::community::transport::memory::MemoryRelay;
use crate::community::transport::{Query, Transport};
use crate::types::Message;
use std::sync::Mutex as StdMutex;
#[derive(Default)]
struct Recorder {
got: StdMutex<Vec<(String, String)>>,
}
impl InboundEventHandler for Recorder {
fn on_community_message(&self, chat_id: &str, msg: &Message, _new: bool) {
self.got.lock().unwrap().push((chat_id.to_string(), msg.content.clone()));
}
}
let _g = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
crate::db::close_database();
crate::db::clear_id_caches();
let tmp = tempfile::tempdir().unwrap();
let acct = {
const B: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l";
let mut s = String::from("npub1");
for i in 0..58 {
s.push(B[(i * 5 + 1) % 32] as char);
}
s
};
std::fs::create_dir_all(tmp.path().join(&acct)).unwrap();
crate::db::set_app_data_dir(tmp.path().to_path_buf());
crate::db::set_current_account(acct.clone()).unwrap();
crate::db::init_database(&acct).unwrap();
let _ = crate::state::take_nostr_client();
let me = Keys::generate();
crate::state::MY_SECRET_KEY.store_from_keys(&me, &[]);
crate::state::set_my_public_key(me.public_key());
let relay = MemoryRelay::new();
let community = super::super::service::create_community(&relay, "Live", vec!["wss://r".into()], None).await.unwrap();
let general = community.channels[0].id;
let member = Keys::generate();
let group = derive::channel_group_key(&community.community_root, &general, community.root_epoch);
let rumor = super::super::chat::build_message_rumor(member.public_key(), &general, community.root_epoch, "live ping", None, &[], vec![], 5_000);
let (wrap, _) = super::super::chat::seal_chat_rumor(&rumor, &group, &member, nostr_sdk::prelude::Timestamp::from_secs(5), false).unwrap();
let _ = relay.publish(&wrap, &community.relays).await;
let q = Query { kinds: vec![stream::KIND_WRAP], authors: vec![group.pk_hex()], ..Default::default() };
let wrap = relay.fetch(&q, &community.relays).await.unwrap().into_iter().find(|w| w.pubkey == group.pk()).unwrap();
let rec = Arc::new(Recorder::default());
let session = SessionGuard::capture();
crate::community::v2::realtime::clear().await; dispatch_event(&session, wrap.clone(), rec.clone()).await;
dispatch_event(&session, wrap, rec.clone()).await;
let got = rec.got.lock().unwrap();
assert_eq!(got.len(), 1, "a re-delivered wrap fires the handler exactly once");
assert_eq!(got[0].1, "live ping");
assert_eq!(got[0].0, crate::simd::hex::bytes_to_hex_32(&general.0));
}
#[tokio::test]
async fn follow_queue_coalesces_a_burst_and_re_enqueues_after_processing() {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<CommunityId>();
*V2_FOLLOW_TX.lock().unwrap() = Some(tx);
V2_FOLLOW_PENDING.lock().unwrap().clear();
let id = CommunityId([0x11; 32]);
enqueue_follow(&id);
enqueue_follow(&id);
enqueue_follow(&id);
assert_eq!(rx.recv().await, Some(id), "first trigger queues a follow");
assert!(rx.try_recv().is_err(), "the burst coalesced to exactly one");
V2_FOLLOW_PENDING.lock().unwrap().remove(&id.0);
enqueue_follow(&id);
assert_eq!(rx.recv().await, Some(id), "a trigger after processing re-queues");
let id2 = CommunityId([0x22; 32]);
enqueue_follow(&id2);
assert_eq!(rx.recv().await, Some(id2));
*V2_FOLLOW_TX.lock().unwrap() = None;
V2_FOLLOW_PENDING.lock().unwrap().clear();
}
#[tokio::test]
async fn a_dissolved_community_honors_no_new_events_and_fires_death_once() {
use super::super::service;
use crate::community::transport::memory::MemoryRelay;
use crate::community::transport::Transport;
use crate::types::Message;
use std::sync::Mutex as StdMutex;
#[derive(Default)]
struct Recorder {
messages: StdMutex<Vec<String>>,
deaths: StdMutex<Vec<String>>,
}
impl InboundEventHandler for Recorder {
fn on_community_message(&self, _chat: &str, msg: &Message, _new: bool) {
self.messages.lock().unwrap().push(msg.content.clone());
}
fn on_community_dissolved(&self, community_id: &str) {
self.deaths.lock().unwrap().push(community_id.to_string());
}
}
let _g = crate::db::DB_TEST_GUARD.lock().unwrap_or_else(|e| e.into_inner());
crate::db::close_database();
crate::db::clear_id_caches();
let tmp = tempfile::tempdir().unwrap();
let acct = {
const B: &[u8] = b"qpzry9x8gf2tvdw0s3jn54khce6mua7l";
let mut s = String::from("npub1");
for i in 0..58 {
s.push(B[(i * 3 + 2) % 32] as char);
}
s
};
std::fs::create_dir_all(tmp.path().join(&acct)).unwrap();
crate::db::set_app_data_dir(tmp.path().to_path_buf());
crate::db::set_current_account(acct.clone()).unwrap();
crate::db::init_database(&acct).unwrap();
let _ = crate::state::take_nostr_client();
let me = Keys::generate();
crate::state::MY_SECRET_KEY.store_from_keys(&me, &[]);
crate::state::set_my_public_key(me.public_key());
let relay = MemoryRelay::new();
let community = service::create_community(&relay, "Doomed", vec!["wss://r".into()], None).await.unwrap();
let general = community.channels[0].id;
let rumor = super::super::dissolution::dissolved_tombstone_rumor(me.public_key(), community.id(), 8_000);
let tombstone = super::super::dissolution::seal_dissolved(&rumor, community.id(), &me, nostr_sdk::prelude::Timestamp::from_secs(8_000)).unwrap();
let _ = relay.publish(&tombstone, &community.relays).await;
assert!(!crate::db::community::get_community_dissolved(&crate::simd::hex::bytes_to_hex_32(&community.id().0)).unwrap(), "not yet locally sealed");
let rec = Arc::new(Recorder::default());
let session = SessionGuard::capture();
clear().await;
dispatch_event(&session, tombstone.clone(), rec.clone()).await;
dispatch_event(&session, tombstone, rec.clone()).await;
assert!(crate::db::community::get_community_dissolved(&crate::simd::hex::bytes_to_hex_32(&community.id().0)).unwrap());
let member = Keys::generate();
let cgroup = derive::channel_group_key(&community.community_root, &general, community.root_epoch);
let rumor = super::super::chat::build_message_rumor(member.public_key(), &general, community.root_epoch, "into the grave", None, &[], vec![], 9_000);
let (mw, _) = super::super::chat::seal_chat_rumor(&rumor, &cgroup, &member, nostr_sdk::prelude::Timestamp::from_secs(9), false).unwrap();
dispatch_event(&session, mw, rec.clone()).await;
assert_eq!(rec.deaths.lock().unwrap().len(), 1, "death is announced exactly once");
assert!(rec.messages.lock().unwrap().is_empty(), "a post-tombstone message is never honored (CORD-02 §9)");
}
#[test]
fn a_private_channel_subscribes_to_its_own_chat_plane() {
let mut c = a_community("Priv");
c.channels.push(super::super::community::ChannelV2 {
id: crate::community::ChannelId([0x33; 32]),
name: "mods".into(),
private: true,
key: Some([0x44; 32]),
epoch: Epoch(1),
voice: None,
meta_custom: None,
meta_extra: Default::default(),
});
let authors = plane_authors(std::slice::from_ref(&c));
let priv_chat = derive::channel_group_key(&[0x44; 32], &c.channels[1].id, Epoch(1)).pk();
assert!(authors.contains(&priv_chat), "a private channel subscribes to its own chat plane");
let next_rekey = derive::channel_rekey_group_key(&c.community_root, &c.channels[1].id, Epoch(2)).pk();
assert!(authors.contains(&next_rekey), "a private channel's next rekey plane is subscribed");
}
}