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