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