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