diff options
author | Cara Salter <cara@devcara.com> | 2022-05-03 13:57:09 -0400 |
---|---|---|
committer | Cara Salter <cara@devcara.com> | 2022-05-03 13:57:09 -0400 |
commit | 5999e6a803a7b848acf054918fec9ee5024d5697 (patch) | |
tree | eae1f3986c41f7d7c1a956e4606a8120c6032e05 /src/models.rs | |
parent | 8f4277c55a2079edf1c9a69383c353e1cb9ef55c (diff) | |
download | glitch-ng-5999e6a803a7b848acf054918fec9ee5024d5697.tar.gz glitch-ng-5999e6a803a7b848acf054918fec9ee5024d5697.zip |
filter: Initial message filter implementation
Also a custom error type, tracing_subscriber, and unsafe impls
Diffstat (limited to 'src/models.rs')
-rw-r--r-- | src/models.rs | 73 |
1 files changed, 73 insertions, 0 deletions
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<Self, Self::Err> { + 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<FilterAction> for AutocompleteChoice<String> { + 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() + } + } + } +} |