This commit is contained in:
Ren Amamiya
2026-04-03 15:23:26 +07:00
parent fbc9883680
commit 163865eb46
4 changed files with 67 additions and 108 deletions

View File

@@ -41,8 +41,6 @@ pub enum ChatEvent {
CloseRoom(u64),
/// An event to notify UI about a new chat request
Ping,
/// An event to notify UI that the chat registry has subscribed to messaging relays
Subscribed,
/// An error occurred
Error(SharedString),
}
@@ -78,6 +76,9 @@ impl Signal {
/// Chat Registry
#[derive(Debug)]
pub struct ChatRegistry {
/// Whether the chat registry is currently initializing.
pub initializing: bool,
/// Chat rooms
rooms: Vec<Entity<Room>>,
@@ -130,9 +131,9 @@ impl ChatRegistry {
cx.subscribe(&nostr, |this, _state, event, cx| {
if event == &StateEvent::SignerSet {
this.reset(cx);
this.get_rooms(cx);
this.get_contact_list(cx);
this.get_messages(cx);
this.get_rooms(cx);
};
}),
);
@@ -145,6 +146,7 @@ impl ChatRegistry {
});
Self {
initializing: true,
rooms: vec![],
trashes: cx.new(|_| BTreeSet::default()),
seens: Arc::new(RwLock::new(HashMap::default())),
@@ -327,13 +329,13 @@ impl ChatRegistry {
/// Get all messages for current user
pub fn get_messages(&mut self, cx: &mut Context<Self>) {
let task = self.subscribe(cx);
let task = self.subscribe_gift_wrap_events(cx);
self.tasks.push(cx.spawn(async move |this, cx| {
match task.await {
Ok(_) => {
this.update(cx, |_this, cx| {
cx.emit(ChatEvent::Subscribed);
this.update(cx, |this, cx| {
this.set_initializing(false, cx);
})?;
}
Err(e) => {
@@ -354,6 +356,7 @@ impl ChatRegistry {
cx.background_spawn(async move {
let public_key = signer.get_public_key().await?;
let id = SubscriptionId::new("inbox-relay");
// Construct filter for inbox relays
let filter = Filter::new()
@@ -364,12 +367,12 @@ impl ChatRegistry {
// Stream events from user's write relays
let mut stream = client
.stream_events(filter)
.with_id(id)
.timeout(Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
if let Ok(event) = res {
log::debug!("Got event: {:?}", event);
let urls: Vec<RelayUrl> = nip17::extract_owned_relay_list(event).collect();
return Ok(urls);
}
@@ -380,7 +383,7 @@ impl ChatRegistry {
}
/// Continuously get gift wrap events for the current user in their messaging relays
fn subscribe(&self, cx: &App) -> Task<Result<(), Error>> {
fn subscribe_gift_wrap_events(&self, cx: &App) -> Task<Result<(), Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let signer = nostr.read(cx).signer();
@@ -414,6 +417,12 @@ impl ChatRegistry {
})
}
/// Set the initializing status of the chat registry
fn set_initializing(&mut self, initializing: bool, cx: &mut Context<Self>) {
self.initializing = initializing;
cx.notify();
}
/// Get the loading status of the chat registry
pub fn loading(&self) -> bool {
self.tracking_flag.load(Ordering::Acquire)
@@ -557,7 +566,12 @@ impl ChatRegistry {
/// Reset the registry.
pub fn reset(&mut self, cx: &mut Context<Self>) {
self.initializing = true;
self.rooms.clear();
self.trashes.update(cx, |this, cx| {
this.clear();
cx.notify();
});
cx.notify();
}