nip4e: fix message encryption and sync (#57)

Reviewed-on: #57
This commit was merged in pull request #57.
This commit is contained in:
2026-10-04 06:33:32 +00:00
parent e14b815404
commit 2fc1358530
17 changed files with 520 additions and 246 deletions
+10 -32
View File
@@ -1,4 +1,3 @@
use gpui::SharedString;
use nostr_sdk::prelude::*;
/// Announcement
@@ -6,50 +5,29 @@ use nostr_sdk::prelude::*;
pub struct Announcement {
/// The public key of the device that created this announcement.
public_key: PublicKey,
/// The name of the device that created this announcement.
client_name: Option<String>,
}
impl From<&Event> for Announcement {
fn from(val: &Event) -> Self {
let public_key = val
impl Announcement {
/// Parse an announcement from an event, returning `None` when the `n`
/// tag is missing or malformed so callers never mistake the author's
/// identity key for an encryption key.
pub fn from_event(event: &Event) -> Option<Self> {
let public_key = event
.tags
.iter()
.find(|tag| tag.kind() == "n")
.and_then(|tag| tag.content())
.and_then(|c| PublicKey::parse(c).ok())
.unwrap_or(val.pubkey);
.and_then(|c| PublicKey::parse(c).ok())?;
let client_name = val
.tags
.iter()
.find(|tag| tag.kind() == "client")
.and_then(|tag| tag.content())
.map(|c| c.to_string());
Self::new(public_key, client_name)
Some(Self { public_key })
}
}
impl Announcement {
pub fn new(public_key: PublicKey, client_name: Option<String>) -> Self {
Self {
public_key,
client_name,
}
pub fn new(public_key: PublicKey) -> Self {
Self { public_key }
}
/// Returns the public key of the device that created this announcement.
pub fn public_key(&self) -> PublicKey {
self.public_key
}
/// Returns the client name of the device that created this announcement.
pub fn client_name(&self) -> SharedString {
self.client_name
.as_ref()
.map(SharedString::from)
.unwrap_or(SharedString::from("Unknown"))
}
}