feat: sharpen chat experiences (#9)
* feat: add global account and refactor chat registry * chore: improve last seen * chore: reduce string alloc * wip: refactor room * chore: fix edit profile panel * chore: refactor open window in main * chore: refactor sidebar * chore: refactor room
This commit is contained in:
@@ -1,12 +1,11 @@
|
||||
use crate::room::Room;
|
||||
use anyhow::anyhow;
|
||||
use common::utils::{compare, room_hash, signer_public_key};
|
||||
use common::{last_seen::LastSeen, utils::room_hash};
|
||||
use gpui::{App, AppContext, Context, Entity, Global, WeakEntity};
|
||||
use itertools::Itertools;
|
||||
use nostr_sdk::prelude::*;
|
||||
use state::get_client;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
use crate::room::Room;
|
||||
use std::{cmp::Reverse, rc::Rc, sync::RwLock};
|
||||
|
||||
pub fn init(cx: &mut App) {
|
||||
ChatRegistry::register(cx);
|
||||
@@ -17,7 +16,7 @@ struct GlobalChatRegistry(Entity<ChatRegistry>);
|
||||
impl Global for GlobalChatRegistry {}
|
||||
|
||||
pub struct ChatRegistry {
|
||||
rooms: Vec<Entity<Room>>,
|
||||
rooms: Rc<RwLock<Vec<Entity<Room>>>>,
|
||||
is_loading: bool,
|
||||
}
|
||||
|
||||
@@ -40,83 +39,67 @@ impl ChatRegistry {
|
||||
// Set global state
|
||||
cx.set_global(GlobalChatRegistry(entity.clone()));
|
||||
|
||||
// Observe and load metadata for any new rooms
|
||||
cx.observe_new::<Room>(|this, _window, cx| {
|
||||
let client = get_client();
|
||||
let pubkeys = this.pubkeys();
|
||||
let (tx, rx) = oneshot::channel::<Vec<(PublicKey, Metadata)>>();
|
||||
|
||||
cx.background_spawn(async move {
|
||||
let mut profiles = Vec::new();
|
||||
|
||||
for public_key in pubkeys.into_iter() {
|
||||
if let Ok(metadata) = client.database().metadata(public_key).await {
|
||||
profiles.push((public_key, metadata.unwrap_or_default()));
|
||||
}
|
||||
}
|
||||
|
||||
_ = tx.send(profiles);
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.spawn(|this, mut cx| async move {
|
||||
if let Ok(profiles) = rx.await {
|
||||
if let Some(room) = this.upgrade() {
|
||||
_ = cx.update_entity(&room, |this, cx| {
|
||||
for profile in profiles.into_iter() {
|
||||
this.set_metadata(profile.0, profile.1);
|
||||
}
|
||||
cx.notify();
|
||||
});
|
||||
}
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
})
|
||||
.detach();
|
||||
|
||||
entity
|
||||
})
|
||||
}
|
||||
|
||||
fn new(_cx: &mut Context<Self>) -> Self {
|
||||
Self {
|
||||
rooms: vec![],
|
||||
rooms: Rc::new(RwLock::new(vec![])),
|
||||
is_loading: true,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_rooms_ids(&self, cx: &mut Context<Self>) -> Vec<u64> {
|
||||
self.rooms.iter().map(|room| room.read(cx).id).collect()
|
||||
self.rooms
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.map(|room| room.read(cx).id)
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub fn load_chat_rooms(&mut self, cx: &mut Context<Self>) {
|
||||
let client = get_client();
|
||||
let (tx, rx) = oneshot::channel::<Vec<Event>>();
|
||||
let (tx, rx) = oneshot::channel::<Option<Vec<Event>>>();
|
||||
|
||||
cx.background_spawn(async move {
|
||||
if let Ok(public_key) = signer_public_key(client).await {
|
||||
let filter = Filter::new()
|
||||
let result = async {
|
||||
let signer = client.signer().await?;
|
||||
let public_key = signer.get_public_key().await?;
|
||||
|
||||
let send = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(public_key);
|
||||
|
||||
// Get all DM events from database
|
||||
if let Ok(events) = client.database().query(filter).await {
|
||||
let result: Vec<Event> = events
|
||||
.into_iter()
|
||||
.filter(|ev| ev.tags.public_keys().peekable().peek().is_some())
|
||||
.unique_by(room_hash)
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
.collect();
|
||||
let recv = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.pubkey(public_key);
|
||||
|
||||
_ = tx.send(result);
|
||||
}
|
||||
let send_events = client.database().query(send).await?;
|
||||
let recv_events = client.database().query(recv).await?;
|
||||
|
||||
Ok::<_, anyhow::Error>(send_events.merge(recv_events))
|
||||
}
|
||||
.await;
|
||||
|
||||
if let Ok(events) = result {
|
||||
let result: Vec<Event> = events
|
||||
.into_iter()
|
||||
.filter(|ev| ev.tags.public_keys().peekable().peek().is_some())
|
||||
.unique_by(room_hash)
|
||||
.sorted_by_key(|ev| Reverse(ev.created_at))
|
||||
.collect();
|
||||
|
||||
_ = tx.send(Some(result));
|
||||
} else {
|
||||
_ = tx.send(None);
|
||||
}
|
||||
})
|
||||
.detach();
|
||||
|
||||
cx.spawn(|this, cx| async move {
|
||||
if let Ok(events) = rx.await {
|
||||
if let Ok(Some(events)) = rx.await {
|
||||
if !events.is_empty() {
|
||||
_ = cx.update(|cx| {
|
||||
_ = this.update(cx, |this, cx| {
|
||||
@@ -127,14 +110,14 @@ impl ChatRegistry {
|
||||
let new = room_hash(&ev);
|
||||
// Filter all seen events
|
||||
if !current_rooms.iter().any(|this| this == &new) {
|
||||
Some(cx.new(|cx| Room::parse(&ev, cx)))
|
||||
Some(Room::new(&ev, cx))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
this.rooms.extend(items);
|
||||
this.rooms.write().unwrap().extend(items);
|
||||
this.is_loading = false;
|
||||
|
||||
cx.notify();
|
||||
@@ -146,8 +129,8 @@ impl ChatRegistry {
|
||||
.detach();
|
||||
}
|
||||
|
||||
pub fn rooms(&self) -> &Vec<Entity<Room>> {
|
||||
&self.rooms
|
||||
pub fn rooms(&self) -> Vec<Entity<Room>> {
|
||||
self.rooms.read().unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn is_loading(&self) -> bool {
|
||||
@@ -156,18 +139,25 @@ impl ChatRegistry {
|
||||
|
||||
pub fn get(&self, id: &u64, cx: &App) -> Option<WeakEntity<Room>> {
|
||||
self.rooms
|
||||
.read()
|
||||
.unwrap()
|
||||
.iter()
|
||||
.find(|model| model.read(cx).id == *id)
|
||||
.map(|room| room.downgrade())
|
||||
}
|
||||
|
||||
pub fn new_room(&mut self, room: Room, cx: &mut Context<Self>) -> Result<(), anyhow::Error> {
|
||||
if !self
|
||||
.rooms
|
||||
pub fn push_room(
|
||||
&mut self,
|
||||
room: Entity<Room>,
|
||||
cx: &mut Context<Self>,
|
||||
) -> Result<(), anyhow::Error> {
|
||||
let mut rooms = self.rooms.write().unwrap();
|
||||
|
||||
if !rooms
|
||||
.iter()
|
||||
.any(|current| compare(¤t.read(cx).pubkeys(), &room.pubkeys()))
|
||||
.any(|current| current.read(cx) == room.read(cx))
|
||||
{
|
||||
self.rooms.insert(0, cx.new(|_| room));
|
||||
rooms.insert(0, room);
|
||||
cx.notify();
|
||||
|
||||
Ok(())
|
||||
@@ -177,32 +167,27 @@ impl ChatRegistry {
|
||||
}
|
||||
|
||||
pub fn push_message(&mut self, event: Event, cx: &mut Context<Self>) {
|
||||
// Get all pubkeys from event's tags for comparision
|
||||
let mut pubkeys: Vec<_> = event.tags.public_keys().copied().collect();
|
||||
pubkeys.push(event.pubkey);
|
||||
let id = room_hash(&event);
|
||||
let mut rooms = self.rooms.write().unwrap();
|
||||
|
||||
if let Some(room) = self
|
||||
.rooms
|
||||
.iter()
|
||||
.find(|room| compare(&room.read(cx).pubkeys(), &pubkeys))
|
||||
{
|
||||
if let Some(room) = rooms.iter().find(|room| room.read(cx).id == id) {
|
||||
room.update(cx, |this, cx| {
|
||||
this.last_seen.set(event.created_at);
|
||||
this.new_messages.update(cx, |this, cx| {
|
||||
this.push(event);
|
||||
cx.notify();
|
||||
});
|
||||
if let Some(last_seen) = Rc::get_mut(&mut this.last_seen) {
|
||||
*last_seen = LastSeen(event.created_at);
|
||||
}
|
||||
this.new_messages.push(event);
|
||||
cx.notify();
|
||||
});
|
||||
|
||||
// Re sort rooms by last seen
|
||||
self.rooms
|
||||
.sort_by_key(|room| Reverse(room.read(cx).last_seen()));
|
||||
rooms.sort_by_key(|room| Reverse(room.read(cx).last_seen()));
|
||||
|
||||
cx.notify();
|
||||
} else {
|
||||
let room = cx.new(|cx| Room::parse(&event, cx));
|
||||
self.rooms.insert(0, room);
|
||||
let mut rooms = self.rooms.write().unwrap();
|
||||
let new_room = Room::new(&event, cx);
|
||||
|
||||
rooms.insert(0, new_room);
|
||||
cx.notify();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user