From: Vijay Ramesh Date: Tue, 20 Jun 2023 06:17:54 +0000 (-0700) Subject: after 30 days post deletion, replace comment.content and post.body with 'Permanently... X-Git-Url: http://these/git/%7B%60https:/%7B%60css/themes/%22https:/nerdica.net/%7BpictrsAvatarThumbnail%28this.props.site.site.icon%29%7D?a=commitdiff_plain;h=4db65c191c8b9198913c12f192fb641bbb709c08;p=lemmy.git after 30 days post deletion, replace comment.content and post.body with 'Permanently Deleted' --- diff --git a/crates/db_schema/src/impls/comment.rs b/crates/db_schema/src/impls/comment.rs index 46045cd1..8aa8c179 100644 --- a/crates/db_schema/src/impls/comment.rs +++ b/crates/db_schema/src/impls/comment.rs @@ -11,7 +11,7 @@ use crate::{ CommentUpdateForm, }, traits::{Crud, Likeable, Saveable}, - utils::{get_conn, naive_now, DbPool}, + utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT}, }; use diesel::{ dsl::{insert_into, sql_query}, @@ -29,9 +29,10 @@ impl Comment { for_creator_id: PersonId, ) -> Result, Error> { let conn = &mut get_conn(pool).await?; + diesel::update(comment.filter(creator_id.eq(for_creator_id))) .set(( - content.eq("*Permananently Deleted*"), + content.eq(DELETED_REPLACEMENT_TEXT), deleted.eq(true), updated.eq(naive_now()), )) diff --git a/crates/db_schema/src/impls/post.rs b/crates/db_schema/src/impls/post.rs index a3309428..7f59d29e 100644 --- a/crates/db_schema/src/impls/post.rs +++ b/crates/db_schema/src/impls/post.rs @@ -27,7 +27,14 @@ use crate::{ PostUpdateForm, }, traits::{Crud, Likeable, Readable, Saveable}, - utils::{get_conn, naive_now, DbPool, FETCH_LIMIT_MAX}, + utils::{ + get_conn, + naive_now, + DbPool, + DELETED_REPLACEMENT_TEXT, + DELETED_REPLACEMENT_URL, + FETCH_LIMIT_MAX, + }, }; use ::url::Url; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; @@ -111,14 +118,11 @@ impl Post { ) -> Result, Error> { let conn = &mut get_conn(pool).await?; - let perma_deleted = "*Permananently Deleted*"; - let perma_deleted_url = "https://deleted.com"; - diesel::update(post.filter(creator_id.eq(for_creator_id))) .set(( - name.eq(perma_deleted), - url.eq(perma_deleted_url), - body.eq(perma_deleted), + name.eq(DELETED_REPLACEMENT_TEXT), + url.eq(DELETED_REPLACEMENT_URL), + body.eq(DELETED_REPLACEMENT_TEXT), deleted.eq(true), updated.eq(naive_now()), )) diff --git a/crates/db_schema/src/utils.rs b/crates/db_schema/src/utils.rs index 98d3952a..9954f623 100644 --- a/crates/db_schema/src/utils.rs +++ b/crates/db_schema/src/utils.rs @@ -222,6 +222,9 @@ pub mod functions { sql_function!(fn lower(x: Text) -> Text); } +pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*"; +pub const DELETED_REPLACEMENT_URL: &str = "https://join-lemmy.org/"; + impl ToSql for DbUrl { fn to_sql(&self, out: &mut Output) -> diesel::serialize::Result { >::to_sql(&self.0.to_string(), &mut out.reborrow()) diff --git a/src/scheduled_tasks.rs b/src/scheduled_tasks.rs index 9fb1ba70..891dca36 100644 --- a/src/scheduled_tasks.rs +++ b/src/scheduled_tasks.rs @@ -3,6 +3,7 @@ use diesel::{ dsl::{now, IntervalDsl}, Connection, ExpressionMethods, + NullableExpressionMethods, QueryDsl, }; // Import week days and WeekDay @@ -11,15 +12,17 @@ use lemmy_api_common::context::LemmyContext; use lemmy_db_schema::{ schema::{ activity, + comment, comment_aggregates, community_aggregates, community_person_ban, instance, person, + post, post_aggregates, }, source::instance::{Instance, InstanceForm}, - utils::{functions::hot_rank, naive_now}, + utils::{functions::hot_rank, naive_now, DELETED_REPLACEMENT_TEXT}, }; use lemmy_routes::nodeinfo::NodeInfo; use lemmy_utils::{error::LemmyError, REQWEST_TIMEOUT}; @@ -66,6 +69,13 @@ pub fn setup( context_1.settings_updated_channel().remove_older_than(hour); }); + // Overwrite deleted & removed posts and comments every day + let url = db_url.clone(); + scheduler.every(CTimeUnits::days(1)).run(move || { + let mut conn = PgConnection::establish(&url).expect("could not establish connection"); + overwrite_deleted_posts_and_comments(&mut conn); + }); + // Update the Instance Software scheduler.every(CTimeUnits::days(1)).run(move || { let mut conn = PgConnection::establish(&db_url).expect("could not establish connection"); @@ -86,6 +96,7 @@ fn startup_jobs(db_url: &str) { update_hot_ranks(&mut conn, false); update_banned_when_expired(&mut conn); clear_old_activities(&mut conn); + overwrite_deleted_posts_and_comments(&mut conn); } /// Update the hot_rank columns for the aggregates tables @@ -166,6 +177,48 @@ fn clear_old_activities(conn: &mut PgConnection) { } } +/// overwrite posts and comments 30d after deletion +fn overwrite_deleted_posts_and_comments(conn: &mut PgConnection) { + info!("Overwriting deleted posts..."); + match diesel::update( + post::table + .filter(post::deleted.eq(true)) + .filter(post::updated.lt(now.nullable() - 1.months())) + .filter(post::body.ne(DELETED_REPLACEMENT_TEXT)), + ) + .set(( + post::body.eq(DELETED_REPLACEMENT_TEXT), + post::name.eq(DELETED_REPLACEMENT_TEXT), + )) + .execute(conn) + { + Ok(_) => { + info!("Done."); + } + Err(e) => { + error!("Failed to overwrite deleted posts: {}", e) + } + } + + info!("Overwriting deleted comments..."); + match diesel::update( + comment::table + .filter(comment::deleted.eq(true)) + .filter(comment::updated.lt(now.nullable() - 1.months())) + .filter(comment::content.ne(DELETED_REPLACEMENT_TEXT)), + ) + .set(comment::content.eq(DELETED_REPLACEMENT_TEXT)) + .execute(conn) + { + Ok(_) => { + info!("Done."); + } + Err(e) => { + error!("Failed to overwrite deleted comments: {}", e) + } + } +} + /// Re-calculate the site and community active counts every 12 hours fn active_counts(conn: &mut PgConnection) { info!("Updating active site and community aggregates ...");