add nip4e settings

This commit is contained in:
2026-06-04 16:06:16 +07:00
parent 1f04a824d7
commit ce8f431aaa
4 changed files with 60 additions and 26 deletions

View File

@@ -11,6 +11,8 @@ use gpui::{
};
use nostr_sdk::prelude::*;
use person::PersonRegistry;
use settings::AppSettings;
use smallvec::{SmallVec, smallvec};
use state::{Announcement, CLIENT_NAME, NostrRegistry, StateEvent, TIMEOUT};
use theme::ActiveTheme;
use ui::avatar::Avatar;
@@ -19,8 +21,6 @@ use ui::notification::{Notification, NotificationKind};
use ui::{Disableable, Sizable, StyledExt, WindowExtension, h_flex, v_flex};
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) {
DeviceRegistry::set_global(cx.new(|cx| DeviceRegistry::new(window, cx)), cx);
@@ -67,7 +67,7 @@ pub struct DeviceRegistry {
tasks: Vec<Task<Result<(), Error>>>,
/// Event subscription
_subscription: Option<Subscription>,
_subscriptions: SmallVec<[Subscription; 2]>,
}
impl EventEmitter<DeviceEvent> for DeviceRegistry {}
@@ -86,14 +86,28 @@ impl DeviceRegistry {
/// Create a new device registry instance
fn new(window: &mut Window, cx: &mut Context<Self>) -> Self {
let nostr = NostrRegistry::global(cx);
let settings = AppSettings::global(cx);
let is_nip4e_enabled = settings.read(cx).is_nip4e_enabled();
// Subscribe to nostr state events
let subscription = cx.subscribe_in(&nostr, window, |this, _e, event, _window, cx| {
if event == &StateEvent::SignerSet {
this.set_initializing(true, cx);
this.get_announcement(cx);
};
});
let mut subscriptions = smallvec![];
subscriptions.push(
// Subscribe to nostr state events
cx.observe(&settings, move |this, settings, cx| {
if settings.read(cx).is_nip4e_enabled() {
this.get_announcement(cx);
};
}),
);
subscriptions.push(
// Subscribe to nostr state events
cx.subscribe(&nostr, move |this, _e, event, cx| {
if event == &StateEvent::SignerSet && is_nip4e_enabled {
this.get_announcement(cx);
};
}),
);
cx.defer_in(window, |this, window, cx| {
this.handle_notifications(window, cx);
@@ -103,7 +117,7 @@ impl DeviceRegistry {
initializing: true,
pending_request: false,
tasks: vec![],
_subscription: Some(subscription),
_subscriptions: subscriptions,
}
}
@@ -219,6 +233,9 @@ impl DeviceRegistry {
let nostr = NostrRegistry::global(cx);
let client = nostr.read(cx).client();
// Show the loading state
self.set_initializing(true, cx);
let task: Task<Result<Event, Error>> = cx.background_spawn(async move {
let signer = client.signer().context("Signer not found")?;
let public_key = signer.get_public_key().await?;
@@ -586,6 +603,9 @@ impl DeviceRegistry {
/// Build a notification for the encryption request.
fn notification(&self, event: Event, cx: &Context<Self>) -> Notification {
const MSG: &str = "You've requested an encryption key from another device. \
Approve to allow Coop to share with it.";
let request = Announcement::from(&event);
let persons = PersonRegistry::global(cx);
let profile = persons.read(cx).get(&request.public_key(), cx);