noteguard

the nostr relay spam guardian
git clone git://jb55.com/noteguard
Log | Files | Refs | README | LICENSE

kinds.rs (910B)


      1 use crate::{Action, InputMessage, NoteFilter, OutputMessage};
      2 use serde::Deserialize;
      3 use std::collections::HashMap;
      4 
      5 #[derive(Deserialize, Default)]
      6 pub struct Kinds {
      7     kinds: Vec<i64>,
      8     messages: Option<HashMap<String, String>>,
      9 }
     10 
     11 impl NoteFilter for Kinds {
     12     fn filter_note(&mut self, input: &InputMessage) -> OutputMessage {
     13         let kind = input.event.kind;
     14         if self.kinds.contains(&kind) {
     15             let msg = self
     16                 .messages
     17                 .as_ref()
     18                 .and_then(|msgs| msgs.get(&kind.to_string()).cloned())
     19                 .unwrap_or_else(|| "blocked: note kind is not allowed here".to_string());
     20             OutputMessage::new(input.event.id.clone(), Action::Reject, Some(msg))
     21         } else {
     22             OutputMessage::new(input.event.id.clone(), Action::Accept, None)
     23         }
     24     }
     25 
     26     fn name(&self) -> &'static str {
     27         "kinds"
     28     }
     29 }