From 5999e6a803a7b848acf054918fec9ee5024d5697 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Tue, 3 May 2022 13:57:09 -0400 Subject: filter: Initial message filter implementation Also a custom error type, tracing_subscriber, and unsafe impls --- src/models.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'src/models.rs') diff --git a/src/models.rs b/src/models.rs index 09d5d7c..b8dd5b1 100644 --- a/src/models.rs +++ b/src/models.rs @@ -1,3 +1,9 @@ +use std::str::FromStr; + +use poise::AutocompleteChoice; + +use crate::errors::Error; + /** * Describes a Reaction Role as it appears in SQL */ @@ -17,3 +23,70 @@ pub struct ReactionRole { /// The ID of the role to be toggled by the menu option pub role_id: String, } + + +#[derive(Debug, Clone, sqlx::Type)] +#[sqlx(type_name="filter_action", rename_all="lowercase")] +pub enum FilterAction { + Review, + Delete +} +/** + * Describes a message filter as it appears in SQL + */ + +#[derive(Debug, Clone, sqlx::FromRow)] +pub struct MessageFilter { + /// Primary Key + pub id: i32, + + /// Pattern + pub pattern: String, + + pub action: FilterAction, + + pub guild_id: String, +} + +impl FromStr for FilterAction { + type Err = Error; + fn from_str(s: &str) -> Result { + match s { + "review" => Ok(FilterAction::Review), + "delete" => Ok(FilterAction::Delete), + _ => { + return Err(Error::Unknown("invalid option".to_string())); + } + } + } +} + +impl ToString for FilterAction { + fn to_string(&self) -> String { + match self { + FilterAction::Review => "Review".to_string(), + FilterAction::Delete => "Delete".to_string(), + } + } +} + +impl ToString for MessageFilter { + fn to_string(&self) -> String { + format!("`{}`: {}", self.pattern, self.action.to_string()) + } +} + +impl From for AutocompleteChoice { + fn from(m: FilterAction) -> Self { + match m { + FilterAction::Review => AutocompleteChoice { + name: "Flag the message for review and send it to a channel for moderators".to_string(), + value: "review".to_string() + }, + FilterAction::Delete => AutocompleteChoice { + name: "Deletes the message and logs the deletion in a channel for moderators".to_string(), + value: "delete".to_string() + } + } + } +} -- cgit v1.2.3