feat: rewrite the nip-4e implementation (#1)
Some checks are pending
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run

Make NIP-4e a core feature, not an optional feature.

Note:
- The UI is broken and needs to be updated in a separate PR.

Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
2026-01-13 16:00:08 +08:00
parent bb455871e5
commit 75c3783522
50 changed files with 2818 additions and 3458 deletions

547
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@@ -1,22 +0,0 @@
[package]
name = "account"
version.workspace = true
edition.workspace = true
publish.workspace = true
[dependencies]
state = { path = "../state" }
settings = { path = "../settings" }
common = { path = "../common" }
theme = { path = "../theme" }
ui = { path = "../ui" }
gpui.workspace = true
nostr-sdk.workspace = true
anyhow.workspace = true
smallvec.workspace = true
smol.workspace = true
log.workspace = true
serde.workspace = true
serde_json.workspace = true

View File

@@ -1,208 +0,0 @@
use std::time::Duration;
use anyhow::Error;
use common::BOOTSTRAP_RELAYS;
use gpui::{App, AppContext, Context, Entity, Global, Task};
use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec};
use state::NostrRegistry;
pub fn init(cx: &mut App) {
Account::set_global(cx.new(Account::new), cx);
}
struct GlobalAccount(Entity<Account>);
impl Global for GlobalAccount {}
pub struct Account {
/// The public key of the account
public_key: Option<PublicKey>,
/// Status of the current user NIP-65 relays
pub nip65_status: Entity<RelayStatus>,
/// Status of the current user NIP-17 relays
pub nip17_status: Entity<RelayStatus>,
/// Tasks for asynchronous operations
_tasks: SmallVec<[Task<()>; 2]>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RelayStatus {
#[default]
Initial,
NotSet,
Set,
}
impl Account {
/// Retrieve the global account state
pub fn global(cx: &App) -> Entity<Self> {
cx.global::<GlobalAccount>().0.clone()
}
/// Check if the global account state exists
pub fn has_global(cx: &App) -> bool {
cx.has_global::<GlobalAccount>()
}
/// Remove the global account state
pub fn remove_global(cx: &mut App) {
cx.remove_global::<GlobalAccount>();
}
/// Set the global account instance
fn set_global(state: Entity<Self>, cx: &mut App) {
cx.set_global(GlobalAccount(state));
}
/// Create a new account instance
fn new(cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let nip65_status = cx.new(|_| RelayStatus::default());
let nip17_status = cx.new(|_| RelayStatus::default());
let mut tasks = smallvec![];
tasks.push(
// Observe the nostr signer and set the public key when it sets
cx.spawn(async move |this, cx| {
let result = cx
.background_spawn(async move { Self::observe_signer(&client).await })
.await;
if let Some(public_key) = result {
this.update(cx, |this, cx| {
this.set_account(public_key, cx);
})
.expect("Entity has been released")
}
}),
);
Self {
public_key: None,
nip65_status,
nip17_status,
_tasks: tasks,
}
}
/// Observe the signer and return the public key when it sets
async fn observe_signer(client: &Client) -> Option<PublicKey> {
let loop_duration = Duration::from_millis(800);
loop {
if let Ok(signer) = client.signer().await {
if let Ok(public_key) = signer.get_public_key().await {
// Get current user's gossip relays
Self::get_gossip_relays(client, public_key).await.ok()?;
return Some(public_key);
}
}
smol::Timer::after(loop_duration).await;
}
}
/// Get gossip relays for a given public key
async fn get_gossip_relays(client: &Client, public_key: PublicKey) -> Result<(), Error> {
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let filter = Filter::new()
.kind(Kind::RelayList)
.author(public_key)
.limit(1);
// Subscribe to events from the bootstrapping relays
client
.subscribe_to(BOOTSTRAP_RELAYS, filter, Some(opts))
.await?;
Ok(())
}
/// Ensure the user has NIP-65 relays
async fn ensure_nip65_relays(client: &Client, public_key: PublicKey) -> Result<bool, Error> {
let filter = Filter::new()
.kind(Kind::RelayList)
.author(public_key)
.limit(1);
// Count the number of nip65 relays event in the database
let total = client.database().count(filter).await.unwrap_or(0);
Ok(total > 0)
}
/// Ensure the user has NIP-17 relays
async fn ensure_nip17_relays(client: &Client, public_key: PublicKey) -> Result<bool, Error> {
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.limit(1);
// Count the number of nip17 relays event in the database
let total = client.database().count(filter).await.unwrap_or(0);
Ok(total > 0)
}
/// Set the public key of the account
pub fn set_account(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
// Update account's public key
self.public_key = Some(public_key);
// Add background task
self._tasks.push(
// Verify user's nip65 and nip17 relays
cx.spawn(async move |this, cx| {
cx.background_executor().timer(Duration::from_secs(5)).await;
// Fetch the NIP-65 relays event in the local database
let ensure_nip65 = Self::ensure_nip65_relays(&client, public_key).await;
// Fetch the NIP-17 relays event in the local database
let ensure_nip17 = Self::ensure_nip17_relays(&client, public_key).await;
this.update(cx, |this, cx| {
this.nip65_status.update(cx, |this, cx| {
*this = match ensure_nip65 {
Ok(true) => RelayStatus::Set,
_ => RelayStatus::NotSet,
};
cx.notify();
});
this.nip17_status.update(cx, |this, cx| {
*this = match ensure_nip17 {
Ok(true) => RelayStatus::Set,
_ => RelayStatus::NotSet,
};
cx.notify();
});
})
.expect("Entity has been released")
}),
);
cx.notify();
}
/// Check if the account entity has a public key
pub fn has_account(&self) -> bool {
self.public_key.is_some()
}
/// Get the public key of the account
pub fn public_key(&self) -> PublicKey {
// This method is only called when user is logged in, so unwrap safely
self.public_key.unwrap()
}
}

View File

@@ -253,12 +253,10 @@ impl AutoUpdater {
} }
fn check_for_updates(version: Version, cx: &AsyncApp) -> Task<Result<Vec<EventId>, Error>> { fn check_for_updates(version: Version, cx: &AsyncApp) -> Task<Result<Vec<EventId>, Error>> {
let Ok(client) = cx.update(|cx| { let client = cx.update(|cx| {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
nostr.read(cx).client() nostr.read(cx).client()
}) else { });
return Task::ready(Err(anyhow!("Entity has been released")));
};
cx.background_spawn(async move { cx.background_spawn(async move {
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE); let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
@@ -416,7 +414,7 @@ async fn install_release_macos(
downloaded_dmg: PathBuf, downloaded_dmg: PathBuf,
cx: &AsyncApp, cx: &AsyncApp,
) -> Result<(), Error> { ) -> Result<(), Error> {
let running_app_path = cx.update(|cx| cx.app_path())??; let running_app_path = cx.update(|cx| cx.app_path())?;
let running_app_filename = running_app_path let running_app_filename = running_app_path
.file_name() .file_name()
.with_context(|| format!("invalid running app path {running_app_path:?}"))?; .with_context(|| format!("invalid running app path {running_app_path:?}"))?;

View File

@@ -7,8 +7,7 @@ publish.workspace = true
[dependencies] [dependencies]
common = { path = "../common" } common = { path = "../common" }
state = { path = "../state" } state = { path = "../state" }
account = { path = "../account" } device = { path = "../device" }
encryption = { path = "../encryption" }
person = { path = "../person" } person = { path = "../person" }
settings = { path = "../settings" } settings = { path = "../settings" }

View File

@@ -5,24 +5,26 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use account::Account;
use anyhow::{anyhow, Context as AnyhowContext, Error}; use anyhow::{anyhow, Context as AnyhowContext, Error};
use common::{EventUtils, BOOTSTRAP_RELAYS, METADATA_BATCH_LIMIT}; use common::EventUtils;
use encryption::Encryption; use device::DeviceRegistry;
use flume::Sender; use flume::Sender;
use fuzzy_matcher::skim::SkimMatcherV2; use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher; use fuzzy_matcher::FuzzyMatcher;
use gpui::{App, AppContext, Context, Entity, EventEmitter, Global, Subscription, Task}; use gpui::{
pub use message::*; App, AppContext, Context, Entity, EventEmitter, Global, Subscription, Task, WeakEntity,
};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
pub use room::*;
use settings::AppSettings; use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::{initialized_at, NostrRegistry, GIFTWRAP_SUBSCRIPTION}; use state::{tracker, NostrRegistry, GIFTWRAP_SUBSCRIPTION};
mod message; mod message;
mod room; mod room;
pub use message::*;
pub use room::*;
pub fn init(cx: &mut App) { pub fn init(cx: &mut App) {
ChatRegistry::set_global(cx.new(ChatRegistry::new), cx); ChatRegistry::set_global(cx.new(ChatRegistry::new), cx);
} }
@@ -31,37 +33,51 @@ struct GlobalChatRegistry(Entity<ChatRegistry>);
impl Global for GlobalChatRegistry {} impl Global for GlobalChatRegistry {}
/// Chat event.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum ChatEvent {
/// An event to open a room by its ID
OpenRoom(u64),
/// An event to close a room by its ID
CloseRoom(u64),
/// An event to notify UI about a new chat request
Ping,
}
/// Channel signal.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
enum NostrEvent {
/// Message received from relay pool
Message(NewMessage),
/// Unwrapping status
Unwrapping(bool),
/// Eose received from relay pool
Eose,
}
/// Chat Registry /// Chat Registry
#[derive(Debug)] #[derive(Debug)]
pub struct ChatRegistry { pub struct ChatRegistry {
/// Collection of all chat rooms /// Collection of all chat rooms
pub rooms: Vec<Entity<Room>>, rooms: Vec<Entity<Room>>,
/// Loading status of the registry /// Loading status of the registry
pub loading: bool, loading: bool,
/// Async task for handling notifications /// Tracking the status of unwrapping gift wrap events.
handle_notifications: Task<()>, tracking_flag: Arc<AtomicBool>,
/// Event subscriptions /// Channel's sender for communication between nostr and gpui
_subscriptions: SmallVec<[Subscription; 1]>, sender: Sender<NostrEvent>,
/// Handle notifications asynchronous task
notifications: Option<Task<Result<(), Error>>>,
/// Tasks for asynchronous operations /// Tasks for asynchronous operations
_tasks: SmallVec<[Task<()>; 4]>, tasks: Vec<Task<()>>,
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] /// Subscriptions
pub enum ChatEvent { _subscriptions: SmallVec<[Subscription; 1]>,
OpenRoom(u64),
CloseRoom(u64),
NewChatRequest(RoomKind),
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Signal {
Loading(bool),
Message(NewMessage),
Eose,
} }
impl EventEmitter<ChatEvent> for ChatRegistry {} impl EventEmitter<ChatEvent> for ChatRegistry {}
@@ -79,81 +95,65 @@ impl ChatRegistry {
/// Create a new chat registry instance /// Create a new chat registry instance
fn new(cx: &mut Context<Self>) -> Self { fn new(cx: &mut Context<Self>) -> Self {
let encryption = Encryption::global(cx);
let encryption_key = encryption.read(cx).encryption.clone();
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let identity = nostr.read(cx).identity();
let status = Arc::new(AtomicBool::new(true)); let device = DeviceRegistry::global(cx);
let (tx, rx) = flume::bounded::<Signal>(2048); let device_signer = device.read(cx).device_signer.clone();
let handle_notifications = cx.background_spawn({ // A flag to indicate if the registry is loading
let client = nostr.read(cx).client(); let tracking_flag = Arc::new(AtomicBool::new(true));
let status = Arc::clone(&status);
let tx = tx.clone();
let signer: Option<Arc<dyn NostrSigner>> = None;
async move { Self::handle_notifications(&client, &signer, &tx, &status).await } // Channel for communication between nostr and gpui
}); let (tx, rx) = flume::bounded::<NostrEvent>(2048);
let mut tasks = vec![];
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
let mut tasks = smallvec![];
subscriptions.push( subscriptions.push(
// Observe the encryption global state // Observe the identity
cx.observe(&encryption_key, { cx.observe(&identity, |this, state, cx| {
let status = Arc::clone(&status); if state.read(cx).has_public_key() {
let tx = tx.clone(); // Handle nostr notifications
this.handle_notifications(cx);
move |this, state, cx| { // Track unwrapping progress
if let Some(signer) = state.read(cx).clone() { this.tracking(cx);
this.handle_notifications = cx.background_spawn({
let client = nostr.read(cx).client();
let status = Arc::clone(&status);
let tx = tx.clone();
let signer = Some(signer);
async move {
Self::handle_notifications(&client, &signer, &tx, &status).await
}
});
cx.notify();
} }
}),
);
subscriptions.push(
// Observe the device signer state
cx.observe(&device_signer, |this, state, cx| {
if state.read(cx).is_some() {
this.handle_notifications(cx);
} }
}), }),
); );
tasks.push( tasks.push(
// Handle unwrapping status // Update GPUI states
cx.background_spawn(
async move { Self::handle_unwrapping(&client, &status, &tx).await },
),
);
tasks.push(
// Handle new messages
cx.spawn(async move |this, cx| { cx.spawn(async move |this, cx| {
while let Ok(message) = rx.recv_async().await { while let Ok(message) = rx.recv_async().await {
match message { match message {
Signal::Message(message) => { NostrEvent::Message(message) => {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.new_message(message, cx); this.new_message(message, cx);
}) })
.expect("Entity has been released"); .ok();
} }
Signal::Eose => { NostrEvent::Eose => {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.get_rooms(cx); this.get_rooms(cx);
}) })
.expect("Entity has been released"); .ok();
} }
Signal::Loading(status) => { NostrEvent::Unwrapping(status) => {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.set_loading(status, cx); this.set_loading(status, cx);
this.get_rooms(cx); this.get_rooms(cx);
}) })
.expect("Entity has been released"); .ok();
} }
}; };
} }
@@ -163,25 +163,30 @@ impl ChatRegistry {
Self { Self {
rooms: vec![], rooms: vec![],
loading: true, loading: true,
handle_notifications, tracking_flag,
sender: tx.clone(),
notifications: None,
tasks,
_subscriptions: subscriptions, _subscriptions: subscriptions,
_tasks: tasks,
} }
} }
async fn handle_notifications<T>( /// Handle nostr notifications
client: &Client, fn handle_notifications(&mut self, cx: &mut Context<Self>) {
signer: &Option<T>, let nostr = NostrRegistry::global(cx);
tx: &Sender<Signal>, let client = nostr.read(cx).client();
status: &Arc<AtomicBool>,
) where let device = DeviceRegistry::global(cx);
T: NostrSigner, let device_signer = device.read(cx).signer(cx);
{
let initialized_at = initialized_at(); let status = self.tracking_flag.clone();
let tx = self.sender.clone();
self.tasks.push(cx.background_spawn(async move {
let initialized_at = Timestamp::now();
let subscription_id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION); let subscription_id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION);
let mut notifications = client.notifications(); let mut notifications = client.notifications();
let mut public_keys = HashSet::new();
let mut processed_events = HashSet::new(); let mut processed_events = HashSet::new();
while let Ok(notification) = notifications.recv().await { while let Ok(notification) = notifications.recv().await {
@@ -203,54 +208,54 @@ impl ChatRegistry {
} }
// Extract the rumor from the gift wrap event // Extract the rumor from the gift wrap event
match Self::extract_rumor(client, signer, event.as_ref()).await { match Self::extract_rumor(&client, &device_signer, event.as_ref()).await {
Ok(rumor) => { Ok(rumor) => match rumor.created_at >= initialized_at {
// Get all public keys
public_keys.extend(rumor.all_pubkeys());
let limit_reached = public_keys.len() >= METADATA_BATCH_LIMIT;
let done = !status.load(Ordering::Acquire) && !public_keys.is_empty();
// Get metadata for all public keys if the limit is reached
if limit_reached || done {
let public_keys = std::mem::take(&mut public_keys);
// Get metadata for the public keys
Self::get_metadata(client, public_keys).await.ok();
}
match &rumor.created_at >= initialized_at {
true => { true => {
// Check if the event is sent by coop
let sent_by_coop = {
let tracker = tracker().read().await;
tracker.is_sent_by_coop(&event.id)
};
// No need to emit if sent by coop
// the event is already emitted
if !sent_by_coop {
let new_message = NewMessage::new(event.id, rumor); let new_message = NewMessage::new(event.id, rumor);
let signal = Signal::Message(new_message); let signal = NostrEvent::Message(new_message);
if let Err(e) = tx.send_async(signal).await { tx.send_async(signal).await.ok();
log::error!("Failed to send signal: {}", e);
} }
} }
false => { false => {
status.store(true, Ordering::Release); status.store(true, Ordering::Release);
} }
} },
}
Err(e) => { Err(e) => {
log::warn!("Failed to unwrap gift wrap event: {}", e); log::warn!("Failed to unwrap: {e}");
} }
} }
} }
RelayMessage::EndOfStoredEvents(id) => { RelayMessage::EndOfStoredEvents(id) => {
if id.as_ref() == &subscription_id { if id.as_ref() == &subscription_id {
if let Err(e) = tx.send_async(Signal::Eose).await { tx.send_async(NostrEvent::Eose).await.ok();
log::error!("Failed to send signal: {}", e);
}
} }
} }
_ => {} _ => {}
} }
} }
}));
} }
async fn handle_unwrapping(client: &Client, status: &Arc<AtomicBool>, tx: &Sender<Signal>) { /// Tracking the status of unwrapping gift wrap events.
let loop_duration = Duration::from_secs(20); fn tracking(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let status = self.tracking_flag.clone();
let tx = self.sender.clone();
self.notifications = Some(cx.background_spawn(async move {
let loop_duration = Duration::from_secs(12);
let mut is_start_processing = false; let mut is_start_processing = false;
let mut total_loops = 0; let mut total_loops = 0;
@@ -260,22 +265,21 @@ impl ChatRegistry {
if status.load(Ordering::Acquire) { if status.load(Ordering::Acquire) {
is_start_processing = true; is_start_processing = true;
// Reset gift wrap processing flag // Reset gift wrap processing flag
_ = status.compare_exchange(true, false, Ordering::Release, Ordering::Relaxed); _ = status.compare_exchange(
true,
false,
Ordering::Release,
Ordering::Relaxed,
);
// Send loading signal tx.send_async(NostrEvent::Unwrapping(true)).await.ok();
if let Err(e) = tx.send_async(Signal::Loading(true)).await {
log::error!("Failed to send signal: {}", e);
}
} else { } else {
// Only run further if we are already processing // Only run further if we are already processing
// Wait until after 2 loops to prevent exiting early while events are still being processed // Wait until after 2 loops to prevent exiting early while events are still being processed
if is_start_processing && total_loops >= 2 { if is_start_processing && total_loops >= 2 {
// Send loading signal tx.send_async(NostrEvent::Unwrapping(false)).await.ok();
if let Err(e) = tx.send_async(Signal::Loading(false)).await {
log::error!("Failed to send signal: {}", e);
}
// Reset the counter // Reset the counter
is_start_processing = false; is_start_processing = false;
total_loops = 0; total_loops = 0;
@@ -284,6 +288,12 @@ impl ChatRegistry {
} }
smol::Timer::after(loop_duration).await; smol::Timer::after(loop_duration).await;
} }
}));
}
/// Get the loading status of the chat registry
pub fn loading(&self) -> bool {
self.loading
} }
/// Set the loading status of the chat registry /// Set the loading status of the chat registry
@@ -292,12 +302,12 @@ impl ChatRegistry {
cx.notify(); cx.notify();
} }
/// Get a room by its ID. /// Get a weak reference to a room by its ID.
pub fn room(&self, id: &u64, cx: &App) -> Option<Entity<Room>> { pub fn room(&self, id: &u64, cx: &App) -> Option<WeakEntity<Room>> {
self.rooms self.rooms
.iter() .iter()
.find(|model| model.read(cx).id == *id) .find(|this| &this.read(cx).id == id)
.cloned() .map(|this| this.downgrade())
} }
/// Get all ongoing rooms. /// Get all ongoing rooms.
@@ -319,11 +329,30 @@ impl ChatRegistry {
} }
/// Add a new room to the start of list. /// Add a new room to the start of list.
pub fn add_room(&mut self, room: Entity<Room>, cx: &mut Context<Self>) { pub fn add_room<I>(&mut self, room: I, cx: &mut Context<Self>)
self.rooms.insert(0, room); where
I: Into<Room>,
{
self.rooms.insert(0, cx.new(|_| room.into()));
cx.notify(); cx.notify();
} }
/// Emit an open room event.
/// If the room is new, add it to the registry.
pub fn emit_room(&mut self, room: WeakEntity<Room>, cx: &mut Context<Self>) {
if let Some(room) = room.upgrade() {
let id = room.read(cx).id;
// If the room is new, add it to the registry.
if !self.rooms.iter().any(|r| r.read(cx).id == id) {
self.rooms.insert(0, room);
}
// Emit the open room event.
cx.emit(ChatEvent::OpenRoom(id));
}
}
/// Close a room. /// Close a room.
pub fn close_room(&mut self, id: u64, cx: &mut Context<Self>) { pub fn close_room(&mut self, id: u64, cx: &mut Context<Self>) {
if self.rooms.iter().any(|r| r.read(cx).id == id) { if self.rooms.iter().any(|r| r.read(cx).id == id) {
@@ -367,17 +396,6 @@ impl ChatRegistry {
cx.notify(); cx.notify();
} }
/// Push a new room to the chat registry
pub fn push_room(&mut self, room: Entity<Room>, cx: &mut Context<Self>) {
let id = room.read(cx).id;
if !self.rooms.iter().any(|r| r.read(cx).id == id) {
self.add_room(room, cx);
}
cx.emit(ChatEvent::OpenRoom(id));
}
/// Extend the registry with new rooms. /// Extend the registry with new rooms.
fn extend_rooms(&mut self, rooms: HashSet<Room>, cx: &mut Context<Self>) { fn extend_rooms(&mut self, rooms: HashSet<Room>, cx: &mut Context<Self>) {
let mut room_map: HashMap<u64, usize> = self let mut room_map: HashMap<u64, usize> = self
@@ -410,7 +428,7 @@ impl ChatRegistry {
pub fn get_rooms(&mut self, cx: &mut Context<Self>) { pub fn get_rooms(&mut self, cx: &mut Context<Self>) {
let task = self.create_get_rooms_task(cx); let task = self.create_get_rooms_task(cx);
self._tasks.push( self.tasks.push(
// Run and finished in the background // Run and finished in the background
cx.spawn(async move |this, cx| { cx.spawn(async move |this, cx| {
match task.await { match task.await {
@@ -528,59 +546,61 @@ impl ChatRegistry {
/// If the room doesn't exist, it will be created. /// If the room doesn't exist, it will be created.
/// Updates room ordering based on the most recent messages. /// Updates room ordering based on the most recent messages.
pub fn new_message(&mut self, message: NewMessage, cx: &mut Context<Self>) { pub fn new_message(&mut self, message: NewMessage, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
// Get the unique id
let id = message.rumor.uniq_id(); let id = message.rumor.uniq_id();
// Get the author
let author = message.rumor.pubkey; let author = message.rumor.pubkey;
let account = Account::global(cx);
if let Some(room) = self.rooms.iter().find(|room| room.read(cx).id == id) { match self.rooms.iter().find(|room| room.read(cx).id == id) {
let is_new_event = message.rumor.created_at > room.read(cx).created_at; Some(room) => {
let new_message = message.rumor.created_at > room.read(cx).created_at;
let created_at = message.rumor.created_at; let created_at = message.rumor.created_at;
let event_for_emit = message.rumor.clone();
// Update room // Update room
room.update(cx, |this, cx| { room.update(cx, |this, cx| {
if is_new_event { // Update the last timestamp if the new message is newer
if new_message {
this.set_created_at(created_at, cx); this.set_created_at(created_at, cx);
} }
// Set this room is ongoing if the new message is from current user // Set this room is ongoing if the new message is from current user
if author == account.read(cx).public_key() { if author == nostr.read(cx).identity().read(cx).public_key() {
this.set_ongoing(cx); this.set_ongoing(cx);
} }
// Emit the new message to the room // Emit the new message to the room
this.emit_message(message.gift_wrap, event_for_emit.clone(), cx); this.emit_message(message, cx);
}); });
// Resort all rooms in the registry by their created at (after updated) // Resort all rooms in the registry by their created at (after updated)
if is_new_event { if new_message {
self.sort(cx); self.sort(cx);
} }
} else { }
None => {
// Push the new room to the front of the list // Push the new room to the front of the list
self.add_room(cx.new(|_| Room::from(&message.rumor)), cx); self.add_room(&message.rumor, cx);
// Notify the UI about the new room // Notify the UI about the new room
cx.emit(ChatEvent::NewChatRequest(RoomKind::default())); cx.emit(ChatEvent::Ping);
}
} }
} }
// Unwraps a gift-wrapped event and processes its contents. // Unwraps a gift-wrapped event and processes its contents.
async fn extract_rumor<T>( async fn extract_rumor(
client: &Client, client: &Client,
signer: &Option<T>, device_signer: &Option<Arc<dyn NostrSigner>>,
gift_wrap: &Event, gift_wrap: &Event,
) -> Result<UnsignedEvent, Error> ) -> Result<UnsignedEvent, Error> {
where
T: NostrSigner,
{
// Try to get cached rumor first // Try to get cached rumor first
if let Ok(event) = Self::get_rumor(client, gift_wrap.id).await { if let Ok(event) = Self::get_rumor(client, gift_wrap.id).await {
return Ok(event); return Ok(event);
} }
// Try to unwrap with the available signer // Try to unwrap with the available signer
let unwrapped = Self::try_unwrap(client, signer, gift_wrap).await?; let unwrapped = Self::try_unwrap(client, device_signer, gift_wrap).await?;
let mut rumor_unsigned = unwrapped.rumor; let mut rumor_unsigned = unwrapped.rumor;
// Generate event id for the rumor if it doesn't have one // Generate event id for the rumor if it doesn't have one
@@ -593,38 +613,27 @@ impl ChatRegistry {
} }
// Helper method to try unwrapping with different signers // Helper method to try unwrapping with different signers
async fn try_unwrap<T>( async fn try_unwrap(
client: &Client, client: &Client,
signer: &Option<T>, device_signer: &Option<Arc<dyn NostrSigner>>,
gift_wrap: &Event, gift_wrap: &Event,
) -> Result<UnwrappedGift, Error> ) -> Result<UnwrappedGift, Error> {
where if let Some(signer) = device_signer.as_ref() {
T: NostrSigner, let seal = signer
{
if let Some(custom_signer) = signer.as_ref() {
if let Ok(seal) = custom_signer
.nip44_decrypt(&gift_wrap.pubkey, &gift_wrap.content) .nip44_decrypt(&gift_wrap.pubkey, &gift_wrap.content)
.await .await?;
{
let seal: Event = Event::from_json(seal)?; let seal: Event = Event::from_json(seal)?;
seal.verify_with_ctx(&SECP256K1)?; seal.verify_with_ctx(&SECP256K1)?;
// Decrypt the rumor let rumor = signer.nip44_decrypt(&seal.pubkey, &seal.content).await?;
// TODO: verify the sender
let rumor = custom_signer
.nip44_decrypt(&seal.pubkey, &seal.content)
.await?;
// Construct the unsigned event
let rumor = UnsignedEvent::from_json(rumor)?; let rumor = UnsignedEvent::from_json(rumor)?;
// Return the unwrapped gift
return Ok(UnwrappedGift { return Ok(UnwrappedGift {
sender: rumor.pubkey, sender: seal.pubkey,
rumor, rumor,
}); });
} }
}
let signer = client.signer().await?; let signer = client.signer().await?;
let unwrapped = UnwrappedGift::from_gift_wrap(&signer, gift_wrap).await?; let unwrapped = UnwrappedGift::from_gift_wrap(&signer, gift_wrap).await?;
@@ -695,33 +704,6 @@ impl ChatRegistry {
} }
} }
/// Get metadata for a list of public keys
async fn get_metadata<I>(client: &Client, public_keys: I) -> Result<(), Error>
where
I: IntoIterator<Item = PublicKey>,
{
let authors: Vec<PublicKey> = public_keys.into_iter().collect();
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let kinds = vec![Kind::Metadata, Kind::ContactList];
// Return if the list is empty
if authors.is_empty() {
return Err(anyhow!("You need at least one public key".to_string(),));
}
let filter = Filter::new()
.limit(authors.len() * kinds.len())
.authors(authors)
.kinds(kinds);
// Subscribe to filters to the bootstrap relays
client
.subscribe_to(BOOTSTRAP_RELAYS, filter, Some(opts))
.await?;
Ok(())
}
/// Get the conversation ID for a given rumor (message). /// Get the conversation ID for a given rumor (message).
fn conversation_id(rumor: &UnsignedEvent) -> u64 { fn conversation_id(rumor: &UnsignedEvent) -> u64 {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();

View File

@@ -2,6 +2,7 @@ use std::hash::Hash;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
/// New message.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct NewMessage { pub struct NewMessage {
pub gift_wrap: EventId, pub gift_wrap: EventId,
@@ -14,6 +15,7 @@ impl NewMessage {
} }
} }
/// Message.
#[derive(Debug, Clone, Hash, PartialEq, Eq)] #[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum Message { pub enum Message {
User(RenderedMessage), User(RenderedMessage),
@@ -22,11 +24,17 @@ pub enum Message {
} }
impl Message { impl Message {
pub fn user(user: impl Into<RenderedMessage>) -> Self { pub fn user<I>(user: I) -> Self
where
I: Into<RenderedMessage>,
{
Self::User(user.into()) Self::User(user.into())
} }
pub fn warning(content: impl Into<String>) -> Self { pub fn warning<I>(content: I) -> Self
where
I: Into<String>,
{
Self::Warning(content.into(), Timestamp::now()) Self::Warning(content.into(), Timestamp::now())
} }
@@ -43,6 +51,18 @@ impl Message {
} }
} }
impl From<&NewMessage> for Message {
fn from(val: &NewMessage) -> Self {
Self::User(val.into())
}
}
impl From<&UnsignedEvent> for Message {
fn from(val: &UnsignedEvent) -> Self {
Self::User(val.into())
}
}
impl Ord for Message { impl Ord for Message {
fn cmp(&self, other: &Self) -> std::cmp::Ordering { fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match (self, other) { match (self, other) {
@@ -63,6 +83,7 @@ impl PartialOrd for Message {
} }
} }
/// Rendered message.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct RenderedMessage { pub struct RenderedMessage {
pub id: EventId, pub id: EventId,
@@ -78,48 +99,53 @@ pub struct RenderedMessage {
pub replies_to: Vec<EventId>, pub replies_to: Vec<EventId>,
} }
impl From<Event> for RenderedMessage { impl From<&Event> for RenderedMessage {
fn from(inner: Event) -> Self { fn from(val: &Event) -> Self {
let mentions = extract_mentions(&inner.content); let mentions = extract_mentions(&val.content);
let replies_to = extract_reply_ids(&inner.tags); let replies_to = extract_reply_ids(&val.tags);
Self { Self {
id: inner.id, id: val.id,
author: inner.pubkey, author: val.pubkey,
content: inner.content, content: val.content.clone(),
created_at: inner.created_at, created_at: val.created_at,
mentions, mentions,
replies_to, replies_to,
} }
} }
} }
impl From<UnsignedEvent> for RenderedMessage { impl From<&UnsignedEvent> for RenderedMessage {
fn from(inner: UnsignedEvent) -> Self { fn from(val: &UnsignedEvent) -> Self {
let mentions = extract_mentions(&inner.content); let mentions = extract_mentions(&val.content);
let replies_to = extract_reply_ids(&inner.tags); let replies_to = extract_reply_ids(&val.tags);
Self { Self {
// Event ID must be known // Event ID must be known
id: inner.id.unwrap(), id: val.id.unwrap(),
author: inner.pubkey, author: val.pubkey,
content: inner.content, content: val.content.clone(),
created_at: inner.created_at, created_at: val.created_at,
mentions, mentions,
replies_to, replies_to,
} }
} }
} }
impl From<Box<Event>> for RenderedMessage { impl From<&NewMessage> for RenderedMessage {
fn from(inner: Box<Event>) -> Self { fn from(val: &NewMessage) -> Self {
(*inner).into() let mentions = extract_mentions(&val.rumor.content);
} let replies_to = extract_reply_ids(&val.rumor.tags);
}
impl From<&Box<Event>> for RenderedMessage { Self {
fn from(inner: &Box<Event>) -> Self { // Event ID must be known
inner.to_owned().into() id: val.rumor.id.unwrap(),
author: val.rumor.pubkey,
content: val.rumor.content.clone(),
created_at: val.rumor.created_at,
mentions,
replies_to,
}
} }
} }
@@ -149,6 +175,7 @@ impl Hash for RenderedMessage {
} }
} }
/// Extracts all mentions (public keys) from a content string.
fn extract_mentions(content: &str) -> Vec<PublicKey> { fn extract_mentions(content: &str) -> Vec<PublicKey> {
let parser = NostrParser::new(); let parser = NostrParser::new();
let tokens = parser.parse(content); let tokens = parser.parse(content);
@@ -165,6 +192,7 @@ fn extract_mentions(content: &str) -> Vec<PublicKey> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
} }
/// Extracts all reply (ids) from the event tags.
fn extract_reply_ids(inner: &Tags) -> Vec<EventId> { fn extract_reply_ids(inner: &Tags) -> Vec<EventId> {
let mut replies_to = vec![]; let mut replies_to = vec![];

View File

@@ -3,43 +3,18 @@ use std::collections::{HashMap, HashSet};
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::time::Duration; use std::time::Duration;
use account::Account; use anyhow::Error;
use anyhow::{anyhow, Error}; use common::EventUtils;
use common::{EventUtils, RenderedProfile};
use encryption::{Encryption, SignerKind};
use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task}; use gpui::{App, AppContext, Context, EventEmitter, SharedString, Task};
use itertools::Itertools; use itertools::Itertools;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::PersonRegistry; use person::{Person, PersonRegistry};
use state::NostrRegistry; use state::{tracker, NostrRegistry};
use crate::NewMessage;
const SEND_RETRY: usize = 10; const SEND_RETRY: usize = 10;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct SendOptions {
pub backup: bool,
pub signer_kind: SignerKind,
}
impl SendOptions {
pub fn new() -> Self {
Self {
backup: true,
signer_kind: SignerKind::default(),
}
}
pub fn backup(&self) -> bool {
self.backup
}
}
impl Default for SendOptions {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SendReport { pub struct SendReport {
pub receiver: PublicKey, pub receiver: PublicKey,
@@ -107,17 +82,21 @@ impl SendReport {
} }
} }
#[derive(Debug, Clone)] /// Room event.
pub enum RoomSignal { #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
NewMessage((EventId, UnsignedEvent)), pub enum RoomEvent {
Refresh, /// Incoming message.
Incoming(NewMessage),
/// Reloads the current room's messages.
Reload,
} }
/// Room kind.
#[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Default)] #[derive(Clone, Copy, Hash, Debug, PartialEq, Eq, PartialOrd, Ord, Default)]
pub enum RoomKind { pub enum RoomKind {
Ongoing,
#[default] #[default]
Request, Request,
Ongoing,
} }
#[derive(Debug)] #[derive(Debug)]
@@ -160,7 +139,7 @@ impl Hash for Room {
impl Eq for Room {} impl Eq for Room {}
impl EventEmitter<RoomSignal> for Room {} impl EventEmitter<RoomEvent> for Room {}
impl From<&UnsignedEvent> for Room { impl From<&UnsignedEvent> for Room {
fn from(val: &UnsignedEvent) -> Self { fn from(val: &UnsignedEvent) -> Self {
@@ -168,7 +147,7 @@ impl From<&UnsignedEvent> for Room {
let created_at = val.created_at; let created_at = val.created_at;
// Get the members from the event's tags and event's pubkey // Get the members from the event's tags and event's pubkey
let members = val.all_pubkeys(); let members = val.extract_public_keys();
// Get subject from tags // Get subject from tags
let subject = val let subject = val
@@ -248,6 +227,28 @@ impl Room {
self.members.clone() self.members.clone()
} }
/// Returns the members of the room with their messaging relays
pub fn members_with_relays(&self, cx: &App) -> Task<Vec<(PublicKey, Vec<RelayUrl>)>> {
let nostr = NostrRegistry::global(cx);
let mut tasks = vec![];
for member in self.members.iter() {
let task = nostr.read(cx).messaging_relays(member, cx);
tasks.push((*member, task));
}
cx.background_spawn(async move {
let mut results = vec![];
for (public_key, task) in tasks.into_iter() {
let urls = task.await;
results.push((public_key, urls));
}
results
})
}
/// Checks if the room has more than two members (group) /// Checks if the room has more than two members (group)
pub fn is_group(&self) -> bool { pub fn is_group(&self) -> bool {
self.members.len() > 2 self.members.len() > 2
@@ -263,9 +264,9 @@ impl Room {
} }
/// Gets the display image for the room /// Gets the display image for the room
pub fn display_image(&self, proxy: bool, cx: &App) -> SharedString { pub fn display_image(&self, cx: &App) -> SharedString {
if !self.is_group() { if !self.is_group() {
self.display_member(cx).avatar(proxy) self.display_member(cx).avatar()
} else { } else {
SharedString::from("brand/group.png") SharedString::from("brand/group.png")
} }
@@ -274,10 +275,10 @@ impl Room {
/// Get a member to represent the room /// Get a member to represent the room
/// ///
/// Display member is always different from the current user. /// Display member is always different from the current user.
pub fn display_member(&self, cx: &App) -> Profile { pub fn display_member(&self, cx: &App) -> Person {
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let account = Account::global(cx); let nostr = NostrRegistry::global(cx);
let public_key = account.read(cx).public_key(); let public_key = nostr.read(cx).identity().read(cx).public_key();
let target_member = self let target_member = self
.members .members
@@ -286,7 +287,7 @@ impl Room {
.or_else(|| self.members.first()) .or_else(|| self.members.first())
.expect("Room should have at least one member"); .expect("Room should have at least one member");
persons.read(cx).get_person(target_member, cx) persons.read(cx).get(target_member, cx)
} }
/// Merge the names of the first two members of the room. /// Merge the names of the first two members of the room.
@@ -294,10 +295,10 @@ impl Room {
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
if self.is_group() { if self.is_group() {
let profiles: Vec<Profile> = self let profiles: Vec<Person> = self
.members .members
.iter() .iter()
.map(|public_key| persons.read(cx).get_person(public_key, cx)) .map(|public_key| persons.read(cx).get(public_key, cx))
.collect(); .collect();
let mut name = profiles let mut name = profiles
@@ -313,18 +314,18 @@ impl Room {
SharedString::from(name) SharedString::from(name)
} else { } else {
self.display_member(cx).display_name() self.display_member(cx).name()
} }
} }
/// Emits a new message signal to the current room /// Emits a new message signal to the current room
pub fn emit_message(&self, id: EventId, event: UnsignedEvent, cx: &mut Context<Self>) { pub fn emit_message(&self, message: NewMessage, cx: &mut Context<Self>) {
cx.emit(RoomSignal::NewMessage((id, event))); cx.emit(RoomEvent::Incoming(message));
} }
/// Emits a signal to refresh the current room's messages. /// Emits a signal to reload the current room's messages.
pub fn emit_refresh(&mut self, cx: &mut Context<Self>) { pub fn emit_refresh(&mut self, cx: &mut Context<Self>) {
cx.emit(RoomSignal::Refresh); cx.emit(RoomEvent::Reload);
} }
/// Get gossip relays for each member /// Get gossip relays for each member
@@ -332,11 +333,16 @@ impl Room {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let members = self.members(); let members = self.members();
let id = SubscriptionId::new(format!("room-{}", self.id));
cx.background_spawn(async move { cx.background_spawn(async move {
let signer = client.signer().await?; let signer = client.signer().await?;
let public_key = signer.get_public_key().await?; let public_key = signer.get_public_key().await?;
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
// Subscription options
let opts = SubscribeAutoCloseOptions::default()
.timeout(Some(Duration::from_secs(2)))
.exit_policy(ReqExitPolicy::ExitOnEOSE);
for member in members.into_iter() { for member in members.into_iter() {
if member == public_key { if member == public_key {
@@ -347,7 +353,9 @@ impl Room {
let filter = Filter::new().kind(Kind::RelayList).author(member).limit(1); let filter = Filter::new().kind(Kind::RelayList).author(member).limit(1);
// Subscribe to get member's gossip relays // Subscribe to get member's gossip relays
client.subscribe(filter, Some(opts)).await?; client
.subscribe_with_id(id.clone(), filter, Some(opts))
.await?;
} }
Ok(()) Ok(())
@@ -381,12 +389,9 @@ impl Room {
/// Create a new message event (unsigned) /// Create a new message event (unsigned)
pub fn create_message(&self, content: &str, replies: &[EventId], cx: &App) -> UnsignedEvent { pub fn create_message(&self, content: &str, replies: &[EventId], cx: &App) -> UnsignedEvent {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let gossip = nostr.read(cx).gossip();
let read_gossip = gossip.read_blocking();
// Get current user // Get current user
let account = Account::global(cx); let public_key = nostr.read(cx).identity().read(cx).public_key();
let public_key = account.read(cx).public_key();
// Get room's subject // Get room's subject
let subject = self.subject.clone(); let subject = self.subject.clone();
@@ -398,7 +403,7 @@ impl Room {
// NOTE: current user will be removed from the list of receivers // NOTE: current user will be removed from the list of receivers
for member in self.members.iter() { for member in self.members.iter() {
// Get relay hint if available // Get relay hint if available
let relay_url = read_gossip.messaging_relays(member).first().cloned(); let relay_url = nostr.read(cx).relay_hint(member, cx);
// Construct a public key tag with relay hint // Construct a public key tag with relay hint
let tag = TagStandard::PublicKey { let tag = TagStandard::PublicKey {
@@ -449,98 +454,65 @@ impl Room {
pub fn send_message( pub fn send_message(
&self, &self,
rumor: &UnsignedEvent, rumor: &UnsignedEvent,
opts: &SendOptions,
cx: &App, cx: &App,
) -> Task<Result<Vec<SendReport>, Error>> { ) -> Task<Result<Vec<SendReport>, Error>> {
let encryption = Encryption::global(cx);
let encryption_key = encryption.read(cx).encryption_key(cx);
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
let tracker = nostr.read(cx).tracker(); // Get current user's public key and relays
let current_user = nostr.read(cx).identity().read(cx).public_key();
let current_user_relays = nostr.read(cx).messaging_relays(&current_user, cx);
let rumor = rumor.to_owned(); let rumor = rumor.to_owned();
let opts = opts.to_owned();
// Get all members // Get all members and their messaging relays
let mut members = self.members(); let task = self.members_with_relays(cx);
cx.background_spawn(async move { cx.background_spawn(async move {
let signer_kind = opts.signer_kind; let signer = client.signer().await?;
let gossip = gossip.read().await; let current_user_relays = current_user_relays.await;
let mut members = task.await;
// Get current user's signer and public key
let user_signer = client.signer().await?;
let user_pubkey = user_signer.get_public_key().await?;
// Get the encryption public key
let encryption_pubkey = if let Some(signer) = encryption_key.as_ref() {
signer.get_public_key().await.ok()
} else {
None
};
// Remove the current user's public key from the list of receivers // Remove the current user's public key from the list of receivers
// the current user will be handled separately // the current user will be handled separately
members.retain(|&pk| pk != user_pubkey); members.retain(|(this, _)| this != &current_user);
// Determine the signer will be used based on the provided options
let signer = Self::select_signer(&opts.signer_kind, user_signer, encryption_key)?;
// Collect the send reports // Collect the send reports
let mut reports: Vec<SendReport> = vec![]; let mut reports: Vec<SendReport> = vec![];
for member in members.into_iter() { for (receiver, relays) in members.into_iter() {
// Get user's messaging relays
let urls = gossip.messaging_relays(&member);
// Get user's encryption public key if available
let encryption = gossip.announcement(&member).map(|a| a.public_key());
// Check if there are any relays to send the message to // Check if there are any relays to send the message to
if urls.is_empty() { if relays.is_empty() {
reports.push(SendReport::new(member).relays_not_found()); reports.push(SendReport::new(receiver).relays_not_found());
continue; continue;
} }
// Skip sending if using encryption signer but receiver's encryption keys not found // Ensure relay connection
if encryption.is_none() && matches!(signer_kind, SignerKind::Encryption) { for url in relays.iter() {
reports.push(SendReport::new(member).device_not_found()); client.add_relay(url).await?;
continue; client.connect_relay(url).await?;
} }
// Ensure connections to the relays
gossip.ensure_connections(&client, &urls).await;
// Determine the receiver based on the signer kind
let receiver = Self::select_receiver(&signer_kind, member, encryption)?;
// Construct the gift wrap event // Construct the gift wrap event
let event = EventBuilder::gift_wrap( let event =
&signer, EventBuilder::gift_wrap(&signer, &receiver, rumor.clone(), vec![]).await?;
&receiver,
rumor.clone(),
vec![Tag::public_key(member)],
)
.await?;
// Send the gift wrap event to the messaging relays // Send the gift wrap event to the messaging relays
match client.send_event_to(urls, &event).await { match client.send_event_to(relays, &event).await {
Ok(output) => { Ok(output) => {
let id = output.id().to_owned(); let id = output.id().to_owned();
let auth = output.failed.iter().any(|(_, s)| s.starts_with("auth-")); let auth = output.failed.iter().any(|(_, s)| s.starts_with("auth-"));
let report = SendReport::new(receiver).status(output); let report = SendReport::new(receiver).status(output);
let tracker = tracker().read().await;
if auth { if auth {
// Wait for authenticated and resent event successfully // Wait for authenticated and resent event successfully
for attempt in 0..=SEND_RETRY { for attempt in 0..=SEND_RETRY {
let tracker = tracker.read().await;
let ids = tracker.resent_ids();
// Check if event was successfully resent // Check if event was successfully resent
if let Some(output) = ids.iter().find(|e| e.id() == &id).cloned() { if tracker.is_sent_by_coop(&id) {
let output = SendReport::new(receiver).status(output); let output = Output::new(id);
reports.push(output); let report = SendReport::new(receiver).status(output);
reports.push(report);
break; break;
} }
@@ -562,55 +534,35 @@ impl Room {
} }
} }
// Return early if the user disabled backup.
//
// Coop will not send a gift wrap event to the current user.
if !opts.backup() {
return Ok(reports);
}
// Skip sending if using encryption signer but receiver's encryption keys not found
if encryption_pubkey.is_none() && matches!(signer_kind, SignerKind::Encryption) {
reports.push(SendReport::new(user_pubkey).device_not_found());
return Ok(reports);
}
// Determine the receiver based on the signer kind
let receiver = Self::select_receiver(&signer_kind, user_pubkey, encryption_pubkey)?;
// Construct the gift-wrapped event // Construct the gift-wrapped event
let event = EventBuilder::gift_wrap( let event =
&signer, EventBuilder::gift_wrap(&signer, &current_user, rumor.clone(), vec![]).await?;
&receiver,
rumor.clone(),
vec![Tag::public_key(user_pubkey)],
)
.await?;
// Only send a backup message to current user if sent successfully to others // Only send a backup message to current user if sent successfully to others
if reports.iter().all(|r| r.is_sent_success()) { if reports.iter().all(|r| r.is_sent_success()) {
let urls = gossip.messaging_relays(&user_pubkey);
// Check if there are any relays to send the event to // Check if there are any relays to send the event to
if urls.is_empty() { if current_user_relays.is_empty() {
reports.push(SendReport::new(user_pubkey).relays_not_found()); reports.push(SendReport::new(current_user).relays_not_found());
return Ok(reports); return Ok(reports);
} }
// Ensure connections to the relays // Ensure relay connection
gossip.ensure_connections(&client, &urls).await; for url in current_user_relays.iter() {
client.add_relay(url).await?;
client.connect_relay(url).await?;
}
// Send the event to the messaging relays // Send the event to the messaging relays
match client.send_event_to(urls, &event).await { match client.send_event_to(current_user_relays, &event).await {
Ok(output) => { Ok(output) => {
reports.push(SendReport::new(user_pubkey).status(output)); reports.push(SendReport::new(current_user).status(output));
} }
Err(e) => { Err(e) => {
reports.push(SendReport::new(user_pubkey).error(e.to_string())); reports.push(SendReport::new(current_user).error(e.to_string()));
} }
} }
} else { } else {
reports.push(SendReport::new(user_pubkey).on_hold(event)); reports.push(SendReport::new(current_user).on_hold(event));
} }
Ok(reports) Ok(reports)
@@ -625,10 +577,8 @@ impl Room {
) -> Task<Result<Vec<SendReport>, Error>> { ) -> Task<Result<Vec<SendReport>, Error>> {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
cx.background_spawn(async move { cx.background_spawn(async move {
let gossip = gossip.read().await;
let mut resend_reports = vec![]; let mut resend_reports = vec![];
for report in reports.into_iter() { for report in reports.into_iter() {
@@ -657,17 +607,8 @@ impl Room {
// Process the on hold event if it exists // Process the on hold event if it exists
if let Some(event) = report.on_hold { if let Some(event) = report.on_hold {
let urls = gossip.messaging_relays(&receiver);
// Check if there are any relays to send the event to
if urls.is_empty() {
resend_reports.push(SendReport::new(receiver).relays_not_found());
} else {
// Ensure connections to the relays
gossip.ensure_connections(&client, &urls).await;
// Send the event to the messaging relays // Send the event to the messaging relays
match client.send_event_to(urls, &event).await { match client.send_event(&event).await {
Ok(output) => { Ok(output) => {
resend_reports.push(SendReport::new(receiver).status(output)); resend_reports.push(SendReport::new(receiver).status(output));
} }
@@ -677,36 +618,8 @@ impl Room {
} }
} }
} }
}
Ok(resend_reports) Ok(resend_reports)
}) })
} }
fn select_signer<T>(kind: &SignerKind, user: T, encryption: Option<T>) -> Result<T, Error>
where
T: NostrSigner,
{
match kind {
SignerKind::Encryption => {
Ok(encryption.ok_or_else(|| anyhow!("No encryption key found"))?)
}
SignerKind::User => Ok(user),
SignerKind::Auto => Ok(encryption.unwrap_or(user)),
}
}
fn select_receiver(
kind: &SignerKind,
member: PublicKey,
encryption: Option<PublicKey>,
) -> Result<PublicKey, Error> {
match kind {
SignerKind::Encryption => {
Ok(encryption.ok_or_else(|| anyhow!("Receiver's encryption key not found"))?)
}
SignerKind::User => Ok(member),
SignerKind::Auto => Ok(encryption.unwrap_or(member)),
}
}
} }

View File

@@ -9,8 +9,6 @@ state = { path = "../state" }
ui = { path = "../ui" } ui = { path = "../ui" }
theme = { path = "../theme" } theme = { path = "../theme" }
common = { path = "../common" } common = { path = "../common" }
account = { path = "../account" }
encryption = { path = "../encryption" }
person = { path = "../person" } person = { path = "../person" }
chat = { path = "../chat" } chat = { path = "../chat" }
settings = { path = "../settings" } settings = { path = "../settings" }

View File

@@ -1,4 +1,3 @@
use encryption::SignerKind;
use gpui::Action; use gpui::Action;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use serde::Deserialize; use serde::Deserialize;
@@ -7,10 +6,6 @@ use serde::Deserialize;
#[action(namespace = chat, no_json)] #[action(namespace = chat, no_json)]
pub struct SeenOn(pub EventId); pub struct SeenOn(pub EventId);
#[derive(Action, Clone, PartialEq, Eq, Deserialize)]
#[action(namespace = chat, no_json)]
pub struct SetSigner(pub SignerKind);
/// Define a open public key action /// Define a open public key action
#[derive(Action, Clone, PartialEq, Eq, Deserialize, Debug)] #[derive(Action, Clone, PartialEq, Eq, Deserialize, Debug)]
#[action(namespace = pubkey, no_json)] #[action(namespace = pubkey, no_json)]

View File

@@ -2,22 +2,21 @@ use std::collections::HashSet;
use std::time::Duration; use std::time::Duration;
pub use actions::*; pub use actions::*;
use chat::{Message, RenderedMessage, Room, RoomKind, RoomSignal, SendOptions, SendReport}; use chat::{Message, RenderedMessage, Room, RoomEvent, RoomKind, SendReport};
use common::{nip96_upload, RenderedProfile, RenderedTimestamp}; use common::{nip96_upload, RenderedTimestamp};
use encryption::SignerKind;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, img, list, px, red, relative, rems, svg, white, AnyElement, App, AppContext, div, img, list, px, red, relative, rems, svg, white, AnyElement, App, AppContext,
ClipboardItem, Context, Element, Entity, EventEmitter, Flatten, FocusHandle, Focusable, ClipboardItem, Context, Entity, EventEmitter, FocusHandle, Focusable, InteractiveElement,
InteractiveElement, IntoElement, ListAlignment, ListOffset, ListState, MouseButton, ObjectFit, IntoElement, ListAlignment, ListOffset, ListState, MouseButton, ObjectFit, ParentElement,
ParentElement, PathPromptOptions, Render, RetainAllImageCache, SharedString, PathPromptOptions, Render, RetainAllImageCache, SharedString, StatefulInteractiveElement,
StatefulInteractiveElement, Styled, StyledImage, Subscription, Task, Window, Styled, StyledImage, Subscription, Task, WeakEntity, Window,
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use indexset::{BTreeMap, BTreeSet}; use indexset::{BTreeMap, BTreeSet};
use itertools::Itertools; use itertools::Itertools;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::PersonRegistry; use person::{Person, PersonRegistry};
use settings::AppSettings; use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use smol::fs; use smol::fs;
@@ -28,7 +27,6 @@ use ui::button::{Button, ButtonVariants};
use ui::context_menu::ContextMenuExt; use ui::context_menu::ContextMenuExt;
use ui::dock_area::panel::{Panel, PanelEvent}; use ui::dock_area::panel::{Panel, PanelEvent};
use ui::input::{InputEvent, InputState, TextInput}; use ui::input::{InputEvent, InputState, TextInput};
use ui::modal::ModalButtonProps;
use ui::notification::Notification; use ui::notification::Notification;
use ui::popup_menu::PopupMenuExt; use ui::popup_menu::PopupMenuExt;
use ui::{ use ui::{
@@ -41,43 +39,54 @@ use crate::text::RenderedText;
mod actions; mod actions;
mod emoji; mod emoji;
mod subject;
mod text; mod text;
pub fn init(room: Entity<Room>, window: &mut Window, cx: &mut App) -> Entity<ChatPanel> { pub fn init(room: WeakEntity<Room>, window: &mut Window, cx: &mut App) -> Entity<ChatPanel> {
cx.new(|cx| ChatPanel::new(room, window, cx)) cx.new(|cx| ChatPanel::new(room, window, cx))
} }
/// Chat Panel
pub struct ChatPanel { pub struct ChatPanel {
// Chat Room
room: Entity<Room>,
// Messages
list_state: ListState,
messages: BTreeSet<Message>,
rendered_texts_by_id: BTreeMap<EventId, RenderedText>,
reports_by_id: BTreeMap<EventId, Vec<SendReport>>,
// New Message
input: Entity<InputState>,
options: Entity<SendOptions>,
replies_to: Entity<HashSet<EventId>>,
// Media Attachment
attachments: Entity<Vec<Url>>,
uploading: bool,
// Panel
id: SharedString, id: SharedString,
focus_handle: FocusHandle, focus_handle: FocusHandle,
image_cache: Entity<RetainAllImageCache>, image_cache: Entity<RetainAllImageCache>,
_subscriptions: SmallVec<[Subscription; 3]>, /// Chat Room
_tasks: SmallVec<[Task<()>; 2]>, room: WeakEntity<Room>,
/// Message list state
list_state: ListState,
/// All messages
messages: BTreeSet<Message>,
/// Mapping message ids to their rendered texts
rendered_texts_by_id: BTreeMap<EventId, RenderedText>,
/// Mapping message ids to their reports
reports_by_id: BTreeMap<EventId, Vec<SendReport>>,
/// Input state
input: Entity<InputState>,
/// Replies to
replies_to: Entity<HashSet<EventId>>,
/// Media Attachment
attachments: Entity<Vec<Url>>,
/// Upload state
uploading: bool,
/// Async operations
tasks: SmallVec<[Task<()>; 2]>,
/// Event subscriptions
_subscriptions: SmallVec<[Subscription; 2]>,
} }
impl ChatPanel { impl ChatPanel {
pub fn new(room: Entity<Room>, window: &mut Window, cx: &mut Context<Self>) -> Self { pub fn new(room: WeakEntity<Room>, window: &mut Window, cx: &mut Context<Self>) -> Self {
let input = cx.new(|cx| { let input = cx.new(|cx| {
InputState::new(window, cx) InputState::new(window, cx)
.placeholder("Message...") .placeholder("Message...")
@@ -88,18 +97,18 @@ impl ChatPanel {
let attachments = cx.new(|_| vec![]); let attachments = cx.new(|_| vec![]);
let replies_to = cx.new(|_| HashSet::new()); let replies_to = cx.new(|_| HashSet::new());
let options = cx.new(|_| SendOptions::default());
let id = room.read(cx).id.to_string().into();
let messages = BTreeSet::from([Message::system()]); let messages = BTreeSet::from([Message::system()]);
let list_state = ListState::new(messages.len(), ListAlignment::Bottom, px(1024.)); let list_state = ListState::new(messages.len(), ListAlignment::Bottom, px(1024.));
let connect = room.read(cx).connect(cx); let id: SharedString = room
let get_messages = room.read(cx).get_messages(cx); .read_with(cx, |this, _cx| this.id.to_string().into())
.unwrap_or("Unknown".into());
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
let mut tasks = smallvec![]; let mut tasks = smallvec![];
if let Ok(connect) = room.read_with(cx, |this, cx| this.connect(cx)) {
tasks.push( tasks.push(
// Get messaging relays and encryption keys announcement for each member // Get messaging relays and encryption keys announcement for each member
cx.background_spawn(async move { cx.background_spawn(async move {
@@ -108,7 +117,9 @@ impl ChatPanel {
} }
}), }),
); );
};
if let Ok(get_messages) = room.read_with(cx, |this, cx| this.get_messages(cx)) {
tasks.push( tasks.push(
// Load all messages belonging to this room // Load all messages belonging to this room
cx.spawn_in(window, async move |this, cx| { cx.spawn_in(window, async move |this, cx| {
@@ -117,7 +128,7 @@ impl ChatPanel {
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {
match result { match result {
Ok(events) => { Ok(events) => {
this.insert_messages(events, cx); this.insert_messages(&events, cx);
} }
Err(e) => { Err(e) => {
window.push_notification(e.to_string(), cx); window.push_notification(e.to_string(), cx);
@@ -127,6 +138,23 @@ impl ChatPanel {
.ok(); .ok();
}), }),
); );
}
if let Some(room) = room.upgrade() {
subscriptions.push(
// Subscribe to room events
cx.subscribe_in(&room, window, move |this, _room, event, window, cx| {
match event {
RoomEvent::Incoming(message) => {
this.insert_message(message, false, cx);
}
RoomEvent::Reload => {
this.load_messages(window, cx);
}
};
}),
);
}
subscriptions.push( subscriptions.push(
// Subscribe to input events // Subscribe to input events
@@ -141,47 +169,6 @@ impl ChatPanel {
), ),
); );
subscriptions.push(
// Subscribe to room events
cx.subscribe_in(&room, window, move |this, _, signal, window, cx| {
match signal {
RoomSignal::NewMessage((gift_wrap_id, event)) => {
let nostr = NostrRegistry::global(cx);
let tracker = nostr.read(cx).tracker();
let gift_wrap_id = gift_wrap_id.to_owned();
let message = Message::user(event.clone());
cx.spawn_in(window, async move |this, cx| {
let tracker = tracker.read().await;
this.update_in(cx, |this, _window, cx| {
if !tracker.sent_ids().contains(&gift_wrap_id) {
this.insert_message(message, false, cx);
}
})
.ok();
})
.detach();
}
RoomSignal::Refresh => {
this.load_messages(window, cx);
}
};
}),
);
subscriptions.push(
// Observe when user close chat panel
cx.on_release_in(window, move |this, window, cx| {
this.messages.clear();
this.rendered_texts_by_id.clear();
this.reports_by_id.clear();
this.image_cache.update(cx, |this, cx| {
this.clear(window, cx);
});
}),
);
Self { Self {
id, id,
messages, messages,
@@ -190,30 +177,26 @@ impl ChatPanel {
input, input,
replies_to, replies_to,
attachments, attachments,
options,
rendered_texts_by_id: BTreeMap::new(), rendered_texts_by_id: BTreeMap::new(),
reports_by_id: BTreeMap::new(), reports_by_id: BTreeMap::new(),
uploading: false, uploading: false,
image_cache: RetainAllImageCache::new(cx), image_cache: RetainAllImageCache::new(cx),
focus_handle: cx.focus_handle(), focus_handle: cx.focus_handle(),
_subscriptions: subscriptions, _subscriptions: subscriptions,
_tasks: tasks, tasks,
} }
} }
/// Load all messages belonging to this room /// Load all messages belonging to this room
fn load_messages(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn load_messages(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let get_messages = self.room.read(cx).get_messages(cx); if let Ok(get_messages) = self.room.read_with(cx, |this, cx| this.get_messages(cx)) {
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
self._tasks.push(
// Run the task in the background
cx.spawn_in(window, async move |this, cx| {
let result = get_messages.await; let result = get_messages.await;
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {
match result { match result {
Ok(events) => { Ok(events) => {
this.insert_messages(events, cx); this.insert_messages(&events, cx);
} }
Err(e) => { Err(e) => {
window.push_notification(Notification::error(e.to_string()), cx); window.push_notification(Notification::error(e.to_string()), cx);
@@ -221,12 +204,13 @@ impl ChatPanel {
}; };
}) })
.ok(); .ok();
}), }));
); }
} }
/// Get user input content and merged all attachments /// Get user input content and merged all attachments
fn input_content(&self, cx: &Context<Self>) -> String { fn input_content(&self, cx: &Context<Self>) -> String {
// Get input's value
let mut content = self.input.read(cx).value().trim().to_string(); let mut content = self.input.read(cx).value().trim().to_string();
// Get all attaches and merge its with message // Get all attaches and merge its with message
@@ -260,26 +244,20 @@ impl ChatPanel {
return; return;
} }
// Temporary disable the message input // Get the current room entity
self.input.update(cx, |this, cx| { let Some(room) = self.room.upgrade().map(|this| this.read(cx)) else {
this.set_loading(false, cx); return;
this.set_disabled(false, cx); };
this.set_value("", window, cx);
});
// Get replies_to if it's present // Get replies_to if it's present
let replies: Vec<EventId> = self.replies_to.read(cx).iter().copied().collect(); let replies: Vec<EventId> = self.replies_to.read(cx).iter().copied().collect();
// Get the current room entity
let room = self.room.read(cx);
let opts = self.options.read(cx);
// Create a temporary message for optimistic update // Create a temporary message for optimistic update
let rumor = room.create_message(&content, replies.as_ref(), cx); let rumor = room.create_message(&content, replies.as_ref(), cx);
let rumor_id = rumor.id.unwrap(); let rumor_id = rumor.id.unwrap();
// Create a task for sending the message in the background // Create a task for sending the message in the background
let send_message = room.send_message(&rumor, opts, cx); let send_message = room.send_message(&rumor, cx);
// Optimistically update message list // Optimistically update message list
cx.spawn_in(window, async move |this, cx| { cx.spawn_in(window, async move |this, cx| {
@@ -290,29 +268,32 @@ impl ChatPanel {
// Update the message list and reset the states // Update the message list and reset the states
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {
this.insert_message(Message::user(rumor), true, cx);
this.remove_all_replies(cx); this.remove_all_replies(cx);
this.remove_all_attachments(cx); this.remove_all_attachments(cx);
// Reset the input to its default state
this.input.update(cx, |this, cx| { this.input.update(cx, |this, cx| {
this.set_loading(false, cx); this.set_loading(false, cx);
this.set_disabled(false, cx); this.set_disabled(false, cx);
this.set_value("", window, cx); this.set_value("", window, cx);
}); });
// Update the message list
this.insert_message(&rumor, true, cx);
}) })
.ok(); .ok();
}) })
.detach(); .detach();
self._tasks.push( self.tasks.push(cx.spawn_in(window, async move |this, cx| {
// Continue sending the message in the background
cx.spawn_in(window, async move |this, cx| {
let result = send_message.await; let result = send_message.await;
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {
match result { match result {
Ok(reports) => { Ok(reports) => {
// Update room's status // Update room's status
this.room.update(cx, |this, cx| { this.room
.update(cx, |this, cx| {
if this.kind != RoomKind::Ongoing { if this.kind != RoomKind::Ongoing {
// Update the room kind to ongoing, // Update the room kind to ongoing,
// but keep the room kind if send failed // but keep the room kind if send failed
@@ -321,7 +302,8 @@ impl ChatPanel {
cx.notify(); cx.notify();
} }
} }
}); })
.ok();
// Insert the sent reports // Insert the sent reports
this.reports_by_id.insert(rumor_id, reports); this.reports_by_id.insert(rumor_id, reports);
@@ -334,37 +316,7 @@ impl ChatPanel {
} }
}) })
.ok(); .ok();
}), }));
);
}
/// Resend a failed message
#[allow(dead_code)]
fn resend_message(&mut self, id: &EventId, window: &mut Window, cx: &mut Context<Self>) {
if let Some(reports) = self.reports_by_id.get(id).cloned() {
let id_clone = id.to_owned();
let resend = self.room.read(cx).resend_message(reports, cx);
cx.spawn_in(window, async move |this, cx| {
let result = resend.await;
this.update_in(cx, |this, window, cx| {
match result {
Ok(reports) => {
this.reports_by_id.entry(id_clone).and_modify(|this| {
*this = reports;
});
cx.notify();
}
Err(e) => {
window.push_notification(Notification::error(e.to_string()), cx);
}
};
})
.ok();
})
.detach();
}
} }
/// Insert a message into the chat panel /// Insert a message into the chat panel
@@ -390,21 +342,13 @@ impl ChatPanel {
} }
/// Convert and insert a vector of nostr events into the chat panel /// Convert and insert a vector of nostr events into the chat panel
fn insert_messages(&mut self, events: Vec<UnsignedEvent>, cx: &mut Context<Self>) { fn insert_messages(&mut self, events: &[UnsignedEvent], cx: &mut Context<Self>) {
for event in events { for event in events.iter() {
let m = Message::user(event);
// Bulk inserting messages, so no need to scroll to the latest message // Bulk inserting messages, so no need to scroll to the latest message
self.insert_message(m, false, cx); self.insert_message(event, false, cx);
} }
} }
/// Insert a warning message into the chat panel
#[allow(dead_code)]
fn insert_warning(&mut self, content: impl Into<String>, cx: &mut Context<Self>) {
let m = Message::warning(content.into());
self.insert_message(m, true, cx);
}
/// Check if a message failed to send by its ID /// Check if a message failed to send by its ID
fn is_sent_failed(&self, id: &EventId) -> bool { fn is_sent_failed(&self, id: &EventId) -> bool {
self.reports_by_id self.reports_by_id
@@ -436,15 +380,6 @@ impl ChatPanel {
}) })
} }
fn profile(&self, public_key: &PublicKey, cx: &Context<Self>) -> Profile {
let persons = PersonRegistry::global(cx);
persons.read(cx).get_person(public_key, cx)
}
fn signer_kind(&self, cx: &App) -> SignerKind {
self.options.read(cx).signer_kind
}
fn scroll_to(&self, id: EventId) { fn scroll_to(&self, id: EventId) {
if let Some(ix) = self.messages.iter().position(|m| { if let Some(ix) = self.messages.iter().position(|m| {
if let Message::User(msg) = m { if let Message::User(msg) = m {
@@ -511,25 +446,19 @@ impl ChatPanel {
Some(url) Some(url)
}); });
if let Ok(task) = upload { if let Ok(task) = upload.await {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.set_uploading(true, cx); this.set_uploading(true, cx);
}) })
.ok(); .ok();
let result = Flatten::flatten(task.await.map_err(|e| e.into())); this.update_in(cx, |this, _window, cx| {
match task {
this.update_in(cx, |this, window, cx| { Some(url) => {
match result {
Ok(Some(url)) => {
this.add_attachment(url, cx); this.add_attachment(url, cx);
this.set_uploading(false, cx); this.set_uploading(false, cx);
} }
Ok(None) => { None => {
this.set_uploading(false, cx);
}
Err(e) => {
window.push_notification(Notification::error(e.to_string()), cx);
this.set_uploading(false, cx); this.set_uploading(false, cx);
} }
}; };
@@ -570,6 +499,11 @@ impl ChatPanel {
}); });
} }
fn profile(&self, public_key: &PublicKey, cx: &Context<Self>) -> Person {
let persons = PersonRegistry::global(cx);
persons.read(cx).get(public_key, cx)
}
fn render_announcement(&self, ix: usize, cx: &Context<Self>) -> AnyElement { fn render_announcement(&self, ix: usize, cx: &Context<Self>) -> AnyElement {
v_flex() v_flex()
.id(ix) .id(ix)
@@ -660,7 +594,6 @@ impl ChatPanel {
text: AnyElement, text: AnyElement,
cx: &Context<Self>, cx: &Context<Self>,
) -> AnyElement { ) -> AnyElement {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let hide_avatar = AppSettings::get_hide_user_avatars(cx); let hide_avatar = AppSettings::get_hide_user_avatars(cx);
let id = message.id; let id = message.id;
@@ -691,7 +624,7 @@ impl ChatPanel {
this.child( this.child(
div() div()
.id(SharedString::from(format!("{ix}-avatar"))) .id(SharedString::from(format!("{ix}-avatar")))
.child(Avatar::new(author.avatar(proxy)).size(rems(2.))) .child(Avatar::new(author.avatar()).size(rems(2.)))
.context_menu(move |this, _window, _cx| { .context_menu(move |this, _window, _cx| {
let view = Box::new(OpenPublicKey(public_key)); let view = Box::new(OpenPublicKey(public_key));
let copy = Box::new(CopyPublicKey(public_key)); let copy = Box::new(CopyPublicKey(public_key));
@@ -716,7 +649,7 @@ impl ChatPanel {
div() div()
.font_semibold() .font_semibold()
.text_color(cx.theme().text) .text_color(cx.theme().text)
.child(author.display_name()), .child(author.name()),
) )
.child(message.created_at.to_human_time()) .child(message.created_at.to_human_time())
.when_some(is_sent_success, |this, status| { .when_some(is_sent_success, |this, status| {
@@ -773,7 +706,7 @@ impl ChatPanel {
.child( .child(
div() div()
.text_color(cx.theme().text_accent) .text_color(cx.theme().text_accent)
.child(author.display_name()), .child(author.name()),
) )
.child( .child(
div() div()
@@ -854,9 +787,9 @@ impl ChatPanel {
fn render_report(report: &SendReport, cx: &App) -> impl IntoElement { fn render_report(report: &SendReport, cx: &App) -> impl IntoElement {
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get_person(&report.receiver, cx); let profile = persons.read(cx).get(&report.receiver, cx);
let name = profile.display_name(); let name = profile.name();
let avatar = profile.avatar(true); let avatar = profile.avatar();
v_flex() v_flex()
.gap_2() .gap_2()
@@ -1116,7 +1049,7 @@ impl ChatPanel {
fn render_reply(&self, id: &EventId, cx: &Context<Self>) -> impl IntoElement { fn render_reply(&self, id: &EventId, cx: &Context<Self>) -> impl IntoElement {
if let Some(text) = self.message(id) { if let Some(text) = self.message(id) {
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get_person(&text.author, cx); let profile = persons.read(cx).get(&text.author, cx);
div() div()
.w_full() .w_full()
@@ -1139,7 +1072,7 @@ impl ChatPanel {
.child( .child(
div() div()
.text_color(cx.theme().text_accent) .text_color(cx.theme().text_accent)
.child(profile.display_name()), .child(profile.name()),
), ),
) )
.child( .child(
@@ -1181,126 +1114,6 @@ impl ChatPanel {
items items
} }
fn subject_button(&self, cx: &App) -> Button {
let room = self.room.downgrade();
let subject = self
.room
.read(cx)
.subject
.as_ref()
.map(|subject| subject.to_string());
Button::new("subject")
.icon(IconName::Edit)
.tooltip("Change the subject of the conversation")
.on_click(move |_, window, cx| {
let view = subject::init(subject.clone(), window, cx);
let room = room.clone();
let weak_view = view.downgrade();
window.open_modal(cx, move |this, _window, _cx| {
let room = room.clone();
let weak_view = weak_view.clone();
this.confirm()
.title("Change the subject of the conversation")
.child(view.clone())
.button_props(ModalButtonProps::default().ok_text("Change"))
.on_ok(move |_, _window, cx| {
if let Ok(subject) =
weak_view.read_with(cx, |this, cx| this.new_subject(cx))
{
room.update(cx, |this, cx| {
this.set_subject(subject, cx);
})
.ok();
}
// true to close the modal
true
})
});
})
}
fn reload_button(&self, _cx: &App) -> Button {
let room = self.room.downgrade();
Button::new("reload")
.icon(IconName::Refresh)
.tooltip("Reload")
.on_click(move |_ev, window, cx| {
_ = room.update(cx, |this, cx| {
this.emit_refresh(cx);
window.push_notification("Reloaded", cx);
});
})
}
fn on_open_seen_on(&mut self, ev: &SeenOn, window: &mut Window, cx: &mut Context<Self>) {
let id = ev.0;
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let tracker = nostr.read(cx).tracker();
let task: Task<Result<Vec<RelayUrl>, Error>> = cx.background_spawn(async move {
let tracker = tracker.read().await;
let mut relays: Vec<RelayUrl> = vec![];
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.event(id)
.limit(1);
if let Some(event) = client.database().query(filter).await?.first_owned() {
if let Some(Ok(id)) = event.tags.identifier().map(EventId::parse) {
if let Some(urls) = tracker.seen_on_relays.get(&id).cloned() {
relays.extend(urls);
}
}
}
Ok(relays)
});
cx.spawn_in(window, async move |_, cx| {
if let Ok(urls) = task.await {
cx.update(|window, cx| {
window.open_modal(cx, move |this, _window, cx| {
this.show_close(true)
.title(SharedString::from("Seen on"))
.child(v_flex().pb_4().gap_2().children({
let mut items = Vec::with_capacity(urls.len());
for url in urls.clone().into_iter() {
items.push(
h_flex()
.h_8()
.px_2()
.bg(cx.theme().elevated_surface_background)
.rounded(cx.theme().radius)
.font_semibold()
.text_xs()
.child(SharedString::from(url.to_string())),
)
}
items
}))
});
})
.ok();
}
})
.detach();
}
fn on_set_encryption(&mut self, ev: &SetSigner, _: &mut Window, cx: &mut Context<Self>) {
self.options.update(cx, move |this, cx| {
this.signer_kind = ev.0;
cx.notify();
});
}
} }
impl Panel for ChatPanel { impl Panel for ChatPanel {
@@ -1309,24 +1122,18 @@ impl Panel for ChatPanel {
} }
fn title(&self, cx: &App) -> AnyElement { fn title(&self, cx: &App) -> AnyElement {
self.room.read_with(cx, |this, cx| { self.room
let proxy = AppSettings::get_proxy_user_avatars(cx); .read_with(cx, |this, cx| {
let label = this.display_name(cx); let label = this.display_name(cx);
let url = this.display_image(proxy, cx); let url = this.display_image(cx);
h_flex() h_flex()
.gap_1p5() .gap_1p5()
.child(Avatar::new(url).size(rems(1.25))) .child(Avatar::new(url).size(rems(1.25)))
.child(label) .child(label)
.into_any() .into_any_element()
}) })
} .unwrap_or(div().child("Unknown").into_any_element())
fn toolbar_buttons(&self, _window: &Window, cx: &App) -> Vec<Button> {
let subject_button = self.subject_button(cx);
let reload_button = self.reload_button(cx);
vec![subject_button, reload_button]
} }
} }
@@ -1340,11 +1147,7 @@ impl Focusable for ChatPanel {
impl Render for ChatPanel { impl Render for ChatPanel {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let kind = self.signer_kind(cx);
v_flex() v_flex()
.on_action(cx.listener(Self::on_open_seen_on))
.on_action(cx.listener(Self::on_set_encryption))
.image_cache(self.image_cache.clone()) .image_cache(self.image_cache.clone())
.size_full() .size_full()
.child( .child(
@@ -1399,31 +1202,7 @@ impl Render for ChatPanel {
.large(), .large(),
), ),
) )
.child(TextInput::new(&self.input)) .child(TextInput::new(&self.input)),
.child(
Button::new("encryptions")
.icon(IconName::Encryption)
.ghost()
.large()
.popup_menu(move |this, _window, _cx| {
this.label("Encrypt by:")
.menu_with_check(
"Encryption Key",
matches!(kind, SignerKind::Encryption),
Box::new(SetSigner(SignerKind::Encryption)),
)
.menu_with_check(
"User's Identity",
matches!(kind, SignerKind::User),
Box::new(SetSigner(SignerKind::User)),
)
.menu_with_check(
"Auto",
matches!(kind, SignerKind::Auto),
Box::new(SetSigner(SignerKind::Auto)),
)
}),
),
), ),
), ),
) )

View File

@@ -1,7 +1,6 @@
use std::ops::Range; use std::ops::Range;
use std::sync::Arc; use std::sync::Arc;
use common::RenderedProfile;
use gpui::{ use gpui::{
AnyElement, App, ElementId, HighlightStyle, InteractiveText, IntoElement, SharedString, AnyElement, App, ElementId, HighlightStyle, InteractiveText, IntoElement, SharedString,
StyledText, UnderlineStyle, Window, StyledText, UnderlineStyle, Window,
@@ -254,8 +253,8 @@ fn render_pubkey(
cx: &App, cx: &App,
) { ) {
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get_person(&public_key, cx); let profile = persons.read(cx).get(&public_key, cx);
let display_name = format!("@{}", profile.display_name()); let display_name = format!("@{}", profile.name());
text.replace_range(range.clone(), &display_name); text.replace_range(range.clone(), &display_name);

View File

@@ -2,12 +2,11 @@ pub const CLIENT_NAME: &str = "Coop";
pub const APP_ID: &str = "su.reya.coop"; pub const APP_ID: &str = "su.reya.coop";
/// Bootstrap Relays. /// Bootstrap Relays.
pub const BOOTSTRAP_RELAYS: [&str; 5] = [ pub const BOOTSTRAP_RELAYS: [&str; 4] = [
"wss://relay.damus.io", "wss://relay.damus.io",
"wss://relay.primal.net", "wss://relay.primal.net",
"wss://relay.nos.social", "wss://relay.nos.social",
"wss://user.kindpag.es", "wss://user.kindpag.es",
"wss://purplepag.es",
]; ];
/// Search Relays. /// Search Relays.
@@ -28,8 +27,5 @@ pub const NOSTR_CONNECT_TIMEOUT: u64 = 200;
/// Default timeout (in seconds) for Nostr Connect (Bunker) /// Default timeout (in seconds) for Nostr Connect (Bunker)
pub const BUNKER_TIMEOUT: u64 = 30; pub const BUNKER_TIMEOUT: u64 = 30;
/// Total metadata requests will be grouped.
pub const METADATA_BATCH_LIMIT: usize = 20;
/// Default width of the sidebar. /// Default width of the sidebar.
pub const DEFAULT_SIDEBAR_WIDTH: f32 = 240.; pub const DEFAULT_SIDEBAR_WIDTH: f32 = 240.;

View File

@@ -5,19 +5,19 @@ use nostr_sdk::prelude::*;
pub trait EventUtils { pub trait EventUtils {
fn uniq_id(&self) -> u64; fn uniq_id(&self) -> u64;
fn all_pubkeys(&self) -> Vec<PublicKey>; fn extract_public_keys(&self) -> Vec<PublicKey>;
} }
impl EventUtils for Event { impl EventUtils for Event {
fn uniq_id(&self) -> u64 { fn uniq_id(&self) -> u64 {
let mut hasher = DefaultHasher::new(); let mut hasher = DefaultHasher::new();
let mut pubkeys: Vec<PublicKey> = self.all_pubkeys(); let mut pubkeys: Vec<PublicKey> = self.extract_public_keys();
pubkeys.sort(); pubkeys.sort();
pubkeys.hash(&mut hasher); pubkeys.hash(&mut hasher);
hasher.finish() hasher.finish()
} }
fn all_pubkeys(&self) -> Vec<PublicKey> { fn extract_public_keys(&self) -> Vec<PublicKey> {
let mut public_keys: Vec<PublicKey> = self.tags.public_keys().copied().collect(); let mut public_keys: Vec<PublicKey> = self.tags.public_keys().copied().collect();
public_keys.push(self.pubkey); public_keys.push(self.pubkey);
@@ -45,7 +45,7 @@ impl EventUtils for UnsignedEvent {
hasher.finish() hasher.finish()
} }
fn all_pubkeys(&self) -> Vec<PublicKey> { fn extract_public_keys(&self) -> Vec<PublicKey> {
let mut public_keys: Vec<PublicKey> = self.tags.public_keys().copied().collect(); let mut public_keys: Vec<PublicKey> = self.tags.public_keys().copied().collect();
public_keys.push(self.pubkey); public_keys.push(self.pubkey);
public_keys.into_iter().unique().sorted().collect() public_keys.into_iter().unique().sorted().collect()

View File

@@ -33,14 +33,12 @@ title_bar = { path = "../title_bar" }
theme = { path = "../theme" } theme = { path = "../theme" }
common = { path = "../common" } common = { path = "../common" }
state = { path = "../state" } state = { path = "../state" }
device = { path = "../device" }
key_store = { path = "../key_store" } key_store = { path = "../key_store" }
chat = { path = "../chat" } chat = { path = "../chat" }
chat_ui = { path = "../chat_ui" } chat_ui = { path = "../chat_ui" }
settings = { path = "../settings" } settings = { path = "../settings" }
auto_update = { path = "../auto_update" } auto_update = { path = "../auto_update" }
account = { path = "../account" }
encryption = { path = "../encryption" }
encryption_ui = { path = "../encryption_ui" }
person = { path = "../person" } person = { path = "../person" }
relay_auth = { path = "../relay_auth" } relay_auth = { path = "../relay_auth" }

View File

@@ -83,8 +83,7 @@ pub fn reset(cx: &mut App) {
cx.update(|cx| { cx.update(|cx| {
cx.restart(); cx.restart();
}) });
.ok();
}) })
.detach(); .detach();
} }

View File

@@ -1,12 +1,9 @@
use std::sync::Arc; use std::sync::Arc;
use account::Account;
use auto_update::{AutoUpdateStatus, AutoUpdater}; use auto_update::{AutoUpdateStatus, AutoUpdater};
use chat::{ChatEvent, ChatRegistry}; use chat::{ChatEvent, ChatRegistry};
use chat_ui::{CopyPublicKey, OpenPublicKey}; use chat_ui::{CopyPublicKey, OpenPublicKey};
use common::{RenderedProfile, DEFAULT_SIDEBAR_WIDTH}; use common::DEFAULT_SIDEBAR_WIDTH;
use encryption::Encryption;
use encryption_ui::EncryptionPanel;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
deferred, div, px, relative, rems, App, AppContext, Axis, ClipboardItem, Context, Entity, deferred, div, px, relative, rems, App, AppContext, Axis, ClipboardItem, Context, Entity,
@@ -17,8 +14,8 @@ use key_store::{Credential, KeyItem, KeyStore};
use nostr_connect::prelude::*; use nostr_connect::prelude::*;
use person::PersonRegistry; use person::PersonRegistry;
use relay_auth::RelayAuth; use relay_auth::RelayAuth;
use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::NostrRegistry;
use theme::{ActiveTheme, Theme, ThemeMode, ThemeRegistry}; use theme::{ActiveTheme, Theme, ThemeMode, ThemeRegistry};
use title_bar::TitleBar; use title_bar::TitleBar;
use ui::avatar::Avatar; use ui::avatar::Avatar;
@@ -27,7 +24,6 @@ use ui::dock_area::dock::DockPlacement;
use ui::dock_area::panel::PanelView; use ui::dock_area::panel::PanelView;
use ui::dock_area::{ClosePanel, DockArea, DockItem}; use ui::dock_area::{ClosePanel, DockArea, DockItem};
use ui::modal::ModalButtonProps; use ui::modal::ModalButtonProps;
use ui::popover::{Popover, PopoverContent};
use ui::popup_menu::PopupMenuExt; use ui::popup_menu::PopupMenuExt;
use ui::{h_flex, v_flex, ContextModal, IconName, Root, Sizable, StyledExt}; use ui::{h_flex, v_flex, ContextModal, IconName, Root, Sizable, StyledExt};
@@ -61,9 +57,6 @@ pub struct ChatSpace {
/// App's Dock Area /// App's Dock Area
dock: Entity<DockArea>, dock: Entity<DockArea>,
/// App's Encryption Panel
encryption_panel: Entity<EncryptionPanel>,
/// Determines if the chat space is ready to use /// Determines if the chat space is ready to use
ready: bool, ready: bool,
@@ -73,13 +66,14 @@ pub struct ChatSpace {
impl ChatSpace { impl ChatSpace {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx);
let chat = ChatRegistry::global(cx); let chat = ChatRegistry::global(cx);
let keystore = KeyStore::global(cx); let keystore = KeyStore::global(cx);
let account = Account::global(cx);
let title_bar = cx.new(|_| TitleBar::new()); let title_bar = cx.new(|_| TitleBar::new());
let dock = cx.new(|cx| DockArea::new(window, cx)); let dock = cx.new(|cx| DockArea::new(window, cx));
let encryption_panel = encryption_ui::init(window, cx);
let identity = nostr.read(cx).identity();
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
@@ -92,8 +86,8 @@ impl ChatSpace {
subscriptions.push( subscriptions.push(
// Observe account entity changes // Observe account entity changes
cx.observe_in(&account, window, move |this, state, window, cx| { cx.observe_in(&identity, window, move |this, state, window, cx| {
if !this.ready && state.read(cx).has_account() { if !this.ready && state.read(cx).has_public_key() {
this.set_default_layout(window, cx); this.set_default_layout(window, cx);
// Load all chat room in the database if available // Load all chat room in the database if available
@@ -141,15 +135,20 @@ impl ChatSpace {
ChatEvent::OpenRoom(id) => { ChatEvent::OpenRoom(id) => {
if let Some(room) = chat.read(cx).room(id, cx) { if let Some(room) = chat.read(cx).room(id, cx) {
this.dock.update(cx, |this, cx| { this.dock.update(cx, |this, cx| {
let panel = chat_ui::init(room, window, cx); this.add_panel(
this.add_panel(Arc::new(panel), DockPlacement::Center, window, cx); Arc::new(chat_ui::init(room, window, cx)),
DockPlacement::Center,
window,
cx,
);
}); });
} }
} }
ChatEvent::CloseRoom(..) => { ChatEvent::CloseRoom(..) => {
this.dock.update(cx, |this, cx| { this.dock.update(cx, |this, cx| {
// Force focus to the tab panel
this.focus_tab_panel(window, cx); this.focus_tab_panel(window, cx);
// Dispatch the close panel action
cx.defer_in(window, |_, window, cx| { cx.defer_in(window, |_, window, cx| {
window.dispatch_action(Box::new(ClosePanel), cx); window.dispatch_action(Box::new(ClosePanel), cx);
window.close_all_modals(cx); window.close_all_modals(cx);
@@ -175,7 +174,6 @@ impl ChatSpace {
Self { Self {
dock, dock,
title_bar, title_bar,
encryption_panel,
ready: false, ready: false,
_subscriptions: subscriptions, _subscriptions: subscriptions,
} }
@@ -258,9 +256,9 @@ impl ChatSpace {
this.update_in(cx, |_, window, cx| { this.update_in(cx, |_, window, cx| {
match result { match result {
Ok(profile) => { Ok(person) => {
persons.update(cx, |this, cx| { persons.update(cx, |this, cx| {
this.insert_or_update_person(profile, cx); this.insert(person, cx);
// Close the edit profile modal // Close the edit profile modal
window.close_all_modals(cx); window.close_all_modals(cx);
}); });
@@ -447,11 +445,11 @@ impl ChatSpace {
} }
fn titlebar_left(&mut self, _window: &mut Window, cx: &Context<Self>) -> impl IntoElement { fn titlebar_left(&mut self, _window: &mut Window, cx: &Context<Self>) -> impl IntoElement {
let account = Account::global(cx); let nostr = NostrRegistry::global(cx);
let chat = ChatRegistry::global(cx); let chat = ChatRegistry::global(cx);
let status = chat.read(cx).loading; let status = chat.read(cx).loading();
if !account.read(cx).has_account() { if !nostr.read(cx).identity().read(cx).has_public_key() {
return div(); return div();
} }
@@ -477,12 +475,13 @@ impl ChatSpace {
} }
fn titlebar_right(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn titlebar_right(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let auto_update = AutoUpdater::global(cx); let auto_update = AutoUpdater::global(cx);
let account = Account::global(cx);
let relay_auth = RelayAuth::global(cx); let relay_auth = RelayAuth::global(cx);
let pending_requests = relay_auth.read(cx).pending_requests(cx); let pending_requests = relay_auth.read(cx).pending_requests(cx);
let encryption_panel = self.encryption_panel.downgrade();
let nostr = NostrRegistry::global(cx);
let identity = nostr.read(cx).identity();
h_flex() h_flex()
.gap_2() .gap_2()
@@ -542,15 +541,9 @@ impl ChatSpace {
}), }),
) )
}) })
.when(account.read(cx).has_account(), |this| { .when_some(identity.read(cx).public_key, |this, public_key| {
let account = Account::global(cx);
let public_key = account.read(cx).public_key();
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get_person(&public_key, cx); let profile = persons.read(cx).get(&public_key, cx);
let encryption = Encryption::global(cx);
let has_encryption = encryption.read(cx).has_encryption(cx);
let keystore = KeyStore::global(cx); let keystore = KeyStore::global(cx);
let is_using_file_keystore = keystore.read(cx).is_using_file_keystore(); let is_using_file_keystore = keystore.read(cx).is_using_file_keystore();
@@ -562,45 +555,14 @@ impl ChatSpace {
}; };
this.child( this.child(
h_flex()
.gap_1()
.child(
Popover::new("encryption")
.trigger(
Button::new("encryption-trigger")
.tooltip("Manage Encryption Key")
.icon(IconName::Encryption)
.rounded()
.small()
.cta()
.map(|this| match has_encryption {
true => this.ghost_alt(),
false => this.warning(),
}),
)
.content(move |window, cx| {
let encryption_panel = encryption_panel.clone();
cx.new(|cx| {
PopoverContent::new(window, cx, move |_window, _cx| {
if let Some(view) = encryption_panel.upgrade() {
view.clone().into_any_element()
} else {
div().into_any_element()
}
})
})
}),
)
.child(
Button::new("user") Button::new("user")
.small() .small()
.reverse() .reverse()
.transparent() .transparent()
.icon(IconName::CaretDown) .icon(IconName::CaretDown)
.child(Avatar::new(profile.avatar(proxy)).size(rems(1.45))) .child(Avatar::new(profile.avatar()).size(rems(1.45)))
.popup_menu(move |this, _window, _cx| { .popup_menu(move |this, _window, _cx| {
this.label(profile.display_name()) this.label(profile.name())
.menu_with_icon( .menu_with_icon(
"Profile", "Profile",
IconName::EmojiFill, IconName::EmojiFill,
@@ -620,24 +582,11 @@ impl ChatSpace {
!is_using_file_keystore, !is_using_file_keystore,
) )
.separator() .separator()
.menu_with_icon( .menu_with_icon("Dark Mode", IconName::Sun, Box::new(DarkMode))
"Dark Mode",
IconName::Sun,
Box::new(DarkMode),
)
.menu_with_icon("Themes", IconName::Moon, Box::new(Themes)) .menu_with_icon("Themes", IconName::Moon, Box::new(Themes))
.menu_with_icon( .menu_with_icon("Settings", IconName::Settings, Box::new(Settings))
"Settings", .menu_with_icon("Sign Out", IconName::Logout, Box::new(Logout))
IconName::Settings,
Box::new(Settings),
)
.menu_with_icon(
"Sign Out",
IconName::Logout,
Box::new(Logout),
)
}), }),
),
) )
}) })
} }

View File

@@ -210,12 +210,10 @@ impl Login {
fn connect(&mut self, signer: NostrConnect, cx: &mut Context<Self>) { fn connect(&mut self, signer: NostrConnect, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
cx.background_spawn(async move { nostr.update(cx, |this, cx| {
client.set_signer(signer).await; this.set_signer(signer, cx);
}) });
.detach();
} }
pub fn login_with_password(&mut self, content: &str, pwd: &str, cx: &mut Context<Self>) { pub fn login_with_password(&mut self, content: &str, pwd: &str, cx: &mut Context<Self>) {
@@ -260,10 +258,6 @@ impl Login {
pub fn login_with_keys(&mut self, keys: Keys, cx: &mut Context<Self>) { pub fn login_with_keys(&mut self, keys: Keys, cx: &mut Context<Self>) {
let keystore = KeyStore::global(cx).read(cx).backend(); let keystore = KeyStore::global(cx).read(cx).backend();
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let username = keys.public_key().to_hex(); let username = keys.public_key().to_hex();
let secret = keys.secret_key().to_secret_hex().into_bytes(); let secret = keys.secret_key().to_secret_hex().into_bytes();
@@ -281,11 +275,14 @@ impl Login {
.ok(); .ok();
} }
// Update the signer this.update(cx, |_this, cx| {
cx.background_spawn(async move { let nostr = NostrRegistry::global(cx);
client.set_signer(keys).await;
nostr.update(cx, |this, cx| {
this.set_signer(keys, cx);
});
}) })
.detach(); .ok();
}) })
.detach(); .detach();
} }

View File

@@ -73,7 +73,6 @@ fn main() {
// Bring the app to the foreground // Bring the app to the foreground
cx.activate(true); cx.activate(true);
// Root Entity
cx.new(|cx| { cx.new(|cx| {
// Initialize the tokio runtime // Initialize the tokio runtime
gpui_tokio::init(cx); gpui_tokio::init(cx);
@@ -90,27 +89,27 @@ fn main() {
// Initialize the nostr client // Initialize the nostr client
state::init(cx); state::init(cx);
// Initialize person registry // Initialize device signer
person::init(cx); //
// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
device::init(cx);
// Initialize settings // Initialize settings
settings::init(cx); settings::init(cx);
// Initialize account state // Initialize relay auth registry
account::init(cx); relay_auth::init(window, cx);
// Initialize encryption state
encryption::init(cx);
// Initialize app registry // Initialize app registry
chat::init(cx); chat::init(cx);
// Initialize relay auth registry // Initialize person registry
relay_auth::init(window, cx); person::init(cx);
// Initialize auto update // Initialize auto update
auto_update::init(cx); auto_update::init(cx);
// Root Entity
Root::new(chatspace::init(window, cx).into(), window, cx) Root::new(chatspace::init(window, cx).into(), window, cx)
}) })
}) })

View File

@@ -3,8 +3,8 @@ use std::time::Duration;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use common::home_dir; use common::home_dir;
use gpui::{ use gpui::{
div, App, AppContext, ClipboardItem, Context, Entity, Flatten, IntoElement, ParentElement, div, App, AppContext, ClipboardItem, Context, Entity, IntoElement, ParentElement, Render,
Render, SharedString, Styled, Task, Window, SharedString, Styled, Task, Window,
}; };
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
@@ -60,7 +60,7 @@ impl Backup {
let nsec = self.secret_input.read(cx).value().to_string(); let nsec = self.secret_input.read(cx).value().to_string();
cx.spawn_in(window, async move |this, cx| { cx.spawn_in(window, async move |this, cx| {
match Flatten::flatten(path.await.map_err(|e| e.into())) { match path.await {
Ok(Ok(Some(path))) => { Ok(Ok(Some(path))) => {
if let Err(e) = smol::fs::write(&path, nsec).await { if let Err(e) = smol::fs::write(&path, nsec).await {
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {

View File

@@ -1,9 +1,8 @@
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use common::{default_nip17_relays, default_nip65_relays, nip96_upload, BOOTSTRAP_RELAYS}; use common::{default_nip17_relays, default_nip65_relays, nip96_upload, BOOTSTRAP_RELAYS};
use gpui::{ use gpui::{
rems, AnyElement, App, AppContext, Context, Entity, EventEmitter, Flatten, FocusHandle, rems, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
Focusable, IntoElement, ParentElement, PathPromptOptions, Render, SharedString, Styled, Task, IntoElement, ParentElement, PathPromptOptions, Render, SharedString, Styled, Task, Window,
Window,
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use key_store::{KeyItem, KeyStore}; use key_store::{KeyItem, KeyStore};
@@ -221,8 +220,8 @@ impl NewAccount {
}); });
let task = Tokio::spawn(cx, async move { let task = Tokio::spawn(cx, async move {
match Flatten::flatten(paths.await.map_err(|e| e.into())) { match paths.await {
Ok(Some(mut paths)) => { Ok(Ok(Some(mut paths))) => {
if let Some(path) = paths.pop() { if let Some(path) = paths.pop() {
let file = fs::read(path).await?; let file = fs::read(path).await?;
let url = nip96_upload(&client, &nip96_server, file).await?; let url = nip96_upload(&client, &nip96_server, file).await?;
@@ -232,13 +231,12 @@ impl NewAccount {
Err(anyhow!("Path not found")) Err(anyhow!("Path not found"))
} }
} }
Ok(None) => Err(anyhow!("User cancelled")), _ => Err(anyhow!("Error")),
Err(e) => Err(anyhow!("File dialog error: {e}")),
} }
}); });
cx.spawn_in(window, async move |this, cx| { cx.spawn_in(window, async move |this, cx| {
let result = Flatten::flatten(task.await.map_err(|e| e.into())); let result = task.await;
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {
match result { match result {

View File

@@ -1,20 +1,18 @@
use std::ops::Range; use std::ops::Range;
use std::time::Duration; use std::time::Duration;
use account::Account;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use chat::{ChatEvent, ChatRegistry, Room, RoomKind}; use chat::{ChatEvent, ChatRegistry, Room, RoomKind};
use common::{DebouncedDelay, RenderedTimestamp, TextUtils, BOOTSTRAP_RELAYS, SEARCH_RELAYS}; use common::{DebouncedDelay, RenderedTimestamp, TextUtils, BOOTSTRAP_RELAYS, SEARCH_RELAYS};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
deferred, div, relative, uniform_list, AnyElement, App, AppContext, Context, Entity, deferred, div, relative, uniform_list, App, AppContext, Context, Entity, EventEmitter,
EventEmitter, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render, FocusHandle, Focusable, InteractiveElement, IntoElement, ParentElement, Render,
RetainAllImageCache, SharedString, Styled, Subscription, Task, Window, RetainAllImageCache, SharedString, Styled, Subscription, Task, Window,
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use list_item::RoomListItem; use list_item::RoomListItem;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::{NostrRegistry, GIFTWRAP_SUBSCRIPTION}; use state::{NostrRegistry, GIFTWRAP_SUBSCRIPTION};
use theme::ActiveTheme; use theme::ActiveTheme;
@@ -35,6 +33,7 @@ pub fn init(window: &mut Window, cx: &mut App) -> Entity<Sidebar> {
cx.new(|cx| Sidebar::new(window, cx)) cx.new(|cx| Sidebar::new(window, cx))
} }
/// Sidebar.
pub struct Sidebar { pub struct Sidebar {
name: SharedString, name: SharedString,
@@ -50,73 +49,75 @@ pub struct Sidebar {
/// Async search operation /// Async search operation
search_task: Option<Task<()>>, search_task: Option<Task<()>>,
/// Search input state
find_input: Entity<InputState>, find_input: Entity<InputState>,
/// Debounced delay for search input
find_debouncer: DebouncedDelay<Self>, find_debouncer: DebouncedDelay<Self>,
/// Whether searching is in progress
finding: bool, finding: bool,
indicator: Entity<Option<RoomKind>>, /// New request flag
new_request: bool,
/// Current chat room filter
active_filter: Entity<RoomKind>, active_filter: Entity<RoomKind>,
/// Event subscriptions /// Event subscriptions
_subscriptions: SmallVec<[Subscription; 3]>, _subscriptions: SmallVec<[Subscription; 2]>,
} }
impl Sidebar { impl Sidebar {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let active_filter = cx.new(|_| RoomKind::Ongoing); let active_filter = cx.new(|_| RoomKind::Ongoing);
let indicator = cx.new(|_| None);
let search_results = cx.new(|_| None); let search_results = cx.new(|_| None);
let find_input = // Define the find input state
cx.new(|cx| InputState::new(window, cx).placeholder("Find or start a conversation")); let find_input = cx.new(|cx| {
InputState::new(window, cx)
.placeholder("Find or start a conversation")
.clean_on_escape()
});
// Get the chat registry
let chat = ChatRegistry::global(cx); let chat = ChatRegistry::global(cx);
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
subscriptions.push(
// Clear the image cache when sidebar is closed
cx.on_release_in(window, move |this, window, cx| {
this.image_cache.update(cx, |this, cx| {
this.clear(window, cx);
})
}),
);
subscriptions.push( subscriptions.push(
// Subscribe for registry new events // Subscribe for registry new events
cx.subscribe_in(&chat, window, move |this, _, event, _window, cx| { cx.subscribe_in(&chat, window, move |this, _s, event, _window, cx| {
if let ChatEvent::NewChatRequest(kind) = event { if event == &ChatEvent::Ping {
this.indicator.update(cx, |this, cx| { this.new_request = true;
*this = Some(kind.to_owned());
cx.notify(); cx.notify();
}); };
}
}), }),
); );
subscriptions.push( subscriptions.push(
// Subscribe for find input events // Subscribe for find input events
cx.subscribe_in(&find_input, window, |this, state, event, window, cx| { cx.subscribe_in(&find_input, window, |this, state, event, window, cx| {
let delay = Duration::from_millis(FIND_DELAY);
match event { match event {
InputEvent::PressEnter { .. } => { InputEvent::PressEnter { .. } => {
this.search(window, cx); this.search(window, cx);
} }
InputEvent::Change => { InputEvent::Change => {
// Clear the result when input is empty
if state.read(cx).value().is_empty() { if state.read(cx).value().is_empty() {
// Clear the result when input is empty
this.clear(window, cx); this.clear(window, cx);
} else { } else {
// Run debounced search // Run debounced search
this.find_debouncer.fire_new( this.find_debouncer
Duration::from_millis(FIND_DELAY), .fire_new(delay, window, cx, |this, window, cx| {
window, this.debounced_search(window, cx)
cx, });
|this, window, cx| this.debounced_search(window, cx),
);
} }
} }
_ => {} _ => {}
} };
}), }),
); );
@@ -126,7 +127,7 @@ impl Sidebar {
image_cache: RetainAllImageCache::new(cx), image_cache: RetainAllImageCache::new(cx),
find_debouncer: DebouncedDelay::new(), find_debouncer: DebouncedDelay::new(),
finding: false, finding: false,
indicator, new_request: false,
active_filter, active_filter,
find_input, find_input,
search_results, search_results,
@@ -199,11 +200,9 @@ impl Sidebar {
} }
fn search_by_nip50(&mut self, query: &str, window: &mut Window, cx: &mut Context<Self>) { fn search_by_nip50(&mut self, query: &str, window: &mut Window, cx: &mut Context<Self>) {
let account = Account::global(cx);
let public_key = account.read(cx).public_key();
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let query = query.to_owned(); let query = query.to_owned();
@@ -387,13 +386,13 @@ impl Sidebar {
} }
fn set_finding(&mut self, status: bool, _window: &mut Window, cx: &mut Context<Self>) { fn set_finding(&mut self, status: bool, _window: &mut Window, cx: &mut Context<Self>) {
self.finding = status;
// Disable the input to prevent duplicate requests // Disable the input to prevent duplicate requests
self.find_input.update(cx, |this, cx| { self.find_input.update(cx, |this, cx| {
this.set_disabled(status, cx); this.set_disabled(status, cx);
this.set_loading(status, cx); this.set_loading(status, cx);
}); });
// Set the finding status
self.finding = status;
cx.notify(); cx.notify();
} }
@@ -415,47 +414,46 @@ impl Sidebar {
} }
fn set_filter(&mut self, kind: RoomKind, cx: &mut Context<Self>) { fn set_filter(&mut self, kind: RoomKind, cx: &mut Context<Self>) {
self.indicator.update(cx, |this, cx| {
*this = None;
cx.notify();
});
self.active_filter.update(cx, |this, cx| { self.active_filter.update(cx, |this, cx| {
*this = kind; *this = kind;
cx.notify(); cx.notify();
}); });
self.new_request = false;
cx.notify();
} }
fn open_room(&mut self, id: u64, window: &mut Window, cx: &mut Context<Self>) { fn open(&mut self, id: u64, window: &mut Window, cx: &mut Context<Self>) {
let chat = ChatRegistry::global(cx); let chat = ChatRegistry::global(cx);
let room = if let Some(room) = chat.read(cx).room(&id, cx) {
room
} else {
let Some(result) = self.search_results.read(cx).as_ref() else {
window.push_notification("Failed to open room. Please try again later.", cx);
return;
};
let Some(room) = result.iter().find(|this| this.read(cx).id == id).cloned() else {
window.push_notification("Failed to open room. Please try again later.", cx);
return;
};
match chat.read(cx).room(&id, cx) {
Some(room) => {
chat.update(cx, |this, cx| {
this.emit_room(room, cx);
});
}
None => {
if let Some(room) = self
.search_results
.read(cx)
.as_ref()
.and_then(|results| results.iter().find(|this| this.read(cx).id == id))
.map(|this| this.downgrade())
{
chat.update(cx, |this, cx| {
this.emit_room(room, cx);
});
// Clear all search results // Clear all search results
self.clear(window, cx); self.clear(window, cx);
}
room }
}; }
chat.update(cx, |this, cx| {
this.push_room(room, cx);
});
} }
fn on_reload(&mut self, _ev: &Reload, window: &mut Window, cx: &mut Context<Self>) { fn on_reload(&mut self, _ev: &Reload, window: &mut Window, cx: &mut Context<Self>) {
ChatRegistry::global(cx).update(cx, |this, cx| { ChatRegistry::global(cx).update(cx, |this, cx| {
this.get_rooms(cx); this.get_rooms(cx);
}); });
window.push_notification("Refreshed", cx); window.push_notification("Reload", cx);
} }
fn on_manage(&mut self, _ev: &RelayStatus, window: &mut Window, cx: &mut Context<Self>) { fn on_manage(&mut self, _ev: &RelayStatus, window: &mut Window, cx: &mut Context<Self>) {
@@ -541,7 +539,6 @@ impl Sidebar {
range: Range<usize>, range: Range<usize>,
cx: &Context<Self>, cx: &Context<Self>,
) -> Vec<impl IntoElement> { ) -> Vec<impl IntoElement> {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let mut items = Vec::with_capacity(range.end - range.start); let mut items = Vec::with_capacity(range.end - range.start);
for ix in range { for ix in range {
@@ -556,7 +553,7 @@ impl Sidebar {
let handler = cx.listener({ let handler = cx.listener({
move |this, _, window, cx| { move |this, _, window, cx| {
this.open_room(room_id, window, cx); this.open(room_id, window, cx);
} }
}); });
@@ -564,7 +561,7 @@ impl Sidebar {
RoomListItem::new(ix) RoomListItem::new(ix)
.room_id(room_id) .room_id(room_id)
.name(this.display_name(cx)) .name(this.display_name(cx))
.avatar(this.display_image(proxy, cx)) .avatar(this.display_image(cx))
.public_key(member.public_key()) .public_key(member.public_key())
.kind(this.kind) .kind(this.kind)
.created_at(this.created_at.to_ago()) .created_at(this.created_at.to_ago())
@@ -580,10 +577,6 @@ impl Panel for Sidebar {
fn panel_id(&self) -> SharedString { fn panel_id(&self) -> SharedString {
self.name.clone() self.name.clone()
} }
fn title(&self, _cx: &App) -> AnyElement {
self.name.clone().into_any_element()
}
} }
impl EventEmitter<PanelEvent> for Sidebar {} impl EventEmitter<PanelEvent> for Sidebar {}
@@ -597,7 +590,7 @@ impl Focusable for Sidebar {
impl Render for Sidebar { impl Render for Sidebar {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let chat = ChatRegistry::global(cx); let chat = ChatRegistry::global(cx);
let loading = chat.read(cx).loading; let loading = chat.read(cx).loading();
// Get rooms from either search results or the chat registry // Get rooms from either search results or the chat registry
let rooms = if let Some(results) = self.search_results.read(cx).as_ref() { let rooms = if let Some(results) = self.search_results.read(cx).as_ref() {
@@ -675,13 +668,6 @@ impl Render for Sidebar {
Button::new("all") Button::new("all")
.label("All") .label("All")
.tooltip("All ongoing conversations") .tooltip("All ongoing conversations")
.when_some(self.indicator.read(cx).as_ref(), |this, kind| {
this.when(kind == &RoomKind::Ongoing, |this| {
this.child(
div().size_1().rounded_full().bg(cx.theme().cursor),
)
})
})
.small() .small()
.cta() .cta()
.bold() .bold()
@@ -696,13 +682,11 @@ impl Render for Sidebar {
Button::new("requests") Button::new("requests")
.label("Requests") .label("Requests")
.tooltip("Incoming new conversations") .tooltip("Incoming new conversations")
.when_some(self.indicator.read(cx).as_ref(), |this, kind| { .when(self.new_request, |this| {
this.when(kind != &RoomKind::Ongoing, |this| {
this.child( this.child(
div().size_1().rounded_full().bg(cx.theme().cursor), div().size_1().rounded_full().bg(cx.theme().cursor),
) )
}) })
})
.small() .small()
.cta() .cta()
.bold() .bold()

View File

@@ -5,11 +5,12 @@ use anyhow::{anyhow, Error};
use common::{nip96_upload, shorten_pubkey}; use common::{nip96_upload, shorten_pubkey};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, img, App, AppContext, ClipboardItem, Context, Entity, Flatten, IntoElement, ParentElement, div, img, App, AppContext, ClipboardItem, Context, Entity, IntoElement, ParentElement,
PathPromptOptions, Render, SharedString, Styled, Task, Window, PathPromptOptions, Render, SharedString, Styled, Task, Window,
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::Person;
use settings::AppSettings; use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use smol::fs; use smol::fs;
@@ -193,8 +194,8 @@ impl UserProfile {
}); });
let task = Tokio::spawn(cx, async move { let task = Tokio::spawn(cx, async move {
match Flatten::flatten(paths.await.map_err(|e| e.into())) { match paths.await {
Ok(Some(mut paths)) => { Ok(Ok(Some(mut paths))) => {
if let Some(path) = paths.pop() { if let Some(path) = paths.pop() {
let file = fs::read(path).await?; let file = fs::read(path).await?;
let url = nip96_upload(&client, &nip96_server, file).await?; let url = nip96_upload(&client, &nip96_server, file).await?;
@@ -204,13 +205,12 @@ impl UserProfile {
Err(anyhow!("Path not found")) Err(anyhow!("Path not found"))
} }
} }
Ok(None) => Err(anyhow!("User cancelled")), _ => Err(anyhow!("Error")),
Err(e) => Err(anyhow!("File dialog error: {e}")),
} }
}); });
cx.spawn_in(window, async move |this, cx| { cx.spawn_in(window, async move |this, cx| {
let result = Flatten::flatten(task.await.map_err(|e| e.into())); let result = task.await;
this.update_in(cx, |this, window, cx| { this.update_in(cx, |this, window, cx| {
match result { match result {
@@ -233,7 +233,7 @@ impl UserProfile {
.detach(); .detach();
} }
pub fn set_metadata(&mut self, cx: &mut Context<Self>) -> Task<Result<Profile, Error>> { pub fn set_metadata(&mut self, cx: &mut Context<Self>) -> Task<Result<Person, Error>> {
let avatar = self.avatar_input.read(cx).value().to_string(); let avatar = self.avatar_input.read(cx).value().to_string();
let name = self.name_input.read(cx).value().to_string(); let name = self.name_input.read(cx).value().to_string();
let bio = self.bio_input.read(cx).value().to_string(); let bio = self.bio_input.read(cx).value().to_string();
@@ -259,27 +259,22 @@ impl UserProfile {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip(); let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
cx.background_spawn(async move { cx.background_spawn(async move {
let urls = write_relays.await;
let signer = client.signer().await?; let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let gossip = gossip.read().await;
let write_relays = gossip.inbox_relays(&public_key);
// Ensure connections to the write relays
gossip.ensure_connections(&client, &write_relays).await;
// Sign the new metadata event // Sign the new metadata event
let event = EventBuilder::metadata(&new_metadata).sign(&signer).await?; let event = EventBuilder::metadata(&new_metadata).sign(&signer).await?;
// Send event to user's write relayss // Send event to user's write relayss
client.send_event_to(write_relays, &event).await?; client.send_event_to(urls, &event).await?;
// Return the updated profile // Return the updated profile
let metadata = Metadata::from_json(&event.content).unwrap_or_default(); let metadata = Metadata::from_json(&event.content).unwrap_or_default();
let profile = Profile::new(event.pubkey, metadata); let profile = Person::new(event.pubkey, metadata);
Ok(profile) Ok(profile)
}) })

View File

@@ -1,6 +1,6 @@
use std::time::Duration; use std::time::Duration;
use common::{nip05_verify, shorten_pubkey, RenderedProfile}; use common::{nip05_verify, shorten_pubkey};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, relative, rems, App, AppContext, ClipboardItem, Context, Entity, IntoElement, div, relative, rems, App, AppContext, ClipboardItem, Context, Entity, IntoElement,
@@ -8,8 +8,7 @@ use gpui::{
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::PersonRegistry; use person::{Person, PersonRegistry};
use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::NostrRegistry; use state::NostrRegistry;
use theme::ActiveTheme; use theme::ActiveTheme;
@@ -23,7 +22,7 @@ pub fn init(public_key: PublicKey, window: &mut Window, cx: &mut App) -> Entity<
#[derive(Debug)] #[derive(Debug)]
pub struct ProfileViewer { pub struct ProfileViewer {
profile: Profile, profile: Person,
/// Follow status /// Follow status
followed: bool, followed: bool,
@@ -44,7 +43,7 @@ impl ProfileViewer {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get_person(&target, cx); let profile = persons.read(cx).get(&target, cx);
let mut tasks = smallvec![]; let mut tasks = smallvec![];
@@ -134,7 +133,6 @@ impl ProfileViewer {
impl Render for ProfileViewer { impl Render for ProfileViewer {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let bech32 = shorten_pubkey(self.profile.public_key(), 16); let bech32 = shorten_pubkey(self.profile.public_key(), 16);
let shared_bech32 = SharedString::from(bech32); let shared_bech32 = SharedString::from(bech32);
@@ -147,14 +145,14 @@ impl Render for ProfileViewer {
.items_center() .items_center()
.justify_center() .justify_center()
.text_center() .text_center()
.child(Avatar::new(self.profile.avatar(proxy)).size(rems(4.))) .child(Avatar::new(self.profile.avatar()).size(rems(4.)))
.child( .child(
v_flex() v_flex()
.child( .child(
div() div()
.font_semibold() .font_semibold()
.line_height(relative(1.25)) .line_height(relative(1.25))
.child(self.profile.display_name()), .child(self.profile.name()),
) )
.when_some(self.address(cx), |this, address| { .when_some(self.address(cx), |this, address| {
this.child( this.child(

View File

@@ -1,10 +1,9 @@
use std::ops::Range; use std::ops::Range;
use std::time::Duration; use std::time::Duration;
use account::Account;
use anyhow::{anyhow, Error}; use anyhow::{anyhow, Error};
use chat::{ChatRegistry, Room}; use chat::{ChatRegistry, Room};
use common::{nip05_profile, RenderedProfile, TextUtils, BOOTSTRAP_RELAYS}; use common::{nip05_profile, TextUtils, BOOTSTRAP_RELAYS};
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, px, relative, rems, uniform_list, App, AppContext, Context, Entity, InteractiveElement, div, px, relative, rems, uniform_list, App, AppContext, Context, Entity, InteractiveElement,
@@ -14,7 +13,6 @@ use gpui::{
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::PersonRegistry; use person::PersonRegistry;
use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::NostrRegistry; use state::NostrRegistry;
use theme::ActiveTheme; use theme::ActiveTheme;
@@ -312,9 +310,8 @@ impl Compose {
fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) { fn submit(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let chat = ChatRegistry::global(cx); let chat = ChatRegistry::global(cx);
let nostr = NostrRegistry::global(cx);
let account = Account::global(cx); let public_key = nostr.read(cx).identity().read(cx).public_key();
let public_key = account.read(cx).public_key();
let receivers: Vec<PublicKey> = self.selected(cx); let receivers: Vec<PublicKey> = self.selected(cx);
let subject_input = self.title_input.read(cx).value(); let subject_input = self.title_input.read(cx).value();
@@ -326,7 +323,8 @@ impl Compose {
}; };
chat.update(cx, |this, cx| { chat.update(cx, |this, cx| {
this.push_room(cx.new(|_| Room::new(subject, public_key, receivers)), cx); let room = cx.new(|_| Room::new(subject, public_key, receivers));
this.emit_room(room.downgrade(), cx);
}); });
window.close_modal(cx); window.close_modal(cx);
@@ -360,7 +358,6 @@ impl Compose {
} }
fn list_items(&self, range: Range<usize>, cx: &Context<Self>) -> Vec<impl IntoElement> { fn list_items(&self, range: Range<usize>, cx: &Context<Self>) -> Vec<impl IntoElement> {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let mut items = Vec::with_capacity(self.contacts.read(cx).len()); let mut items = Vec::with_capacity(self.contacts.read(cx).len());
@@ -370,7 +367,7 @@ impl Compose {
}; };
let public_key = contact.public_key; let public_key = contact.public_key;
let profile = persons.read(cx).get_person(&public_key, cx); let profile = persons.read(cx).get(&public_key, cx);
items.push( items.push(
h_flex() h_flex()
@@ -384,8 +381,8 @@ impl Compose {
h_flex() h_flex()
.gap_1p5() .gap_1p5()
.text_sm() .text_sm()
.child(Avatar::new(profile.avatar(proxy)).size(rems(1.75))) .child(Avatar::new(profile.avatar()).size(rems(1.75)))
.child(profile.display_name()), .child(profile.name()),
) )
.when(contact.selected, |this| { .when(contact.selected, |this| {
this.child( this.child(

View File

@@ -8,8 +8,7 @@ use gpui::{
}; };
use gpui_tokio::Tokio; use gpui_tokio::Tokio;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use person::PersonRegistry; use person::{Person, PersonRegistry};
use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::NostrRegistry; use state::NostrRegistry;
use theme::ActiveTheme; use theme::ActiveTheme;
@@ -23,7 +22,7 @@ pub fn init(public_key: PublicKey, window: &mut Window, cx: &mut App) -> Entity<
} }
pub struct Screening { pub struct Screening {
profile: Profile, profile: Person,
verified: bool, verified: bool,
followed: bool, followed: bool,
last_active: Option<Timestamp>, last_active: Option<Timestamp>,
@@ -37,7 +36,7 @@ impl Screening {
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get_person(&public_key, cx); let profile = persons.read(cx).get(&public_key, cx);
let mut tasks = smallvec![]; let mut tasks = smallvec![];
@@ -225,7 +224,6 @@ impl Screening {
impl Render for Screening { impl Render for Screening {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let proxy = AppSettings::get_proxy_user_avatars(cx);
let shorten_pubkey = shorten_pubkey(self.profile.public_key(), 8); let shorten_pubkey = shorten_pubkey(self.profile.public_key(), 8);
let total_mutuals = self.mutual_contacts.len(); let total_mutuals = self.mutual_contacts.len();
let last_active = self.last_active.map(|_| true); let last_active = self.last_active.map(|_| true);
@@ -238,12 +236,12 @@ impl Render for Screening {
.items_center() .items_center()
.justify_center() .justify_center()
.text_center() .text_center()
.child(Avatar::new(self.profile.avatar(proxy)).size(rems(4.))) .child(Avatar::new(self.profile.avatar()).size(rems(4.)))
.child( .child(
div() div()
.font_semibold() .font_semibold()
.line_height(relative(1.25)) .line_height(relative(1.25))
.child(self.profile.display_name()), .child(self.profile.name()),
), ),
) )
.child( .child(

View File

@@ -158,17 +158,14 @@ impl SetupRelay {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip(); let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let relays = self.relays.clone(); let relays = self.relays.clone();
let task: Task<Result<(), Error>> = cx.background_spawn(async move { let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
let signer = client.signer().await?; let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let gossip = gossip.read().await;
let write_relays = gossip.inbox_relays(&public_key);
// Ensure connections to the write relays
gossip.ensure_connections(&client, &write_relays).await;
let tags: Vec<Tag> = relays let tags: Vec<Tag> = relays
.iter() .iter()
@@ -181,7 +178,7 @@ impl SetupRelay {
.await?; .await?;
// Set messaging relays // Set messaging relays
client.send_event_to(write_relays, &event).await?; client.send_event_to(urls, &event).await?;
// Connect to messaging relays // Connect to messaging relays
for relay in relays.iter() { for relay in relays.iter() {

View File

@@ -1,6 +1,6 @@
use std::time::Duration; use std::time::Duration;
use common::{RenderedProfile, BUNKER_TIMEOUT}; use common::BUNKER_TIMEOUT;
use gpui::prelude::FluentBuilder; use gpui::prelude::FluentBuilder;
use gpui::{ use gpui::{
div, relative, rems, svg, AnyElement, App, AppContext, Context, Entity, EventEmitter, div, relative, rems, svg, AnyElement, App, AppContext, Context, Entity, EventEmitter,
@@ -29,11 +29,16 @@ pub fn init(cre: Credential, window: &mut Window, cx: &mut App) -> Entity<Startu
/// Startup /// Startup
#[derive(Debug)] #[derive(Debug)]
pub struct Startup { pub struct Startup {
credential: Credential,
loading: bool,
name: SharedString, name: SharedString,
focus_handle: FocusHandle, focus_handle: FocusHandle,
/// Local user credentials
credential: Credential,
/// Whether the loadng is in progress
loading: bool,
/// Image cache
image_cache: Entity<RetainAllImageCache>, image_cache: Entity<RetainAllImageCache>,
/// Event subscriptions /// Event subscriptions
@@ -164,15 +169,12 @@ impl Startup {
} }
fn login_with_keys(&mut self, secret: SecretKey, cx: &mut Context<Self>) { fn login_with_keys(&mut self, secret: SecretKey, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let keys = Keys::new(secret); let keys = Keys::new(secret);
let nostr = NostrRegistry::global(cx);
// Update the signer nostr.update(cx, |this, cx| {
cx.background_spawn(async move { this.set_signer(keys, cx);
client.set_signer(keys).await;
}) })
.detach();
} }
fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) { fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
@@ -203,9 +205,7 @@ impl Render for Startup {
fn render(&mut self, _window: &mut gpui::Window, cx: &mut Context<Self>) -> impl IntoElement { fn render(&mut self, _window: &mut gpui::Window, cx: &mut Context<Self>) -> impl IntoElement {
let persons = PersonRegistry::global(cx); let persons = PersonRegistry::global(cx);
let bunker = self.credential.secret().starts_with("bunker://"); let bunker = self.credential.secret().starts_with("bunker://");
let profile = persons let profile = persons.read(cx).get(&self.credential.public_key(), cx);
.read(cx)
.get_person(&self.credential.public_key(), cx);
v_flex() v_flex()
.image_cache(self.image_cache.clone()) .image_cache(self.image_cache.clone())
@@ -266,8 +266,8 @@ impl Render for Startup {
) )
}) })
.when(!self.loading, |this| { .when(!self.loading, |this| {
let avatar = profile.avatar(true); let avatar = profile.avatar();
let name = profile.display_name(); let name = profile.name();
this.child( this.child(
h_flex() h_flex()

View File

@@ -1,22 +1,21 @@
[package] [package]
name = "encryption" name = "device"
version.workspace = true version.workspace = true
edition.workspace = true edition.workspace = true
publish.workspace = true publish.workspace = true
[dependencies] [dependencies]
state = { path = "../state" }
common = { path = "../common" } common = { path = "../common" }
account = { path = "../account" } state = { path = "../state" }
gpui.workspace = true gpui.workspace = true
nostr-sdk.workspace = true nostr-sdk.workspace = true
anyhow.workspace = true anyhow.workspace = true
itertools.workspace = true
smallvec.workspace = true smallvec.workspace = true
smol.workspace = true smol.workspace = true
futures.workspace = true
flume.workspace = true
log.workspace = true log.workspace = true
flume.workspace = true
serde.workspace = true serde.workspace = true
serde_json.workspace = true serde_json.workspace = true

View File

@@ -0,0 +1,62 @@
use gpui::SharedString;
use nostr_sdk::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum DeviceState {
#[default]
Initial,
Requesting,
Set,
}
/// Announcement
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Announcement {
/// The public key of the device that created this announcement.
public_key: PublicKey,
/// The name of the device that created this announcement.
client_name: Option<String>,
}
impl From<&Event> for Announcement {
fn from(val: &Event) -> Self {
let public_key = val
.tags
.iter()
.find(|tag| tag.kind().as_str() == "n")
.and_then(|tag| tag.content())
.and_then(|c| PublicKey::parse(c).ok())
.unwrap_or(val.pubkey);
let client_name = val
.tags
.find(TagKind::Client)
.and_then(|tag| tag.content())
.map(|c| c.to_string());
Self::new(public_key, client_name)
}
}
impl Announcement {
pub fn new(public_key: PublicKey, client_name: Option<String>) -> Self {
Self {
public_key,
client_name,
}
}
/// Returns the public key of the device that created this announcement.
pub fn public_key(&self) -> PublicKey {
self.public_key
}
/// Returns the client name of the device that created this announcement.
pub fn client_name(&self) -> SharedString {
self.client_name
.as_ref()
.map(SharedString::from)
.unwrap_or(SharedString::from("Unknown"))
}
}

632
crates/device/src/lib.rs Normal file
View File

@@ -0,0 +1,632 @@
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{anyhow, Context as AnyhowContext, Error};
use common::app_name;
pub use device::*;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec};
use state::{NostrRegistry, RelayState, GIFTWRAP_SUBSCRIPTION, TIMEOUT};
mod device;
const IDENTIFIER: &str = "coop:device";
pub fn init(cx: &mut App) {
DeviceRegistry::set_global(cx.new(DeviceRegistry::new), cx);
}
struct GlobalDeviceRegistry(Entity<DeviceRegistry>);
impl Global for GlobalDeviceRegistry {}
/// Device Registry
///
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
#[derive(Debug)]
pub struct DeviceRegistry {
/// Device signer
pub device_signer: Entity<Option<Arc<dyn NostrSigner>>>,
/// Device requests
requests: Entity<HashSet<Event>>,
/// Device state
state: DeviceState,
/// Async tasks
tasks: Vec<Task<Result<(), Error>>>,
/// Subscriptions
_subscriptions: SmallVec<[Subscription; 1]>,
}
impl DeviceRegistry {
/// Retrieve the global device registry state
pub fn global(cx: &App) -> Entity<Self> {
cx.global::<GlobalDeviceRegistry>().0.clone()
}
/// Set the global device registry instance
fn set_global(state: Entity<Self>, cx: &mut App) {
cx.set_global(GlobalDeviceRegistry(state));
}
/// Create a new device registry instance
fn new(cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let identity = nostr.read(cx).identity();
let device_signer = cx.new(|_| None);
let requests = cx.new(|_| HashSet::default());
// Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<Event>(100);
let mut subscriptions = smallvec![];
let mut tasks = vec![];
subscriptions.push(
// Observe the identity entity
cx.observe(&identity, |this, state, cx| {
if state.read(cx).has_public_key() {
if state.read(cx).relay_list_state() == RelayState::Set {
this.get_announcement(cx);
}
if state.read(cx).messaging_relays_state() == RelayState::Set {
this.get_messages(cx);
}
}
}),
);
tasks.push(
// Handle nostr notifications
cx.background_spawn(async move { Self::handle_notifications(&client, &tx).await }),
);
tasks.push(
// Update GPUI states
cx.spawn(async move |this, cx| {
while let Ok(event) = rx.recv_async().await {
match event.kind {
Kind::Custom(4454) => {
this.update(cx, |this, cx| {
this.add_request(event, cx);
})?;
}
Kind::Custom(4455) => {
this.update(cx, |this, cx| {
this.parse_response(event, cx);
})?;
}
_ => {}
}
}
Ok(())
}),
);
Self {
device_signer,
requests,
state: DeviceState::default(),
tasks,
_subscriptions: subscriptions,
}
}
/// Handle nostr notifications
async fn handle_notifications(client: &Client, tx: &flume::Sender<Event>) -> Result<(), Error> {
let mut notifications = client.notifications();
let mut processed_events = HashSet::new();
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Message {
message: RelayMessage::Event { event, .. },
..
} = notification
{
if !processed_events.insert(event.id) {
// Skip if the event has already been processed
continue;
}
match event.kind {
Kind::Custom(4454) => {
if Self::verify_author(client, event.as_ref()).await {
tx.send_async(event.into_owned()).await.ok();
}
}
Kind::Custom(4455) => {
if Self::verify_author(client, event.as_ref()).await {
tx.send_async(event.into_owned()).await.ok();
}
}
_ => {}
}
}
}
Ok(())
}
/// Verify the author of an event
async fn verify_author(client: &Client, event: &Event) -> bool {
if let Ok(signer) = client.signer().await {
if let Ok(public_key) = signer.get_public_key().await {
return public_key == event.pubkey;
}
}
false
}
/// Encrypt and store device keys in the local database.
async fn set_keys(client: &Client, secret: &str) -> Result<(), Error> {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
// Encrypt the value
let content = signer.nip44_encrypt(&public_key, secret).await?;
// Construct the application data event
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tag(Tag::identifier(IDENTIFIER))
.build(public_key)
.sign(&Keys::generate())
.await?;
// Save the event to the database
client.database().save_event(&event).await?;
Ok(())
}
/// Get device keys from the local database.
async fn get_keys(client: &Client) -> Result<Keys, Error> {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(IDENTIFIER);
if let Some(event) = client.database().query(filter).await?.first() {
let content = signer.nip44_decrypt(&public_key, &event.content).await?;
let secret = SecretKey::parse(&content)?;
let keys = Keys::new(secret);
Ok(keys)
} else {
Err(anyhow!("Key not found"))
}
}
/// Returns the device signer entity
pub fn signer(&self, cx: &App) -> Option<Arc<dyn NostrSigner>> {
self.device_signer.read(cx).clone()
}
/// Set the decoupled encryption key for the current user
fn set_device_signer<S>(&mut self, signer: S, cx: &mut Context<Self>)
where
S: NostrSigner + 'static,
{
self.set_state(DeviceState::Set, cx);
self.device_signer.update(cx, |this, cx| {
*this = Some(Arc::new(signer));
cx.notify();
});
}
/// Set the device state
fn set_state(&mut self, state: DeviceState, cx: &mut Context<Self>) {
self.state = state;
cx.notify();
}
/// Add a request for device keys
fn add_request(&mut self, request: Event, cx: &mut Context<Self>) {
self.requests.update(cx, |this, cx| {
this.insert(request);
cx.notify();
});
}
/// Continuously get gift wrap events for the current user in their messaging relays
fn get_messages(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let device_signer = self.device_signer.read(cx).clone();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let messaging_relays = nostr.read(cx).messaging_relays(&public_key, cx);
cx.background_spawn(async move {
let urls = messaging_relays.await;
let id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION);
let mut filters = vec![];
// Construct a filter to get user messages
filters.push(Filter::new().kind(Kind::GiftWrap).pubkey(public_key));
// Construct a filter to get dekey messages if available
if let Some(signer) = device_signer.as_ref() {
if let Ok(pubkey) = signer.get_public_key().await {
filters.push(Filter::new().kind(Kind::GiftWrap).pubkey(pubkey));
}
}
if let Err(e) = client.subscribe_with_id_to(urls, id, filters, None).await {
log::error!("Failed to subscribe to gift wrap events: {e}");
}
})
.detach();
}
/// Get device announcement for current user
fn get_announcement(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let task: Task<Result<Event, Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
// Construct the filter for the device announcement event
let filter = Filter::new()
.kind(Kind::Custom(10044))
.author(public_key)
.limit(1);
let mut stream = client
.stream_events_from(&urls, vec![filter], Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
match res {
Ok(event) => {
log::info!("Received device announcement event: {event:?}");
return Ok(event);
}
Err(e) => {
log::error!("Failed to receive device announcement event: {e}");
}
}
}
Err(anyhow!("Device announcement not found"))
});
self.tasks.push(cx.spawn(async move |this, cx| {
match task.await {
Ok(event) => {
this.update(cx, |this, cx| {
this.init_device_signer(&event, cx);
})?;
}
Err(_) => {
this.update(cx, |this, cx| {
this.announce_device(cx);
})?;
}
}
Ok(())
}));
}
/// Create a new device signer and announce it
fn announce_device(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let keys = Keys::generate();
let secret = keys.secret_key().to_secret_hex();
let n = keys.public_key();
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let signer = client.signer().await?;
let urls = write_relays.await;
// Construct an announcement event
let event = EventBuilder::new(Kind::Custom(10044), "")
.tags(vec![
Tag::custom(TagKind::custom("n"), vec![n]),
Tag::client(app_name()),
])
.sign(&signer)
.await?;
// Publish announcement
client.send_event_to(&urls, &event).await?;
// Save device keys to the database
Self::set_keys(&client, &secret).await?;
Ok(())
});
cx.spawn(async move |this, cx| {
if task.await.is_ok() {
this.update(cx, |this, cx| {
this.set_device_signer(keys, cx);
this.listen_device_request(cx);
})
.ok();
}
})
.detach();
}
/// Initialize device signer (decoupled encryption key) for the current user
fn init_device_signer(&mut self, event: &Event, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let announcement = Announcement::from(event);
let device_pubkey = announcement.public_key();
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
if let Ok(keys) = Self::get_keys(&client).await {
if keys.public_key() != device_pubkey {
return Err(anyhow!("Key mismatch"));
};
Ok(keys)
} else {
Err(anyhow!("Key not found"))
}
});
cx.spawn(async move |this, cx| {
match task.await {
Ok(keys) => {
this.update(cx, |this, cx| {
this.set_device_signer(keys, cx);
this.listen_device_request(cx);
})
.ok();
}
Err(e) => {
this.update(cx, |this, cx| {
this.request_device_keys(cx);
this.listen_device_approval(cx);
})
.ok();
log::warn!("Failed to initialize device signer: {e}");
}
};
})
.detach();
}
/// Listen for device key requests on user's write relays
fn listen_device_request(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
// Construct a filter for device key requests
let filter = Filter::new()
.kind(Kind::Custom(4454))
.author(public_key)
.since(Timestamp::now());
// Subscribe to the device key requests on user's write relays
client.subscribe_to(&urls, vec![filter], None).await?;
Ok(())
});
task.detach();
}
/// Listen for device key approvals on user's write relays
fn listen_device_approval(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
// Construct a filter for device key requests
let filter = Filter::new()
.kind(Kind::Custom(4455))
.author(public_key)
.since(Timestamp::now());
// Subscribe to the device key requests on user's write relays
client.subscribe_to(&urls, vec![filter], None).await?;
Ok(())
});
task.detach();
}
/// Request encryption keys from other device
fn request_device_keys(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let app_keys = nostr.read(cx).app_keys().clone();
let app_pubkey = app_keys.public_key();
let task: Task<Result<Option<Keys>, Error>> = cx.background_spawn(async move {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::Custom(4455))
.author(public_key)
.pubkey(app_pubkey)
.limit(1);
match client.database().query(filter).await?.first_owned() {
Some(event) => {
let root_device = event
.tags
.find(TagKind::custom("P"))
.and_then(|tag| tag.content())
.and_then(|content| PublicKey::parse(content).ok())
.context("Invalid event's tags")?;
let payload = event.content.as_str();
let decrypted = app_keys.nip44_decrypt(&root_device, payload).await?;
let secret = SecretKey::from_hex(&decrypted)?;
let keys = Keys::new(secret);
Ok(Some(keys))
}
None => {
let urls = write_relays.await;
// Construct an event for device key request
let event = EventBuilder::new(Kind::Custom(4454), "")
.tags(vec![
Tag::client(app_name()),
Tag::custom(TagKind::custom("P"), vec![app_pubkey]),
])
.sign(&signer)
.await?;
// Send the event to write relays
client.send_event_to(&urls, &event).await?;
Ok(None)
}
}
});
cx.spawn(async move |this, cx| {
match task.await {
Ok(Some(keys)) => {
this.update(cx, |this, cx| {
this.set_device_signer(keys, cx);
})
.ok();
}
Ok(None) => {
this.update(cx, |this, cx| {
this.set_state(DeviceState::Requesting, cx);
})
.ok();
}
Err(e) => {
log::error!("Failed to request the encryption key: {e}");
}
};
})
.detach();
}
/// Parse the response event for device keys from other devices
fn parse_response(&mut self, event: Event, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let app_keys = nostr.read(cx).app_keys().clone();
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
let root_device = event
.tags
.find(TagKind::custom("P"))
.and_then(|tag| tag.content())
.and_then(|content| PublicKey::parse(content).ok())
.context("Invalid event's tags")?;
let payload = event.content.as_str();
let decrypted = app_keys.nip44_decrypt(&root_device, payload).await?;
let secret = SecretKey::from_hex(&decrypted)?;
let keys = Keys::new(secret);
Ok(keys)
});
cx.spawn(async move |this, cx| {
match task.await {
Ok(keys) => {
this.update(cx, |this, cx| {
this.set_device_signer(keys, cx);
})
.ok();
}
Err(e) => {
log::error!("Error: {e}")
}
};
})
.detach();
}
/// Approve requests for device keys from other devices
#[allow(dead_code)]
fn approve(&mut self, event: Event, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let public_key = nostr.read(cx).identity().read(cx).public_key();
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
let signer = client.signer().await?;
// Get device keys
let keys = Self::get_keys(&client).await?;
let secret = keys.secret_key().to_secret_hex();
// Extract the target public key from the event tags
let target = event
.tags
.find(TagKind::custom("P"))
.and_then(|tag| tag.content())
.and_then(|content| PublicKey::parse(content).ok())
.context("Target is not a valid public key")?;
// Encrypt the device keys with the user's signer
let payload = signer.nip44_encrypt(&target, &secret).await?;
// Construct the response event
//
// P tag: the current device's public key
// p tag: the requester's public key
let event = EventBuilder::new(Kind::Custom(4455), payload)
.tags(vec![
Tag::custom(TagKind::custom("P"), vec![keys.public_key()]),
Tag::public_key(target),
])
.sign(&signer)
.await?;
// Send the response event to the user's relay list
client.send_event_to(&urls, &event).await?;
Ok(())
});
task.detach();
}
}

View File

@@ -1,682 +0,0 @@
use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration;
use account::Account;
use anyhow::{anyhow, Context as AnyhowContext, Error};
use common::app_name;
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use nostr_sdk::prelude::*;
pub use signer::*;
use smallvec::{smallvec, SmallVec};
use state::{Announcement, NostrRegistry};
mod signer;
pub fn init(cx: &mut App) {
Encryption::set_global(cx.new(Encryption::new), cx);
}
struct GlobalEncryption(Entity<Encryption>);
impl Global for GlobalEncryption {}
pub struct Encryption {
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
///
/// Client Signer that used for communication between devices
client_signer: Entity<Option<Arc<dyn NostrSigner>>>,
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
///
/// Encryption Key used for encryption and decryption instead of the user's identity
pub encryption: Entity<Option<Arc<dyn NostrSigner>>>,
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
///
/// Encryption Key announcement
announcement: Option<Arc<Announcement>>,
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
///
/// Requests for encryption keys from other devices
requests: Entity<HashSet<Announcement>>,
/// Async task for handling notifications
handle_notifications: Option<Task<()>>,
/// Async task for handling requests
handle_requests: Option<Task<()>>,
/// Event subscriptions
_subscriptions: SmallVec<[Subscription; 2]>,
/// Tasks for asynchronous operations
_tasks: SmallVec<[Task<()>; 1]>,
}
impl Encryption {
/// Retrieve the global encryption state
pub fn global(cx: &App) -> Entity<Self> {
cx.global::<GlobalEncryption>().0.clone()
}
/// Set the global encryption instance
fn set_global(state: Entity<Self>, cx: &mut App) {
cx.set_global(GlobalEncryption(state));
}
/// Create a new encryption instance
fn new(cx: &mut Context<Self>) -> Self {
let account = Account::global(cx);
let requests = cx.new(|_| HashSet::default());
let encryption = cx.new(|_| None);
let client_signer = cx.new(|_| None);
let mut subscriptions = smallvec![];
subscriptions.push(
// Observe the account state
cx.observe(&account, |this, state, cx| {
if state.read(cx).has_account() && this.client_signer.read(cx).is_none() {
this.get_client(cx);
}
}),
);
subscriptions.push(
// Observe the client signer state
cx.observe(&client_signer, |this, state, cx| {
if state.read(cx).is_some() {
this.get_announcement(cx);
}
}),
);
subscriptions.push(
// Observe the encryption signer state
cx.observe(&encryption, |this, state, cx| {
if state.read(cx).is_some() {
this._tasks.push(this.resubscribe_messages(cx));
}
}),
);
Self {
requests,
client_signer,
encryption,
announcement: None,
handle_notifications: None,
handle_requests: None,
_subscriptions: subscriptions,
_tasks: smallvec![],
}
}
/// Encrypt and store a key in the local database.
async fn set_keys<T>(client: &Client, kind: T, value: String) -> Result<(), Error>
where
T: Into<String>,
{
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
// Encrypt the value
let content = signer.nip44_encrypt(&public_key, value.as_ref()).await?;
// Construct the application data event
let event = EventBuilder::new(Kind::ApplicationSpecificData, content)
.tag(Tag::identifier(format!("coop:{}", kind.into())))
.build(public_key)
.sign(&Keys::generate())
.await?;
// Save the event to the database
client.database().save_event(&event).await?;
Ok(())
}
/// Get and decrypt a key from the local database.
async fn get_keys<T>(client: &Client, kind: T) -> Result<Keys, Error>
where
T: Into<String>,
{
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::ApplicationSpecificData)
.identifier(format!("coop:{}", kind.into()));
if let Some(event) = client.database().query(filter).await?.first() {
let content = signer.nip44_decrypt(&public_key, &event.content).await?;
let secret = SecretKey::parse(&content)?;
let keys = Keys::new(secret);
Ok(keys)
} else {
Err(anyhow!("Key not found"))
}
}
/// Get the client keys from the database
fn get_client(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
self._tasks.push(
// Run in the main thread
cx.spawn(async move |this, cx| {
match Self::get_keys(&client, "client").await {
Ok(keys) => {
this.update(cx, |this, cx| {
this.set_client(Arc::new(keys), cx);
})
.expect("Entity has been released");
}
Err(_) => {
let keys = Keys::generate();
let secret = keys.secret_key().to_secret_hex();
// Store the key in the database for future use
Self::set_keys(&client, "client", secret).await.ok();
// Update global state
this.update(cx, |this, cx| {
this.set_client(Arc::new(keys), cx);
})
.expect("Entity has been released");
}
}
}),
)
}
/// Get the announcement from the database
fn get_announcement(&mut self, cx: &mut Context<Self>) {
let task = self._get_announcement(cx);
let delay = Duration::from_secs(5);
self._tasks.push(
// Run task in the background
cx.spawn(async move |this, cx| {
cx.background_executor().timer(delay).await;
if let Ok(announcement) = task.await {
this.update(cx, |this, cx| {
this.load_encryption(&announcement, cx);
// Set the announcement
this.announcement = Some(Arc::new(announcement));
cx.notify();
})
.expect("Entity has been released");
}
}),
);
}
fn _get_announcement(&self, cx: &App) -> Task<Result<Announcement, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
cx.background_spawn(async move {
let user_signer = client.signer().await?;
let public_key = user_signer.get_public_key().await?;
let filter = Filter::new()
.kind(Kind::Custom(10044))
.author(public_key)
.limit(1);
if let Some(event) = client.database().query(filter).await?.first() {
Ok(NostrRegistry::extract_announcement(event)?)
} else {
Err(anyhow!("Announcement not found"))
}
})
}
/// Load the encryption key that stored in the database
///
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
fn load_encryption(&mut self, announcement: &Announcement, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let n = announcement.public_key();
cx.spawn(async move |this, cx| {
let result = Self::get_keys(&client, "encryption").await;
this.update(cx, |this, cx| {
if let Ok(keys) = result {
if keys.public_key() == n {
this.set_encryption(Arc::new(keys), cx);
this.listen_request(cx);
}
}
this.load_response(cx);
})
.expect("Entity has been released");
})
.detach();
}
pub fn load_response(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
// Get the client signer
let Some(client_signer) = self.client_signer.read(cx).clone() else {
return;
};
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let filter = Filter::new()
.author(public_key)
.kind(Kind::Custom(4455))
.limit(1);
if let Some(event) = client.database().query(filter).await?.first_owned() {
let response = NostrRegistry::extract_response(&client, &event).await?;
// Decrypt the payload using the client signer
let decrypted = client_signer
.nip44_decrypt(&response.public_key(), response.payload())
.await?;
// Construct the encryption keys
let secret = SecretKey::parse(&decrypted)?;
let keys = Keys::new(secret);
return Ok(keys);
}
Err(anyhow!("not found"))
});
cx.spawn(async move |this, cx| {
match task.await {
Ok(keys) => {
this.update(cx, |this, cx| {
this.set_encryption(Arc::new(keys), cx);
})
.expect("Entity has been released");
}
Err(e) => {
log::warn!("Failed to load encryption response: {e}");
}
};
})
.detach();
}
/// Listen for the encryption key request from other devices
///
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
pub fn listen_request(&mut self, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let (tx, rx) = flume::bounded::<Announcement>(50);
let task: Task<Result<(), Error>> = cx.background_spawn({
let client = nostr.read(cx).client();
async move {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let id = SubscriptionId::new("listen-request");
let filter = Filter::new()
.author(public_key)
.kind(Kind::Custom(4454))
.since(Timestamp::now());
// Unsubscribe from the previous subscription
client.unsubscribe(&id).await;
// Subscribe to the new subscription
client.subscribe_with_id(id, filter, None).await?;
Ok(())
}
});
// Run this task and finish in the background
task.detach();
// Handle notifications
self.handle_notifications = Some(cx.background_spawn(async move {
let mut notifications = client.notifications();
let mut processed_events = HashSet::new();
while let Ok(notification) = notifications.recv().await {
let RelayPoolNotification::Message { message, .. } = notification else {
// Skip if the notification is not a message
continue;
};
if let RelayMessage::Event { event, .. } = message {
if !processed_events.insert(event.id) {
// Skip if the event has already been processed
continue;
}
if event.kind != Kind::Custom(4454) {
// Skip if the event is not a encryption events
continue;
};
if NostrRegistry::is_self_authored(&client, &event).await {
if let Ok(announcement) = NostrRegistry::extract_announcement(&event) {
tx.send_async(announcement).await.ok();
}
}
}
}
}));
// Handle requests
self.handle_requests = Some(cx.spawn(async move |this, cx| {
while let Ok(request) = rx.recv_async().await {
this.update(cx, |this, cx| {
this.set_request(request, cx);
})
.expect("Entity has been released");
}
}));
}
/// Overwrite the encryption key
///
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
pub fn new_encryption(&self, cx: &App) -> Task<Result<Keys, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
let keys = Keys::generate();
let public_key = keys.public_key();
let secret = keys.secret_key().to_secret_hex();
// Create a task announce the encryption key
cx.background_spawn(async move {
// Store the encryption key to the database
Self::set_keys(&client, "encryption", secret).await?;
let signer = client.signer().await?;
let signer_pubkey = signer.get_public_key().await?;
let gossip = gossip.read().await;
let write_relays = gossip.outbox_relays(&signer_pubkey);
// Ensure connections to the write relays
gossip.ensure_connections(&client, &write_relays).await;
// Construct the announcement event
let event = EventBuilder::new(Kind::Custom(10044), "")
.tags(vec![
Tag::client(app_name()),
Tag::custom(TagKind::custom("n"), vec![public_key]),
])
.build(signer_pubkey)
.sign(&signer)
.await?;
// Send the announcement event to user's relays
client.send_event_to(write_relays, &event).await?;
Ok(keys)
})
}
/// Send a request for encryption key from other clients
///
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
pub fn send_request(&self, cx: &App) -> Task<Result<Option<Keys>, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
// Get the client signer
let Some(client_signer) = self.client_signer.read(cx).clone() else {
return Task::ready(Err(anyhow!("Client Signer is required")));
};
cx.background_spawn(async move {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let client_pubkey = client_signer.get_public_key().await?;
// Get the encryption key approval response from the database first
let filter = Filter::new()
.kind(Kind::Custom(4455))
.author(public_key)
.pubkey(client_pubkey)
.limit(1);
match client.database().query(filter).await?.first_owned() {
Some(event) => {
let root_device = event
.tags
.find(TagKind::custom("P"))
.and_then(|tag| tag.content())
.and_then(|content| PublicKey::parse(content).ok())
.context("Invalid event's tags")?;
let payload = event.content.as_str();
let decrypted = client_signer.nip44_decrypt(&root_device, payload).await?;
let secret = SecretKey::from_hex(&decrypted)?;
let keys = Keys::new(secret);
Ok(Some(keys))
}
None => {
let gossip = gossip.read().await;
let write_relays = gossip.outbox_relays(&public_key);
// Ensure connections to the write relays
gossip.ensure_connections(&client, &write_relays).await;
// Construct encryption keys request event
let event = EventBuilder::new(Kind::Custom(4454), "")
.tags(vec![
Tag::client(app_name()),
Tag::custom(TagKind::custom("pubkey"), vec![client_pubkey]),
])
.sign(&signer)
.await?;
// Send a request for encryption keys from other devices
client.send_event_to(&write_relays, &event).await?;
// Create a unique ID to control the subscription later
let subscription_id = SubscriptionId::new("listen-response");
let filter = Filter::new()
.kind(Kind::Custom(4455))
.author(public_key)
.since(Timestamp::now());
// Subscribe to the approval response event
client
.subscribe_with_id_to(&write_relays, subscription_id, filter, None)
.await?;
Ok(None)
}
}
})
}
/// Send the approval response event
///
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
pub fn send_response(&self, target: PublicKey, cx: &App) -> Task<Result<(), Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
// Get the client signer
let Some(client_signer) = self.client_signer.read(cx).clone() else {
return Task::ready(Err(anyhow!("Client Signer is required")));
};
cx.background_spawn(async move {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
let gossip = gossip.read().await;
let write_relays = gossip.outbox_relays(&public_key);
// Ensure connections to the write relays
gossip.ensure_connections(&client, &write_relays).await;
let encryption = Self::get_keys(&client, "encryption").await?;
let client_pubkey = client_signer.get_public_key().await?;
// Encrypt the encryption keys with the client's signer
let payload = client_signer
.nip44_encrypt(&target, &encryption.secret_key().to_secret_hex())
.await?;
// Construct the response event
//
// P tag: the current client's public key
// p tag: the requester's public key
let event = EventBuilder::new(Kind::Custom(4455), payload)
.tags(vec![
Tag::custom(TagKind::custom("P"), vec![client_pubkey]),
Tag::public_key(target),
])
.build(public_key)
.sign(&signer)
.await?;
// Send the response event to the user's relay list
client.send_event_to(write_relays, &event).await?;
Ok(())
})
}
/// Wait for the approval response event
///
/// NIP-4e: https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md
pub fn wait_for_approval(&self, cx: &App) -> Task<Result<Keys, Error>> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
// Get the client signer
let Some(client_signer) = self.client_signer.read(cx).clone() else {
return Task::ready(Err(anyhow!("Client Signer is required")));
};
cx.background_spawn(async move {
let mut notifications = client.notifications();
let mut processed_events = HashSet::new();
while let Ok(notification) = notifications.recv().await {
let RelayPoolNotification::Message { message, .. } = notification else {
// Skip non-message notifications
continue;
};
if let RelayMessage::Event { event, .. } = message {
if !processed_events.insert(event.id) {
// Skip if the event has already been processed
continue;
}
if event.kind != Kind::Custom(4455) {
// Skip non-response events
continue;
}
if let Ok(response) = NostrRegistry::extract_response(&client, &event).await {
let public_key = response.public_key();
let payload = response.payload();
// Decrypt the payload using the client signer
let decrypted = client_signer.nip44_decrypt(&public_key, payload).await?;
let secret = SecretKey::parse(&decrypted)?;
// Construct the encryption keys
let keys = Keys::new(secret);
return Ok(keys);
} else {
log::error!("Failed to extract response from event");
}
}
}
Err(anyhow!("Failed to handle Encryption Key approval response"))
})
}
/// Set the client signer for the account
pub fn set_client(&mut self, signer: Arc<dyn NostrSigner>, cx: &mut Context<Self>) {
self.client_signer.update(cx, |this, cx| {
*this = Some(signer);
cx.notify();
});
}
/// Set the encryption signer for the account
pub fn set_encryption(&mut self, signer: Arc<dyn NostrSigner>, cx: &mut Context<Self>) {
self.encryption.update(cx, |this, cx| {
*this = Some(signer);
cx.notify();
});
}
/// Check if the account entity has an encryption key
pub fn has_encryption(&self, cx: &App) -> bool {
self.encryption.read(cx).is_some()
}
/// Returns the encryption key
pub fn encryption_key(&self, cx: &App) -> Option<Arc<dyn NostrSigner>> {
self.encryption.read(cx).clone()
}
/// Returns the encryption announcement
pub fn announcement(&self) -> Option<Arc<Announcement>> {
self.announcement.clone()
}
/// Returns the encryption requests
pub fn requests(&self) -> Entity<HashSet<Announcement>> {
self.requests.clone()
}
/// Push the encryption request
pub fn set_request(&mut self, request: Announcement, cx: &mut Context<Self>) {
self.requests.update(cx, |this, cx| {
this.insert(request);
cx.notify();
});
}
/// Resubscribe to gift wrap events
fn resubscribe_messages(&self, cx: &App) -> Task<()> {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let gossip = nostr.read(cx).gossip();
let account = Account::global(cx);
let public_key = account.read(cx).public_key();
cx.background_spawn(async move {
let gossip = gossip.read().await;
let relays = gossip.messaging_relays(&public_key);
NostrRegistry::get_messages(&client, public_key, &relays).await;
})
}
}

View File

@@ -1,9 +0,0 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Default, Deserialize, Serialize)]
pub enum SignerKind {
Encryption,
#[default]
User,
Auto,
}

View File

@@ -1,27 +0,0 @@
[package]
name = "encryption_ui"
version.workspace = true
edition.workspace = true
publish.workspace = true
[dependencies]
state = { path = "../state" }
ui = { path = "../ui" }
theme = { path = "../theme" }
common = { path = "../common" }
account = { path = "../account" }
encryption = { path = "../encryption" }
person = { path = "../person" }
settings = { path = "../settings" }
gpui.workspace = true
nostr-sdk.workspace = true
anyhow.workspace = true
itertools.workspace = true
smallvec.workspace = true
smol.workspace = true
log.workspace = true
futures.workspace = true
serde.workspace = true
serde_json.workspace = true

View File

@@ -1,464 +0,0 @@
use std::cell::Cell;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;
use anyhow::anyhow;
use common::shorten_pubkey;
use encryption::Encryption;
use futures::FutureExt;
use gpui::prelude::FluentBuilder;
use gpui::{
div, px, App, AppContext, Context, Entity, IntoElement, ParentElement, Render, SharedString,
Styled, Subscription, Window,
};
use smallvec::{smallvec, SmallVec};
use state::Announcement;
use theme::ActiveTheme;
use ui::button::{Button, ButtonVariants};
use ui::notification::Notification;
use ui::{h_flex, v_flex, ContextModal, Disableable, Icon, IconName, Sizable, StyledExt};
pub fn init(window: &mut Window, cx: &mut App) -> Entity<EncryptionPanel> {
cx.new(|cx| EncryptionPanel::new(window, cx))
}
#[derive(Debug)]
pub struct EncryptionPanel {
/// Whether the panel is currently requesting encryption.
requesting: bool,
/// Whether the panel is currently creating encryption.
creating: bool,
/// Whether the panel is currently showing an error.
error: Entity<Option<SharedString>>,
/// Event subscriptions
_subscriptions: SmallVec<[Subscription; 1]>,
}
impl EncryptionPanel {
pub fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let error = cx.new(|_| None);
let encryption = Encryption::global(cx);
let requests = encryption.read(cx).requests();
let mut subscriptions = smallvec![];
subscriptions.push(
// Observe encryption request
cx.observe_in(&requests, window, |this, state, window, cx| {
for req in state.read(cx).clone().into_iter() {
this.ask_for_approval(req, window, cx);
}
}),
);
Self {
requesting: false,
creating: false,
error,
_subscriptions: subscriptions,
}
}
fn set_requesting(&mut self, status: bool, cx: &mut Context<Self>) {
self.requesting = status;
cx.notify();
}
fn set_creating(&mut self, status: bool, cx: &mut Context<Self>) {
self.creating = status;
cx.notify();
}
fn set_error(&mut self, error: impl Into<SharedString>, cx: &mut Context<Self>) {
self.error.update(cx, |this, cx| {
*this = Some(error.into());
cx.notify();
});
}
fn request(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let encryption = Encryption::global(cx);
let send_request = encryption.read(cx).send_request(cx);
// Ensure the user has not sent multiple requests
if self.requesting {
return;
}
self.set_requesting(true, cx);
cx.spawn_in(window, async move |this, cx| {
match send_request.await {
Ok(Some(keys)) => {
this.update(cx, |this, cx| {
this.set_requesting(false, cx);
// Set the encryption key
encryption.update(cx, |this, cx| {
this.set_encryption(Arc::new(keys), cx);
});
})
.expect("Entity has been released");
}
Ok(None) => {
this.update_in(cx, |this, window, cx| {
this.wait_for_approval(window, cx);
})
.expect("Entity has been released");
}
Err(e) => {
this.update(cx, |this, cx| {
this.set_requesting(false, cx);
this.set_error(e.to_string(), cx);
})
.expect("Entity has been released");
}
}
})
.detach();
}
fn new_encryption(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let encryption = Encryption::global(cx);
let reset = encryption.read(cx).new_encryption(cx);
// Ensure the user has not sent multiple requests
if self.requesting {
return;
}
self.set_creating(true, cx);
cx.spawn_in(window, async move |this, cx| {
match reset.await {
Ok(keys) => {
this.update(cx, |this, cx| {
this.set_creating(false, cx);
// Set the encryption key
encryption.update(cx, |this, cx| {
this.set_encryption(Arc::new(keys), cx);
this.listen_request(cx);
});
})
.expect("Entity has been released");
}
Err(e) => {
this.update(cx, |this, cx| {
this.set_creating(false, cx);
this.set_error(e.to_string(), cx);
})
.expect("Entity has been released");
}
}
})
.detach();
}
fn wait_for_approval(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let encryption = Encryption::global(cx);
let wait_for_approval = encryption.read(cx).wait_for_approval(cx);
cx.spawn_in(window, async move |this, cx| {
let timeout = cx.background_executor().timer(Duration::from_secs(30));
let result = futures::select! {
result = wait_for_approval.fuse() => {
// Ok(keys)
result
},
_ = timeout.fuse() => {
Err(anyhow!("Timeout"))
}
};
this.update(cx, |this, cx| {
match result {
Ok(keys) => {
this.set_requesting(false, cx);
// Set the encryption key
encryption.update(cx, |this, cx| {
this.set_encryption(Arc::new(keys), cx);
});
}
Err(e) => {
this.set_requesting(false, cx);
this.set_error(e.to_string(), cx);
}
};
})
.expect("Entity has been released");
})
.detach();
}
fn ask_for_approval(&mut self, req: Announcement, window: &mut Window, cx: &mut Context<Self>) {
let client_name = req.client_name();
let target = req.public_key();
let id = SharedString::from(req.id().to_hex());
let loading = Rc::new(Cell::new(false));
let note = Notification::new()
.custom_id(id.clone())
.autohide(false)
.icon(IconName::Encryption)
.title(SharedString::from("Encryption Key Request"))
.content(move |_window, cx| {
v_flex()
.gap_2()
.text_sm()
.child(SharedString::from(
"You've requested for the Encryption Key from:",
))
.child(
v_flex()
.h_12()
.items_center()
.justify_center()
.px_2()
.rounded(cx.theme().radius)
.bg(cx.theme().warning_background)
.text_color(cx.theme().warning_foreground)
.child(client_name.clone()),
)
.child(
h_flex()
.h_7()
.w_full()
.px_2()
.rounded(cx.theme().radius)
.bg(cx.theme().elevated_surface_background)
.child(SharedString::from(target.to_hex())),
)
.into_any_element()
})
.action(move |_window, _cx| {
Button::new("approve")
.label("Approve")
.small()
.primary()
.loading(loading.get())
.disabled(loading.get())
.on_click({
let loading = Rc::clone(&loading);
let id = id.clone();
move |_ev, window, cx| {
// Set loading state to true
loading.set(true);
let encryption = Encryption::global(cx);
let send_response = encryption.read(cx).send_response(target, cx);
let id = id.clone();
window
.spawn(cx, async move |cx| {
let result = send_response.await;
cx.update(|window, cx| {
match result {
Ok(_) => {
window.clear_notification_by_id(id, cx);
}
Err(e) => {
window.push_notification(e.to_string(), cx);
}
};
})
.expect("Entity has been released");
})
.detach();
}
})
});
// Push the notification to the current window
window.push_notification(note, cx);
// Focus the window if it's not active
if !window.is_window_hovered() {
window.activate_window();
}
}
}
impl Render for EncryptionPanel {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
const NOTICE: &str = "Found an Encryption Announcement";
const SUGGEST: &str = "Please request the Encryption Key to continue using.";
const DESCRIPTION: &str = "Encryption Key is used to replace the User's Identity in encryption and decryption messages. Coop will automatically fallback to User's Identity if needed.";
const WARNING: &str = "Encryption Key is still in the alpha stage. Please be cautious.";
let encryption = Encryption::global(cx);
let announcement = encryption.read(cx).announcement();
let has_encryption = encryption.read(cx).has_encryption(cx);
v_flex()
.p_2()
.max_w(px(340.))
.w(px(340.))
.text_sm()
.when_some(announcement.as_ref(), |this, announcement| {
let pubkey = shorten_pubkey(announcement.public_key(), 16);
let client_name = announcement.client_name();
this.child(
v_flex()
.gap_2()
.when(has_encryption, |this| {
this.child(
h_flex()
.gap_1p5()
.text_sm()
.font_semibold()
.child(
Icon::new(IconName::CheckCircle)
.text_color(cx.theme().element_foreground)
.small(),
)
.child(SharedString::from("Encryption Key has been set")),
)
})
.when(!has_encryption, |this| {
this.child(div().font_semibold().child(SharedString::from(NOTICE)))
})
.child(
v_flex()
.gap_1()
.child(
div()
.text_xs()
.font_semibold()
.text_color(cx.theme().text_muted)
.child(SharedString::from("Client Name:")),
)
.child(
h_flex()
.h_12()
.items_center()
.justify_center()
.rounded(cx.theme().radius)
.bg(cx.theme().elevated_surface_background)
.child(client_name.clone()),
),
)
.child(
v_flex()
.gap_1()
.child(
div()
.text_xs()
.font_semibold()
.text_color(cx.theme().text_muted)
.child(SharedString::from("Client Public Key:")),
)
.child(
h_flex()
.h_7()
.w_full()
.px_2()
.rounded(cx.theme().radius)
.bg(cx.theme().elevated_surface_background)
.child(SharedString::from(pubkey)),
),
)
.when(!has_encryption, |this| {
this.child(
v_flex()
.gap_2()
.child(
div()
.text_xs()
.text_color(cx.theme().text_muted)
.child(SharedString::from(SUGGEST)),
)
.child(
h_flex()
.mt_2()
.gap_1()
.when(!self.requesting, |this| {
this.child(
Button::new("reset")
.label("Reset")
.flex_1()
.small()
.ghost_alt()
.loading(self.creating)
.disabled(self.creating)
.on_click(cx.listener(
move |this, _ev, window, cx| {
this.new_encryption(window, cx);
},
)),
)
})
.when(!self.creating, |this| {
this.child(
Button::new("request")
.label({
if self.requesting {
"Wait for approval"
} else {
"Request"
}
})
.flex_1()
.small()
.primary()
.loading(self.requesting)
.disabled(self.requesting)
.on_click(cx.listener(
move |this, _ev, window, cx| {
this.request(window, cx);
},
)),
)
}),
),
)
}),
)
})
.when_none(&announcement, |this| {
this.child(
v_flex()
.gap_2()
.child(
div()
.font_semibold()
.child(SharedString::from("Set up Encryption Key")),
)
.child(SharedString::from(DESCRIPTION))
.child(
div()
.text_xs()
.text_color(cx.theme().warning_foreground)
.child(SharedString::from(WARNING)),
)
.child(
Button::new("create")
.label("Setup")
.flex_1()
.small()
.primary()
.loading(self.creating)
.disabled(self.creating)
.on_click(cx.listener(move |this, _ev, window, cx| {
this.new_encryption(window, cx);
})),
),
)
})
.when_some(self.error.read(cx).as_ref(), |this, error| {
this.child(
div()
.text_xs()
.text_center()
.text_color(cx.theme().danger_foreground)
.child(error.clone()),
)
})
}
}

View File

@@ -96,7 +96,7 @@ impl KeyBackend for KeyringProvider {
url: &'a str, url: &'a str,
cx: &'a AsyncApp, cx: &'a AsyncApp,
) -> Pin<Box<dyn Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>> { ) -> Pin<Box<dyn Future<Output = Result<Option<(String, Vec<u8>)>>> + 'a>> {
async move { cx.update(|cx| cx.read_credentials(url))?.await }.boxed_local() async move { cx.update(|cx| cx.read_credentials(url)).await }.boxed_local()
} }
fn write_credentials<'a>( fn write_credentials<'a>(
@@ -107,7 +107,7 @@ impl KeyBackend for KeyringProvider {
cx: &'a AsyncApp, cx: &'a AsyncApp,
) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> { ) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> {
async move { async move {
cx.update(move |cx| cx.write_credentials(url, username, password))? cx.update(move |cx| cx.write_credentials(url, username, password))
.await .await
} }
.boxed_local() .boxed_local()
@@ -118,7 +118,7 @@ impl KeyBackend for KeyringProvider {
url: &'a str, url: &'a str,
cx: &'a AsyncApp, cx: &'a AsyncApp,
) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> { ) -> Pin<Box<dyn Future<Output = Result<()>> + 'a>> {
async move { cx.update(move |cx| cx.delete_credentials(url))?.await }.boxed_local() async move { cx.update(move |cx| cx.delete_credentials(url)).await }.boxed_local()
} }
} }

View File

@@ -11,7 +11,7 @@ state = { path = "../state" }
gpui.workspace = true gpui.workspace = true
nostr-sdk.workspace = true nostr-sdk.workspace = true
anyhow.workspace = true anyhow.workspace = true
itertools.workspace = true
smallvec.workspace = true smallvec.workspace = true
smol.workspace = true smol.workspace = true
flume.workspace = true
log.workspace = true log.workspace = true

View File

@@ -1,9 +1,17 @@
use std::cell::RefCell;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::rc::Rc;
use std::time::Duration;
use anyhow::{anyhow, Error};
use common::{EventUtils, BOOTSTRAP_RELAYS};
use gpui::{App, AppContext, Context, Entity, Global, Task}; use gpui::{App, AppContext, Context, Entity, Global, Task};
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
pub use person::*;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::NostrRegistry; use state::{Announcement, NostrRegistry, TIMEOUT};
mod person;
pub fn init(cx: &mut App) { pub fn init(cx: &mut App) {
PersonRegistry::set_global(cx.new(PersonRegistry::new), cx); PersonRegistry::set_global(cx.new(PersonRegistry::new), cx);
@@ -13,39 +21,123 @@ struct GlobalPersonRegistry(Entity<PersonRegistry>);
impl Global for GlobalPersonRegistry {} impl Global for GlobalPersonRegistry {}
#[derive(Debug, Clone)]
enum Dispatch {
Person(Box<Person>),
Announcement(Box<Event>),
}
/// Person Registry /// Person Registry
#[derive(Debug)] #[derive(Debug)]
pub struct PersonRegistry { pub struct PersonRegistry {
/// Collection of all persons (user profiles) /// Collection of all persons (user profiles)
pub persons: HashMap<PublicKey, Entity<Profile>>, persons: HashMap<PublicKey, Entity<Person>>,
/// Set of public keys that have been seen
seen: Rc<RefCell<HashSet<PublicKey>>>,
/// Sender for requesting metadata
sender: flume::Sender<PublicKey>,
/// Tasks for asynchronous operations /// Tasks for asynchronous operations
_tasks: SmallVec<[Task<()>; 2]>, _tasks: SmallVec<[Task<()>; 4]>,
} }
impl PersonRegistry { impl PersonRegistry {
/// Retrieve the global person registry state /// Retrieve the global person registry
pub fn global(cx: &App) -> Entity<Self> { pub fn global(cx: &App) -> Entity<Self> {
cx.global::<GlobalPersonRegistry>().0.clone() cx.global::<GlobalPersonRegistry>().0.clone()
} }
/// Set the global person registry instance /// Set the global person registry instance
pub(crate) fn set_global(state: Entity<Self>, cx: &mut App) { fn set_global(state: Entity<Self>, cx: &mut App) {
cx.set_global(GlobalPersonRegistry(state)); cx.set_global(GlobalPersonRegistry(state));
} }
/// Create a new person registry instance /// Create a new person registry instance
pub(crate) fn new(cx: &mut Context<Self>) -> Self { fn new(cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
// Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<Dispatch>(100);
let (mta_tx, mta_rx) = flume::bounded::<PublicKey>(100);
let mut tasks = smallvec![]; let mut tasks = smallvec![];
tasks.push( tasks.push(
// Handle notifications // Handle nostr notifications
cx.spawn({ cx.background_spawn({
let client = nostr.read(cx).client(); let client = client.clone();
async move |this, cx| { async move {
Self::handle_notifications(&client, &tx).await;
}
}),
);
tasks.push(
// Handle metadata requests
cx.background_spawn({
let client = client.clone();
async move {
Self::handle_requests(&client, &mta_rx).await;
}
}),
);
tasks.push(
// Update GPUI state
cx.spawn(async move |this, cx| {
while let Ok(event) = rx.recv_async().await {
this.update(cx, |this, cx| {
match event {
Dispatch::Person(person) => {
this.insert(*person, cx);
}
Dispatch::Announcement(event) => {
this.set_announcement(&event, cx);
}
};
})
.ok();
}
}),
);
tasks.push(
// Load all user profiles from the database
cx.spawn(async move |this, cx| {
let result = cx
.background_executor()
.await_on_background(async move { Self::load_persons(&client).await })
.await;
match result {
Ok(persons) => {
this.update(cx, |this, cx| {
this.bulk_inserts(persons, cx);
})
.ok();
}
Err(e) => {
log::error!("Failed to load all persons from the database: {e}");
}
};
}),
);
Self {
persons: HashMap::new(),
seen: Rc::new(RefCell::new(HashSet::new())),
sender: mta_tx,
_tasks: tasks,
}
}
/// Handle nostr notifications
async fn handle_notifications(client: &Client, tx: &flume::Sender<Dispatch>) {
let mut notifications = client.notifications(); let mut notifications = client.notifications();
let mut processed_events = HashSet::new(); let mut processed_events = HashSet::new();
@@ -61,111 +153,166 @@ impl PersonRegistry {
continue; continue;
} }
if event.kind != Kind::Metadata { match event.kind {
// Skip if the event is not a metadata event Kind::Metadata => {
continue;
};
let metadata = Metadata::from_json(&event.content).unwrap_or_default(); let metadata = Metadata::from_json(&event.content).unwrap_or_default();
let profile = Profile::new(event.pubkey, metadata); let person = Person::new(event.pubkey, metadata);
let val = Box::new(person);
this.update(cx, |this, cx| { // Send
this.insert_or_update_person(profile, cx); tx.send_async(Dispatch::Person(val)).await.ok();
}) }
.expect("Entity has been released") Kind::Custom(10044) => {
let val = Box::new(event.into_owned());
// Send
tx.send_async(Dispatch::Announcement(val)).await.ok();
}
Kind::ContactList => {
let public_keys = event.extract_public_keys();
// Get metadata for all public keys
Self::get_metadata(client, public_keys).await.ok();
}
_ => {}
}
} }
} }
} }
}),
);
tasks.push( /// Handle request for metadata
// Load all user profiles from the database async fn handle_requests(client: &Client, rx: &flume::Receiver<PublicKey>) {
cx.spawn(async move |this, cx| { let mut batch: HashSet<PublicKey> = HashSet::new();
let result = cx
.background_spawn(async move { Self::load_persons(&client).await })
.await;
match result { loop {
Ok(profiles) => { match flume::Selector::new()
this.update(cx, |this, cx| { .recv(rx, |result| result.ok())
this.bulk_insert_persons(profiles, cx); .wait_timeout(Duration::from_secs(2))
}) {
Ok(Some(public_key)) => {
log::info!("Received public key: {}", public_key);
batch.insert(public_key);
// Process the batch if it's full
if batch.len() >= 20 {
Self::get_metadata(client, std::mem::take(&mut batch))
.await
.ok(); .ok();
} }
Err(e) => {
log::error!("Failed to load persons: {e}");
} }
}; _ => {
}), Self::get_metadata(client, std::mem::take(&mut batch))
); .await
.ok();
}
}
}
}
Self { /// Get metadata for all public keys in a event
persons: HashMap::new(), async fn get_metadata<I>(client: &Client, public_keys: I) -> Result<(), Error>
_tasks: tasks, where
I: IntoIterator<Item = PublicKey>,
{
let authors: Vec<PublicKey> = public_keys.into_iter().collect();
let limit = authors.len();
if authors.is_empty() {
return Err(anyhow!("You need at least one public key"));
} }
// Construct the subscription option
let opts = SubscribeAutoCloseOptions::default()
.exit_policy(ReqExitPolicy::ExitOnEOSE)
.timeout(Some(Duration::from_secs(TIMEOUT)));
// Construct the filter for metadata
let filter = Filter::new()
.kind(Kind::Metadata)
.authors(authors)
.limit(limit);
client
.subscribe_to(BOOTSTRAP_RELAYS, filter, Some(opts))
.await?;
Ok(())
} }
/// Load all user profiles from the database /// Load all user profiles from the database
async fn load_persons(client: &Client) -> Result<Vec<Profile>, Error> { async fn load_persons(client: &Client) -> Result<Vec<Person>, Error> {
let filter = Filter::new().kind(Kind::Metadata).limit(200); let filter = Filter::new().kind(Kind::Metadata).limit(200);
let events = client.database().query(filter).await?; let events = client.database().query(filter).await?;
let mut profiles = vec![]; let mut persons = vec![];
for event in events.into_iter() { for event in events.into_iter() {
let metadata = Metadata::from_json(event.content).unwrap_or_default(); let metadata = Metadata::from_json(event.content).unwrap_or_default();
let profile = Profile::new(event.pubkey, metadata); let person = Person::new(event.pubkey, metadata);
profiles.push(profile); persons.push(person);
} }
Ok(profiles) Ok(persons)
}
/// Set profile encryption keys announcement
fn set_announcement(&mut self, event: &Event, cx: &mut App) {
if let Some(person) = self.persons.get(&event.pubkey) {
let announcement = Announcement::from(event);
person.update(cx, |person, cx| {
person.set_announcement(announcement);
cx.notify();
});
}
} }
/// Insert batch of persons /// Insert batch of persons
fn bulk_insert_persons(&mut self, profiles: Vec<Profile>, cx: &mut Context<Self>) { fn bulk_inserts(&mut self, persons: Vec<Person>, cx: &mut Context<Self>) {
for profile in profiles.into_iter() { for person in persons.into_iter() {
self.persons self.persons.insert(person.public_key(), cx.new(|_| person));
.insert(profile.public_key(), cx.new(|_| profile));
} }
cx.notify(); cx.notify();
} }
/// Insert or update a person /// Insert or update a person
pub fn insert_or_update_person(&mut self, profile: Profile, cx: &mut App) { pub fn insert(&mut self, person: Person, cx: &mut App) {
let public_key = profile.public_key(); let public_key = person.public_key();
match self.persons.get(&public_key) { match self.persons.get(&public_key) {
Some(person) => { Some(this) => {
person.update(cx, |this, cx| { this.update(cx, |this, cx| {
*this = profile; *this = person;
cx.notify(); cx.notify();
}); });
} }
None => { None => {
self.persons.insert(public_key, cx.new(|_| profile)); self.persons.insert(public_key, cx.new(|_| person));
} }
} }
} }
/// Get single person /// Get single person by public key
pub fn get_person(&self, public_key: &PublicKey, cx: &App) -> Profile { pub fn get(&self, public_key: &PublicKey, cx: &App) -> Person {
self.persons if let Some(person) = self.persons.get(public_key) {
.get(public_key) return person.read(cx).clone();
.map(|e| e.read(cx))
.cloned()
.unwrap_or(Profile::new(public_key.to_owned(), Metadata::default()))
} }
/// Get group of persons let public_key = *public_key;
pub fn get_group_person(&self, public_keys: &[PublicKey], cx: &App) -> Vec<Profile> { let mut seen = self.seen.borrow_mut();
let mut profiles = vec![];
for public_key in public_keys.iter() { if seen.insert(public_key) {
let profile = self.get_person(public_key, cx); let sender = self.sender.clone();
profiles.push(profile);
// Spawn background task to request metadata
cx.background_spawn(async move {
if let Err(e) = sender.send_async(public_key).await {
log::warn!("Failed to send public key for metadata request: {}", e);
}
})
.detach();
} }
profiles // Return a temporary profile with default metadata
Person::new(public_key, Metadata::default())
} }
} }

122
crates/person/src/person.rs Normal file
View File

@@ -0,0 +1,122 @@
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
use gpui::SharedString;
use nostr_sdk::prelude::*;
use state::Announcement;
/// Person
#[derive(Debug, Clone)]
pub struct Person {
/// Public Key
public_key: PublicKey,
/// Metadata (profile)
metadata: Metadata,
/// Dekey (NIP-4e) announcement
announcement: Option<Announcement>,
}
impl PartialEq for Person {
fn eq(&self, other: &Self) -> bool {
self.public_key == other.public_key
}
}
impl Eq for Person {}
impl PartialOrd for Person {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for Person {
fn cmp(&self, other: &Self) -> Ordering {
self.name().cmp(&other.name())
}
}
impl Hash for Person {
fn hash<H: Hasher>(&self, state: &mut H) {
self.public_key.hash(state)
}
}
impl From<PublicKey> for Person {
fn from(public_key: PublicKey) -> Self {
Self::new(public_key, Metadata::default())
}
}
impl Person {
pub fn new(public_key: PublicKey, metadata: Metadata) -> Self {
Self {
public_key,
metadata,
announcement: None,
}
}
/// Get profile public key
pub fn public_key(&self) -> PublicKey {
self.public_key
}
/// Get profile metadata
pub fn metadata(&self) -> Metadata {
self.metadata.clone()
}
/// Get profile encryption keys announcement
pub fn announcement(&self) -> Option<Announcement> {
self.announcement.clone()
}
/// Set profile encryption keys announcement
pub fn set_announcement(&mut self, announcement: Announcement) {
self.announcement = Some(announcement);
log::info!("Updated announcement for: {}", self.public_key());
}
/// Get profile avatar
pub fn avatar(&self) -> SharedString {
self.metadata()
.picture
.as_ref()
.filter(|picture| !picture.is_empty())
.map(|picture| picture.into())
.unwrap_or_else(|| "brand/avatar.png".into())
}
/// Get profile name
pub fn name(&self) -> SharedString {
if let Some(display_name) = self.metadata().display_name.as_ref() {
if !display_name.is_empty() {
return SharedString::from(display_name);
}
}
if let Some(name) = self.metadata().name.as_ref() {
if !name.is_empty() {
return SharedString::from(name);
}
}
SharedString::from(shorten_pubkey(self.public_key(), 4))
}
}
/// Shorten a [`PublicKey`] to a string with the first and last `len` characters
///
/// Ex. `00000000:00000002`
pub fn shorten_pubkey(public_key: PublicKey, len: usize) -> String {
let Ok(pubkey) = public_key.to_bech32();
format!(
"{}:{}",
&pubkey[0..(len + 1)],
&pubkey[pubkey.len() - len..]
)
}

View File

@@ -17,4 +17,5 @@ nostr-sdk.workspace = true
anyhow.workspace = true anyhow.workspace = true
smallvec.workspace = true smallvec.workspace = true
smol.workspace = true smol.workspace = true
flume.workspace = true
log.workspace = true log.workspace = true

View File

@@ -1,6 +1,6 @@
use std::borrow::Cow; use std::borrow::Cow;
use std::cell::Cell; use std::cell::Cell;
use std::collections::{HashMap, HashSet}; use std::collections::HashSet;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::rc::Rc; use std::rc::Rc;
@@ -12,7 +12,7 @@ use gpui::{
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use settings::AppSettings; use settings::AppSettings;
use smallvec::{smallvec, SmallVec}; use smallvec::{smallvec, SmallVec};
use state::NostrRegistry; use state::{tracker, NostrRegistry};
use theme::ActiveTheme; use theme::ActiveTheme;
use ui::button::{Button, ButtonVariants}; use ui::button::{Button, ButtonVariants};
use ui::notification::Notification; use ui::notification::Notification;
@@ -25,10 +25,7 @@ pub fn init(window: &mut Window, cx: &mut App) {
RelayAuth::set_global(cx.new(|cx| RelayAuth::new(window, cx)), cx); RelayAuth::set_global(cx.new(|cx| RelayAuth::new(window, cx)), cx);
} }
struct GlobalRelayAuth(Entity<RelayAuth>); /// Authentication request
impl Global for GlobalRelayAuth {}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct AuthRequest { pub struct AuthRequest {
pub url: RelayUrl, pub url: RelayUrl,
@@ -50,6 +47,11 @@ impl AuthRequest {
} }
} }
struct GlobalRelayAuth(Entity<RelayAuth>);
impl Global for GlobalRelayAuth {}
// Relay authentication
#[derive(Debug)] #[derive(Debug)]
pub struct RelayAuth { pub struct RelayAuth {
/// Entity for managing auth requests /// Entity for managing auth requests
@@ -77,8 +79,13 @@ impl RelayAuth {
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self { fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
// Get the current entity
let entity = cx.entity(); let entity = cx.entity();
// Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<AuthRequest>(100);
let mut subscriptions = smallvec![]; let mut subscriptions = smallvec![];
let mut tasks = smallvec![]; let mut tasks = smallvec![];
@@ -93,36 +100,28 @@ impl RelayAuth {
if auto_auth && is_authenticated { if auto_auth && is_authenticated {
// Automatically authenticate if the relay is authenticated before // Automatically authenticate if the relay is authenticated before
this.response(req.to_owned(), window, cx); this.response(req, window, cx);
} else { } else {
// Otherwise open the auth request popup // Otherwise open the auth request popup
this.ask_for_approval(req.to_owned(), window, cx); this.ask_for_approval(req, window, cx);
} }
} }
}), }),
); );
tasks.push( tasks.push(
// Handle notifications // Handle nostr notifications
cx.background_spawn(async move { Self::handle_notifications(&client, &tx).await }),
);
tasks.push(
// Update GPUI states
cx.spawn(async move |this, cx| { cx.spawn(async move |this, cx| {
let mut notifications = client.notifications(); while let Ok(request) = rx.recv_async().await {
let mut challenges: HashSet<Cow<'_, str>> = HashSet::new();
while let Ok(notification) = notifications.recv().await {
let RelayPoolNotification::Message { message, relay_url } = notification else {
// Skip if the notification is not a message
continue;
};
if let RelayMessage::Auth { challenge } = message {
if challenges.insert(challenge.clone()) {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.requests.insert(AuthRequest::new(challenge, relay_url)); this.add_request(request, cx);
cx.notify();
}) })
.expect("Entity has been released"); .ok();
};
}
} }
}), }),
); );
@@ -134,6 +133,31 @@ impl RelayAuth {
} }
} }
// Handle nostr notifications
async fn handle_notifications(client: &Client, tx: &flume::Sender<AuthRequest>) {
let mut notifications = client.notifications();
while let Ok(notification) = notifications.recv().await {
if let RelayPoolNotification::Message {
message: RelayMessage::Auth { challenge },
relay_url,
} = notification
{
let request = AuthRequest::new(challenge, relay_url);
if let Err(e) = tx.send_async(request).await {
log::error!("Failed to send auth request: {}", e);
}
}
}
}
/// Add a new authentication request.
fn add_request(&mut self, request: AuthRequest, cx: &mut Context<Self>) {
self.requests.insert(request);
cx.notify();
}
/// Get the number of pending requests. /// Get the number of pending requests.
pub fn pending_requests(&self, _cx: &App) -> usize { pub fn pending_requests(&self, _cx: &App) -> usize {
self.requests.len() self.requests.len()
@@ -152,7 +176,6 @@ impl RelayAuth {
let nostr = NostrRegistry::global(cx); let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client(); let client = nostr.read(cx).client();
let tracker = nostr.read(cx).tracker();
let challenge = req.challenge.to_owned(); let challenge = req.challenge.to_owned();
let url = req.url.to_owned(); let url = req.url.to_owned();
@@ -190,30 +213,14 @@ impl RelayAuth {
// Re-subscribe to previous subscription // Re-subscribe to previous subscription
relay.resubscribe().await?; relay.resubscribe().await?;
// Get all failed events that need to be resent // Get all pending events that need to be resent
let mut tracker = tracker.write().await; let mut tracker = tracker().write().await;
let ids: Vec<EventId> = tracker.pending_resend(relay_url);
let ids: Vec<EventId> = tracker
.resend_queue
.iter()
.filter(|(_, url)| relay_url == *url)
.map(|(id, _)| *id)
.collect();
for id in ids.into_iter() { for id in ids.into_iter() {
if let Some(relay_url) = tracker.resend_queue.remove(&id) {
if let Some(event) = client.database().event_by_id(&id).await? { if let Some(event) = client.database().event_by_id(&id).await? {
let event_id = relay.send_event(&event).await?; let event_id = relay.send_event(&event).await?;
tracker.sent(event_id);
let output = Output {
val: event_id,
failed: HashMap::new(),
success: HashSet::from([relay_url]),
};
tracker.sent_ids.insert(event_id);
tracker.resent_ids.push(output);
}
} }
} }
@@ -306,12 +313,13 @@ impl RelayAuth {
move |_ev, window, cx| { move |_ev, window, cx| {
// Set loading state to true // Set loading state to true
loading.set(true); loading.set(true);
// Process to approve the request // Process to approve the request
entity entity
.update(cx, |this, cx| { .update(cx, |this, cx| {
this.response(req.clone(), window, cx); this.response(req.clone(), window, cx);
}) })
.expect("Entity has been released"); .ok();
} }
}) })
}); });
@@ -319,9 +327,7 @@ impl RelayAuth {
// Push the notification to the current window // Push the notification to the current window
window.push_notification(note, cx); window.push_notification(note, cx);
// Focus the window if it's not active // Bring the window to the front
if !window.is_window_hovered() { cx.activate(true);
window.activate_window();
}
} }
} }

View File

@@ -12,10 +12,8 @@ nostr-lmdb.workspace = true
gpui.workspace = true gpui.workspace = true
smol.workspace = true smol.workspace = true
smallvec.workspace = true flume.workspace = true
log.workspace = true log.workspace = true
anyhow.workspace = true anyhow.workspace = true
serde.workspace = true
serde_json.workspace = true
rustls = "0.23.23" rustls = "0.23"

View File

@@ -0,0 +1,62 @@
use gpui::SharedString;
use nostr_sdk::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub enum DeviceState {
#[default]
Initial,
Requesting,
Set,
}
/// Announcement
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Announcement {
/// The public key of the device that created this announcement.
public_key: PublicKey,
/// The name of the device that created this announcement.
client_name: Option<String>,
}
impl From<&Event> for Announcement {
fn from(val: &Event) -> Self {
let public_key = val
.tags
.iter()
.find(|tag| tag.kind().as_str() == "n" || tag.kind().as_str() == "P")
.and_then(|tag| tag.content())
.and_then(|c| PublicKey::parse(c).ok())
.unwrap_or(val.pubkey);
let client_name = val
.tags
.find(TagKind::Client)
.and_then(|tag| tag.content())
.map(|c| c.to_string());
Self::new(public_key, client_name)
}
}
impl Announcement {
pub fn new(public_key: PublicKey, client_name: Option<String>) -> Self {
Self {
public_key,
client_name,
}
}
/// Returns the public key of the device that created this announcement.
pub fn public_key(&self) -> PublicKey {
self.public_key
}
/// Returns the client name of the device that created this announcement.
pub fn client_name(&self) -> SharedString {
self.client_name
.as_ref()
.map(SharedString::from)
.unwrap_or(SharedString::from("Unknown"))
}
}

46
crates/state/src/event.rs Normal file
View File

@@ -0,0 +1,46 @@
use std::collections::HashSet;
use std::sync::{Arc, OnceLock};
use nostr_sdk::prelude::*;
use smol::lock::RwLock;
static TRACKER: OnceLock<Arc<RwLock<EventTracker>>> = OnceLock::new();
pub fn tracker() -> &'static Arc<RwLock<EventTracker>> {
TRACKER.get_or_init(|| Arc::new(RwLock::new(EventTracker::default())))
}
/// Event tracker
#[derive(Debug, Clone, Default)]
pub struct EventTracker {
/// Tracking events sent by Coop in the current session
sent_ids: HashSet<EventId>,
/// Events that need to be resent later
pending_resend: HashSet<(EventId, RelayUrl)>,
}
impl EventTracker {
/// Check if an event was sent by Coop in the current session.
pub fn is_sent_by_coop(&self, id: &EventId) -> bool {
self.sent_ids.contains(id)
}
/// Mark an event as sent by Coop.
pub fn sent(&mut self, id: EventId) {
self.sent_ids.insert(id);
}
/// Get all events that need to be resent later for a specific relay.
pub fn pending_resend(&mut self, relay: &RelayUrl) -> Vec<EventId> {
self.pending_resend
.extract_if(|(_id, url)| url == relay)
.map(|(id, _url)| id)
.collect()
}
/// Add an event (id and relay url) to the pending resend set.
pub fn add_to_pending(&mut self, id: EventId, url: RelayUrl) {
self.pending_resend.insert((id, url));
}
}

View File

@@ -1,80 +1,19 @@
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use gpui::SharedString;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use crate::NostrRegistry; /// Gossip
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Announcement {
id: EventId,
public_key: PublicKey,
client_name: Option<String>,
}
impl Announcement {
pub fn new(id: EventId, client_name: Option<String>, public_key: PublicKey) -> Self {
Self {
id,
client_name,
public_key,
}
}
pub fn id(&self) -> EventId {
self.id
}
pub fn public_key(&self) -> PublicKey {
self.public_key
}
pub fn client_name(&self) -> SharedString {
self.client_name
.as_ref()
.map(SharedString::from)
.unwrap_or(SharedString::from("Unknown"))
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub struct Response {
payload: String,
public_key: PublicKey,
}
impl Response {
pub fn new(payload: String, public_key: PublicKey) -> Self {
Self {
payload,
public_key,
}
}
pub fn public_key(&self) -> PublicKey {
self.public_key
}
pub fn payload(&self) -> &str {
self.payload.as_str()
}
}
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Gossip { pub struct Gossip {
/// Gossip relays for each public key /// Gossip relays for each public key
relays: HashMap<PublicKey, HashSet<(RelayUrl, Option<RelayMetadata>)>>, relays: HashMap<PublicKey, HashSet<(RelayUrl, Option<RelayMetadata>)>>,
/// Messaging relays for each public key /// Messaging relays for each public key
messaging_relays: HashMap<PublicKey, HashSet<RelayUrl>>, messaging_relays: HashMap<PublicKey, HashSet<RelayUrl>>,
/// Encryption announcement for each public key
announcements: HashMap<PublicKey, Option<Announcement>>,
} }
impl Gossip { impl Gossip {
/// Get inbox relays for a public key /// Get read relays for a given public key
pub fn inbox_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> { pub fn read_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.relays self.relays
.get(public_key) .get(public_key)
.map(|relays| { .map(|relays| {
@@ -92,8 +31,8 @@ impl Gossip {
.unwrap_or_default() .unwrap_or_default()
} }
/// Get outbox relays for a public key /// Get write relays for a given public key
pub fn outbox_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> { pub fn write_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.relays self.relays
.get(public_key) .get(public_key)
.map(|relays| { .map(|relays| {
@@ -130,9 +69,11 @@ impl Gossip {
}) })
.take(3), .take(3),
); );
log::info!("Updating gossip relays for: {}", event.pubkey);
} }
/// Get messaging relays for a public key /// Get messaging relays for a given public key
pub fn messaging_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> { pub fn messaging_relays(&self, public_key: &PublicKey) -> Vec<RelayUrl> {
self.messaging_relays self.messaging_relays
.get(public_key) .get(public_key)
@@ -160,30 +101,7 @@ impl Gossip {
}) })
.take(3), .take(3),
); );
}
/// Ensure connections for the given relay list log::info!("Updating messaging relays for: {}", event.pubkey);
pub async fn ensure_connections(&self, client: &Client, urls: &[RelayUrl]) {
for url in urls {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
}
/// Get announcement for a public key
pub fn announcement(&self, public_key: &PublicKey) -> Option<Announcement> {
self.announcements
.get(public_key)
.cloned()
.unwrap_or_default()
}
/// Insert announcement for a public key
pub fn insert_announcement(&mut self, event: &Event) {
let announcement = NostrRegistry::extract_announcement(event).ok();
self.announcements
.entry(event.pubkey)
.or_insert(announcement);
} }
} }

View File

@@ -0,0 +1,86 @@
use nostr_sdk::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RelayState {
#[default]
Initial,
NotSet,
Set,
}
impl RelayState {
pub fn is_initial(&self) -> bool {
matches!(self, RelayState::Initial)
}
}
/// Identity
#[derive(Debug, Clone, Default)]
pub struct Identity {
/// The public key of the account
pub public_key: Option<PublicKey>,
/// Status of the current user NIP-65 relays
relay_list: RelayState,
/// Status of the current user NIP-17 relays
messaging_relays: RelayState,
}
impl AsRef<Identity> for Identity {
fn as_ref(&self) -> &Identity {
self
}
}
impl Identity {
pub fn new() -> Self {
Self {
public_key: None,
relay_list: RelayState::default(),
messaging_relays: RelayState::default(),
}
}
/// Sets the state of the NIP-65 relays.
pub fn set_relay_list_state(&mut self, state: RelayState) {
self.relay_list = state;
}
/// Returns the state of the NIP-65 relays.
pub fn relay_list_state(&self) -> RelayState {
self.relay_list
}
/// Sets the state of the NIP-17 relays.
pub fn set_messaging_relays_state(&mut self, state: RelayState) {
self.messaging_relays = state;
}
/// Returns the state of the NIP-17 relays.
pub fn messaging_relays_state(&self) -> RelayState {
self.messaging_relays
}
/// Force getting the public key of the identity.
///
/// Panics if the public key is not set.
pub fn public_key(&self) -> PublicKey {
self.public_key.unwrap()
}
/// Returns true if the identity has a public key.
pub fn has_public_key(&self) -> bool {
self.public_key.is_some()
}
/// Sets the public key of the identity.
pub fn set_public_key(&mut self, public_key: PublicKey) {
self.public_key = Some(public_key);
}
/// Unsets the public key of the identity.
pub fn unset_public_key(&mut self) {
self.public_key = None;
}
}

View File

@@ -1,26 +1,34 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::sync::Arc;
use std::time::Duration; use std::time::Duration;
use anyhow::{anyhow, Context as AnyhowContext, Error}; use anyhow::Error;
use common::{config_dir, BOOTSTRAP_RELAYS, SEARCH_RELAYS}; use common::{config_dir, BOOTSTRAP_RELAYS, SEARCH_RELAYS};
use gpui::{App, AppContext, Context, Entity, Global, Task}; use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task};
use nostr_lmdb::NostrLmdb; use nostr_lmdb::NostrLmdb;
use nostr_sdk::prelude::*; use nostr_sdk::prelude::*;
use smallvec::{smallvec, SmallVec};
use smol::lock::RwLock;
pub use storage::*;
pub use tracker::*;
mod storage; mod device;
mod tracker; mod event;
mod gossip;
mod identity;
pub const GIFTWRAP_SUBSCRIPTION: &str = "gift-wrap-events"; pub use device::*;
pub use event::*;
pub use gossip::*;
pub use identity::*;
use crate::identity::Identity;
pub fn init(cx: &mut App) { pub fn init(cx: &mut App) {
NostrRegistry::set_global(cx.new(NostrRegistry::new), cx); NostrRegistry::set_global(cx.new(NostrRegistry::new), cx);
} }
/// Default timeout for subscription
pub const TIMEOUT: u64 = 3;
/// Default subscription id for gift wrap events
pub const GIFTWRAP_SUBSCRIPTION: &str = "giftwrap-events";
struct GlobalNostrRegistry(Entity<NostrRegistry>); struct GlobalNostrRegistry(Entity<NostrRegistry>);
impl Global for GlobalNostrRegistry {} impl Global for GlobalNostrRegistry {}
@@ -28,17 +36,27 @@ impl Global for GlobalNostrRegistry {}
/// Nostr Registry /// Nostr Registry
#[derive(Debug)] #[derive(Debug)]
pub struct NostrRegistry { pub struct NostrRegistry {
/// Nostr Client /// Nostr client
client: Client, client: Client,
/// Custom gossip implementation /// App keys
gossip: Arc<RwLock<Gossip>>, ///
/// Used for Nostr Connect and NIP-4e operations
app_keys: Keys,
/// Tracks activity related to Nostr events /// Current identity (user's public key)
tracker: Arc<RwLock<EventTracker>>, ///
/// Set by the current Nostr signer
identity: Entity<Identity>,
/// Gossip implementation
gossip: Entity<Gossip>,
/// Tasks for asynchronous operations /// Tasks for asynchronous operations
_tasks: SmallVec<[Task<()>; 1]>, tasks: Vec<Task<Result<(), Error>>>,
/// Subscriptions
_subscriptions: Vec<Subscription>,
} }
impl NostrRegistry { impl NostrRegistry {
@@ -79,72 +97,114 @@ impl NostrRegistry {
// Construct the nostr client // Construct the nostr client
let client = ClientBuilder::default().database(lmdb).opts(opts).build(); let client = ClientBuilder::default().database(lmdb).opts(opts).build();
let _ = tracker();
let tracker = Arc::new(RwLock::new(EventTracker::default())); // Get the app keys
let gossip = Arc::new(RwLock::new(Gossip::default())); let app_keys = Self::create_or_init_app_keys().unwrap();
let mut tasks = smallvec![]; // Construct the gossip entity
let gossip = cx.new(|_| Gossip::default());
let async_gossip = gossip.downgrade();
// Construct the identity entity
let identity = cx.new(|_| Identity::default());
// Channel for communication between nostr and gpui
let (tx, rx) = flume::bounded::<Event>(2048);
let mut subscriptions = vec![];
let mut tasks = vec![];
subscriptions.push(
// Observe the identity entity
cx.observe(&identity, |this, state, cx| {
if state.read(cx).has_public_key() {
match state.read(cx).relay_list_state() {
RelayState::Initial => {
this.get_relay_list(cx);
}
RelayState::Set => {
if state.read(cx).messaging_relays_state() == RelayState::Initial {
this.get_profile(cx);
this.get_messaging_relays(cx);
};
}
_ => {}
}
}
}),
);
tasks.push( tasks.push(
// Establish connection to the bootstrap relays // Handle nostr notifications
//
// And handle notifications from the nostr relay pool channel
cx.background_spawn({ cx.background_spawn({
let client = client.clone(); let client = client.clone();
let gossip = Arc::clone(&gossip);
let tracker = Arc::clone(&tracker);
let _ = initialized_at();
async move { async move { Self::handle_notifications(&client, &tx).await }
// Connect to the bootstrap relays }),
Self::connect(&client).await; );
// Handle notifications from the relay pool tasks.push(
Self::handle_notifications(&client, &gossip, &tracker).await; // Update GPUI states
cx.spawn(async move |_this, cx| {
while let Ok(event) = rx.recv_async().await {
match event.kind {
Kind::RelayList => {
async_gossip.update(cx, |this, cx| {
this.insert_relays(&event);
cx.notify();
})?;
} }
Kind::InboxRelays => {
async_gossip.update(cx, |this, cx| {
this.insert_messaging_relays(&event);
cx.notify();
})?;
}
_ => {}
}
}
Ok(())
}), }),
); );
Self { Self {
client, client,
tracker, app_keys,
identity,
gossip, gossip,
_tasks: tasks, _subscriptions: subscriptions,
tasks,
} }
} }
/// Establish connection to the bootstrap relays /// Handle nostr notifications
async fn connect(client: &Client) { async fn handle_notifications(client: &Client, tx: &flume::Sender<Event>) -> Result<(), Error> {
// Get all bootstrapping relays // Add bootstrap relay to the relay pool
let mut urls = vec![]; for url in BOOTSTRAP_RELAYS.into_iter() {
urls.extend(BOOTSTRAP_RELAYS); client.add_relay(url).await?;
urls.extend(SEARCH_RELAYS); }
// Add relay to the relay pool // Add search relay to the relay pool
for url in urls.into_iter() { for url in SEARCH_RELAYS.into_iter() {
client.add_relay(url).await.ok(); client.add_relay(url).await?;
} }
// Connect to all added relays // Connect to all added relays
client.connect().await; client.connect().await;
}
async fn handle_notifications( // Handle nostr notifications
client: &Client,
gossip: &Arc<RwLock<Gossip>>,
tracker: &Arc<RwLock<EventTracker>>,
) {
let mut notifications = client.notifications(); let mut notifications = client.notifications();
let mut processed_events = HashSet::new(); let mut processed_events = HashSet::new();
while let Ok(notification) = notifications.recv().await { while let Ok(notification) = notifications.recv().await {
let RelayPoolNotification::Message { message, relay_url } = notification else { if let RelayPoolNotification::Message { message, relay_url } = notification {
// Skip if the notification is not a message
continue;
};
match message { match message {
RelayMessage::Event { event, .. } => { RelayMessage::Event {
event,
subscription_id,
} => {
if !processed_events.insert(event.id) { if !processed_events.insert(event.id) {
// Skip if the event has already been processed // Skip if the event has already been processed
continue; continue;
@@ -152,72 +212,32 @@ impl NostrRegistry {
match event.kind { match event.kind {
Kind::RelayList => { Kind::RelayList => {
let mut gossip = gossip.write().await; // Automatically get messaging relays for each member when the user opens a room
gossip.insert_relays(&event); if subscription_id.as_str().starts_with("room-") {
Self::get_adv_events_by(client, event.as_ref()).await?;
let urls: Vec<RelayUrl> = Self::extract_write_relays(&event);
let author = event.pubkey;
log::info!("Write relays: {urls:?}");
// Fetch user's encryption announcement event
Self::get(client, &urls, author, Kind::Custom(10044)).await;
// Fetch user's messaging relays event
Self::get(client, &urls, author, Kind::InboxRelays).await;
// Verify if the event is belonging to the current user
if Self::is_self_authored(client, &event).await {
// Fetch user's metadata event
Self::get(client, &urls, author, Kind::Metadata).await;
// Fetch user's contact list event
Self::get(client, &urls, author, Kind::ContactList).await;
} }
tx.send_async(event.into_owned()).await?;
} }
Kind::InboxRelays => { Kind::InboxRelays => {
let mut gossip = gossip.write().await; tx.send_async(event.into_owned()).await?;
gossip.insert_messaging_relays(&event);
if Self::is_self_authored(client, &event).await {
// Extract user's messaging relays
let urls: Vec<RelayUrl> =
nip17::extract_relay_list(&event).cloned().collect();
// Fetch user's inbox messages in the extracted relays
Self::get_messages(client, event.pubkey, &urls).await;
}
}
Kind::Custom(10044) => {
let mut gossip = gossip.write().await;
gossip.insert_announcement(&event);
}
Kind::ContactList => {
if Self::is_self_authored(client, &event).await {
let public_keys: Vec<PublicKey> =
event.tags.public_keys().copied().collect();
if let Err(e) =
Self::get_metadata_for_list(client, public_keys).await
{
log::error!("Failed to get metadata for list: {e}");
}
}
} }
_ => {} _ => {}
}; }
} }
RelayMessage::Ok { RelayMessage::Ok {
event_id, message, .. event_id, message, ..
} => { } => {
let msg = MachineReadablePrefix::parse(&message); let msg = MachineReadablePrefix::parse(&message);
let mut tracker = tracker.write().await; let mut tracker = tracker().write().await;
// Message that need to be authenticated will be handled separately // Handle authentication messages
if let Some(MachineReadablePrefix::AuthRequired) = msg { if let Some(MachineReadablePrefix::AuthRequired) = msg {
// Keep track of events that need to be resent after authentication // Keep track of events that need to be resent after authentication
tracker.resend_queue.insert(event_id, relay_url); tracker.add_to_pending(event_id, relay_url);
} else { } else {
// Keep track of events sent by Coop // Keep track of events sent by Coop
tracker.sent_ids.insert(event_id); tracker.sent(event_id)
} }
} }
_ => {} _ => {}
@@ -225,164 +245,352 @@ impl NostrRegistry {
} }
} }
/// Check if event is published by current user Ok(())
pub async fn is_self_authored(client: &Client, event: &Event) -> bool {
if let Ok(signer) = client.signer().await {
if let Ok(public_key) = signer.get_public_key().await {
return public_key == event.pubkey;
}
}
false
} }
/// Get event that match the given kind for a given author /// Automatically get messaging relays and encryption announcement from a received relay list
async fn get(client: &Client, urls: &[RelayUrl], author: PublicKey, kind: Kind) { async fn get_adv_events_by(client: &Client, event: &Event) -> Result<(), Error> {
// Skip if no relays are provided // Subscription options
if urls.is_empty() { let opts = SubscribeAutoCloseOptions::default()
return; .timeout(Some(Duration::from_secs(TIMEOUT)))
.exit_policy(ReqExitPolicy::ExitOnEOSE);
// Extract write relays from event
let write_relays: Vec<&RelayUrl> = nip65::extract_relay_list(event)
.filter_map(|(url, metadata)| {
if metadata.is_none() || metadata == &Some(RelayMetadata::Write) {
Some(url)
} else {
None
} }
})
.collect();
// Ensure relay connections // Ensure relay connections
for url in urls.iter() { for relay in write_relays.iter() {
client.add_relay(url).await.ok(); client.add_relay(*relay).await?;
client.connect_relay(url).await.ok(); client.connect_relay(*relay).await?;
} }
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE); // Construct filter for inbox relays
let filter = Filter::new().author(author).kind(kind).limit(1); let inbox = Filter::new()
.kind(Kind::InboxRelays)
.author(event.pubkey)
.limit(1);
// Subscribe to filters from the user's write relays // Construct filter for encryption announcement
if let Err(e) = client.subscribe_to(urls, filter, Some(opts)).await { let announcement = Filter::new()
log::error!("Failed to subscribe: {}", e); .kind(Kind::Custom(10044))
} .author(event.pubkey)
} .limit(1);
/// Get all gift wrap events in the messaging relays for a given public key
pub async fn get_messages(client: &Client, public_key: PublicKey, urls: &[RelayUrl]) {
// Verify that there are relays provided
if urls.is_empty() {
return;
}
// Ensure relay connection
for url in urls.iter() {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
let id = SubscriptionId::new(GIFTWRAP_SUBSCRIPTION);
let filter = Filter::new().kind(Kind::GiftWrap).pubkey(public_key);
// Unsubscribe from the previous subscription
client.unsubscribe(&id).await;
// Subscribe to filters to user's messaging relays
if let Err(e) = client.subscribe_with_id_to(urls, id, filter, None).await {
log::error!("Failed to subscribe: {}", e);
} else {
log::info!("Subscribed to gift wrap events for public key {public_key}",);
}
}
/// Get metadata for a list of public keys
async fn get_metadata_for_list(client: &Client, pubkeys: Vec<PublicKey>) -> Result<(), Error> {
let opts = SubscribeAutoCloseOptions::default().exit_policy(ReqExitPolicy::ExitOnEOSE);
let kinds = vec![Kind::Metadata, Kind::ContactList];
// Return if the list is empty
if pubkeys.is_empty() {
return Err(anyhow!("You need at least one public key".to_string(),));
}
let filter = Filter::new()
.limit(pubkeys.len() * kinds.len())
.authors(pubkeys)
.kinds(kinds);
// Subscribe to filters to the bootstrap relays
client client
.subscribe_to(BOOTSTRAP_RELAYS, filter, Some(opts)) .subscribe_to(write_relays, vec![inbox, announcement], Some(opts))
.await?; .await?;
Ok(()) Ok(())
} }
pub fn extract_read_relays(event: &Event) -> Vec<RelayUrl> { /// Get or create a new app keys
nip65::extract_relay_list(event) fn create_or_init_app_keys() -> Result<Keys, Error> {
.filter_map(|(url, metadata)| { let dir = config_dir().join(".app_keys");
if metadata.is_none() || metadata == &Some(RelayMetadata::Read) { let content = match std::fs::read(&dir) {
Some(url.to_owned()) Ok(content) => content,
} else { Err(_) => {
None // Generate new keys if file doesn't exist
let keys = Keys::generate();
let secret_key = keys.secret_key();
std::fs::create_dir_all(dir.parent().unwrap())?;
std::fs::write(&dir, secret_key.to_secret_bytes())?;
return Ok(keys);
} }
}) };
.take(3) let secret_key = SecretKey::from_slice(&content)?;
.collect() let keys = Keys::new(secret_key);
Ok(keys)
} }
pub fn extract_write_relays(event: &Event) -> Vec<RelayUrl> { /// Get the nostr client
nip65::extract_relay_list(event)
.filter_map(|(url, metadata)| {
if metadata.is_none() || metadata == &Some(RelayMetadata::Write) {
Some(url.to_owned())
} else {
None
}
})
.take(3)
.collect()
}
/// Extract an encryption keys announcement from an event.
pub fn extract_announcement(event: &Event) -> Result<Announcement, Error> {
let public_key = event
.tags
.iter()
.find(|tag| tag.kind().as_str() == "n" || tag.kind().as_str() == "pubkey")
.and_then(|tag| tag.content())
.and_then(|c| PublicKey::parse(c).ok())
.context("Cannot parse public key from the event's tags")?;
let client_name = event
.tags
.find(TagKind::Client)
.and_then(|tag| tag.content())
.map(|c| c.to_string());
Ok(Announcement::new(event.id, client_name, public_key))
}
/// Extract an encryption keys response from an event.
pub async fn extract_response(client: &Client, event: &Event) -> Result<Response, Error> {
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
if event.pubkey != public_key {
return Err(anyhow!("Event does not belong to current user"));
}
let client_pubkey = event
.tags
.find(TagKind::custom("P"))
.and_then(|tag| tag.content())
.and_then(|c| PublicKey::parse(c).ok())
.context("Cannot parse public key from the event's tags")?;
Ok(Response::new(event.content.clone(), client_pubkey))
}
/// Returns a reference to the nostr client.
pub fn client(&self) -> Client { pub fn client(&self) -> Client {
self.client.clone() self.client.clone()
} }
/// Returns a reference to the event tracker. /// Get the app keys
pub fn tracker(&self) -> Arc<RwLock<EventTracker>> { pub fn app_keys(&self) -> &Keys {
Arc::clone(&self.tracker) &self.app_keys
} }
/// Returns a reference to the cache manager. /// Get current identity
pub fn gossip(&self) -> Arc<RwLock<Gossip>> { pub fn identity(&self) -> Entity<Identity> {
Arc::clone(&self.gossip) self.identity.clone()
}
/// Get a relay hint (messaging relay) for a given public key
pub fn relay_hint(&self, public_key: &PublicKey, cx: &App) -> Option<RelayUrl> {
self.gossip
.read(cx)
.messaging_relays(public_key)
.first()
.cloned()
}
/// Get a list of write relays for a given public key
pub fn write_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let relays = self.gossip.read(cx).write_relays(public_key);
cx.background_spawn(async move {
// Ensure relay connections
for url in relays.iter() {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
relays
})
}
/// Get a list of read relays for a given public key
pub fn read_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let relays = self.gossip.read(cx).read_relays(public_key);
cx.background_spawn(async move {
// Ensure relay connections
for url in relays.iter() {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
relays
})
}
/// Get a list of messaging relays for a given public key
pub fn messaging_relays(&self, public_key: &PublicKey, cx: &App) -> Task<Vec<RelayUrl>> {
let client = self.client();
let relays = self.gossip.read(cx).messaging_relays(public_key);
cx.background_spawn(async move {
// Ensure relay connections
for url in relays.iter() {
client.add_relay(url).await.ok();
client.connect_relay(url).await.ok();
}
relays
})
}
/// Set the signer for the nostr client and verify the public key
pub fn set_signer<T>(&mut self, signer: T, cx: &mut Context<Self>)
where
T: NostrSigner + 'static,
{
let client = self.client();
let identity = self.identity.downgrade();
// Create a task to update the signer and verify the public key
let task: Task<Result<PublicKey, Error>> = cx.background_spawn(async move {
// Update signer
client.set_signer(signer).await;
// Verify signer
let signer = client.signer().await?;
let public_key = signer.get_public_key().await?;
Ok(public_key)
});
self.tasks.push(cx.spawn(async move |_this, cx| {
match task.await {
Ok(public_key) => {
identity.update(cx, |this, cx| {
this.set_public_key(public_key);
cx.notify();
})?;
}
Err(e) => {
log::error!("Failed to set signer: {e}");
}
};
Ok(())
}));
}
/// Unset the current signer
pub fn unset_signer(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let async_identity = self.identity.downgrade();
self.tasks.push(cx.spawn(async move |_this, cx| {
// Unset the signer from nostr client
cx.background_executor()
.await_on_background(async move {
client.unset_signer().await;
})
.await;
// Unset the current identity
async_identity
.update(cx, |this, cx| {
this.unset_public_key();
cx.notify();
})
.ok();
Ok(())
}));
}
// Get relay list for current user
fn get_relay_list(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let async_identity = self.identity.downgrade();
let public_key = self.identity().read(cx).public_key();
let task: Task<Result<RelayState, Error>> = cx.background_spawn(async move {
let filter = Filter::new()
.kind(Kind::RelayList)
.author(public_key)
.limit(1);
let mut stream = client
.stream_events_from(BOOTSTRAP_RELAYS, vec![filter], Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
match res {
Ok(event) => {
log::info!("Received relay list event: {event:?}");
return Ok(RelayState::Set);
}
Err(e) => {
log::error!("Failed to receive relay list event: {e}");
}
}
}
Ok(RelayState::NotSet)
});
self.tasks.push(cx.spawn(async move |_this, cx| {
match task.await {
Ok(state) => {
async_identity
.update(cx, |this, cx| {
this.set_relay_list_state(state);
cx.notify();
})
.ok();
}
Err(e) => {
log::error!("Failed to get relay list: {e}");
}
}
Ok(())
}));
}
/// Get profile and contact list for current user
fn get_profile(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let public_key = self.identity().read(cx).public_key();
let write_relays = self.write_relays(&public_key, cx);
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
let mut urls = vec![];
urls.extend(write_relays.await);
urls.extend(
BOOTSTRAP_RELAYS
.iter()
.filter_map(|url| RelayUrl::parse(url).ok()),
);
// Construct subscription options
let opts = SubscribeAutoCloseOptions::default()
.exit_policy(ReqExitPolicy::ExitOnEOSE)
.timeout(Some(Duration::from_secs(TIMEOUT)));
// Filter for metadata
let metadata = Filter::new()
.kind(Kind::Metadata)
.limit(1)
.author(public_key);
// Filter for contact list
let contact_list = Filter::new()
.kind(Kind::ContactList)
.limit(1)
.author(public_key);
client
.subscribe_to(urls, vec![metadata, contact_list], Some(opts))
.await?;
Ok(())
});
task.detach();
}
/// Get messaging relays for current user
fn get_messaging_relays(&mut self, cx: &mut Context<Self>) {
let client = self.client();
let async_identity = self.identity.downgrade();
let public_key = self.identity().read(cx).public_key();
let write_relays = self.write_relays(&public_key, cx);
let task: Task<Result<RelayState, Error>> = cx.background_spawn(async move {
let urls = write_relays.await;
// Construct the filter for inbox relays
let filter = Filter::new()
.kind(Kind::InboxRelays)
.author(public_key)
.limit(1);
// Stream events from the write relays
let mut stream = client
.stream_events_from(urls, vec![filter], Duration::from_secs(TIMEOUT))
.await?;
while let Some((_url, res)) = stream.next().await {
match res {
Ok(event) => {
log::info!("Received messaging relays event: {event:?}");
return Ok(RelayState::Set);
}
Err(e) => {
log::error!("Failed to get messaging relays: {e}");
}
}
}
Ok(RelayState::NotSet)
});
self.tasks.push(cx.spawn(async move |_this, cx| {
match task.await {
Ok(state) => {
async_identity
.update(cx, |this, cx| {
this.set_messaging_relays_state(state);
cx.notify();
})
.ok();
}
Err(e) => {
log::error!("Failed to get messaging relays: {e}");
}
}
Ok(())
}));
} }
} }

View File

@@ -1,43 +0,0 @@
use std::collections::{HashMap, HashSet};
use std::sync::OnceLock;
use nostr_sdk::prelude::*;
static INITIALIZED_AT: OnceLock<Timestamp> = OnceLock::new();
pub fn initialized_at() -> &'static Timestamp {
INITIALIZED_AT.get_or_init(Timestamp::now)
}
#[derive(Debug, Clone, Default)]
pub struct EventTracker {
/// Tracking events that have been resent by Coop in the current session
pub resent_ids: Vec<Output<EventId>>,
/// Temporarily store events that need to be resent later
pub resend_queue: HashMap<EventId, RelayUrl>,
/// Tracking events sent by Coop in the current session
pub sent_ids: HashSet<EventId>,
/// Tracking events seen on which relays in the current session
pub seen_on_relays: HashMap<EventId, HashSet<RelayUrl>>,
}
impl EventTracker {
pub fn resent_ids(&self) -> &Vec<Output<EventId>> {
&self.resent_ids
}
pub fn resend_queue(&self) -> &HashMap<EventId, RelayUrl> {
&self.resend_queue
}
pub fn sent_ids(&self) -> &HashSet<EventId> {
&self.sent_ids
}
pub fn seen_on_relays(&self) -> &HashMap<EventId, HashSet<RelayUrl>> {
&self.seen_on_relays
}
}

View File

@@ -182,16 +182,15 @@ impl TabPanel {
// Sync the active state to all panels // Sync the active state to all panels
cx.spawn(async move |view, cx| { cx.spawn(async move |view, cx| {
_ = cx.update(|cx| { view.update(cx, |view, cx| {
_ = view.update(cx, |view, cx| {
if let Some(last_active) = view.panels.get(last_active_ix) { if let Some(last_active) = view.panels.get(last_active_ix) {
last_active.set_active(false, cx); last_active.set_active(false, cx);
} }
if let Some(active) = view.panels.get(view.active_ix) { if let Some(active) = view.panels.get(view.active_ix) {
active.set_active(true, cx); active.set_active(true, cx);
} }
}); })
}); .ok();
}) })
.detach(); .detach();
@@ -923,11 +922,10 @@ impl TabPanel {
cx.spawn({ cx.spawn({
let is_zoomed = self.is_zoomed; let is_zoomed = self.is_zoomed;
async move |view, cx| { async move |view, cx| {
_ = cx.update(|cx| { view.update(cx, |view, cx| {
_ = view.update(cx, |view, cx| {
view.set_zoomed(is_zoomed, cx); view.set_zoomed(is_zoomed, cx);
}); })
}); .ok();
} }
}) })
.detach(); .detach();

View File

@@ -56,7 +56,7 @@ impl BlinkCursor {
cx.spawn(async move |this, cx| { cx.spawn(async move |this, cx| {
Timer::after(INTERVAL).await; Timer::after(INTERVAL).await;
if let Some(this) = this.upgrade() { if let Some(this) = this.upgrade() {
this.update(cx, |this, cx| this.blink(epoch, cx)).ok(); this.update(cx, |this, cx| this.blink(epoch, cx));
} }
}) })
.detach(); .detach();
@@ -82,8 +82,7 @@ impl BlinkCursor {
this.update(cx, |this, cx| { this.update(cx, |this, cx| {
this.paused = false; this.paused = false;
this.blink(epoch, cx); this.blink(epoch, cx);
}) });
.ok();
} }
}) })
.detach(); .detach();