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();
}
}

View File

@@ -1,10 +1,11 @@
use anyhow::Error;
use std::collections::HashSet;
use anyhow::{anyhow, Context, Error};
use common::{last_seen::LastSeen, profile::NostrProfile, utils::room_hash};
use global::{constants::DEVICE_ANNOUNCEMENT_KIND, get_client, get_device_keys};
use gpui::{App, AppContext, Entity, EventEmitter, SharedString, Task};
use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec};
use state::get_client;
use std::{collections::HashSet, rc::Rc};
#[derive(Debug, Clone)]
pub struct IncomingEvent {
@@ -13,7 +14,7 @@ pub struct IncomingEvent {
pub struct Room {
pub id: u64,
pub last_seen: Rc<LastSeen>,
pub last_seen: LastSeen,
/// Subject of the room
pub name: Option<SharedString>,
/// All members of the room
@@ -31,7 +32,7 @@ impl PartialEq for Room {
impl Room {
pub fn new(event: &Event, cx: &mut App) -> Entity<Self> {
let id = room_hash(event);
let last_seen = Rc::new(LastSeen(event.created_at));
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) {
@@ -60,7 +61,7 @@ impl Room {
let mut name = profiles
.iter()
.take(2)
.map(|profile| profile.name().to_string())
.map(|profile| profile.name.to_string())
.collect::<Vec<_>>()
.join(", ");
@@ -94,7 +95,7 @@ impl Room {
pub fn member(&self, public_key: &PublicKey) -> Option<NostrProfile> {
self.members
.iter()
.find(|m| &m.public_key() == public_key)
.find(|m| &m.public_key == public_key)
.cloned()
}
@@ -105,7 +106,7 @@ 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.members.iter().map(|m| m.public_key).collect()
}
/// Get room's display name
@@ -119,8 +120,8 @@ impl Room {
}
/// Get room's last seen
pub fn last_seen(&self) -> Rc<LastSeen> {
self.last_seen.clone()
pub fn last_seen(&self) -> LastSeen {
self.last_seen
}
/// Get room's last seen as ago format
@@ -158,20 +159,26 @@ impl Room {
}
/// Send message to all room's members
///
/// NIP-4e: Message will be signed by the device signer
pub fn send_message(&self, content: String, cx: &App) -> Task<Result<Vec<String>, Error>> {
let client = get_client();
let pubkeys = self.public_keys();
cx.background_spawn(async move {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let Some(device) = get_device_keys().await else {
return Err(anyhow!("Device not found. Please restart the application."));
};
let mut msg = Vec::new();
let user_signer = client.signer().await?;
let user_pubkey = user_signer.get_public_key().await?;
let mut report = Vec::with_capacity(pubkeys.len());
let tags: Vec<Tag> = pubkeys
.iter()
.filter_map(|pubkey| {
if pubkey != &public_key {
if pubkey != &user_pubkey {
Some(Tag::public_key(*pubkey))
} else {
None
@@ -180,17 +187,52 @@ impl Room {
.collect();
for pubkey in pubkeys.iter() {
if let Err(e) = client
.send_private_msg(*pubkey, &content, tags.clone())
.await
{
log::error!("Failed to send message to {}: {}", pubkey.to_bech32()?, e);
// Convert error into string
msg.push(e.to_string());
let filter = Filter::new()
.kind(Kind::Custom(DEVICE_ANNOUNCEMENT_KIND))
.author(*pubkey)
.limit(1);
// Check if the pubkey has a device announcement,
// then choose the appropriate signer based on device presence
if let Some(event) = client.database().query(filter).await?.first_owned() {
log::info!("Use device signer to send message");
let signer = &device;
// Get the device's public key of other user
let n_tag = event.tags.find(TagKind::custom("n")).context("Not found")?;
let hex = n_tag.content().context("Not found")?;
let target_pubkey = PublicKey::parse(hex)?;
let rumor = EventBuilder::private_msg_rumor(*pubkey, &content)
.tags(tags.clone())
.build(user_pubkey);
let event = EventBuilder::gift_wrap(
signer,
&target_pubkey,
rumor,
vec![Tag::public_key(*pubkey)],
)
.await?;
if let Err(e) = client.send_event(&event).await {
// Convert error into string, then push it to the report
report.push(e.to_string());
}
} else {
log::info!("Use user signer to send message");
let signer = &client.signer().await?;
let event =
EventBuilder::private_msg(signer, *pubkey, &content, tags.clone()).await?;
if let Err(e) = client.send_event(&event).await {
report.push(e.to_string());
}
}
}
Ok(msg)
Ok(report)
})
}