Overlord commands for adding/removing a bookmark

This commit is contained in:
Mike Dilger 2024-07-01 10:46:26 +12:00
parent 4b58713f51
commit a26f53a967
2 changed files with 63 additions and 2 deletions

View File

@ -4,8 +4,8 @@ use crate::nip46::{Approval, ParsedCommand};
use crate::people::PersonList;
use crate::relay::Relay;
use nostr_types::{
Event, EventAddr, Id, IdHex, Metadata, MilliSatoshi, Profile, PublicKey, RelayUrl, Tag,
UncheckedUrl, Unixtime,
Event, EventAddr, EventReference, Id, IdHex, Metadata, MilliSatoshi, Profile, PublicKey,
RelayUrl, Tag, UncheckedUrl, Unixtime,
};
use std::fmt;
use std::hash::{Hash, Hasher};
@ -34,6 +34,14 @@ pub enum ToOverlordMessage {
/// pass 'true' as the second parameter for a permanent approval
AuthDeclined(RelayUrl, bool),
/// Calls [bookmark_add](crate::Overlord::bookmark_add)
/// Adds a bookmark, possibly privately, and publishes new bookmarks list
BookmarkAdd(EventReference, bool),
/// Calls [bookmark_rm](crate::Overlord::bookmark_rm)
/// Removess a bookmark, and publishes new bookmarks list
BookmarkRm(EventReference),
/// Calls [change_passphrase](crate::Overlord::change_passphrase)
ChangePassphrase { old: String, new: String },

View File

@ -621,6 +621,12 @@ impl Overlord {
ToOverlordMessage::AuthDeclined(relay_url, permanent) => {
self.auth_declined(relay_url, permanent)?;
}
ToOverlordMessage::BookmarkAdd(er, private) => {
self.bookmark_add(er, private).await?;
}
ToOverlordMessage::BookmarkRm(er) => {
self.bookmark_rm(er).await?;
}
ToOverlordMessage::ChangePassphrase { old, new } => {
Self::change_passphrase(old, new).await?;
}
@ -1019,6 +1025,53 @@ impl Overlord {
Ok(())
}
async fn post_bookmarks(&mut self, event: Event) -> Result<(), Error> {
// Process this event locally (ignore any error)
let _ = crate::process::process_new_event(&event, None, None, false, false).await;
let config_relays: Vec<RelayUrl> = Relay::choose_relay_urls(Relay::WRITE, |_| true)?;
for relay_url in config_relays.iter() {
self.engage_minion(
relay_url.to_owned(),
vec![RelayJob {
reason: RelayConnectionReason::PostEvent,
payload: ToMinionPayload {
job_id: rand::random::<u64>(),
detail: ToMinionPayloadDetail::PostEvents(vec![event.clone()]),
},
}],
)
.await?;
}
Ok(())
}
/// Adds or removes a bookmark, and publishes new bookmarks list
pub async fn bookmark_add(&mut self, er: EventReference, private: bool) -> Result<(), Error> {
let added = GLOBALS.bookmarks.write().add(er, private)?;
if added {
let event = GLOBALS.bookmarks.read().into_event()?;
self.post_bookmarks(event).await?;
}
Ok(())
}
/// Adds or removes a bookmark, and publishes new bookmarks list
pub async fn bookmark_rm(&mut self, er: EventReference) -> Result<(), Error> {
let removed = GLOBALS.bookmarks.write().remove(er)?;
if removed {
let event = GLOBALS.bookmarks.read().into_event()?;
self.post_bookmarks(event).await?;
}
Ok(())
}
/// Change the user's passphrase.
pub async fn change_passphrase(mut old: String, mut new: String) -> Result<(), Error> {
GLOBALS.identity.change_passphrase(&old, &new).await?;