Move media deletion helper to media module

This commit is contained in:
silverpill 2023-03-17 22:48:18 +00:00
parent b56e11e81d
commit f037a4d58c
6 changed files with 40 additions and 36 deletions

View file

@ -14,7 +14,7 @@ use mitra::ethereum::{
sync::save_current_block_number,
utils::key_to_ethereum_address,
};
use mitra::media::remove_files;
use mitra::media::{remove_files, remove_media};
use mitra::models::{
attachments::queries::delete_unused_attachments,
cleanup::find_orphaned_files,
@ -252,7 +252,7 @@ impl DeleteProfile {
maybe_delete_person = Some(activity);
};
let deletion_queue = delete_profile(db_client, &profile.id).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
// Send Delete(Person) activities
if let Some(activity) = maybe_delete_person {
activity.enqueue(db_client).await?;
@ -287,7 +287,7 @@ impl DeletePost {
maybe_delete_note = Some(activity);
};
let deletion_queue = delete_post(db_client, &post.id).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
// Send Delete(Note) activity
if let Some(activity) = maybe_delete_note {
activity.enqueue(db_client).await?;
@ -316,7 +316,7 @@ impl DeleteEmoji {
self.hostname.as_deref(),
).await?;
let deletion_queue = delete_emoji(db_client, &emoji.id).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
println!("emoji deleted");
Ok(())
}
@ -338,7 +338,7 @@ impl DeleteExtraneousPosts {
let posts = find_extraneous_posts(db_client, &updated_before).await?;
for post_id in posts {
let deletion_queue = delete_post(db_client, &post_id).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
println!("post {} deleted", post_id);
};
Ok(())
@ -362,7 +362,7 @@ impl DeleteUnusedAttachments {
db_client,
&created_before,
).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
println!("unused attachments deleted");
Ok(())
}
@ -412,7 +412,7 @@ impl DeleteEmptyProfiles {
for profile_id in profiles {
let profile = get_profile_by_id(db_client, &profile_id).await?;
let deletion_queue = delete_profile(db_client, &profile.id).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
println!("profile {} deleted", profile.acct);
};
Ok(())

View file

@ -9,6 +9,7 @@ use crate::activitypub::{
};
use crate::database::{DatabaseClient, DatabaseError};
use crate::errors::ValidationError;
use crate::media::remove_media;
use crate::models::{
posts::queries::{
delete_post,
@ -49,7 +50,7 @@ pub async fn handle_delete(
let deletion_queue = delete_profile(db_client, &profile.id).await?;
let config = config.clone();
tokio::spawn(async move {
deletion_queue.process(&config).await;
remove_media(&config, deletion_queue).await;
});
log::info!("deleted profile {}", profile.acct);
return Ok(Some(PERSON));
@ -73,7 +74,7 @@ pub async fn handle_delete(
let deletion_queue = delete_post(db_client, &post.id).await?;
let config = config.clone();
tokio::spawn(async move {
deletion_queue.process(&config).await;
remove_media(&config, deletion_queue).await;
});
Ok(Some(NOTE))
}

View file

@ -17,6 +17,7 @@ use crate::ethereum::{
update_expired_subscriptions,
},
};
use crate::media::remove_media;
use crate::monero::subscriptions::check_monero_subscriptions;
use crate::models::{
posts::queries::{delete_post, find_extraneous_posts},
@ -138,7 +139,7 @@ pub async fn delete_extraneous_posts(
let posts = find_extraneous_posts(db_client, &updated_before).await?;
for post_id in posts {
let deletion_queue = delete_post(db_client, &post_id).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
log::info!("deleted post {}", post_id);
};
Ok(())
@ -157,7 +158,7 @@ pub async fn delete_empty_profiles(
for profile_id in profiles {
let profile = get_profile_by_id(db_client, &profile_id).await?;
let deletion_queue = delete_profile(db_client, &profile.id).await?;
deletion_queue.process(config).await;
remove_media(config, deletion_queue).await;
log::info!("deleted profile {}", profile.acct);
};
Ok(())

View file

@ -34,6 +34,7 @@ use crate::mastodon_api::{
errors::MastodonError,
oauth::auth::get_current_user,
};
use crate::media::remove_media;
use crate::models::{
posts::helpers::{can_create_post, can_view_post},
posts::queries::{
@ -286,7 +287,7 @@ async fn delete_status(
).await?;
let deletion_queue = delete_post(db_client, &status_id).await?;
tokio::spawn(async move {
deletion_queue.process(&config).await;
remove_media(&config, deletion_queue).await;
});
delete_note.enqueue(db_client).await?;

View file

@ -4,8 +4,12 @@ use std::path::Path;
use sha2::{Digest, Sha256};
use mitra_config::Config;
use mitra_utils::files::{get_media_type_extension, write_file};
use crate::ipfs::store as ipfs_store;
use crate::models::cleanup::DeletionQueue;
pub const SUPPORTED_MEDIA_TYPES: [&str; 11] = [
"audio/mpeg",
"audio/ogg",
@ -62,6 +66,27 @@ pub fn remove_files(files: Vec<String>, from_dir: &Path) -> () {
};
}
pub async fn remove_media(
config: &Config,
queue: DeletionQueue,
) -> () {
remove_files(queue.files, &config.media_dir());
if !queue.ipfs_objects.is_empty() {
match &config.ipfs_api_url {
Some(ipfs_api_url) => {
ipfs_store::remove(ipfs_api_url, queue.ipfs_objects).await
.unwrap_or_else(|err| log::error!("{}", err));
},
None => {
log::error!(
"can not remove objects because IPFS API URL is not set: {:?}",
queue.ipfs_objects,
);
},
}
}
}
#[cfg(test)]
mod tests {
use mitra_utils::files::sniff_media_type;

View file

@ -1,34 +1,10 @@
use mitra_config::Config;
use crate::database::{DatabaseClient, DatabaseError};
use crate::ipfs::store as ipfs_store;
use crate::media::remove_files;
pub struct DeletionQueue {
pub files: Vec<String>,
pub ipfs_objects: Vec<String>,
}
impl DeletionQueue {
pub async fn process(self, config: &Config) -> () {
remove_files(self.files, &config.media_dir());
if !self.ipfs_objects.is_empty() {
match &config.ipfs_api_url {
Some(ipfs_api_url) => {
ipfs_store::remove(ipfs_api_url, self.ipfs_objects).await
.unwrap_or_else(|err| log::error!("{}", err));
},
None => {
log::error!(
"can not remove objects because IPFS API URL is not set: {:?}",
self.ipfs_objects,
);
},
}
}
}
}
pub async fn find_orphaned_files(
db_client: &impl DatabaseClient,
files: Vec<String>,