add message tracker
This commit is contained in:
@@ -15,6 +15,7 @@ use gpui::{
|
|||||||
};
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use smallvec::{SmallVec, smallvec};
|
use smallvec::{SmallVec, smallvec};
|
||||||
|
use smol::lock::RwLock;
|
||||||
use state::{DEVICE_GIFTWRAP, NostrRegistry, StateEvent, TIMEOUT, USER_GIFTWRAP};
|
use state::{DEVICE_GIFTWRAP, NostrRegistry, StateEvent, TIMEOUT, USER_GIFTWRAP};
|
||||||
|
|
||||||
mod message;
|
mod message;
|
||||||
@@ -60,9 +61,12 @@ enum Signal {
|
|||||||
/// Chat Registry
|
/// Chat Registry
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ChatRegistry {
|
pub struct ChatRegistry {
|
||||||
/// Collection of all chat rooms
|
/// Chat rooms
|
||||||
rooms: Vec<Entity<Room>>,
|
rooms: Vec<Entity<Room>>,
|
||||||
|
|
||||||
|
/// Tracking events seen on which relays in the current session
|
||||||
|
seens: Arc<RwLock<HashMap<EventId, HashSet<RelayUrl>>>>,
|
||||||
|
|
||||||
/// Tracking the status of unwrapping gift wrap events.
|
/// Tracking the status of unwrapping gift wrap events.
|
||||||
tracking_flag: Arc<AtomicBool>,
|
tracking_flag: Arc<AtomicBool>,
|
||||||
|
|
||||||
@@ -119,6 +123,7 @@ impl ChatRegistry {
|
|||||||
|
|
||||||
Self {
|
Self {
|
||||||
rooms: vec![],
|
rooms: vec![],
|
||||||
|
seens: Arc::new(RwLock::new(HashMap::default())),
|
||||||
tracking_flag: Arc::new(AtomicBool::new(false)),
|
tracking_flag: Arc::new(AtomicBool::new(false)),
|
||||||
signal_rx: rx,
|
signal_rx: rx,
|
||||||
signal_tx: tx,
|
signal_tx: tx,
|
||||||
@@ -133,6 +138,7 @@ impl ChatRegistry {
|
|||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
let signer = nostr.read(cx).signer();
|
let signer = nostr.read(cx).signer();
|
||||||
let status = self.tracking_flag.clone();
|
let status = self.tracking_flag.clone();
|
||||||
|
let seens = self.seens.clone();
|
||||||
|
|
||||||
let initialized_at = Timestamp::now();
|
let initialized_at = Timestamp::now();
|
||||||
let sub_id1 = SubscriptionId::new(DEVICE_GIFTWRAP);
|
let sub_id1 = SubscriptionId::new(DEVICE_GIFTWRAP);
|
||||||
@@ -148,20 +154,26 @@ impl ChatRegistry {
|
|||||||
let mut processed_events = HashSet::new();
|
let mut processed_events = HashSet::new();
|
||||||
|
|
||||||
while let Some(notification) = notifications.next().await {
|
while let Some(notification) = notifications.next().await {
|
||||||
let ClientNotification::Message { message, .. } = notification else {
|
let ClientNotification::Message { message, relay_url } = notification else {
|
||||||
// Skip non-message notifications
|
// Skip non-message notifications
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
match message {
|
match message {
|
||||||
RelayMessage::Event { event, .. } => {
|
RelayMessage::Event { event, .. } => {
|
||||||
|
// Keep track of which relays have seen this event
|
||||||
|
{
|
||||||
|
let mut seens = seens.write().await;
|
||||||
|
seens.entry(event.id).or_default().insert(relay_url);
|
||||||
|
}
|
||||||
|
|
||||||
|
// De-duplicate events by their ID
|
||||||
if !processed_events.insert(event.id) {
|
if !processed_events.insert(event.id) {
|
||||||
// Skip if the event has already been processed
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip non-gift wrap events
|
||||||
if event.kind != Kind::GiftWrap {
|
if event.kind != Kind::GiftWrap {
|
||||||
// Skip non-gift wrap events
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -169,26 +181,21 @@ impl ChatRegistry {
|
|||||||
match extract_rumor(&client, &device_signer, event.as_ref()).await {
|
match extract_rumor(&client, &device_signer, event.as_ref()).await {
|
||||||
Ok(rumor) => {
|
Ok(rumor) => {
|
||||||
if rumor.tags.is_empty() {
|
if rumor.tags.is_empty() {
|
||||||
let error: SharedString =
|
let error: SharedString = "No room for message".into();
|
||||||
"Message doesn't belong to any rooms".into();
|
|
||||||
tx.send_async(Signal::Error(error)).await?;
|
tx.send_async(Signal::Error(error)).await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
match rumor.created_at >= initialized_at {
|
if rumor.created_at >= initialized_at {
|
||||||
true => {
|
let new_message = NewMessage::new(event.id, rumor);
|
||||||
let new_message = NewMessage::new(event.id, rumor);
|
let signal = Signal::Message(new_message);
|
||||||
let signal = Signal::Message(new_message);
|
|
||||||
|
|
||||||
tx.send_async(signal).await?;
|
tx.send_async(signal).await?;
|
||||||
}
|
} else {
|
||||||
false => {
|
status.store(true, Ordering::Release);
|
||||||
status.store(true, Ordering::Release);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let error: SharedString =
|
let error: SharedString = format!("Failed to unwrap: {e}").into();
|
||||||
format!("Failed to unwrap the gift wrap event: {e}").into();
|
|
||||||
tx.send_async(Signal::Error(error)).await?;
|
tx.send_async(Signal::Error(error)).await?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -399,6 +406,24 @@ impl ChatRegistry {
|
|||||||
.count()
|
.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Count the number of messages seen by a given relay.
|
||||||
|
pub fn count_messages(&self, relay_url: &RelayUrl) -> usize {
|
||||||
|
self.seens
|
||||||
|
.read_blocking()
|
||||||
|
.values()
|
||||||
|
.filter(|seen| seen.contains(relay_url))
|
||||||
|
.count()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the relays that have seen a given message.
|
||||||
|
pub fn seen_on(&self, id: &EventId) -> HashSet<RelayUrl> {
|
||||||
|
self.seens
|
||||||
|
.read_blocking()
|
||||||
|
.get(id)
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_default()
|
||||||
|
}
|
||||||
|
|
||||||
/// Add a new room to the start of list.
|
/// Add a new room to the start of list.
|
||||||
pub fn add_room<I>(&mut self, room: I, cx: &mut Context<Self>)
|
pub fn add_room<I>(&mut self, room: I, cx: &mut Context<Self>)
|
||||||
where
|
where
|
||||||
|
|||||||
@@ -739,28 +739,49 @@ impl Workspace {
|
|||||||
})
|
})
|
||||||
.when(inbox_connected, |this| this.indicator())
|
.when(inbox_connected, |this| this.indicator())
|
||||||
.dropdown_menu(move |this, _window, cx| {
|
.dropdown_menu(move |this, _window, cx| {
|
||||||
|
let chat = ChatRegistry::global(cx);
|
||||||
let persons = PersonRegistry::global(cx);
|
let persons = PersonRegistry::global(cx);
|
||||||
let profile = persons.read(cx).get(&public_key, cx);
|
let profile = persons.read(cx).get(&public_key, cx);
|
||||||
|
|
||||||
let urls: Vec<SharedString> = profile
|
let urls: Vec<(SharedString, SharedString)> = profile
|
||||||
.messaging_relays()
|
.messaging_relays()
|
||||||
.iter()
|
.iter()
|
||||||
.map(|url| SharedString::from(url.to_string()))
|
.map(|url| {
|
||||||
|
(
|
||||||
|
SharedString::from(url.to_string()),
|
||||||
|
chat.read(cx).count_messages(url).to_string().into(),
|
||||||
|
)
|
||||||
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
// Header
|
// Header
|
||||||
let menu = this.min_w(px(260.)).label("Messaging Relays");
|
let menu = this.min_w(px(260.)).label("Messaging Relays");
|
||||||
|
|
||||||
// Content
|
// Content
|
||||||
let menu = urls.into_iter().fold(menu, |this, url| {
|
let menu = urls.into_iter().fold(menu, |this, (url, count)| {
|
||||||
this.item(PopupMenuItem::element(move |_window, _cx| {
|
this.item(PopupMenuItem::element(move |_window, cx| {
|
||||||
h_flex()
|
h_flex()
|
||||||
.px_1()
|
.px_1()
|
||||||
.w_full()
|
.w_full()
|
||||||
.gap_2()
|
|
||||||
.text_sm()
|
.text_sm()
|
||||||
.child(div().size_1p5().rounded_full().bg(gpui::green()))
|
.justify_between()
|
||||||
.child(url.clone())
|
.child(
|
||||||
|
h_flex()
|
||||||
|
.gap_2()
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.size_1p5()
|
||||||
|
.rounded_full()
|
||||||
|
.bg(cx.theme().icon_accent),
|
||||||
|
)
|
||||||
|
.child(url.clone()),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.text_xs()
|
||||||
|
.text_color(cx.theme().text_muted)
|
||||||
|
.child(count.clone()),
|
||||||
|
)
|
||||||
}))
|
}))
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -116,7 +116,7 @@ impl NostrRegistry {
|
|||||||
.gossip(gossip)
|
.gossip(gossip)
|
||||||
.automatic_authentication(false)
|
.automatic_authentication(false)
|
||||||
.verify_subscriptions(false)
|
.verify_subscriptions(false)
|
||||||
.connect_timeout(Duration::from_secs(TIMEOUT))
|
.connect_timeout(Duration::from_secs(10))
|
||||||
.sleep_when_idle(SleepWhenIdle::Enabled {
|
.sleep_when_idle(SleepWhenIdle::Enabled {
|
||||||
timeout: Duration::from_secs(600),
|
timeout: Duration::from_secs(600),
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -339,11 +339,12 @@ impl Render for Notification {
|
|||||||
.when(only_message, |this| this.items_center())
|
.when(only_message, |this| this.items_center())
|
||||||
.refine_style(&self.style)
|
.refine_style(&self.style)
|
||||||
.when_some(icon, |this, icon| {
|
.when_some(icon, |this, icon| {
|
||||||
this.child(div().flex_shrink_0().child(icon))
|
this.child(div().flex_shrink_0().size_5().child(icon))
|
||||||
})
|
})
|
||||||
.child(
|
.child(
|
||||||
v_flex()
|
v_flex()
|
||||||
.flex_1()
|
.flex_1()
|
||||||
|
.gap_1()
|
||||||
.overflow_hidden()
|
.overflow_hidden()
|
||||||
.when_some(self.title.clone(), |this, title| {
|
.when_some(self.title.clone(), |this, title| {
|
||||||
this.child(h_flex().h_5().text_sm().font_semibold().child(title))
|
this.child(h_flex().h_5().text_sm().font_semibold().child(title))
|
||||||
@@ -352,9 +353,7 @@ impl Render for Notification {
|
|||||||
this.child(
|
this.child(
|
||||||
div()
|
div()
|
||||||
.text_sm()
|
.text_sm()
|
||||||
.when(has_title, |this| {
|
.when(has_title, |this| this.text_color(cx.theme().text_muted))
|
||||||
this.mt_2().text_color(cx.theme().text_muted)
|
|
||||||
})
|
|
||||||
.line_height(relative(1.3))
|
.line_height(relative(1.3))
|
||||||
.child(message),
|
.child(message),
|
||||||
)
|
)
|
||||||
@@ -363,7 +362,6 @@ impl Render for Notification {
|
|||||||
.when_some(action, |this, action| {
|
.when_some(action, |this, action| {
|
||||||
this.child(
|
this.child(
|
||||||
h_flex()
|
h_flex()
|
||||||
.mt_2()
|
|
||||||
.w_full()
|
.w_full()
|
||||||
.flex_1()
|
.flex_1()
|
||||||
.gap_1()
|
.gap_1()
|
||||||
|
|||||||
Reference in New Issue
Block a user