]> Untitled Git - lemmy.git/blob - crates/db_queries/src/source/comment_report.rs
Clean up reporting (#1776)
[lemmy.git] / crates / db_queries / src / source / comment_report.rs
1 use crate::Reportable;
2 use diesel::{dsl::*, result::Error, *};
3 use lemmy_db_schema::{
4   naive_now,
5   source::comment_report::{CommentReport, CommentReportForm},
6   CommentReportId,
7   PersonId,
8 };
9
10 impl Reportable for CommentReport {
11   type Form = CommentReportForm;
12   type IdType = CommentReportId;
13   /// creates a comment report and returns it
14   ///
15   /// * `conn` - the postgres connection
16   /// * `comment_report_form` - the filled CommentReportForm to insert
17   fn report(conn: &PgConnection, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
18     use lemmy_db_schema::schema::comment_report::dsl::*;
19     insert_into(comment_report)
20       .values(comment_report_form)
21       .get_result::<Self>(conn)
22   }
23
24   /// resolve a comment report
25   ///
26   /// * `conn` - the postgres connection
27   /// * `report_id` - the id of the report to resolve
28   /// * `by_resolver_id` - the id of the user resolving the report
29   fn resolve(
30     conn: &PgConnection,
31     report_id: Self::IdType,
32     by_resolver_id: PersonId,
33   ) -> Result<usize, Error> {
34     use lemmy_db_schema::schema::comment_report::dsl::*;
35     update(comment_report.find(report_id))
36       .set((
37         resolved.eq(true),
38         resolver_id.eq(by_resolver_id),
39         updated.eq(naive_now()),
40       ))
41       .execute(conn)
42   }
43
44   /// unresolve a comment report
45   ///
46   /// * `conn` - the postgres connection
47   /// * `report_id` - the id of the report to unresolve
48   /// * `by_resolver_id` - the id of the user unresolving the report
49   fn unresolve(
50     conn: &PgConnection,
51     report_id: Self::IdType,
52     by_resolver_id: PersonId,
53   ) -> Result<usize, Error> {
54     use lemmy_db_schema::schema::comment_report::dsl::*;
55     update(comment_report.find(report_id))
56       .set((
57         resolved.eq(false),
58         resolver_id.eq(by_resolver_id),
59         updated.eq(naive_now()),
60       ))
61       .execute(conn)
62   }
63 }