feat: Chat Folders (#14)

* add room kinds

* add folders

* adjust design

* update

* refactor

* cache

* update
This commit is contained in:
reya
2025-04-06 15:29:36 +07:00
committed by GitHub
parent 16530a3804
commit f7610cc9c9
18 changed files with 1052 additions and 504 deletions

View File

@@ -7,10 +7,10 @@ use common::{
utils::{compare, room_hash},
};
use global::get_client;
use gpui::{App, AppContext, Context, Entity, EventEmitter, SharedString, Task, Window};
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task, Window};
use itertools::Itertools;
use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec};
use smallvec::SmallVec;
use crate::message::{Message, RoomMessage};
@@ -19,13 +19,25 @@ pub struct IncomingEvent {
pub event: RoomMessage,
}
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, Default)]
pub enum RoomKind {
Ongoing,
Trusted,
#[default]
Unknown,
}
pub struct Room {
pub id: u64,
pub last_seen: LastSeen,
/// Subject of the room
pub name: Option<SharedString>,
pub subject: Option<SharedString>,
/// All members of the room
pub members: Arc<SmallVec<[NostrProfile; 2]>>,
/// Kind
pub kind: RoomKind,
/// All public keys of the room members
pubkeys: Vec<PublicKey>,
}
impl EventEmitter<IncomingEvent> for Room {}
@@ -37,66 +49,34 @@ impl PartialEq for Room {
}
impl Room {
pub fn new(event: &Event, cx: &mut App) -> Entity<Self> {
/// Create a new room from an Nostr Event
pub fn new(event: &Event, kind: RoomKind) -> Self {
let id = room_hash(event);
let last_seen = LastSeen(event.created_at);
// Get the subject from the event's tags
let name = if let Some(tag) = event.tags.find(TagKind::Subject) {
let subject = if let Some(tag) = event.tags.find(TagKind::Subject) {
tag.content().map(|s| s.to_owned().into())
} else {
None
};
// Create a task for loading metadata
let load_metadata = Self::load_metadata(event, cx);
// Get all public keys from the event's tags
let mut pubkeys = vec![];
pubkeys.extend(event.tags.public_keys().collect::<HashSet<_>>());
pubkeys.push(event.pubkey);
// Create a new GPUI's Entity
cx.new(|cx| {
let this = Self {
id,
last_seen,
name,
members: Arc::new(smallvec![]),
};
cx.spawn(async move |this, cx| {
if let Ok(profiles) = load_metadata.await {
cx.update(|cx| {
this.update(cx, |this: &mut Room, cx| {
// Update the room's name if it's not already set
if this.name.is_none() {
let mut name = profiles
.iter()
.take(2)
.map(|profile| profile.name.to_string())
.collect::<Vec<_>>()
.join(", ");
if profiles.len() > 2 {
name = format!("{}, +{}", name, profiles.len() - 2);
}
this.name = Some(name.into())
};
let mut new_members = SmallVec::new();
new_members.extend(profiles);
this.members = Arc::new(new_members);
cx.notify();
})
.ok();
})
.ok();
}
})
.detach();
this
})
Self {
id,
last_seen,
subject,
kind,
members: Arc::new(SmallVec::with_capacity(pubkeys.len())),
pubkeys,
}
}
/// Get room's id
pub fn id(&self) -> u64 {
self.id
}
@@ -116,12 +96,17 @@ impl Room {
/// Collect room's member's public keys
pub fn public_keys(&self) -> Vec<PublicKey> {
self.members.iter().map(|m| m.public_key).collect()
self.pubkeys.clone()
}
/// Get room's display name
pub fn name(&self) -> Option<SharedString> {
self.name.clone()
pub fn subject(&self) -> Option<SharedString> {
self.subject.clone()
}
/// Get room's kind
pub fn kind(&self) -> RoomKind {
self.kind
}
/// Determine if room is a group
@@ -145,6 +130,31 @@ impl Room {
self.last_seen.ago()
}
pub fn update_members(&mut self, profiles: Vec<NostrProfile>, cx: &mut Context<Self>) {
// Update the room's name if it's not already set
if self.subject.is_none() {
// Merge all members into a single name
let mut name = profiles
.iter()
.take(2)
.map(|profile| profile.name.to_string())
.collect::<Vec<_>>()
.join(", ");
// Create a specific name for group
if profiles.len() > 2 {
name = format!("{}, +{}", name, profiles.len() - 2);
}
self.subject = Some(name.into());
};
// Update the room's members
self.members = Arc::new(profiles.into());
cx.notify();
}
/// Verify messaging_relays for all room's members
pub fn messaging_relays(&self, cx: &App) -> Task<Result<Vec<(PublicKey, bool)>, Error>> {
let client = get_client();
@@ -208,6 +218,38 @@ impl Room {
})
}
/// Load metadata for all members
pub fn load_metadata(&self, cx: &mut Context<Self>) -> Task<Result<Vec<NostrProfile>, Error>> {
let client = get_client();
let pubkeys = self.public_keys();
cx.background_spawn(async move {
let signer = client.signer().await?;
let signer_pubkey = signer.get_public_key().await?;
let mut profiles = Vec::with_capacity(pubkeys.len());
for public_key in pubkeys.into_iter() {
let metadata = client
.database()
.metadata(public_key)
.await?
.unwrap_or_default();
// Convert metadata to profile
let profile = NostrProfile::new(public_key, metadata);
if public_key == signer_pubkey {
// Room's owner always push to the end of the vector
profiles.push(profile);
} else {
profiles.insert(0, profile);
}
}
Ok(profiles)
})
}
/// Load room messages
pub fn load_messages(&self, cx: &App) -> Task<Result<Vec<RoomMessage>, Error>> {
let client = get_client();
@@ -350,40 +392,4 @@ impl Room {
})
.detach();
}
/// Load metadata for all members
fn load_metadata(event: &Event, cx: &App) -> Task<Result<Vec<NostrProfile>, Error>> {
let client = get_client();
let mut pubkeys = vec![];
// Get all pubkeys from event's tags
pubkeys.extend(event.tags.public_keys().collect::<HashSet<_>>());
pubkeys.push(event.pubkey);
cx.background_spawn(async move {
let signer = client.signer().await?;
let signer_pubkey = signer.get_public_key().await?;
let mut profiles = Vec::with_capacity(pubkeys.len());
for public_key in pubkeys.into_iter() {
let metadata = client
.database()
.metadata(public_key)
.await?
.unwrap_or_default();
// Convert metadata to profile
let profile = NostrProfile::new(public_key, metadata);
if public_key == signer_pubkey {
// Room's owner always push to the end of the vector
profiles.push(profile);
} else {
profiles.insert(0, profile);
}
}
Ok(profiles)
})
}
}