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,
},
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<Vec<Self>, 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()),
))

View File

@ -27,7 +27,7 @@ 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, FETCH_LIMIT_MAX},
};
use ::url::Url;
use diesel::{dsl::insert_into, result::Error, ExpressionMethods, QueryDsl, TextExpressionMethods};
@ -111,14 +111,11 @@ impl Post {
) -> Result<Vec<Self>, 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(Option::<&str>::None),
body.eq(DELETED_REPLACEMENT_TEXT),
deleted.eq(true),
updated.eq(naive_now()),
))

View File

@ -278,6 +278,8 @@ pub mod functions {
sql_function!(fn lower(x: Text) -> Text);
}
pub const DELETED_REPLACEMENT_TEXT: &str = "*Permanently Deleted*";
impl ToSql<Text, Pg> for DbUrl {
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())

View File

@ -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 ...");