chore: follow up to 73b8a1a
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
use std::{str::FromStr, sync::Arc, time::Duration};
|
use std::{collections::HashSet, str::FromStr, sync::Arc, time::Duration};
|
||||||
|
|
||||||
use anyhow::{anyhow, Error};
|
use anyhow::{anyhow, Error};
|
||||||
use common::profile::NostrProfile;
|
use common::profile::NostrProfile;
|
||||||
@@ -123,6 +123,7 @@ pub struct Device {
|
|||||||
client_keys: Arc<Keys>,
|
client_keys: Arc<Keys>,
|
||||||
/// Device State
|
/// Device State
|
||||||
state: Entity<DeviceState>,
|
state: Entity<DeviceState>,
|
||||||
|
requesters: Entity<HashSet<PublicKey>>,
|
||||||
is_processing: bool,
|
is_processing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -174,11 +175,13 @@ pub fn init(window: &mut Window, cx: &App) {
|
|||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
|
|
||||||
|
let requesters = cx.new(|_| HashSet::new());
|
||||||
let entity = cx.new(|_| Device {
|
let entity = cx.new(|_| Device {
|
||||||
profile: None,
|
profile: None,
|
||||||
is_processing: false,
|
is_processing: false,
|
||||||
state,
|
state,
|
||||||
client_keys,
|
client_keys,
|
||||||
|
requesters,
|
||||||
});
|
});
|
||||||
|
|
||||||
window_handle
|
window_handle
|
||||||
@@ -236,6 +239,13 @@ impl Device {
|
|||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn add_requester(&mut self, public_key: PublicKey, cx: &mut Context<Self>) {
|
||||||
|
self.requesters.update(cx, |this, cx| {
|
||||||
|
this.insert(public_key);
|
||||||
|
cx.notify();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/// Login and set user signer
|
/// Login and set user signer
|
||||||
pub fn login<T>(&self, signer: T, cx: &mut Context<Self>) -> Task<Result<(), Error>>
|
pub fn login<T>(&self, signer: T, cx: &mut Context<Self>) -> Task<Result<(), Error>>
|
||||||
where
|
where
|
||||||
@@ -354,6 +364,10 @@ impl Device {
|
|||||||
|
|
||||||
/// Initialize subscription for current user
|
/// Initialize subscription for current user
|
||||||
pub fn start_subscription(&self, cx: &Context<Self>) {
|
pub fn start_subscription(&self, cx: &Context<Self>) {
|
||||||
|
if self.is_processing {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let Some(profile) = self.profile() else {
|
let Some(profile) = self.profile() else {
|
||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
@@ -664,8 +678,10 @@ impl Device {
|
|||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let keys = Arc::new(Keys::parse(&secret)?);
|
let keys = Arc::new(Keys::parse(&secret)?);
|
||||||
|
|
||||||
// Update global state with new device keys
|
// Update global state with new device keys
|
||||||
set_device_keys(keys).await;
|
set_device_keys(keys).await;
|
||||||
|
log::info!("Received master keys");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
@@ -679,12 +695,15 @@ impl Device {
|
|||||||
cx.spawn_in(window, |_, mut cx| async move {
|
cx.spawn_in(window, |_, mut cx| async move {
|
||||||
if let Err(e) = task.await {
|
if let Err(e) = task.await {
|
||||||
cx.update(|window, cx| {
|
cx.update(|window, cx| {
|
||||||
window.push_notification(Notification::error(e.to_string()), cx);
|
window.push_notification(
|
||||||
|
Notification::error(format!("Failed to decrypt: {}", e)),
|
||||||
|
cx,
|
||||||
|
);
|
||||||
})
|
})
|
||||||
.ok();
|
.ok();
|
||||||
} else {
|
} else {
|
||||||
cx.update(|window, cx| {
|
cx.update(|window, cx| {
|
||||||
window.close_modal(cx);
|
window.close_all_modals(cx);
|
||||||
window.push_notification(
|
window.push_notification(
|
||||||
Notification::success("Device Keys request has been approved"),
|
Notification::success("Device Keys request has been approved"),
|
||||||
cx,
|
cx,
|
||||||
@@ -699,7 +718,7 @@ impl Device {
|
|||||||
/// Received Master Keys request from other Nostr client
|
/// Received Master Keys request from other Nostr client
|
||||||
///
|
///
|
||||||
/// NIP-4e: <https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md>
|
/// NIP-4e: <https://github.com/nostr-protocol/nips/blob/per-device-keys/4e.md>
|
||||||
pub fn recv_request(&self, event: Event, window: &mut Window, cx: &mut Context<Self>) {
|
pub fn recv_request(&mut self, event: Event, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
let Some(target_pubkey) = event
|
let Some(target_pubkey) = event
|
||||||
.tags
|
.tags
|
||||||
.find(TagKind::custom("pubkey"))
|
.find(TagKind::custom("pubkey"))
|
||||||
@@ -710,6 +729,13 @@ impl Device {
|
|||||||
return;
|
return;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Prevent processing duplicate requests
|
||||||
|
if self.requesters.read(cx).contains(&target_pubkey) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.add_requester(target_pubkey, cx);
|
||||||
|
|
||||||
let client = get_client();
|
let client = get_client();
|
||||||
let read_keys = cx.read_credentials(MASTER_KEYRING);
|
let read_keys = cx.read_credentials(MASTER_KEYRING);
|
||||||
let local_signer = self.client_keys.clone();
|
let local_signer = self.client_keys.clone();
|
||||||
|
|||||||
Reference in New Issue
Block a user