Merge e0eb0d6933e63e640c979ebac743ab188aa9ccc7 into b06497e53be91ca141e5da487609fc1fce8dbf15

This commit is contained in:
k. 2025-02-08 16:05:53 +00:00 committed by GitHub
commit e57609da72
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 55 additions and 23 deletions

View File

@ -832,3 +832,36 @@ pub fn dump_pubkey_approvals(store: &Store) -> Result<Vec<(Pubkey, bool)>, Error
}
Ok(output)
}
// Marks a pubkey as moderator for nip-86
pub fn mark_pubkey_as_moderator(pubkey: Pubkey, _: Vec<String>) -> 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<String>) -> 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<String>) -> bool {
GLOBALS.config.read().moderator_keys.contains(&pubkey)
}

View File

@ -56,12 +56,7 @@ pub async fn check_auth(request: Request<Incoming>) -> Result<Value, Error> {
}
// 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");
}

View File

@ -97,6 +97,8 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
match &*method {
"supportedmethods" => Ok(Some(json!({
"result": [
"grantadmin",
"revokeadmin",
"allowevent",
"allowpubkey",
"banevent",
@ -106,8 +108,7 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
"listbannedevents",
"listbannedpubkeys",
"supportedmethods",
"numconnections",
"uptime"
"stats"
]
}))),
@ -211,24 +212,27 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, 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()),