Implement automatic pruning of empty profiles

This commit is contained in:
silverpill 2023-02-05 20:28:40 +00:00
parent 6002e58425
commit ad1a658806
5 changed files with 39 additions and 2 deletions

View file

@ -14,7 +14,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
- Added `limits` parameter group to configuration. - Added `limits` parameter group to configuration.
- Made file size limit adjustable with `limits.media.file_size_limit` configuration option. - Made file size limit adjustable with `limits.media.file_size_limit` configuration option.
- Added `limits.posts.character_limit` configuration parameter (replaces `post_character_limit`). - Added `limits.posts.character_limit` configuration parameter (replaces `post_character_limit`).
- Implemented automatic pruning of remote posts (disabled by default). - Implemented automatic pruning of remote posts and empty profiles (disabled by default).
### Changed ### Changed

View file

@ -38,6 +38,7 @@ registration:
# Data retention parameters # Data retention parameters
#retention: #retention:
# extraneous_posts: 50 # extraneous_posts: 50
# empty_profiles: 150
# List of blocked domains # List of blocked domains
#blocked_instances: [] #blocked_instances: []

View file

@ -3,6 +3,7 @@ use serde::Deserialize;
#[derive(Clone, Deserialize)] #[derive(Clone, Deserialize)]
pub struct RetentionConfig { pub struct RetentionConfig {
pub extraneous_posts: Option<u32>, pub extraneous_posts: Option<u32>,
pub empty_profiles: Option<u32>,
} }
#[allow(clippy::derivable_impls)] #[allow(clippy::derivable_impls)]
@ -10,6 +11,7 @@ impl Default for RetentionConfig {
fn default() -> Self { fn default() -> Self {
Self { Self {
extraneous_posts: None, extraneous_posts: None,
empty_profiles: None,
} }
} }
} }

View file

@ -18,7 +18,14 @@ use crate::ethereum::subscriptions::{
update_expired_subscriptions, update_expired_subscriptions,
}; };
use crate::monero::subscriptions::check_monero_subscriptions; use crate::monero::subscriptions::check_monero_subscriptions;
use crate::models::posts::queries::{delete_post, find_extraneous_posts}; use crate::models::{
posts::queries::{delete_post, find_extraneous_posts},
profiles::queries::{
delete_profile,
find_empty_profiles,
get_profile_by_id,
},
};
use crate::utils::datetime::days_before_now; use crate::utils::datetime::days_before_now;
pub async fn nft_monitor( pub async fn nft_monitor(
@ -135,3 +142,22 @@ pub async fn delete_extraneous_posts(
}; };
Ok(()) Ok(())
} }
pub async fn delete_empty_profiles(
config: &Config,
db_pool: &DbPool,
) -> Result<(), Error> {
let db_client = &mut **get_database_client(db_pool).await?;
let updated_before = match config.retention.empty_profiles {
Some(days) => days_before_now(days),
None => return Ok(()), // not configured
};
let profiles = find_empty_profiles(db_client, &updated_before).await?;
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;
log::info!("deleted profile {}", profile.acct);
};
Ok(())
}

View file

@ -19,6 +19,7 @@ enum PeriodicTask {
IncomingActivityQueueExecutor, IncomingActivityQueueExecutor,
OutgoingActivityQueueExecutor, OutgoingActivityQueueExecutor,
DeleteExtraneousPosts, DeleteExtraneousPosts,
DeleteEmptyProfiles,
} }
impl PeriodicTask { impl PeriodicTask {
@ -32,6 +33,7 @@ impl PeriodicTask {
Self::IncomingActivityQueueExecutor => 5, Self::IncomingActivityQueueExecutor => 5,
Self::OutgoingActivityQueueExecutor => 5, Self::OutgoingActivityQueueExecutor => 5,
Self::DeleteExtraneousPosts => 3600, Self::DeleteExtraneousPosts => 3600,
Self::DeleteEmptyProfiles => 3600,
} }
} }
@ -63,6 +65,9 @@ pub fn run(
if config.retention.extraneous_posts.is_some() { if config.retention.extraneous_posts.is_some() {
scheduler_state.insert(PeriodicTask::DeleteExtraneousPosts, None); scheduler_state.insert(PeriodicTask::DeleteExtraneousPosts, None);
}; };
if config.retention.empty_profiles.is_some() {
scheduler_state.insert(PeriodicTask::DeleteEmptyProfiles, None);
};
let mut interval = tokio::time::interval(Duration::from_secs(5)); let mut interval = tokio::time::interval(Duration::from_secs(5));
let mut token_waitlist_map: HashMap<Uuid, DateTime<Utc>> = HashMap::new(); let mut token_waitlist_map: HashMap<Uuid, DateTime<Utc>> = HashMap::new();
@ -103,6 +108,9 @@ pub fn run(
PeriodicTask::DeleteExtraneousPosts => { PeriodicTask::DeleteExtraneousPosts => {
delete_extraneous_posts(&config, &db_pool).await delete_extraneous_posts(&config, &db_pool).await
}, },
PeriodicTask::DeleteEmptyProfiles => {
delete_empty_profiles(&config, &db_pool).await
},
}; };
task_result.unwrap_or_else(|err| { task_result.unwrap_or_else(|err| {
log::error!("{:?}: {}", task, err); log::error!("{:?}: {}", task, err);