refactor chats (#15)
* refactor * update * update * update * remove nostrprofile struct * update * refactor contacts * prevent double login
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
use std::{cmp::Reverse, collections::HashMap};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use common::{last_seen::LastSeen, utils::room_hash};
|
||||
use anyhow::{anyhow, Error};
|
||||
use common::room_hash;
|
||||
use global::get_client;
|
||||
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task, Window};
|
||||
use itertools::Itertools;
|
||||
@@ -11,6 +11,7 @@ use smallvec::{smallvec, SmallVec};
|
||||
|
||||
use crate::room::Room;
|
||||
|
||||
mod constants;
|
||||
pub mod message;
|
||||
pub mod room;
|
||||
|
||||
@@ -22,35 +23,56 @@ struct GlobalChatRegistry(Entity<ChatRegistry>);
|
||||
|
||||
impl Global for GlobalChatRegistry {}
|
||||
|
||||
/// Main registry for managing chat rooms and user profiles
|
||||
///
|
||||
/// The ChatRegistry is responsible for:
|
||||
/// - Managing chat rooms and their states
|
||||
/// - Tracking user profiles
|
||||
/// - Loading room data from the lmdb
|
||||
/// - Handling messages and room creation
|
||||
pub struct ChatRegistry {
|
||||
/// Collection of all chat rooms
|
||||
rooms: Vec<Entity<Room>>,
|
||||
|
||||
/// Map of user public keys to their profile metadata
|
||||
profiles: Entity<HashMap<PublicKey, Option<Metadata>>>,
|
||||
|
||||
/// Indicates if rooms are currently being loaded
|
||||
loading: bool,
|
||||
|
||||
/// Subscriptions for observing changes
|
||||
#[allow(dead_code)]
|
||||
subscriptions: SmallVec<[Subscription; 1]>,
|
||||
}
|
||||
|
||||
impl ChatRegistry {
|
||||
pub fn global(cx: &mut App) -> Entity<Self> {
|
||||
/// Retrieve the global ChatRegistry instance
|
||||
pub fn global(cx: &App) -> Entity<Self> {
|
||||
cx.global::<GlobalChatRegistry>().0.clone()
|
||||
}
|
||||
|
||||
/// Set the global ChatRegistry instance
|
||||
pub fn set_global(state: Entity<Self>, cx: &mut App) {
|
||||
cx.set_global(GlobalChatRegistry(state));
|
||||
}
|
||||
|
||||
/// Create a new ChatRegistry instance
|
||||
fn new(cx: &mut Context<Self>) -> Self {
|
||||
let profiles = cx.new(|_| HashMap::new());
|
||||
let mut subscriptions = smallvec![];
|
||||
|
||||
// Observe new Room creations to collect profile metadata
|
||||
subscriptions.push(cx.observe_new::<Room>(|this, _, cx| {
|
||||
let load_metadata = this.load_metadata(cx);
|
||||
let task = this.metadata(cx);
|
||||
|
||||
cx.spawn(async move |this, cx| {
|
||||
if let Ok(profiles) = load_metadata.await {
|
||||
cx.spawn(async move |_, cx| {
|
||||
if let Ok(data) = task.await {
|
||||
cx.update(|cx| {
|
||||
this.update(cx, |this, cx| {
|
||||
this.update_members(profiles, cx);
|
||||
})
|
||||
.ok();
|
||||
for (public_key, metadata) in data.into_iter() {
|
||||
Self::global(cx).update(cx, |this, cx| {
|
||||
this.add_profile(public_key, metadata, cx);
|
||||
})
|
||||
}
|
||||
})
|
||||
.ok();
|
||||
}
|
||||
@@ -61,24 +83,73 @@ impl ChatRegistry {
|
||||
Self {
|
||||
rooms: vec![],
|
||||
loading: true,
|
||||
profiles,
|
||||
subscriptions,
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the global loading status
|
||||
pub fn loading(&self) -> bool {
|
||||
self.loading
|
||||
}
|
||||
|
||||
/// Get a room by its ID.
|
||||
pub fn room(&self, id: &u64, cx: &App) -> Option<Entity<Room>> {
|
||||
self.rooms
|
||||
.iter()
|
||||
.find(|model| model.read(cx).id == *id)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// Get all rooms grouped by their kind.
|
||||
pub fn rooms(&self, cx: &App) -> HashMap<RoomKind, Vec<&Entity<Room>>> {
|
||||
let mut groups = HashMap::new();
|
||||
groups.insert(RoomKind::Ongoing, Vec::new());
|
||||
groups.insert(RoomKind::Trusted, Vec::new());
|
||||
groups.insert(RoomKind::Unknown, Vec::new());
|
||||
|
||||
for room in self.rooms.iter() {
|
||||
let kind = room.read(cx).kind;
|
||||
groups.entry(kind).or_insert_with(Vec::new).push(room);
|
||||
}
|
||||
|
||||
groups
|
||||
}
|
||||
|
||||
/// Get rooms by their kind.
|
||||
pub fn rooms_by_kind(&self, kind: RoomKind, cx: &App) -> Vec<&Entity<Room>> {
|
||||
self.rooms
|
||||
.iter()
|
||||
.filter(|room| room.read(cx).kind == kind)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get the IDs of all rooms.
|
||||
pub fn room_ids(&self, cx: &mut Context<Self>) -> Vec<u64> {
|
||||
self.rooms.iter().map(|room| room.read(cx).id).collect()
|
||||
}
|
||||
|
||||
/// Load all rooms from the lmdb.
|
||||
///
|
||||
/// This method:
|
||||
/// 1. Fetches all private direct messages from the lmdb
|
||||
/// 2. Groups them by ID
|
||||
/// 3. Determines each room's type based on message frequency and trust status
|
||||
/// 4. Creates Room entities for each unique room
|
||||
pub fn load_rooms(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let client = get_client();
|
||||
let room_ids = self.room_ids(cx);
|
||||
type Rooms = Vec<(Event, usize, bool)>;
|
||||
|
||||
type LoadResult = Result<Vec<(Event, usize, bool)>, Error>;
|
||||
|
||||
let task: Task<LoadResult> = cx.background_spawn(async move {
|
||||
let task: Task<Result<Rooms, Error>> = cx.background_spawn(async move {
|
||||
let client = get_client();
|
||||
let signer = client.signer().await?;
|
||||
let public_key = signer.get_public_key().await?;
|
||||
|
||||
// Get messages sent by the user
|
||||
let send = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.author(public_key);
|
||||
|
||||
// Get messages received by the user
|
||||
let recv = Filter::new()
|
||||
.kind(Kind::PrivateDirectMessage)
|
||||
.pubkey(public_key);
|
||||
@@ -89,26 +160,26 @@ impl ChatRegistry {
|
||||
|
||||
let mut room_map: HashMap<u64, (Event, usize, bool)> = HashMap::new();
|
||||
|
||||
// Process each event and group by room hash
|
||||
for event in events
|
||||
.into_iter()
|
||||
.filter(|ev| ev.tags.public_keys().peekable().peek().is_some())
|
||||
{
|
||||
let hash = room_hash(&event);
|
||||
|
||||
if !room_ids.iter().any(|id| id == &hash) {
|
||||
let filter = Filter::new().kind(Kind::ContactList).pubkey(event.pubkey);
|
||||
let is_trust = client.database().count(filter).await? >= 1;
|
||||
let filter = Filter::new().kind(Kind::ContactList).pubkey(event.pubkey);
|
||||
let is_trust = client.database().count(filter).await? >= 1;
|
||||
|
||||
room_map
|
||||
.entry(hash)
|
||||
.and_modify(|(_, count, trusted)| {
|
||||
*count += 1;
|
||||
*trusted = is_trust;
|
||||
})
|
||||
.or_insert((event, 1, is_trust));
|
||||
}
|
||||
room_map
|
||||
.entry(hash)
|
||||
.and_modify(|(_, count, trusted)| {
|
||||
*count += 1;
|
||||
*trusted = is_trust;
|
||||
})
|
||||
.or_insert((event, 1, is_trust));
|
||||
}
|
||||
|
||||
// Sort rooms by creation date (newest first)
|
||||
let result: Vec<(Event, usize, bool)> = room_map
|
||||
.into_values()
|
||||
.sorted_by_key(|(ev, _, _)| Reverse(ev.created_at))
|
||||
@@ -119,26 +190,31 @@ impl ChatRegistry {
|
||||
|
||||
cx.spawn_in(window, async move |this, cx| {
|
||||
if let Ok(events) = task.await {
|
||||
let rooms: Vec<Entity<Room>> = events
|
||||
.into_iter()
|
||||
.map(|(event, count, trusted)| {
|
||||
let kind = if count > 2 {
|
||||
// If frequency count is greater than 2, mark this room as ongoing
|
||||
RoomKind::Ongoing
|
||||
} else if trusted {
|
||||
RoomKind::Trusted
|
||||
} else {
|
||||
RoomKind::Unknown
|
||||
};
|
||||
|
||||
cx.new(|_| Room::new(&event, kind)).unwrap()
|
||||
})
|
||||
.collect();
|
||||
|
||||
cx.update(|_, cx| {
|
||||
this.update(cx, |this, cx| {
|
||||
let ids = this.room_ids(cx);
|
||||
let rooms: Vec<Entity<Room>> = events
|
||||
.into_iter()
|
||||
.filter_map(|(event, count, trusted)| {
|
||||
let hash = room_hash(&event);
|
||||
if !ids.iter().any(|this| this == &hash) {
|
||||
let kind = if count > 2 {
|
||||
// If frequency count is greater than 2, mark this room as ongoing
|
||||
RoomKind::Ongoing
|
||||
} else if trusted {
|
||||
RoomKind::Trusted
|
||||
} else {
|
||||
RoomKind::Unknown
|
||||
};
|
||||
Some(cx.new(|_| Room::new(&event).kind(kind)))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
this.rooms.extend(rooms);
|
||||
this.rooms.sort_by_key(|r| Reverse(r.read(cx).last_seen()));
|
||||
this.rooms.sort_by_key(|r| Reverse(r.read(cx).created_at));
|
||||
this.loading = false;
|
||||
|
||||
cx.notify();
|
||||
@@ -151,48 +227,40 @@ impl ChatRegistry {
|
||||
.detach();
|
||||
}
|
||||
|
||||
/// Get the IDs of all rooms.
|
||||
pub fn room_ids(&self, cx: &mut Context<Self>) -> Vec<u64> {
|
||||
self.rooms.iter().map(|room| room.read(cx).id).collect()
|
||||
/// Add a user profile to the registry
|
||||
///
|
||||
/// Only adds the profile if it doesn't already exist or is currently none
|
||||
pub fn add_profile(
|
||||
&mut self,
|
||||
public_key: PublicKey,
|
||||
metadata: Option<Metadata>,
|
||||
cx: &mut Context<Self>,
|
||||
) {
|
||||
self.profiles.update(cx, |this, _cx| {
|
||||
this.entry(public_key)
|
||||
.and_modify(|entry| {
|
||||
if entry.is_none() {
|
||||
*entry = metadata.clone();
|
||||
}
|
||||
})
|
||||
.or_insert_with(|| metadata);
|
||||
});
|
||||
}
|
||||
|
||||
/// Get all rooms.
|
||||
pub fn rooms(&self, cx: &App) -> HashMap<RoomKind, Vec<&Entity<Room>>> {
|
||||
let mut groups = HashMap::new();
|
||||
groups.insert(RoomKind::Ongoing, Vec::new());
|
||||
groups.insert(RoomKind::Trusted, Vec::new());
|
||||
groups.insert(RoomKind::Unknown, Vec::new());
|
||||
/// Get a user profile by public key
|
||||
pub fn profile(&self, public_key: &PublicKey, cx: &App) -> Profile {
|
||||
let metadata = if let Some(profile) = self.profiles.read(cx).get(public_key) {
|
||||
profile.clone().unwrap_or_default()
|
||||
} else {
|
||||
Metadata::default()
|
||||
};
|
||||
|
||||
for room in self.rooms.iter() {
|
||||
let kind = room.read(cx).kind();
|
||||
groups.entry(kind).or_insert_with(Vec::new).push(room);
|
||||
}
|
||||
|
||||
groups
|
||||
Profile::new(*public_key, metadata)
|
||||
}
|
||||
|
||||
/// Get rooms by their kind.
|
||||
pub fn rooms_by_kind(&self, kind: RoomKind, cx: &App) -> Vec<&Entity<Room>> {
|
||||
self.rooms
|
||||
.iter()
|
||||
.filter(|room| room.read(cx).kind() == kind)
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Get the loading status of the rooms.
|
||||
pub fn loading(&self) -> bool {
|
||||
self.loading
|
||||
}
|
||||
|
||||
/// Get a room by its ID.
|
||||
pub fn get(&self, id: &u64, cx: &App) -> Option<Entity<Room>> {
|
||||
self.rooms
|
||||
.iter()
|
||||
.find(|model| model.read(cx).id == *id)
|
||||
.cloned()
|
||||
}
|
||||
|
||||
/// Push a room to the list.
|
||||
/// Add a new room to the registry
|
||||
///
|
||||
/// Returns an error if the room already exists
|
||||
pub fn push(&mut self, room: Room, cx: &mut Context<Self>) -> Result<(), anyhow::Error> {
|
||||
let room = cx.new(|_| room);
|
||||
|
||||
@@ -210,21 +278,24 @@ impl ChatRegistry {
|
||||
}
|
||||
}
|
||||
|
||||
/// Push a message to a room.
|
||||
/// Push a new message to a room
|
||||
///
|
||||
/// If the room doesn't exist, it will be created.
|
||||
/// Updates room ordering based on the most recent messages.
|
||||
pub fn push_message(&mut self, event: Event, window: &mut Window, cx: &mut Context<Self>) {
|
||||
let id = room_hash(&event);
|
||||
|
||||
if let Some(room) = self.rooms.iter().find(|room| room.read(cx).id == id) {
|
||||
room.update(cx, |this, cx| {
|
||||
this.set_last_seen(LastSeen(event.created_at), cx);
|
||||
this.created_at(event.created_at, cx);
|
||||
this.emit_message(event, window, cx);
|
||||
});
|
||||
|
||||
// Re-sort rooms by last seen
|
||||
self.rooms
|
||||
.sort_by_key(|room| Reverse(room.read(cx).last_seen()));
|
||||
.sort_by_key(|room| Reverse(room.read(cx).created_at));
|
||||
} else {
|
||||
let new_room = cx.new(|_| Room::new(&event, RoomKind::default()));
|
||||
let new_room = cx.new(|_| Room::new(&event));
|
||||
|
||||
// Push the new room to the front of the list
|
||||
self.rooms.insert(0, new_room);
|
||||
|
||||
Reference in New Issue
Block a user