diff --git a/src/globals.rs b/src/globals.rs index c19c1a68..2979f972 100644 --- a/src/globals.rs +++ b/src/globals.rs @@ -1,5 +1,5 @@ use crate::comms::BusMessage; -use crate::db::DbPerson; +use crate::db::{DbPerson, DbPersonRelay, DbRelay}; use crate::error::Error; use crate::event_related::EventRelated; use nostr_proto::{Event, EventKind, Id, Metadata, PublicKey, PublicKeyHex, Tag, Unixtime}; @@ -278,3 +278,52 @@ async fn followed_pubkeys() -> Vec { .map(|p| p.pubkey.clone()) .collect() } + +#[allow(dead_code)] +pub async fn follow_key_and_relay(pubkey: String, relay: String) -> Result { + let pubkeyhex = PublicKeyHex(pubkey.clone()); + + // Create or update them + let person = match DbPerson::fetch_one(pubkeyhex.clone()) + .await + .map_err(|e| format!("{}", e))? + { + Some(mut person) => { + person.followed = 1; + DbPerson::update(person.clone()) + .await + .map_err(|e| format!("{}", e))?; + person + } + None => { + let mut person = DbPerson::new(pubkeyhex.clone()); + person.followed = 1; + DbPerson::insert(person.clone()) + .await + .map_err(|e| format!("{}", e))?; + person + } + }; + + // Insert (or ignore) this relay + DbRelay::insert(DbRelay::new(relay.clone())) + .await + .map_err(|e| format!("{}", e))?; + + // Insert (or ignore) this person's relay + DbPersonRelay::insert(DbPersonRelay { + person: pubkey, + relay, + recommended: 0, + last_fetched: None, + }) + .await + .map_err(|e| format!("{}", e))?; + + // Tell the overlord to update the minion to watch for their events + // possibly starting a new minion if necessary. + // FIXME TODO + + // Reply to javascript with the person which will be set in the store + Ok(person) +}