This commit is contained in:
2026-06-10 13:00:51 +07:00
parent 0230fcff23
commit e812ae05a9
8 changed files with 94 additions and 67 deletions

View File

@@ -15,7 +15,7 @@ use nostr_sdk::prelude::*;
use person::PersonRegistry;
use settings::AppSettings;
use smallvec::{SmallVec, smallvec};
use state::{Announcement, CLIENT_NAME, NostrRegistry, StateEvent};
use state::{Announcement, CLIENT_NAME, NostrRegistry};
use theme::ActiveTheme;
use ui::avatar::Avatar;
use ui::button::Button;
@@ -91,6 +91,7 @@ impl DeviceRegistry {
/// Create a new device registry instance
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let signer = cx.new(|_| None);
let nostr = NostrRegistry::global(cx);
let user_signer = nostr.read(cx).signer.clone();
@@ -109,7 +110,7 @@ impl DeviceRegistry {
);
subscriptions.push(
// Subscribe to nostr state events
// Observe the user signer
cx.observe(&user_signer, move |this, signer, cx| {
if signer.read(cx).is_some() && is_nip4e_enabled {
this.get_announcement(cx);
@@ -133,14 +134,11 @@ impl DeviceRegistry {
fn handle_notifications(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let current_user = nostr.read(cx).signer_pubkey(cx);
let announcement_existed = self.announcement_existed.clone();
let (tx, rx) = flume::bounded::<Event>(100);
let Some(current_user) = nostr.read(cx).signer_pubkey(cx) else {
return;
};
self.tasks.push(cx.background_spawn(async move {
let mut notifications = client.notifications();
let mut processed_events = HashSet::new();
@@ -155,21 +153,15 @@ impl DeviceRegistry {
}
match event.kind {
Kind::Custom(10044) => {
if current_user == event.pubkey {
announcement_existed.store(true, Ordering::Relaxed);
tx.send_async(event.into_owned()).await?;
}
Kind::Custom(10044) if current_user == Some(event.pubkey) => {
announcement_existed.store(true, Ordering::Relaxed);
tx.send_async(event.into_owned()).await?;
}
Kind::Custom(4454) => {
if current_user == event.pubkey {
tx.send_async(event.into_owned()).await?;
}
Kind::Custom(4454) if current_user == Some(event.pubkey) => {
tx.send_async(event.into_owned()).await?;
}
Kind::Custom(4455) => {
if current_user == event.pubkey {
tx.send_async(event.into_owned()).await?;
}
Kind::Custom(4455) if current_user == Some(event.pubkey) => {
tx.send_async(event.into_owned()).await?;
}
_ => {}
}
@@ -426,14 +418,16 @@ impl DeviceRegistry {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
let app_keys = Keys::generate();
let app_pubkey = app_keys.public_key();
let Some(signer) = nostr.read(cx).signer(cx) else {
return;
};
let Ok(app_keys) = get_or_init_app_keys(cx) else {
return;
};
let task: Task<Result<Option<Event>, Error>> = cx.background_spawn(async move {
let app_pubkey = app_keys.public_key();
let public_key = signer.get_public_key_async().await?;
// Construct a filter to get the latest approval event
@@ -516,8 +510,9 @@ impl DeviceRegistry {
/// Parse the approval event to get encryption key then set it
fn extract_encryption(&mut self, event: Event, cx: &mut Context<Self>) {
let nostr = NostrRegistry::global(cx);
let app_keys = Keys::generate();
let Ok(app_keys) = get_or_init_app_keys(cx) else {
return;
};
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
let master = event
@@ -744,6 +739,35 @@ impl DeviceRegistry {
struct DeviceNotification;
/// Get or create new app keys
fn get_or_init_app_keys(cx: &App) -> Result<Keys, Error> {
let read = cx.read_credentials(CLIENT_NAME);
let stored_keys: Option<Keys> = cx.foreground_executor().block_on(async move {
if let Ok(Some((_, secret))) = read.await {
SecretKey::from_slice(&secret).map(Keys::new).ok()
} else {
None
}
});
if let Some(keys) = stored_keys {
Ok(keys)
} else {
let keys = Keys::generate();
let user = keys.public_key().to_hex();
let secret = keys.secret_key().to_secret_bytes();
let write = cx.write_credentials(CLIENT_NAME, &user, &secret);
cx.foreground_executor().block_on(async move {
if let Err(e) = write.await {
log::error!("Keyring not available or panic: {e}")
}
});
Ok(keys)
}
}
/// Encrypt and store device keys in the local database.
async fn set_keys(client: &Client, signer: &Keys, secret: &str) -> Result<(), Error> {
let public_key = signer.get_public_key_async().await?;