From 15bb8c23c1ed00861c87261ac531789e0f074145 Mon Sep 17 00:00:00 2001 From: Mike Dilger Date: Fri, 22 Mar 2024 07:40:16 +1300 Subject: [PATCH] Deal with event/pubkey approvals in event screens (in and out) --- chorus-bin/src/nostr.rs | 47 +++++++++++++++++++++++++++++++++++++++++ chorus-lib/src/error.rs | 10 +++++++++ 2 files changed, 57 insertions(+) diff --git a/chorus-bin/src/nostr.rs b/chorus-bin/src/nostr.rs index 97bc44a..e265bc2 100644 --- a/chorus-bin/src/nostr.rs +++ b/chorus-bin/src/nostr.rs @@ -215,6 +215,18 @@ impl WebSocketService { PERSONAL_MSG.to_owned(), ) } + ChorusError::BannedEvent => NostrReply::Ok( + id, + false, + NostrReplyPrefix::Blocked, + "Event has been banned".to_string(), + ), + ChorusError::BannedUser => NostrReply::Ok( + id, + false, + NostrReplyPrefix::Blocked, + "Author has been banned".to_string(), + ), _ => NostrReply::Ok(id, false, NostrReplyPrefix::Error, format!("{}", e)), }; self.websocket.send(Message::text(reply.as_json())).await?; @@ -393,6 +405,26 @@ async fn screen_incoming_event( event_flags: EventFlags, authorized_user: bool, ) -> Result { + // Reject if event approval is false + if let Some(false) = GLOBALS + .store + .get() + .unwrap() + .get_event_approval(event.id())? + { + return Err(ChorusError::BannedEvent.into()); + } + + // Reject if pubkey approval is false + if let Some(false) = GLOBALS + .store + .get() + .unwrap() + .get_pubkey_approval(event.pubkey())? + { + return Err(ChorusError::BannedUser.into()); + } + // If the event has a '-' tag, require the user to be AUTHed and match // the event author for mut tag in event.tags()?.iter() { @@ -481,6 +513,21 @@ pub fn screen_outgoing_event( return true; } + // Allow if event is explicitly approved + if let Ok(Some(true)) = GLOBALS.store.get().unwrap().get_event_approval(event.id()) { + return true; + } + + // Allow if author is explicitly approved + if let Ok(Some(true)) = GLOBALS + .store + .get() + .unwrap() + .get_pubkey_approval(event.pubkey()) + { + return true; + } + // Do not allow the rest false } diff --git a/chorus-lib/src/error.rs b/chorus-lib/src/error.rs index 42ab25c..c9b4cde 100644 --- a/chorus-lib/src/error.rs +++ b/chorus-lib/src/error.rs @@ -34,6 +34,12 @@ pub enum ChorusError { // Bad hex input BadHexInput, + // Event is banned + BannedEvent, + + // User is banned + BannedUser, + // Output buffer too small BufferTooSmall, @@ -147,6 +153,8 @@ impl std::fmt::Display for ChorusError { ChorusError::AuthRequired => write!(f, "AUTH required"), ChorusError::BadEventId => write!(f, "Bad event id, does not match hash"), ChorusError::BadHexInput => write!(f, "Bad hex input"), + ChorusError::BannedEvent => write!(f, "Event is banned"), + ChorusError::BannedUser => write!(f, "User is banned"), ChorusError::BufferTooSmall => write!(f, "Output buffer too small"), ChorusError::ChannelRecv(e) => write!(f, "{e}"), ChorusError::ChannelSend(e) => write!(f, "{e}"), @@ -227,6 +235,8 @@ impl ChorusError { ChorusError::AuthRequired => 0.0, ChorusError::BadEventId => 0.1, ChorusError::BadHexInput => 0.4, + ChorusError::BannedEvent => 0.1, + ChorusError::BannedUser => 0.2, ChorusError::BufferTooSmall => 0.0, ChorusError::ChannelRecv(_) => 0.0, ChorusError::ChannelSend(_) => 0.0,