From 97145efad9d645a772cd490704ff5bedf8036112 Mon Sep 17 00:00:00 2001 From: silverpill Date: Sat, 21 Jan 2023 21:25:51 +0000 Subject: [PATCH] Make delete-emoji command accept emoji name and hostname instead of ID --- CHANGELOG.md | 4 ++++ docs/mitractl.md | 4 ++-- src/cli.rs | 11 +++++++++-- src/models/emojis/helpers.rs | 18 ++++++++++++++++++ src/models/emojis/mod.rs | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 src/models/emojis/helpers.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 37edec0..e016dd4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/). - Added `import-emoji` command. - Added support for emoji shortcodes. +### Changed + +- Make `delete-emoji` command accept emoji name and hostname instead of ID. + ### Security - Validate emoji name before saving. diff --git a/docs/mitractl.md b/docs/mitractl.md index f210be4..e2e49e5 100644 --- a/docs/mitractl.md +++ b/docs/mitractl.md @@ -53,7 +53,7 @@ mitractl delete-post 55a3005f-f293-4168-ab70-6ab09a879679 Delete custom emoji: ```shell -mitractl delete-emoji 55a3005f-f293-4168-ab70-6ab09a879679 +mitractl delete-emoji emoji_name example.org ``` Remove remote posts and media older than 30 days: @@ -77,7 +77,7 @@ mitractl delete-empty-profiles 100 Import custom emoji from another instance: ```shell -mitractl import-emoji emojiname example.org +mitractl import-emoji emoji_name example.org ``` Generate ethereum address: diff --git a/src/cli.rs b/src/cli.rs index 2e5d86d..b1c9bcc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -19,6 +19,7 @@ use crate::ethereum::{ use crate::models::{ attachments::queries::delete_unused_attachments, cleanup::find_orphaned_files, + emojis::helpers::get_emoji_by_name, emojis::queries::{ create_emoji, delete_emoji, @@ -267,7 +268,8 @@ impl DeletePost { /// Delete custom emoji #[derive(Parser)] pub struct DeleteEmoji { - id: Uuid, + emoji_name: String, + hostname: Option, } impl DeleteEmoji { @@ -276,7 +278,12 @@ impl DeleteEmoji { config: &Config, db_client: &impl DatabaseClient, ) -> Result<(), Error> { - let deletion_queue = delete_emoji(db_client, &self.id).await?; + let emoji = get_emoji_by_name( + db_client, + &self.emoji_name, + self.hostname.as_deref(), + ).await?; + let deletion_queue = delete_emoji(db_client, &emoji.id).await?; deletion_queue.process(config).await; println!("emoji deleted"); Ok(()) diff --git a/src/models/emojis/helpers.rs b/src/models/emojis/helpers.rs new file mode 100644 index 0000000..82480cf --- /dev/null +++ b/src/models/emojis/helpers.rs @@ -0,0 +1,18 @@ +use crate::database::{DatabaseClient, DatabaseError}; +use super::types::DbEmoji; +use super::queries::{ + get_local_emoji_by_name, + get_emoji_by_name_and_hostname, +}; + +pub async fn get_emoji_by_name( + db_client: &impl DatabaseClient, + emoji_name: &str, + maybe_hostname: Option<&str>, +) -> Result { + if let Some(hostname) = maybe_hostname { + get_emoji_by_name_and_hostname(db_client, emoji_name, hostname).await + } else { + get_local_emoji_by_name(db_client, emoji_name).await + } +} diff --git a/src/models/emojis/mod.rs b/src/models/emojis/mod.rs index a35947b..344a039 100644 --- a/src/models/emojis/mod.rs +++ b/src/models/emojis/mod.rs @@ -1,3 +1,4 @@ +pub mod helpers; pub mod queries; pub mod types; pub mod validators;