filter: add kind blacklist filter

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin 2024-07-09 11:05:21 -07:00
parent f08e805673
commit c85f9cbc3d
5 changed files with 74 additions and 5 deletions

View file

@ -14,15 +14,22 @@ You can add any new filter you want by implementing the `NoteFilter` trait and r
The `pipeline` config specifies the order in which filters are run. When the first `reject` or `shadowReject` action is hit, then the pipeline stops and returns the rejection error. The `pipeline` config specifies the order in which filters are run. When the first `reject` or `shadowReject` action is hit, then the pipeline stops and returns the rejection error.
```toml ```toml
pipeline = ["protected_events", "whitelist", "ratelimit"] pipeline = ["protected_events", "kinds", "whitelist", "ratelimit"]
[filters.ratelimit] [filters.ratelimit]
posts_per_minute = 8 posts_per_minute = 8
whitelist = ["127.0.0.1"] whitelist = ["127.0.0.1"]
[filters.whitelist] [filters.whitelist]
pubkeys = ["32e1827635450ebb3c5a7d12c1f8e7b2b514439ac10a67eef3d9fd9c5c68e245"] pubkeys = ["16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93"]
ips = ["127.0.0.1", "127.0.0.2"] ips = ["127.0.0.1"]
[filters.kinds]
kinds = [30065, 1064]
[filters.kinds.messages]
30065 = "blocked: files on nostr is dumb"
1064 = "blocked: files on nostr is dumb"
[filters.protected_events] [filters.protected_events]
``` ```
@ -57,6 +64,27 @@ The whitelist filter only allows notes to pass if it matches a particular pubkey
Either criteria can match Either criteria can match
### Kinds
* name: `kinds`
A filter that blacklists certain kinds
- `kinds`: a list of kind integers to block
- `kinds.messages` *optional*: a map of kinds to message to deliver when the kind is blocked
Example:
```toml
[filters.kinds]
kinds = [30065, 1064]
[filters.kinds.messages]
30065 = "blocked: files on nostr is dumb"
1064 = "blocked: files on nostr is dumb"
```
### Protected Events ### Protected Events
See [nip70] See [nip70]

View file

@ -1,5 +1,5 @@
pipeline = ["protected_events", "whitelist", "ratelimit"] pipeline = ["protected_events", "kinds", "whitelist", "ratelimit"]
[filters.ratelimit] [filters.ratelimit]
posts_per_minute = 8 posts_per_minute = 8
@ -9,4 +9,13 @@ whitelist = ["127.0.0.1"]
pubkeys = ["16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93"] pubkeys = ["16c21558762108afc34e4ff19e4ed51d9a48f79e0c34531efc423d21ab435e93"]
ips = ["127.0.0.1"] ips = ["127.0.0.1"]
[filters.kinds]
kinds = [30065, 1064, 34550, 4550]
[filters.kinds.messages]
30065 = "blocked: files on nostr is dumb"
1064 = "blocked: files on nostr is dumb"
34550 = "blocked: please use a dedicated relay for moderated communities"
4550 = "blocked: please use a dedicated relay for moderated communities"
[filters.protected_events] [filters.protected_events]

29
src/filters/kinds.rs Normal file
View file

@ -0,0 +1,29 @@
use crate::{Action, InputMessage, NoteFilter, OutputMessage};
use serde::Deserialize;
use std::collections::HashMap;
#[derive(Deserialize, Default)]
pub struct Kinds {
kinds: Vec<i64>,
messages: Option<HashMap<String, String>>,
}
impl NoteFilter for Kinds {
fn filter_note(&mut self, input: &InputMessage) -> OutputMessage {
let kind = input.event.kind;
if self.kinds.contains(&kind) {
let msg = self
.messages
.as_ref()
.and_then(|msgs| msgs.get(&kind.to_string()).cloned())
.unwrap_or_else(|| "blocked: note kind is not allowed here".to_string());
OutputMessage::new(input.event.id.clone(), Action::Reject, Some(msg))
} else {
OutputMessage::new(input.event.id.clone(), Action::Accept, None)
}
}
fn name(&self) -> &'static str {
"kinds"
}
}

View file

@ -1,7 +1,9 @@
mod kinds;
mod protected_events; mod protected_events;
mod ratelimit; mod ratelimit;
mod whitelist; mod whitelist;
pub use kinds::Kinds;
pub use protected_events::ProtectedEvents; pub use protected_events::ProtectedEvents;
pub use ratelimit::RateLimit; pub use ratelimit::RateLimit;
pub use whitelist::Whitelist; pub use whitelist::Whitelist;

View file

@ -1,4 +1,4 @@
use noteguard::filters::{ProtectedEvents, RateLimit, Whitelist}; use noteguard::filters::{Kinds, ProtectedEvents, RateLimit, Whitelist};
use noteguard::{Action, InputMessage, NoteFilter, OutputMessage}; use noteguard::{Action, InputMessage, NoteFilter, OutputMessage};
use serde::de::DeserializeOwned; use serde::de::DeserializeOwned;
use serde::Deserialize; use serde::Deserialize;
@ -43,6 +43,7 @@ impl Noteguard {
self.register_filter::<RateLimit>(); self.register_filter::<RateLimit>();
self.register_filter::<Whitelist>(); self.register_filter::<Whitelist>();
self.register_filter::<ProtectedEvents>(); self.register_filter::<ProtectedEvents>();
self.register_filter::<Kinds>();
} }
/// Run the loaded filters. You must call `load_config` before calling this, otherwise /// Run the loaded filters. You must call `load_config` before calling this, otherwise