]> Untitled Git - lemmy.git/commitdiff
after 30 days post deletion, replace comment.content and post.body with 'Permanently...
authorVijay Ramesh <vramesh@demandbase.com>
Tue, 20 Jun 2023 06:17:54 +0000 (23:17 -0700)
committerVijay Ramesh <vramesh@demandbase.com>
Wed, 21 Jun 2023 19:46:56 +0000 (12:46 -0700)
crates/db_schema/src/impls/comment.rs
crates/db_schema/src/impls/post.rs
crates/db_schema/src/utils.rs
src/scheduled_tasks.rs

index 46045cd10afb68ac834c9e734b6c8ca99abd7d99..8aa8c1793c6726ab3ca4c1fbe3a6101d984e1526 100644 (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()),
       ))
index a3309428cfd406d347d44be3d93a870c13146df3..7f59d29ecbc17e41bb4ba48ad96a04e8bcc6134a 100644 (file)
@@ -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<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(DELETED_REPLACEMENT_URL),
+        body.eq(DELETED_REPLACEMENT_TEXT),
         deleted.eq(true),
         updated.eq(naive_now()),
       ))
index 98d3952abc1c5d2b96ad9de1eaf8ad73cfd9f663..9954f623b60a27c23e7837c2c36cf414663d6437 100644 (file)
@@ -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<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())
index 9fb1ba70229f4d537e1867ce14ab80668448ee74..891dca365da2daf8011998f081eee720ae5b7e49 100644 (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 ...");