Merge branch 'vijaykramesh_clear_deleted_posts_comments'

This commit is contained in:
Felix Ableitner 2023-06-26 10:45:49 +02:00
commit 206789af67
4 changed files with 63 additions and 10 deletions

View File

@ -11,7 +11,7 @@ use crate::{
CommentUpdateForm, CommentUpdateForm,
}, },
traits::{Crud, Likeable, Saveable}, traits::{Crud, Likeable, Saveable},
utils::{get_conn, naive_now, DbPool}, utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT},
}; };
use diesel::{ use diesel::{
dsl::{insert_into, sql_query}, dsl::{insert_into, sql_query},
@ -29,9 +29,10 @@ impl Comment {
for_creator_id: PersonId, for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> { ) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?; let conn = &mut get_conn(pool).await?;
diesel::update(comment.filter(creator_id.eq(for_creator_id))) diesel::update(comment.filter(creator_id.eq(for_creator_id)))
.set(( .set((
content.eq("*Permananently Deleted*"), content.eq(DELETED_REPLACEMENT_TEXT),
deleted.eq(true), deleted.eq(true),
updated.eq(naive_now()), updated.eq(naive_now()),
)) ))

View File

@ -27,7 +27,7 @@ use crate::{
PostUpdateForm, PostUpdateForm,
}, },
traits::{Crud, Likeable, Readable, Saveable}, traits::{Crud, Likeable, Readable, Saveable},
utils::{get_conn, naive_now, DbPool, FETCH_LIMIT_MAX}, utils::{get_conn, naive_now, DbPool, DELETED_REPLACEMENT_TEXT, FETCH_LIMIT_MAX},
}; };
use ::url::Url; use ::url::Url;
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods}; use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
@ -111,14 +111,11 @@ impl Post {
) -> Result<Vec<Self>, Error> { ) -> Result<Vec<Self>, Error> {
let conn = &mut get_conn(pool).await?; 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))) diesel::update(post.filter(creator_id.eq(for_creator_id)))
.set(( .set((
name.eq(perma_deleted), name.eq(DELETED_REPLACEMENT_TEXT),
url.eq(perma_deleted_url), url.eq(Option::<&str>::None),
body.eq(perma_deleted), body.eq(DELETED_REPLACEMENT_TEXT),
deleted.eq(true), deleted.eq(true),
updated.eq(naive_now()), updated.eq(naive_now()),
)) ))

View File

@ -278,6 +278,8 @@ pub mod functions {
sql_function!(fn lower(x: Text) -> Text); sql_function!(fn lower(x: Text) -> Text);
} }
pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*";
impl ToSql<Text, Pg> for DbUrl { impl ToSql<Text, Pg> for DbUrl {
fn to_sql(&self, out: &mut Output<Pg>) -> diesel::serialize::Result { fn to_sql(&self, out: &mut Output<Pg>) -> diesel::serialize::Result {
<std::string::String as ToSql<Text, Pg>>::to_sql(&self.0.to_string(), &mut out.reborrow()) <std::string::String as ToSql<Text, Pg>>::to_sql(&self.0.to_string(), &mut out.reborrow())

View File

@ -3,6 +3,7 @@ use diesel::{
dsl::{now, IntervalDsl}, dsl::{now, IntervalDsl},
Connection, Connection,
ExpressionMethods, ExpressionMethods,
NullableExpressionMethods,
QueryDsl, QueryDsl,
}; };
// Import week days and WeekDay // Import week days and WeekDay
@ -11,15 +12,17 @@ use lemmy_api_common::context::LemmyContext;
use lemmy_db_schema::{ use lemmy_db_schema::{
schema::{ schema::{
activity, activity,
comment,
comment_aggregates, comment_aggregates,
community_aggregates, community_aggregates,
community_person_ban, community_person_ban,
instance, instance,
person, person,
post,
post_aggregates, post_aggregates,
}, },
source::instance::{Instance, InstanceForm}, 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_routes::nodeinfo::NodeInfo;
use lemmy_utils::{error::LemmyError, REQWEST_TIMEOUT}; use lemmy_utils::{error::LemmyError, REQWEST_TIMEOUT};
@ -66,6 +69,13 @@ pub fn setup(
context_1.settings_updated_channel().remove_older_than(hour); 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 // Update the Instance Software
scheduler.every(CTimeUnits::days(1)).run(move || { scheduler.every(CTimeUnits::days(1)).run(move || {
let mut conn = PgConnection::establish(&db_url).expect("could not establish connection"); 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_hot_ranks(&mut conn, false);
update_banned_when_expired(&mut conn); update_banned_when_expired(&mut conn);
clear_old_activities(&mut conn); clear_old_activities(&mut conn);
overwrite_deleted_posts_and_comments(&mut conn);
} }
/// Update the hot_rank columns for the aggregates tables /// 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 /// Re-calculate the site and community active counts every 12 hours
fn active_counts(conn: &mut PgConnection) { fn active_counts(conn: &mut PgConnection) {
info!("Updating active site and community aggregates ..."); info!("Updating active site and community aggregates ...");