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

@@ -10,6 +10,7 @@ state = { path = "../state" }
person = { path = "../person" }
ui = { path = "../ui" }
theme = { path = "../theme" }
settings = { path = "../settings" }
gpui.workspace = true
nostr-sdk.workspace = true

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);

View File

@@ -40,6 +40,7 @@ setting_accessors! {
pub theme_mode: ThemeMode,
pub hide_avatar: bool,
pub screening: bool,
pub encryption_key: bool,
pub auth_mode: AuthMode,
pub trusted_relays: HashSet<RelayUrl>,
pub room_configs: HashMap<u64, RoomConfig>,
@@ -137,6 +138,11 @@ pub struct Settings {
/// Enable screening for unknown chat requests
pub screening: bool,
/// Enable decoupling encryption key
///
/// NIP-4e
pub encryption_key: bool,
/// Authentication mode
pub auth_mode: AuthMode,
@@ -157,6 +163,7 @@ impl Default for Settings {
theme_mode: ThemeMode::default(),
hide_avatar: false,
screening: true,
encryption_key: false,
auth_mode: AuthMode::default(),
trusted_relays: HashSet::default(),
room_configs: HashMap::default(),
@@ -301,6 +308,11 @@ impl AppSettings {
}
}
/// Check if decoupling encryption key is enabled
pub fn is_nip4e_enabled(&self) -> bool {
self.values.encryption_key
}
/// Check if the given relay is already authenticated
pub fn trusted_relay(&self, url: &RelayUrl, _cx: &App) -> bool {
self.values.trusted_relays.iter().any(|relay| {