Automatically resolve report when post/comment is removed (#3850)
authorNutomic <me@nutomic.com>
Tue, 8 Aug 2023 15:19:55 +0000 (17:19 +0200)
committerGitHub <noreply@github.com>
Tue, 8 Aug 2023 15:19:55 +0000 (11:19 -0400)
* Automatically resolve report when post/comment is removed

* also handle apub removes

crates/api_crud/src/comment/remove.rs
crates/api_crud/src/post/remove.rs
crates/apub/src/activities/deletion/delete.rs
crates/db_schema/src/impls/comment_report.rs
crates/db_schema/src/impls/post_report.rs
crates/db_schema/src/impls/private_message_report.rs
crates/db_schema/src/source/post_report.rs
crates/db_schema/src/traits.rs

index 2bb5c75f2f9b9fdf12de3b6ee9f105254eb0eb2a..c18a8c5b7486f24ce217435cbf8cc732781c5941 100644 (file)
@@ -10,10 +10,11 @@ use lemmy_api_common::{
 use lemmy_db_schema::{
   source::{
     comment::{Comment, CommentUpdateForm},
+    comment_report::CommentReport,
     moderator::{ModRemoveComment, ModRemoveCommentForm},
     post::Post,
   },
-  traits::Crud,
+  traits::{Crud, Reportable},
 };
 use lemmy_db_views::structs::CommentView;
 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
@@ -56,6 +57,9 @@ pub async fn remove_comment(
   .await
   .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
 
+  CommentReport::resolve_all_for_object(&mut context.pool(), comment_id, local_user_view.person.id)
+    .await?;
+
   // Mod tables
   let form = ModRemoveCommentForm {
     mod_person_id: local_user_view.person.id,
index ee100cfdc2288518f1cdce84c2621c1819646ca8..2fbd6ccffcd1811b1fd0aa91b0ba7e563bfec14a 100644 (file)
@@ -11,8 +11,9 @@ use lemmy_db_schema::{
   source::{
     moderator::{ModRemovePost, ModRemovePostForm},
     post::{Post, PostUpdateForm},
+    post_report::PostReport,
   },
-  traits::Crud,
+  traits::{Crud, Reportable},
 };
 use lemmy_utils::error::LemmyError;
 
@@ -54,6 +55,9 @@ pub async fn remove_post(
   )
   .await?;
 
+  PostReport::resolve_all_for_object(&mut context.pool(), post_id, local_user_view.person.id)
+    .await?;
+
   // Mod tables
   let form = ModRemovePostForm {
     mod_person_id: local_user_view.person.id,
index 7a35ec5021cd054688acaf6514c91718224687ef..fa0a44aa38bcf1b7c2099fef7cc9a96435bf867a 100644 (file)
@@ -12,6 +12,7 @@ use lemmy_api_common::{context::LemmyContext, utils::sanitize_html_opt};
 use lemmy_db_schema::{
   source::{
     comment::{Comment, CommentUpdateForm},
+    comment_report::CommentReport,
     community::{Community, CommunityUpdateForm},
     moderator::{
       ModRemoveComment,
@@ -22,8 +23,9 @@ use lemmy_db_schema::{
       ModRemovePostForm,
     },
     post::{Post, PostUpdateForm},
+    post_report::PostReport,
   },
-  traits::Crud,
+  traits::{Crud, Reportable},
 };
 use lemmy_utils::error::{LemmyError, LemmyErrorType};
 use url::Url;
@@ -131,6 +133,7 @@ pub(in crate::activities) async fn receive_remove_action(
       .await?;
     }
     DeletableObjects::Post(post) => {
+      PostReport::resolve_all_for_object(&mut context.pool(), post.id, actor.id).await?;
       let form = ModRemovePostForm {
         mod_person_id: actor.id,
         post_id: post.id,
@@ -149,6 +152,7 @@ pub(in crate::activities) async fn receive_remove_action(
       .await?;
     }
     DeletableObjects::Comment(comment) => {
+      CommentReport::resolve_all_for_object(&mut context.pool(), comment.id, actor.id).await?;
       let form = ModRemoveCommentForm {
         mod_person_id: actor.id,
         comment_id: comment.id,
index ff93915e1c0c7d732f730db733055303088c710f..19c12876f7679d8523060a78d7e1665b2b7587a9 100644 (file)
@@ -1,6 +1,9 @@
 use crate::{
-  newtypes::{CommentReportId, PersonId},
-  schema::comment_report::dsl::{comment_report, resolved, resolver_id, updated},
+  newtypes::{CommentId, CommentReportId, PersonId},
+  schema::comment_report::{
+    comment_id,
+    dsl::{comment_report, resolved, resolver_id, updated},
+  },
   source::comment_report::{CommentReport, CommentReportForm},
   traits::Reportable,
   utils::{get_conn, naive_now, DbPool},
@@ -17,6 +20,7 @@ use diesel_async::RunQueryDsl;
 impl Reportable for CommentReport {
   type Form = CommentReportForm;
   type IdType = CommentReportId;
+  type ObjectIdType = CommentId;
   /// creates a comment report and returns it
   ///
   /// * `conn` - the postgres connection
@@ -53,6 +57,22 @@ impl Reportable for CommentReport {
       .await
   }
 
+  async fn resolve_all_for_object(
+    pool: &mut DbPool<'_>,
+    comment_id_: CommentId,
+    by_resolver_id: PersonId,
+  ) -> Result<usize, Error> {
+    let conn = &mut get_conn(pool).await?;
+    update(comment_report.filter(comment_id.eq(comment_id_)))
+      .set((
+        resolved.eq(true),
+        resolver_id.eq(by_resolver_id),
+        updated.eq(naive_now()),
+      ))
+      .execute(conn)
+      .await
+  }
+
   /// unresolve a comment report
   ///
   /// * `conn` - the postgres connection
index b839d62d5d78e2a0848702e65488ff86474ad6e0..face766dba1381c885c75622d588d88b664146e1 100644 (file)
@@ -1,6 +1,9 @@
 use crate::{
-  newtypes::{PersonId, PostReportId},
-  schema::post_report::dsl::{post_report, resolved, resolver_id, updated},
+  newtypes::{PersonId, PostId, PostReportId},
+  schema::post_report::{
+    dsl::{post_report, resolved, resolver_id, updated},
+    post_id,
+  },
   source::post_report::{PostReport, PostReportForm},
   traits::Reportable,
   utils::{get_conn, naive_now, DbPool},
@@ -17,6 +20,7 @@ use diesel_async::RunQueryDsl;
 impl Reportable for PostReport {
   type Form = PostReportForm;
   type IdType = PostReportId;
+  type ObjectIdType = PostId;
 
   async fn report(pool: &mut DbPool<'_>, post_report_form: &PostReportForm) -> Result<Self, Error> {
     let conn = &mut get_conn(pool).await?;
@@ -42,6 +46,22 @@ impl Reportable for PostReport {
       .await
   }
 
+  async fn resolve_all_for_object(
+    pool: &mut DbPool<'_>,
+    post_id_: PostId,
+    by_resolver_id: PersonId,
+  ) -> Result<usize, Error> {
+    let conn = &mut get_conn(pool).await?;
+    update(post_report.filter(post_id.eq(post_id_)))
+      .set((
+        resolved.eq(true),
+        resolver_id.eq(by_resolver_id),
+        updated.eq(naive_now()),
+      ))
+      .execute(conn)
+      .await
+  }
+
   async fn unresolve(
     pool: &mut DbPool<'_>,
     report_id: Self::IdType,
@@ -58,3 +78,97 @@ impl Reportable for PostReport {
       .await
   }
 }
+
+#[cfg(test)]
+mod tests {
+  #![allow(clippy::unwrap_used)]
+  #![allow(clippy::indexing_slicing)]
+
+  use super::*;
+  use crate::{
+    source::{
+      community::{Community, CommunityInsertForm},
+      instance::Instance,
+      person::{Person, PersonInsertForm},
+      post::{Post, PostInsertForm},
+    },
+    traits::Crud,
+    utils::build_db_pool_for_tests,
+  };
+  use serial_test::serial;
+
+  async fn init(pool: &mut DbPool<'_>) -> (Person, PostReport) {
+    let inserted_instance = Instance::read_or_create(pool, "my_domain.tld".to_string())
+      .await
+      .unwrap();
+    let person_form = PersonInsertForm::builder()
+      .name("jim".into())
+      .public_key("pubkey".to_string())
+      .instance_id(inserted_instance.id)
+      .build();
+    let person = Person::create(pool, &person_form).await.unwrap();
+
+    let community_form = CommunityInsertForm::builder()
+      .name("test community_4".to_string())
+      .title("nada".to_owned())
+      .public_key("pubkey".to_string())
+      .instance_id(inserted_instance.id)
+      .build();
+    let community = Community::create(pool, &community_form).await.unwrap();
+
+    let form = PostInsertForm::builder()
+      .name("A test post".into())
+      .creator_id(person.id)
+      .community_id(community.id)
+      .build();
+    let post = Post::create(pool, &form).await.unwrap();
+
+    let report_form = PostReportForm {
+      post_id: post.id,
+      creator_id: person.id,
+      reason: "my reason".to_string(),
+      ..Default::default()
+    };
+    let report = PostReport::report(pool, &report_form).await.unwrap();
+    (person, report)
+  }
+
+  #[tokio::test]
+  #[serial]
+  async fn test_resolve_post_report() {
+    let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
+
+    let (person, report) = init(pool).await;
+
+    let resolved_count = PostReport::resolve(pool, report.id, person.id)
+      .await
+      .unwrap();
+    assert_eq!(resolved_count, 1);
+
+    let unresolved_count = PostReport::unresolve(pool, report.id, person.id)
+      .await
+      .unwrap();
+    assert_eq!(unresolved_count, 1);
+
+    Person::delete(pool, person.id).await.unwrap();
+    Post::delete(pool, report.post_id).await.unwrap();
+  }
+
+  #[tokio::test]
+  #[serial]
+  async fn test_resolve_all_post_reports() {
+    let pool = &build_db_pool_for_tests().await;
+    let pool = &mut pool.into();
+
+    let (person, report) = init(pool).await;
+
+    let resolved_count = PostReport::resolve_all_for_object(pool, report.post_id, person.id)
+      .await
+      .unwrap();
+    assert_eq!(resolved_count, 1);
+
+    Person::delete(pool, person.id).await.unwrap();
+    Post::delete(pool, report.post_id).await.unwrap();
+  }
+}
index ca218796005ba880dc56be12b25d61f2f53edce6..c20783db01988fc927999b7bc0cc6eb231eb8abf 100644 (file)
@@ -1,5 +1,5 @@
 use crate::{
-  newtypes::{PersonId, PrivateMessageReportId},
+  newtypes::{PersonId, PrivateMessageId, PrivateMessageReportId},
   schema::private_message_report::dsl::{private_message_report, resolved, resolver_id, updated},
   source::private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
   traits::Reportable,
@@ -17,6 +17,7 @@ use diesel_async::RunQueryDsl;
 impl Reportable for PrivateMessageReport {
   type Form = PrivateMessageReportForm;
   type IdType = PrivateMessageReportId;
+  type ObjectIdType = PrivateMessageId;
 
   async fn report(
     pool: &mut DbPool<'_>,
@@ -45,6 +46,15 @@ impl Reportable for PrivateMessageReport {
       .await
   }
 
+  // TODO: this is unused because private message doesnt have remove handler
+  async fn resolve_all_for_object(
+    _pool: &mut DbPool<'_>,
+    _pm_id_: PrivateMessageId,
+    _by_resolver_id: PersonId,
+  ) -> Result<usize, Error> {
+    unimplemented!()
+  }
+
   async fn unresolve(
     pool: &mut DbPool<'_>,
     report_id: Self::IdType,
index 0d09b5e9d853cb68f867eeaa0150968a9a4b1655..74e4186702e6e68866b3efdf052cc567a172eb51 100644 (file)
@@ -30,7 +30,7 @@ pub struct PostReport {
   pub updated: Option<chrono::NaiveDateTime>,
 }
 
-#[derive(Clone)]
+#[derive(Clone, Default)]
 #[cfg_attr(feature = "full", derive(Insertable, AsChangeset))]
 #[cfg_attr(feature = "full", diesel(table_name = post_report))]
 pub struct PostReportForm {
index 07f0b07e25937c1481a6a2c879f6a3a70cdee036..cb128339b19f16adc33b001f7f0fb1f7f325c5f1 100644 (file)
@@ -155,6 +155,7 @@ pub trait Readable {
 pub trait Reportable {
   type Form;
   type IdType;
+  type ObjectIdType;
   async fn report(pool: &mut DbPool<'_>, form: &Self::Form) -> Result<Self, Error>
   where
     Self: Sized;
@@ -163,6 +164,13 @@ pub trait Reportable {
     report_id: Self::IdType,
     resolver_id: PersonId,
   ) -> Result<usize, Error>
+  where
+    Self: Sized;
+  async fn resolve_all_for_object(
+    pool: &mut DbPool<'_>,
+    comment_id_: Self::ObjectIdType,
+    by_resolver_id: PersonId,
+  ) -> Result<usize, Error>
   where
     Self: Sized;
   async fn unresolve(