feat: improve compose modal

This commit is contained in:
2025-02-09 14:29:34 +07:00
parent 0daebe5762
commit 600af900e9
14 changed files with 661 additions and 524 deletions

View File

@@ -1,6 +1,6 @@
use anyhow::Error;
use common::utils::{compare, room_hash};
use gpui::{App, AppContext, Entity, Global, WeakEntity};
use gpui::{App, AppContext, Entity, Global, WeakEntity, Window};
use nostr_sdk::prelude::*;
use state::get_client;
@@ -28,8 +28,11 @@ impl ChatRegistry {
let mut profiles = Vec::new();
for public_key in pubkeys.into_iter() {
let query = client.database().metadata(public_key).await?;
let metadata = query.unwrap_or_default();
let metadata = client
.database()
.metadata(public_key)
.await?
.unwrap_or_default();
profiles.push((public_key, metadata));
}
@@ -56,14 +59,16 @@ impl ChatRegistry {
cx.set_global(Self { inbox });
}
pub fn load(&mut self, cx: &mut App) {
pub fn load(&mut self, window: &mut Window, cx: &mut App) {
let window_handle = window.window_handle();
self.inbox.update(cx, |this, cx| {
let task = this.load(cx.to_async());
cx.spawn(|this, mut async_cx| async move {
if let Some(inbox) = this.upgrade() {
if let Ok(events) = task.await {
_ = async_cx.update_entity(&inbox, |this, cx| {
cx.spawn(|this, mut cx| async move {
if let Ok(events) = task.await {
_ = cx.update_window(window_handle, |_, _, cx| {
_ = this.update(cx, |this, cx| {
let current_rooms = this.get_room_ids(cx);
let items: Vec<Entity<Room>> = events
.into_iter()
@@ -83,7 +88,7 @@ impl ChatRegistry {
cx.notify();
});
}
});
}
})
.detach();
@@ -114,29 +119,42 @@ impl ChatRegistry {
})
}
pub fn new_room_message(&mut self, event: Event, cx: &mut App) {
pub fn new_room_message(&mut self, event: Event, window: &mut Window, cx: &mut App) {
let window_handle = window.window_handle();
// Get all pubkeys from event's tags for comparision
let mut pubkeys: Vec<_> = event.tags.public_keys().copied().collect();
pubkeys.push(event.pubkey);
self.inbox.update(cx, |this, cx| {
if let Some(room) = this.rooms.iter().find(|room| {
let all_keys = room.read(cx).get_pubkeys();
compare(&all_keys, &pubkeys)
}) {
room.update(cx, |this, cx| {
this.new_messages.push(event);
cx.notify();
})
} else {
let room = cx.new(|_| Room::parse(&event));
if let Some(room) = self
.inbox
.read(cx)
.rooms
.iter()
.find(|room| compare(&room.read(cx).get_pubkeys(), &pubkeys))
{
let weak_room = room.downgrade();
self.inbox.update(cx, |this, cx| {
this.rooms.insert(0, room);
cx.notify();
})
}
cx.spawn(|mut cx| async move {
if let Err(e) = cx.update_window(window_handle, |_, _, cx| {
_ = weak_room.update(cx, |this, cx| {
this.last_seen = event.created_at;
this.new_messages.push(event);
cx.notify();
})
cx.notify();
});
}) {
println!("Error: {}", e)
}
})
.detach();
} else {
let room = cx.new(|_| Room::parse(&event));
self.inbox.update(cx, |this, cx| {
this.rooms.insert(0, room);
cx.notify();
});
}
}
}

View File

@@ -4,6 +4,7 @@ use common::{
};
use gpui::SharedString;
use nostr_sdk::prelude::*;
use std::collections::HashSet;
#[derive(Debug)]
pub struct Room {
@@ -59,14 +60,19 @@ impl Room {
let id = room_hash(&event.tags);
let last_seen = event.created_at;
// Always equal to current user
let owner = NostrProfile::new(event.pubkey, Metadata::default());
// Get all pubkeys that invole in this group
let members: Vec<NostrProfile> = event
.tags
.public_keys()
.copied()
.map(|public_key| NostrProfile::new(public_key, Metadata::default()))
.collect::<HashSet<_>>()
.into_iter()
.map(|public_key| NostrProfile::new(*public_key, Metadata::default()))
.collect();
// Get title from event's tags
let title = if let Some(tag) = event.tags.find(TagKind::Title) {
tag.content().map(|s| s.to_owned().into())
} else {