Add prune-remote-emojis command

This commit is contained in:
silverpill 2023-03-25 00:12:20 +00:00
parent 76e85a3b7b
commit 37ab3dc456
5 changed files with 61 additions and 0 deletions

View file

@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
- Added `prune-remote-emojis` command.
### Fixed
- Fixed error in emoji update SQL query.

View file

@ -80,6 +80,12 @@ Delete empty remote profiles:
mitractl delete-empty-profiles 100
```
Delete unused remote emojis:
```shell
mitractl prune-remote-emojis
```
Import custom emoji from another instance:
```shell

View file

@ -22,6 +22,7 @@ use mitra::models::{
emojis::queries::{
create_emoji,
delete_emoji,
find_unused_remote_emojis,
get_emoji_by_name_and_hostname,
},
emojis::validators::EMOJI_LOCAL_MAX_SIZE,
@ -82,6 +83,7 @@ pub enum SubCommand {
DeleteUnusedAttachments(DeleteUnusedAttachments),
DeleteOrphanedFiles(DeleteOrphanedFiles),
DeleteEmptyProfiles(DeleteEmptyProfiles),
PruneRemoteEmojis(PruneRemoteEmojis),
ListUnreachableActors(ListUnreachableActors),
ImportEmoji(ImportEmoji),
UpdateCurrentBlock(UpdateCurrentBlock),
@ -425,6 +427,26 @@ impl DeleteEmptyProfiles {
}
}
/// Delete unused remote emojis
#[derive(Parser)]
pub struct PruneRemoteEmojis;
impl PruneRemoteEmojis {
pub async fn execute(
&self,
config: &Config,
db_client: &mut impl DatabaseClient,
) -> Result<(), Error> {
let emojis = find_unused_remote_emojis(db_client).await?;
for emoji_id in emojis {
let deletion_queue = delete_emoji(db_client, &emoji_id).await?;
remove_media(config, deletion_queue).await;
println!("emoji {} deleted", emoji_id);
};
Ok(())
}
}
/// List unreachable actors
#[derive(Parser)]
pub struct ListUnreachableActors {

View file

@ -41,6 +41,7 @@ async fn main() {
SubCommand::DeleteUnusedAttachments(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::DeleteOrphanedFiles(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::DeleteEmptyProfiles(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::PruneRemoteEmojis(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::ListUnreachableActors(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::ImportEmoji(cmd) => cmd.execute(&config, db_client).await.unwrap(),
SubCommand::UpdateCurrentBlock(cmd) => cmd.execute(&config, db_client).await.unwrap(),

View file

@ -188,6 +188,34 @@ pub async fn delete_emoji(
})
}
pub async fn find_unused_remote_emojis(
db_client: &impl DatabaseClient,
) -> Result<Vec<Uuid>, DatabaseError> {
let rows = db_client.query(
"
SELECT emoji.id
FROM emoji
WHERE
emoji.object_id IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM post_emoji
WHERE post_emoji.emoji_id = emoji.id
)
AND NOT EXISTS (
SELECT 1
FROM profile_emoji
WHERE profile_emoji.emoji_id = emoji.id
)
",
&[],
).await?;
let ids: Vec<Uuid> = rows.iter()
.map(|row| row.try_get("id"))
.collect::<Result<_, _>>()?;
Ok(ids)
}
#[cfg(test)]
mod tests {
use serial_test::serial;