]> Untitled Git - lemmy.git/blob - 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
1 use crate::{
2   newtypes::{CommentId, CommentReportId, PersonId},
3   schema::comment_report::{
4     comment_id,
5     dsl::{comment_report, resolved, resolver_id, updated},
6   },
7   source::comment_report::{CommentReport, CommentReportForm},
8   traits::Reportable,
9   utils::{get_conn, naive_now, DbPool},
10 };
11 use diesel::{
12   dsl::{insert_into, update},
13   result::Error,
14   ExpressionMethods,
15   QueryDsl,
16 };
17 use diesel_async::RunQueryDsl;
18
19 #[async_trait]
20 impl Reportable for CommentReport {
21   type Form = CommentReportForm;
22   type IdType = CommentReportId;
23   type ObjectIdType = CommentId;
24   /// creates a comment report and returns it
25   ///
26   /// * `conn` - the postgres connection
27   /// * `comment_report_form` - the filled CommentReportForm to insert
28   async fn report(
29     pool: &mut DbPool<'_>,
30     comment_report_form: &CommentReportForm,
31   ) -> Result<Self, Error> {
32     let conn = &mut get_conn(pool).await?;
33     insert_into(comment_report)
34       .values(comment_report_form)
35       .get_result::<Self>(conn)
36       .await
37   }
38
39   /// resolve a comment report
40   ///
41   /// * `conn` - the postgres connection
42   /// * `report_id` - the id of the report to resolve
43   /// * `by_resolver_id` - the id of the user resolving the report
44   async fn resolve(
45     pool: &mut DbPool<'_>,
46     report_id_: Self::IdType,
47     by_resolver_id: PersonId,
48   ) -> Result<usize, Error> {
49     let conn = &mut get_conn(pool).await?;
50     update(comment_report.find(report_id_))
51       .set((
52         resolved.eq(true),
53         resolver_id.eq(by_resolver_id),
54         updated.eq(naive_now()),
55       ))
56       .execute(conn)
57       .await
58   }
59
60   async fn resolve_all_for_object(
61     pool: &mut DbPool<'_>,
62     comment_id_: CommentId,
63     by_resolver_id: PersonId,
64   ) -> Result<usize, Error> {
65     let conn = &mut get_conn(pool).await?;
66     update(comment_report.filter(comment_id.eq(comment_id_)))
67       .set((
68         resolved.eq(true),
69         resolver_id.eq(by_resolver_id),
70         updated.eq(naive_now()),
71       ))
72       .execute(conn)
73       .await
74   }
75
76   /// unresolve a comment report
77   ///
78   /// * `conn` - the postgres connection
79   /// * `report_id` - the id of the report to unresolve
80   /// * `by_resolver_id` - the id of the user unresolving the report
81   async fn unresolve(
82     pool: &mut DbPool<'_>,
83     report_id_: Self::IdType,
84     by_resolver_id: PersonId,
85   ) -> Result<usize, Error> {
86     let conn = &mut get_conn(pool).await?;
87     update(comment_report.find(report_id_))
88       .set((
89         resolved.eq(false),
90         resolver_id.eq(by_resolver_id),
91         updated.eq(naive_now()),
92       ))
93       .execute(conn)
94       .await
95   }
96 }