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