feat: Implemented NIP-4e (#11)

* chore: refactor account registry

* wip: nip4e

* chore: rename account to device

* feat: nip44 encryption with master signer

* update

* refactor

* feat: unwrap with device keys

* chore: improve handler

* chore: fix rustls

* chore: refactor onboarding

* chore: fix compose

* chore: fix send message

* chore: fix forgot to request device

* fix send message

* chore: fix deadlock

* chore: small fixes

* chore: improve

* fix

* refactor

* refactor

* refactor

* fix

* add fetch request

* save keys

* fix

* update

* update

* update
This commit is contained in:
reya
2025-03-08 19:29:25 +07:00
committed by GitHub
parent 81664e3d4e
commit a53b2181ab
31 changed files with 1744 additions and 1065 deletions

View File

@@ -1,11 +1,13 @@
use crate::room::{IncomingEvent, Room};
use std::cmp::Reverse;
use anyhow::anyhow;
use common::{last_seen::LastSeen, utils::room_hash};
use global::get_client;
use gpui::{App, AppContext, Context, Entity, Global, Task, WeakEntity};
use itertools::Itertools;
use nostr_sdk::prelude::*;
use state::get_client;
use std::{cmp::Reverse, rc::Rc, sync::RwLock};
use crate::room::{IncomingEvent, Room};
pub fn init(cx: &mut App) {
ChatRegistry::register(cx);
@@ -16,7 +18,7 @@ struct GlobalChatRegistry(Entity<ChatRegistry>);
impl Global for GlobalChatRegistry {}
pub struct ChatRegistry {
rooms: Rc<RwLock<Vec<Entity<Room>>>>,
rooms: Vec<Entity<Room>>,
is_loading: bool,
}
@@ -28,13 +30,7 @@ impl ChatRegistry {
pub fn register(cx: &mut App) -> Entity<Self> {
Self::global(cx).unwrap_or_else(|| {
let entity = cx.new(|cx| {
let mut this = Self::new(cx);
// Automatically load chat rooms the database when the registry is created
this.load_chat_rooms(cx);
this
});
let entity = cx.new(Self::new);
// Set global state
cx.set_global(GlobalChatRegistry(entity.clone()));
@@ -45,18 +41,13 @@ impl ChatRegistry {
fn new(_cx: &mut Context<Self>) -> Self {
Self {
rooms: Rc::new(RwLock::new(vec![])),
rooms: vec![],
is_loading: true,
}
}
pub fn current_rooms_ids(&self, cx: &mut Context<Self>) -> Vec<u64> {
self.rooms
.read()
.unwrap()
.iter()
.map(|room| room.read(cx).id)
.collect()
self.rooms.iter().map(|room| room.read(cx).id).collect()
}
pub fn load_chat_rooms(&mut self, cx: &mut Context<Self>) {
@@ -90,10 +81,9 @@ impl ChatRegistry {
cx.spawn(|this, cx| async move {
if let Ok(events) = task.await {
cx.update(|cx| {
if !events.is_empty() {
this.update(cx, |this, cx| {
let mut rooms = this.rooms.write().unwrap();
_ = cx.update(|cx| {
_ = this.update(cx, |this, cx| {
if !events.is_empty() {
let current_ids = this.current_rooms_ids(cx);
let items: Vec<Entity<Room>> = events
.into_iter()
@@ -108,29 +98,25 @@ impl ChatRegistry {
})
.collect();
rooms.extend(items);
rooms.sort_by_key(|room| Reverse(room.read(cx).last_seen()));
this.is_loading = false;
cx.notify();
})
.ok();
} else {
this.update(cx, |this, cx| {
this.rooms.extend(items);
this.rooms
.sort_by_key(|room| Reverse(room.read(cx).last_seen()));
} else {
this.is_loading = false;
cx.notify();
})
.ok();
}
})
.ok();
}
cx.notify();
});
});
}
})
.detach();
}
pub fn rooms(&self) -> Vec<Entity<Room>> {
self.rooms.read().unwrap().clone()
pub fn rooms(&self) -> &[Entity<Room>] {
&self.rooms
}
pub fn is_loading(&self) -> bool {
@@ -139,8 +125,6 @@ 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())
@@ -151,44 +135,40 @@ impl ChatRegistry {
room: Entity<Room>,
cx: &mut Context<Self>,
) -> Result<(), anyhow::Error> {
let mut rooms = self.rooms.write().unwrap();
if !rooms
if !self
.rooms
.iter()
.any(|current| current.read(cx) == room.read(cx))
{
rooms.insert(0, room);
self.rooms.insert(0, room);
cx.notify();
Ok(())
} else {
Err(anyhow!("Room is existed"))
Err(anyhow!("Room already exists"))
}
}
pub fn push_message(&mut self, event: Event, cx: &mut Context<Self>) {
let id = room_hash(&event);
let mut rooms = self.rooms.write().unwrap();
if let Some(room) = rooms.iter().find(|room| room.read(cx).id == id) {
if let Some(room) = self.rooms.iter().find(|room| room.read(cx).id == id) {
room.update(cx, |this, cx| {
if let Some(last_seen) = Rc::get_mut(&mut this.last_seen) {
*last_seen = LastSeen(event.created_at);
}
this.last_seen = LastSeen(event.created_at);
cx.emit(IncomingEvent { event });
cx.notify();
});
// Re sort rooms by last seen
rooms.sort_by_key(|room| Reverse(room.read(cx).last_seen()));
cx.notify();
// Re-sort rooms by last seen
self.rooms
.sort_by_key(|room| Reverse(room.read(cx).last_seen()));
} else {
let new_room = Room::new(&event, cx);
let mut rooms = self.rooms.write().unwrap();
rooms.insert(0, new_room);
cx.notify();
// Push the new room to the front of the list
self.rooms.insert(0, new_room);
}
cx.notify();
}
}