This commit is contained in:
Ren Amamiya
2026-04-04 08:53:20 +07:00
parent 707533c145
commit 0403d0e4ec
2 changed files with 50 additions and 76 deletions

View File

@@ -39,8 +39,6 @@ pub enum DeviceEvent {
Requesting,
/// The device is creating a new encryption key
Creating,
/// Encryption key is not set
NotSet { reason: SharedString },
/// An error occurred
Error(SharedString),
}
@@ -52,15 +50,6 @@ impl DeviceEvent {
{
Self::Error(error.into())
}
pub fn not_set<T>(reason: T) -> Self
where
T: Into<SharedString>,
{
Self::NotSet {
reason: reason.into(),
}
}
}
/// Device Registry
@@ -71,9 +60,6 @@ pub struct DeviceRegistry {
/// Whether the registry is currently initializing
pub initializing: bool,
/// Whether the registry is waiting for encryption key approval from other devices
pub requesting: bool,
/// Whether there is a pending request for encryption key approval
pub pending_request: bool,
@@ -105,7 +91,6 @@ impl DeviceRegistry {
let subscription = cx.subscribe_in(&nostr, window, |this, _e, event, _window, cx| {
if event == &StateEvent::SignerSet {
this.set_initializing(true, cx);
this.set_requesting(false, cx);
this.get_announcement(cx);
};
});
@@ -116,7 +101,6 @@ impl DeviceRegistry {
Self {
initializing: true,
requesting: false,
pending_request: false,
tasks: vec![],
_subscription: Some(subscription),
@@ -188,12 +172,6 @@ impl DeviceRegistry {
cx.notify();
}
/// Set whether the registry is waiting for encryption key approval from other devices
fn set_requesting(&mut self, requesting: bool, cx: &mut Context<Self>) {
self.requesting = requesting;
cx.notify();
}
/// Set whether there is a pending request for encryption key approval
fn set_pending_request(&mut self, pending: bool, cx: &mut Context<Self>) {
self.pending_request = pending;
@@ -353,30 +331,27 @@ impl DeviceRegistry {
// Get encryption key from the database and compare with the announcement
let task: Task<Result<Keys, Error>> = cx.background_spawn(async move {
if let Ok(keys) = get_keys(&client).await {
if keys.public_key() != device_pubkey {
return Err(anyhow!("Encryption Key doesn't match the announcement"));
};
Ok(keys)
} else {
Err(anyhow!("Encryption Key not found. Please create a new key"))
}
let keys = get_keys(&client).await?;
// Compare the public key from the announcement with the one from the database
if keys.public_key() != device_pubkey {
return Err(anyhow!("Encryption Key doesn't match the announcement"));
};
Ok(keys)
});
self.tasks.push(cx.spawn(async move |this, cx| {
match task.await {
Ok(keys) => {
this.update(cx, |this, cx| {
this.set_signer(keys, cx);
this.wait_for_request(cx);
})?;
}
Err(e) => {
this.update(cx, |_this, cx| {
cx.emit(DeviceEvent::not_set(e.to_string()));
})?;
}
};
if let Ok(keys) = task.await {
this.update(cx, |this, cx| {
this.set_signer(keys, cx);
this.wait_for_request(cx);
})?;
} else {
this.update(cx, |this, cx| {
this.request(cx);
})?;
}
Ok(())
}));
}
@@ -454,7 +429,7 @@ impl DeviceRegistry {
}
Ok(None) => {
this.update(cx, |this, cx| {
this.set_requesting(true, cx);
this.set_initializing(false, cx);
this.wait_for_approval(cx);
cx.emit(DeviceEvent::Requesting);
@@ -519,12 +494,11 @@ impl DeviceRegistry {
Ok(keys) => {
this.update(cx, |this, cx| {
this.set_signer(keys, cx);
this.set_requesting(false, cx);
})?;
}
Err(e) => {
this.update(cx, |_this, cx| {
cx.emit(DeviceEvent::not_set(e.to_string()));
cx.emit(DeviceEvent::error(e.to_string()));
})?;
}
}