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