refactor encryption request
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m58s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 1m40s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Rust / build (macos-latest, stable) (pull_request) Has been cancelled
Rust / build (windows-latest, stable) (pull_request) Has been cancelled
Some checks failed
Rust / build (ubuntu-latest, stable) (push) Failing after 1m58s
Rust / build (ubuntu-latest, stable) (pull_request) Failing after 1m40s
Rust / build (macos-latest, stable) (push) Has been cancelled
Rust / build (windows-latest, stable) (push) Has been cancelled
Rust / build (macos-latest, stable) (pull_request) Has been cancelled
Rust / build (windows-latest, stable) (pull_request) Has been cancelled
This commit is contained in:
2
Cargo.lock
generated
2
Cargo.lock
generated
@@ -1668,6 +1668,8 @@ dependencies = [
|
|||||||
"smallvec",
|
"smallvec",
|
||||||
"smol",
|
"smol",
|
||||||
"state",
|
"state",
|
||||||
|
"theme",
|
||||||
|
"ui",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|||||||
@@ -1,292 +0,0 @@
|
|||||||
use anyhow::Error;
|
|
||||||
use device::DeviceRegistry;
|
|
||||||
use gpui::prelude::FluentBuilder;
|
|
||||||
use gpui::{
|
|
||||||
div, px, AnyElement, App, AppContext, Context, Entity, EventEmitter, FocusHandle, Focusable,
|
|
||||||
IntoElement, ParentElement, Render, SharedString, Styled, Task, Window,
|
|
||||||
};
|
|
||||||
use nostr_sdk::prelude::*;
|
|
||||||
use person::{shorten_pubkey, PersonRegistry};
|
|
||||||
use state::Announcement;
|
|
||||||
use theme::ActiveTheme;
|
|
||||||
use ui::button::{Button, ButtonVariants};
|
|
||||||
use ui::dock_area::panel::{Panel, PanelEvent};
|
|
||||||
use ui::notification::Notification;
|
|
||||||
use ui::{divider, h_flex, v_flex, Disableable, IconName, Sizable, StyledExt, WindowExtension};
|
|
||||||
|
|
||||||
const MSG: &str =
|
|
||||||
"Encryption Key is a special key that used to encrypt and decrypt your messages. \
|
|
||||||
Your identity is completely decoupled from all encryption processes to protect your privacy.";
|
|
||||||
|
|
||||||
const NOTICE: &str = "By resetting your encryption key, you will lose access to \
|
|
||||||
all your encrypted messages before. This action cannot be undone.";
|
|
||||||
|
|
||||||
pub fn init(public_key: PublicKey, window: &mut Window, cx: &mut App) -> Entity<EncryptionPanel> {
|
|
||||||
cx.new(|cx| EncryptionPanel::new(public_key, window, cx))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct EncryptionPanel {
|
|
||||||
name: SharedString,
|
|
||||||
focus_handle: FocusHandle,
|
|
||||||
|
|
||||||
/// User's public key
|
|
||||||
public_key: PublicKey,
|
|
||||||
|
|
||||||
/// Whether the panel is loading
|
|
||||||
loading: bool,
|
|
||||||
|
|
||||||
/// Tasks
|
|
||||||
tasks: Vec<Task<Result<(), Error>>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EncryptionPanel {
|
|
||||||
fn new(public_key: PublicKey, _window: &mut Window, cx: &mut Context<Self>) -> Self {
|
|
||||||
Self {
|
|
||||||
name: "Encryption".into(),
|
|
||||||
focus_handle: cx.focus_handle(),
|
|
||||||
public_key,
|
|
||||||
loading: false,
|
|
||||||
tasks: vec![],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_loading(&mut self, status: bool, cx: &mut Context<Self>) {
|
|
||||||
self.loading = status;
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
fn approve(&mut self, event: &Event, window: &mut Window, cx: &mut Context<Self>) {
|
|
||||||
let device = DeviceRegistry::global(cx);
|
|
||||||
let task = device.read(cx).approve(event, cx);
|
|
||||||
let id = event.id;
|
|
||||||
|
|
||||||
// Update loading status
|
|
||||||
self.set_loading(true, cx);
|
|
||||||
|
|
||||||
self.tasks.push(cx.spawn_in(window, async move |this, cx| {
|
|
||||||
match task.await {
|
|
||||||
Ok(_) => {
|
|
||||||
this.update_in(cx, |this, window, cx| {
|
|
||||||
// Reset loading status
|
|
||||||
this.set_loading(false, cx);
|
|
||||||
|
|
||||||
// Remove request
|
|
||||||
device.update(cx, |this, cx| {
|
|
||||||
this.remove_request(&id, cx);
|
|
||||||
});
|
|
||||||
|
|
||||||
window.push_notification("Approved", cx);
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
this.update_in(cx, |this, window, cx| {
|
|
||||||
this.set_loading(false, cx);
|
|
||||||
window.push_notification(Notification::error(e.to_string()), cx);
|
|
||||||
})?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn render_requests(&mut self, cx: &mut Context<Self>) -> Vec<impl IntoElement> {
|
|
||||||
const TITLE: &str = "You've requested for the Encryption Key from:";
|
|
||||||
|
|
||||||
let device = DeviceRegistry::global(cx);
|
|
||||||
let requests = device.read(cx).requests.clone();
|
|
||||||
let mut items = Vec::new();
|
|
||||||
|
|
||||||
for event in requests.into_iter() {
|
|
||||||
let request = Announcement::from(&event);
|
|
||||||
let client_name = request.client_name();
|
|
||||||
let target = request.public_key();
|
|
||||||
|
|
||||||
items.push(
|
|
||||||
v_flex()
|
|
||||||
.gap_2()
|
|
||||||
.text_sm()
|
|
||||||
.child(SharedString::from(TITLE))
|
|
||||||
.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())),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
h_flex().justify_end().gap_2().child(
|
|
||||||
Button::new("approve")
|
|
||||||
.label("Approve")
|
|
||||||
.ghost()
|
|
||||||
.small()
|
|
||||||
.disabled(self.loading)
|
|
||||||
.loading(self.loading)
|
|
||||||
.on_click(cx.listener(move |this, _ev, window, cx| {
|
|
||||||
this.approve(&event, window, cx);
|
|
||||||
})),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
items
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Panel for EncryptionPanel {
|
|
||||||
fn panel_id(&self) -> SharedString {
|
|
||||||
self.name.clone()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn title(&self, _cx: &App) -> AnyElement {
|
|
||||||
self.name.clone().into_any_element()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EventEmitter<PanelEvent> for EncryptionPanel {}
|
|
||||||
|
|
||||||
impl Focusable for EncryptionPanel {
|
|
||||||
fn focus_handle(&self, _: &App) -> gpui::FocusHandle {
|
|
||||||
self.focus_handle.clone()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Render for EncryptionPanel {
|
|
||||||
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
|
|
||||||
let device = DeviceRegistry::global(cx);
|
|
||||||
let state = device.read(cx).state();
|
|
||||||
let has_requests = device.read(cx).has_requests();
|
|
||||||
|
|
||||||
let persons = PersonRegistry::global(cx);
|
|
||||||
let profile = persons.read(cx).get(&self.public_key, cx);
|
|
||||||
|
|
||||||
let Some(announcement) = profile.announcement() else {
|
|
||||||
return div();
|
|
||||||
};
|
|
||||||
|
|
||||||
let pubkey = SharedString::from(shorten_pubkey(announcement.public_key(), 16));
|
|
||||||
let client_name = announcement.client_name();
|
|
||||||
|
|
||||||
v_flex()
|
|
||||||
.p_3()
|
|
||||||
.gap_3()
|
|
||||||
.w_full()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.text_xs()
|
|
||||||
.text_color(cx.theme().text_muted)
|
|
||||||
.child(SharedString::from(MSG)),
|
|
||||||
)
|
|
||||||
.child(divider(cx))
|
|
||||||
.child(
|
|
||||||
v_flex()
|
|
||||||
.gap_3()
|
|
||||||
.text_sm()
|
|
||||||
.child(
|
|
||||||
v_flex()
|
|
||||||
.gap_1p5()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.text_color(cx.theme().text_muted)
|
|
||||||
.child(SharedString::from("Device 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_1p5()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.text_color(cx.theme().text_muted)
|
|
||||||
.child(SharedString::from("Encryption Public Key:")),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
h_flex()
|
|
||||||
.h_7()
|
|
||||||
.w_full()
|
|
||||||
.px_2()
|
|
||||||
.rounded(cx.theme().radius)
|
|
||||||
.bg(cx.theme().elevated_surface_background)
|
|
||||||
.child(pubkey),
|
|
||||||
),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.when(has_requests, |this| {
|
|
||||||
this.child(divider(cx)).child(
|
|
||||||
v_flex()
|
|
||||||
.gap_1p5()
|
|
||||||
.w_full()
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.text_color(cx.theme().text_muted)
|
|
||||||
.child(SharedString::from("Requests:")),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
v_flex()
|
|
||||||
.gap_2()
|
|
||||||
.flex_1()
|
|
||||||
.w_full()
|
|
||||||
.children(self.render_requests(cx)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.child(divider(cx))
|
|
||||||
.when(state.requesting(), |this| {
|
|
||||||
this.child(
|
|
||||||
h_flex()
|
|
||||||
.h_8()
|
|
||||||
.justify_center()
|
|
||||||
.text_xs()
|
|
||||||
.text_center()
|
|
||||||
.text_color(cx.theme().text_accent)
|
|
||||||
.bg(cx.theme().elevated_surface_background)
|
|
||||||
.rounded(cx.theme().radius)
|
|
||||||
.child(SharedString::from(
|
|
||||||
"Please open other device and approve the request",
|
|
||||||
)),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.child(
|
|
||||||
v_flex()
|
|
||||||
.gap_1()
|
|
||||||
.child(
|
|
||||||
Button::new("reset")
|
|
||||||
.icon(IconName::Reset)
|
|
||||||
.label("Reset")
|
|
||||||
.warning()
|
|
||||||
.small()
|
|
||||||
.font_semibold(),
|
|
||||||
)
|
|
||||||
.child(
|
|
||||||
div()
|
|
||||||
.italic()
|
|
||||||
.text_size(px(10.))
|
|
||||||
.text_color(cx.theme().text_muted)
|
|
||||||
.child(SharedString::from(NOTICE)),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
pub mod backup;
|
pub mod backup;
|
||||||
pub mod connect;
|
pub mod connect;
|
||||||
pub mod contact_list;
|
pub mod contact_list;
|
||||||
pub mod encryption_key;
|
|
||||||
pub mod greeter;
|
pub mod greeter;
|
||||||
pub mod import;
|
pub mod import;
|
||||||
pub mod messaging_relays;
|
pub mod messaging_relays;
|
||||||
|
|||||||
@@ -24,9 +24,7 @@ use ui::notification::Notification;
|
|||||||
use ui::{h_flex, v_flex, IconName, Root, Sizable, WindowExtension};
|
use ui::{h_flex, v_flex, IconName, Root, Sizable, WindowExtension};
|
||||||
|
|
||||||
use crate::dialogs::settings;
|
use crate::dialogs::settings;
|
||||||
use crate::panels::{
|
use crate::panels::{backup, contact_list, greeter, messaging_relays, profile, relay_list};
|
||||||
backup, contact_list, encryption_key, greeter, messaging_relays, profile, relay_list,
|
|
||||||
};
|
|
||||||
use crate::sidebar;
|
use crate::sidebar;
|
||||||
|
|
||||||
const ENC_MSG: &str =
|
const ENC_MSG: &str =
|
||||||
@@ -52,7 +50,6 @@ enum Command {
|
|||||||
|
|
||||||
ShowRelayList,
|
ShowRelayList,
|
||||||
ShowMessaging,
|
ShowMessaging,
|
||||||
ShowEncryption,
|
|
||||||
ShowProfile,
|
ShowProfile,
|
||||||
ShowSettings,
|
ShowSettings,
|
||||||
ShowBackup,
|
ShowBackup,
|
||||||
@@ -249,21 +246,6 @@ impl Workspace {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
Command::ShowEncryption => {
|
|
||||||
let nostr = NostrRegistry::global(cx);
|
|
||||||
let signer = nostr.read(cx).signer();
|
|
||||||
|
|
||||||
if let Some(public_key) = signer.public_key() {
|
|
||||||
self.dock.update(cx, |this, cx| {
|
|
||||||
this.add_panel(
|
|
||||||
Arc::new(encryption_key::init(public_key, window, cx)),
|
|
||||||
DockPlacement::Right,
|
|
||||||
window,
|
|
||||||
cx,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Command::ShowMessaging => {
|
Command::ShowMessaging => {
|
||||||
self.dock.update(cx, |this, cx| {
|
self.dock.update(cx, |this, cx| {
|
||||||
this.add_panel(
|
this.add_panel(
|
||||||
@@ -544,8 +526,7 @@ impl Workspace {
|
|||||||
let state = device.read(cx).state();
|
let state = device.read(cx).state();
|
||||||
|
|
||||||
this.min_w(px(260.))
|
this.min_w(px(260.))
|
||||||
.item(
|
.item(PopupMenuItem::element(move |_window, _cx| {
|
||||||
PopupMenuItem::element(move |_window, _cx| {
|
|
||||||
h_flex()
|
h_flex()
|
||||||
.px_1()
|
.px_1()
|
||||||
.w_full()
|
.w_full()
|
||||||
@@ -561,11 +542,7 @@ impl Workspace {
|
|||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
.child(SharedString::from(state.to_string()))
|
.child(SharedString::from(state.to_string()))
|
||||||
})
|
}))
|
||||||
.on_click(|_ev, window, cx| {
|
|
||||||
window.dispatch_action(Box::new(Command::ShowEncryption), cx);
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.separator()
|
.separator()
|
||||||
.menu_with_icon(
|
.menu_with_icon(
|
||||||
"Reload",
|
"Reload",
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ publish.workspace = true
|
|||||||
common = { path = "../common" }
|
common = { path = "../common" }
|
||||||
state = { path = "../state" }
|
state = { path = "../state" }
|
||||||
person = { path = "../person" }
|
person = { path = "../person" }
|
||||||
|
ui = { path = "../ui" }
|
||||||
|
theme = { path = "../theme" }
|
||||||
|
|
||||||
gpui.workspace = true
|
gpui.workspace = true
|
||||||
nostr-sdk.workspace = true
|
nostr-sdk.workspace = true
|
||||||
|
|||||||
@@ -1,16 +1,28 @@
|
|||||||
|
use std::cell::Cell;
|
||||||
use std::collections::{HashMap, HashSet};
|
use std::collections::{HashMap, HashSet};
|
||||||
|
use std::rc::Rc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use anyhow::{anyhow, Context as AnyhowContext, Error};
|
use anyhow::{anyhow, Context as AnyhowContext, Error};
|
||||||
use gpui::{App, AppContext, Context, Entity, Global, Subscription, Task, Window};
|
use gpui::{
|
||||||
|
div, App, AppContext, Context, Entity, Global, IntoElement, ParentElement, SharedString,
|
||||||
|
Styled, Subscription, Task, Window,
|
||||||
|
};
|
||||||
use nostr_sdk::prelude::*;
|
use nostr_sdk::prelude::*;
|
||||||
use person::PersonRegistry;
|
use person::PersonRegistry;
|
||||||
use smallvec::{smallvec, SmallVec};
|
use smallvec::{smallvec, SmallVec};
|
||||||
use state::{
|
use state::{
|
||||||
app_name, Announcement, DeviceState, NostrRegistry, RelayState, DEVICE_GIFTWRAP, TIMEOUT,
|
app_name, Announcement, DeviceState, NostrRegistry, RelayState, DEVICE_GIFTWRAP, TIMEOUT,
|
||||||
};
|
};
|
||||||
|
use theme::ActiveTheme;
|
||||||
|
use ui::avatar::Avatar;
|
||||||
|
use ui::button::{Button, ButtonVariants};
|
||||||
|
use ui::notification::Notification;
|
||||||
|
use ui::{h_flex, v_flex, Disableable, IconName, Sizable, WindowExtension};
|
||||||
|
|
||||||
const IDENTIFIER: &str = "coop:device";
|
const IDENTIFIER: &str = "coop:device";
|
||||||
|
const MSG: &str = "You've requested an encryption key from another device. \
|
||||||
|
Approve to allow Coop to share with it.";
|
||||||
|
|
||||||
pub fn init(window: &mut Window, cx: &mut App) {
|
pub fn init(window: &mut Window, cx: &mut App) {
|
||||||
DeviceRegistry::set_global(cx.new(|cx| DeviceRegistry::new(window, cx)), cx);
|
DeviceRegistry::set_global(cx.new(|cx| DeviceRegistry::new(window, cx)), cx);
|
||||||
@@ -25,9 +37,6 @@ impl Global for GlobalDeviceRegistry {}
|
|||||||
/// 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
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct DeviceRegistry {
|
pub struct DeviceRegistry {
|
||||||
/// Request for encryption key from other devices
|
|
||||||
pub requests: Vec<Event>,
|
|
||||||
|
|
||||||
/// Device state
|
/// Device state
|
||||||
state: DeviceState,
|
state: DeviceState,
|
||||||
|
|
||||||
@@ -64,19 +73,18 @@ impl DeviceRegistry {
|
|||||||
);
|
);
|
||||||
|
|
||||||
// Run at the end of current cycle
|
// Run at the end of current cycle
|
||||||
cx.defer_in(window, |this, _window, cx| {
|
cx.defer_in(window, |this, window, cx| {
|
||||||
this.handle_notifications(cx);
|
this.handle_notifications(window, cx);
|
||||||
});
|
});
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
requests: vec![],
|
|
||||||
state: DeviceState::default(),
|
state: DeviceState::default(),
|
||||||
tasks: vec![],
|
tasks: vec![],
|
||||||
_subscriptions: subscriptions,
|
_subscriptions: subscriptions,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_notifications(&mut self, cx: &mut Context<Self>) {
|
fn handle_notifications(&mut self, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
let (tx, rx) = flume::bounded::<Event>(100);
|
let (tx, rx) = flume::bounded::<Event>(100);
|
||||||
@@ -117,17 +125,19 @@ impl DeviceRegistry {
|
|||||||
|
|
||||||
self.tasks.push(
|
self.tasks.push(
|
||||||
// Update GPUI states
|
// Update GPUI states
|
||||||
cx.spawn(async move |this, cx| {
|
cx.spawn_in(window, async move |this, cx| {
|
||||||
while let Ok(event) = rx.recv_async().await {
|
while let Ok(event) = rx.recv_async().await {
|
||||||
match event.kind {
|
match event.kind {
|
||||||
|
// New request event
|
||||||
Kind::Custom(4454) => {
|
Kind::Custom(4454) => {
|
||||||
this.update(cx, |this, cx| {
|
this.update_in(cx, |this, window, cx| {
|
||||||
this.add_request(event, cx);
|
this.ask_for_approval(event, window, cx);
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
|
// New response event
|
||||||
Kind::Custom(4455) => {
|
Kind::Custom(4455) => {
|
||||||
this.update(cx, |this, cx| {
|
this.update(cx, |this, cx| {
|
||||||
this.parse_response(event, cx);
|
this.extract_encryption(event, cx);
|
||||||
})?;
|
})?;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@@ -174,27 +184,9 @@ impl DeviceRegistry {
|
|||||||
/// Reset the device state
|
/// Reset the device state
|
||||||
fn reset(&mut self, cx: &mut Context<Self>) {
|
fn reset(&mut self, cx: &mut Context<Self>) {
|
||||||
self.state = DeviceState::Idle;
|
self.state = DeviceState::Idle;
|
||||||
self.requests.clear();
|
|
||||||
cx.notify();
|
cx.notify();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a request for device keys
|
|
||||||
fn add_request(&mut self, request: Event, cx: &mut Context<Self>) {
|
|
||||||
self.requests.push(request);
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Remove a request for device keys
|
|
||||||
pub fn remove_request(&mut self, id: &EventId, cx: &mut Context<Self>) {
|
|
||||||
self.requests.retain(|r| r.id != *id);
|
|
||||||
cx.notify();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Check if there are any pending requests
|
|
||||||
pub fn has_requests(&self) -> bool {
|
|
||||||
!self.requests.is_empty()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get all messages for encryption keys
|
/// Get all messages for encryption keys
|
||||||
fn get_messages(&mut self, cx: &mut Context<Self>) {
|
fn get_messages(&mut self, cx: &mut Context<Self>) {
|
||||||
let task = self.subscribe_to_giftwrap_events(cx);
|
let task = self.subscribe_to_giftwrap_events(cx);
|
||||||
@@ -522,30 +514,29 @@ impl DeviceRegistry {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
cx.spawn(async move |this, cx| {
|
self.tasks.push(cx.spawn(async move |this, cx| {
|
||||||
match task.await {
|
match task.await {
|
||||||
Ok(Some(keys)) => {
|
Ok(Some(keys)) => {
|
||||||
this.update(cx, |this, cx| {
|
this.update(cx, |this, cx| {
|
||||||
this.set_signer(keys, cx);
|
this.set_signer(keys, cx);
|
||||||
})
|
})?;
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
this.update(cx, |this, cx| {
|
this.update(cx, |this, cx| {
|
||||||
this.set_state(DeviceState::Requesting, cx);
|
this.set_state(DeviceState::Requesting, cx);
|
||||||
})
|
})?;
|
||||||
.ok();
|
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
log::error!("Failed to request the encryption key: {e}");
|
log::error!("Failed to request the encryption key: {e}");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})
|
|
||||||
.detach();
|
Ok(())
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Parse the response event for device keys from other devices
|
/// Parse the response event for device keys from other devices
|
||||||
fn parse_response(&mut self, event: Event, cx: &mut Context<Self>) {
|
fn extract_encryption(&mut self, event: Event, cx: &mut Context<Self>) {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let app_keys = nostr.read(cx).app_keys().clone();
|
let app_keys = nostr.read(cx).app_keys().clone();
|
||||||
|
|
||||||
@@ -579,7 +570,7 @@ impl DeviceRegistry {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Approve requests for device keys from other devices
|
/// Approve requests for device keys from other devices
|
||||||
pub fn approve(&self, event: &Event, cx: &App) -> Task<Result<(), Error>> {
|
fn approve(&mut self, event: &Event, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
let nostr = NostrRegistry::global(cx);
|
let nostr = NostrRegistry::global(cx);
|
||||||
let client = nostr.read(cx).client();
|
let client = nostr.read(cx).client();
|
||||||
|
|
||||||
@@ -590,8 +581,9 @@ impl DeviceRegistry {
|
|||||||
// Get user's write relays
|
// Get user's write relays
|
||||||
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
let write_relays = nostr.read(cx).write_relays(&public_key, cx);
|
||||||
let event = event.clone();
|
let event = event.clone();
|
||||||
|
let id: SharedString = event.id.to_hex().into();
|
||||||
|
|
||||||
cx.background_spawn(async move {
|
let task: Task<Result<(), Error>> = cx.background_spawn(async move {
|
||||||
let urls = write_relays.await;
|
let urls = write_relays.await;
|
||||||
|
|
||||||
// Get device keys
|
// Get device keys
|
||||||
@@ -613,17 +605,144 @@ impl DeviceRegistry {
|
|||||||
//
|
//
|
||||||
// P tag: the current device's public key
|
// P tag: the current device's public key
|
||||||
// p tag: the requester's public key
|
// p tag: the requester's public key
|
||||||
let event = client
|
let builder = EventBuilder::new(Kind::Custom(4455), payload).tags(vec![
|
||||||
.sign_event_builder(EventBuilder::new(Kind::Custom(4455), payload).tags(vec![
|
|
||||||
Tag::custom(TagKind::custom("P"), vec![keys.public_key()]),
|
Tag::custom(TagKind::custom("P"), vec![keys.public_key()]),
|
||||||
Tag::public_key(target),
|
Tag::public_key(target),
|
||||||
]))
|
]);
|
||||||
.await?;
|
|
||||||
|
// Sign the builder
|
||||||
|
let event = client.sign_event_builder(builder).await?;
|
||||||
|
|
||||||
// Send the response event to the user's relay list
|
// Send the response event to the user's relay list
|
||||||
client.send_event(&event).to(urls).await?;
|
client.send_event(&event).to(urls).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
});
|
||||||
|
|
||||||
|
cx.spawn_in(window, async move |_this, cx| {
|
||||||
|
match task.await {
|
||||||
|
Ok(_) => {
|
||||||
|
cx.update(|window, cx| {
|
||||||
|
window.clear_notification(id, cx);
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
cx.update(|window, cx| {
|
||||||
|
window.push_notification(Notification::error(e.to_string()), cx);
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handle encryption request
|
||||||
|
fn ask_for_approval(&mut self, event: Event, window: &mut Window, cx: &mut Context<Self>) {
|
||||||
|
let notification = self.notification(event, cx);
|
||||||
|
|
||||||
|
cx.spawn_in(window, async move |_this, cx| {
|
||||||
|
cx.update(|window, cx| {
|
||||||
|
window.push_notification(notification, cx);
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
})
|
||||||
|
.detach();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Build a notification for the encryption request.
|
||||||
|
fn notification(&self, event: Event, cx: &Context<Self>) -> Notification {
|
||||||
|
let request = Announcement::from(&event);
|
||||||
|
let persons = PersonRegistry::global(cx);
|
||||||
|
let profile = persons.read(cx).get(&request.public_key(), cx);
|
||||||
|
|
||||||
|
let entity = cx.entity().downgrade();
|
||||||
|
let loading = Rc::new(Cell::new(false));
|
||||||
|
|
||||||
|
Notification::new()
|
||||||
|
.custom_id(SharedString::from(event.id.to_hex()))
|
||||||
|
.autohide(false)
|
||||||
|
.icon(IconName::UserKey)
|
||||||
|
.title(SharedString::from("New request"))
|
||||||
|
.content(move |_window, cx| {
|
||||||
|
v_flex()
|
||||||
|
.gap_2()
|
||||||
|
.text_sm()
|
||||||
|
.child(SharedString::from(MSG))
|
||||||
|
.child(
|
||||||
|
v_flex()
|
||||||
|
.gap_2()
|
||||||
|
.child(
|
||||||
|
v_flex()
|
||||||
|
.gap_1()
|
||||||
|
.text_sm()
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.text_xs()
|
||||||
|
.text_color(cx.theme().text_muted)
|
||||||
|
.child(SharedString::from("Requester:")),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.h_7()
|
||||||
|
.w_full()
|
||||||
|
.px_2()
|
||||||
|
.rounded(cx.theme().radius)
|
||||||
|
.bg(cx.theme().elevated_surface_background)
|
||||||
|
.child(
|
||||||
|
h_flex()
|
||||||
|
.gap_2()
|
||||||
|
.child(Avatar::new(profile.avatar()).xsmall())
|
||||||
|
.child(profile.name()),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
v_flex()
|
||||||
|
.gap_1()
|
||||||
|
.text_sm()
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.text_xs()
|
||||||
|
.text_color(cx.theme().text_muted)
|
||||||
|
.child(SharedString::from("Client:")),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
div()
|
||||||
|
.h_7()
|
||||||
|
.w_full()
|
||||||
|
.px_2()
|
||||||
|
.rounded(cx.theme().radius)
|
||||||
|
.bg(cx.theme().elevated_surface_background)
|
||||||
|
.child(request.client_name()),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.into_any_element()
|
||||||
|
})
|
||||||
|
.action(move |_window, _cx| {
|
||||||
|
let view = entity.clone();
|
||||||
|
let event = event.clone();
|
||||||
|
|
||||||
|
Button::new("approve")
|
||||||
|
.label("Approve")
|
||||||
|
.small()
|
||||||
|
.primary()
|
||||||
|
.loading(loading.get())
|
||||||
|
.disabled(loading.get())
|
||||||
|
.on_click({
|
||||||
|
let loading = Rc::clone(&loading);
|
||||||
|
move |_ev, window, cx| {
|
||||||
|
// Set loading state to true
|
||||||
|
loading.set(true);
|
||||||
|
// Process to approve the request
|
||||||
|
view.update(cx, |this, cx| {
|
||||||
|
this.approve(&event, window, cx);
|
||||||
|
})
|
||||||
|
.ok();
|
||||||
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -343,8 +343,8 @@ impl RelayAuth {
|
|||||||
.px_1p5()
|
.px_1p5()
|
||||||
.rounded_sm()
|
.rounded_sm()
|
||||||
.text_xs()
|
.text_xs()
|
||||||
.bg(cx.theme().warning_background)
|
.bg(cx.theme().elevated_surface_background)
|
||||||
.text_color(cx.theme().warning_foreground)
|
.text_color(cx.theme().text_accent)
|
||||||
.child(url.clone()),
|
.child(url.clone()),
|
||||||
)
|
)
|
||||||
.into_any_element()
|
.into_any_element()
|
||||||
@@ -361,11 +361,9 @@ impl RelayAuth {
|
|||||||
.disabled(loading.get())
|
.disabled(loading.get())
|
||||||
.on_click({
|
.on_click({
|
||||||
let loading = Rc::clone(&loading);
|
let loading = Rc::clone(&loading);
|
||||||
|
|
||||||
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
|
||||||
view.update(cx, |this, cx| {
|
view.update(cx, |this, cx| {
|
||||||
this.response(&req, window, cx);
|
this.response(&req, window, cx);
|
||||||
|
|||||||
Reference in New Issue
Block a user