diff --git a/src/lib.rs b/src/lib.rs index c90416d..82f8196 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -832,3 +832,36 @@ pub fn dump_pubkey_approvals(store: &Store) -> Result, Error } Ok(output) } + +// Marks a pubkey as moderator for nip-86 +pub fn mark_pubkey_as_moderator(pubkey: Pubkey, _: Vec) -> Result<(), Error> { + GLOBALS + .config + .write() + .moderator_hex_keys + .push(pubkey.as_hex_string()?); + GLOBALS.config.write().moderator_keys.push(pubkey); + + Ok(()) +} + +// Clears a pubkey as moderator for nip-86 +pub fn clear_pubkey_as_moderator(pubkey: Pubkey, _: Vec) -> Result<(), Error> { + GLOBALS + .config + .write() + .moderator_hex_keys + .retain(|x| *x != pubkey.as_hex_string().unwrap()); + GLOBALS + .config + .write() + .moderator_keys + .retain(|x| *x != pubkey); + + Ok(()) +} + +// Fetch a pubkey access to nip-86 moderation +pub fn get_pubkey_as_moderator(pubkey: Pubkey, _: Vec) -> bool { + GLOBALS.config.read().moderator_keys.contains(&pubkey) +} diff --git a/src/web/management/auth.rs b/src/web/management/auth.rs index a15c091..382aba4 100644 --- a/src/web/management/auth.rs +++ b/src/web/management/auth.rs @@ -56,12 +56,7 @@ pub async fn check_auth(request: Request) -> Result { } // Nostr event must be signed by a moderator - if !GLOBALS - .config - .read() - .moderator_keys - .contains(&event.pubkey()) - { + if crate::get_pubkey_as_moderator(event.pubkey(), vec![]) { return s_err("Authorization failed as user is not a moderator"); } diff --git a/src/web/management/mod.rs b/src/web/management/mod.rs index 25dff20..05e5ded 100644 --- a/src/web/management/mod.rs +++ b/src/web/management/mod.rs @@ -97,6 +97,8 @@ pub fn handle_inner(command: Value) -> Result, Error> { match &*method { "supportedmethods" => Ok(Some(json!({ "result": [ + "grantadmin", + "revokeadmin", "allowevent", "allowpubkey", "banevent", @@ -106,8 +108,7 @@ pub fn handle_inner(command: Value) -> Result, Error> { "listbannedevents", "listbannedpubkeys", "supportedmethods", - "numconnections", - "uptime" + "stats" ] }))), @@ -211,24 +212,27 @@ pub fn handle_inner(command: Value) -> Result, Error> { "unblockip" => Err(ChorusError::NotImplemented.into()), "listblockedips" => Err(ChorusError::NotImplemented.into()), - // Config - "changerelayname" => Err(ChorusError::NotImplemented.into()), - "changerelaydescription" => Err(ChorusError::NotImplemented.into()), - "changerelayicon" => Err(ChorusError::NotImplemented.into()), - // System - "numconnections" => { - let num = &GLOBALS.num_connections; - Ok(Some(json!({ - "result": num, - }))) + "stats" => Ok(Some(json!({ + "result": { + "uptime": GLOBALS.start_time.elapsed().as_secs(), + "num_connections": &GLOBALS.num_connections, + "bytes_received": &GLOBALS.bytes_inbound, + "bytes_sent": &GLOBALS.bytes_outbound, + }, + }))), + + // Moderation + "grantadmin" => { + let pubkey = get_pubkey_param(obj)?; + crate::mark_pubkey_as_moderator(pubkey, vec![])?; + Ok(None) } - "uptime" => { - let uptime_in_secs = GLOBALS.start_time.elapsed().as_secs(); - Ok(Some(json!({ - "result": uptime_in_secs, - }))) + "revokeadmin" => { + let pubkey = get_pubkey_param(obj)?; + crate::clear_pubkey_as_moderator(pubkey, vec![])?; + Ok(None) } _ => Err(ChorusError::NotImplemented.into()),