]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/comment_report.rs
Automatically resolve report when post/comment is removed (#3850)
[lemmy.git] / crates / db_schema / src / impls / comment_report.rs
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