nip-86: implement updates.

This commit is contained in:
Kay 2025-02-08 13:17:29 +00:00
parent b06497e53b
commit b087ad47ec
3 changed files with 57 additions and 24 deletions

View File

@ -27,6 +27,7 @@ use hyper_tungstenite::tungstenite;
use hyper_tungstenite::{HyperWebsocket, WebSocketStream};
use hyper_util::rt::TokioIo;
use neg_storage::NegentropyStorageVector;
use parking_lot::RwLock;
use pocket_db::Store;
use pocket_types::{Id, OwnedFilter, Pubkey};
use speedy::{Readable, Writable};
@ -832,3 +833,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

@ -5,7 +5,7 @@ use http::header::AUTHORIZATION;
use http_body_util::BodyExt;
use hyper::body::Incoming;
use hyper::Request;
use pocket_types::Event;
use pocket_types::{Event, Pubkey};
use secp256k1::hashes::{sha256, Hash};
use serde_json::Value;
@ -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()),