]> Untitled Git - lemmy.git/blobdiff - crates/db_schema/src/impls/post_report.rs
Automatically resolve report when post/comment is removed (#3850)
[lemmy.git] / crates / db_schema / src / impls / post_report.rs
index d049fbced7abf890a5b292ed266f90d4267b82e2..face766dba1381c885c75622d588d88b664146e1 100644 (file)
@@ -1,37 +1,41 @@
 use crate::{
-  naive_now,
-  newtypes::{PersonId, PostReportId},
-  source::post_report::*,
+  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},
 };
-use diesel::{dsl::*, result::Error, *};
+use diesel::{
+  dsl::{insert_into, update},
+  result::Error,
+  ExpressionMethods,
+  QueryDsl,
+};
+use diesel_async::RunQueryDsl;
 
+#[async_trait]
 impl Reportable for PostReport {
   type Form = PostReportForm;
   type IdType = PostReportId;
+  type ObjectIdType = PostId;
 
-  /// creates a post report and returns it
-  ///
-  /// * `conn` - the postgres connection
-  /// * `post_report_form` - the filled CommentReportForm to insert
-  fn report(conn: &PgConnection, post_report_form: &PostReportForm) -> Result<Self, Error> {
-    use crate::schema::post_report::dsl::*;
+  async fn report(pool: &mut DbPool<'_>, post_report_form: &PostReportForm) -> Result<Self, Error> {
+    let conn = &mut get_conn(pool).await?;
     insert_into(post_report)
       .values(post_report_form)
       .get_result::<Self>(conn)
+      .await
   }
 
-  /// resolve a post report
-  ///
-  /// * `conn` - the postgres connection
-  /// * `report_id` - the id of the report to resolve
-  /// * `by_resolver_id` - the id of the user resolving the report
-  fn resolve(
-    conn: &PgConnection,
+  async fn resolve(
+    pool: &mut DbPool<'_>,
     report_id: Self::IdType,
     by_resolver_id: PersonId,
   ) -> Result<usize, Error> {
-    use crate::schema::post_report::dsl::*;
+    let conn = &mut get_conn(pool).await?;
     update(post_report.find(report_id))
       .set((
         resolved.eq(true),
@@ -39,19 +43,31 @@ impl Reportable for PostReport {
         updated.eq(naive_now()),
       ))
       .execute(conn)
+      .await
   }
 
-  /// resolve a post report
-  ///
-  /// * `conn` - the postgres connection
-  /// * `report_id` - the id of the report to unresolve
-  /// * `by_resolver_id` - the id of the user unresolving the report
-  fn unresolve(
-    conn: &PgConnection,
+  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,
     by_resolver_id: PersonId,
   ) -> Result<usize, Error> {
-    use crate::schema::post_report::dsl::*;
+    let conn = &mut get_conn(pool).await?;
     update(post_report.find(report_id))
       .set((
         resolved.eq(false),
@@ -59,5 +75,100 @@ impl Reportable for PostReport {
         updated.eq(naive_now()),
       ))
       .execute(conn)
+      .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();
   }
 }