]> Untitled Git - lemmy.git/blob - crates/db_schema/src/impls/comment_report.rs
Various pedantic clippy fixes (#2568)
[lemmy.git] / crates / db_schema / src / impls / comment_report.rs
1 use crate::{
2   newtypes::{CommentReportId, PersonId},
3   schema::comment_report::dsl::{comment_report, resolved, resolver_id, updated},
4   source::comment_report::{CommentReport, CommentReportForm},
5   traits::Reportable,
6   utils::{get_conn, naive_now, DbPool},
7 };
8 use diesel::{
9   dsl::{insert_into, update},
10   result::Error,
11   ExpressionMethods,
12   QueryDsl,
13 };
14 use diesel_async::RunQueryDsl;
15
16 #[async_trait]
17 impl Reportable for CommentReport {
18   type Form = CommentReportForm;
19   type IdType = CommentReportId;
20   /// creates a comment report and returns it
21   ///
22   /// * `conn` - the postgres connection
23   /// * `comment_report_form` - the filled CommentReportForm to insert
24   async fn report(pool: &DbPool, comment_report_form: &CommentReportForm) -> Result<Self, Error> {
25     let conn = &mut get_conn(pool).await?;
26     insert_into(comment_report)
27       .values(comment_report_form)
28       .get_result::<Self>(conn)
29       .await
30   }
31
32   /// resolve a comment report
33   ///
34   /// * `conn` - the postgres connection
35   /// * `report_id` - the id of the report to resolve
36   /// * `by_resolver_id` - the id of the user resolving the report
37   async fn resolve(
38     pool: &DbPool,
39     report_id_: Self::IdType,
40     by_resolver_id: PersonId,
41   ) -> Result<usize, Error> {
42     let conn = &mut get_conn(pool).await?;
43     update(comment_report.find(report_id_))
44       .set((
45         resolved.eq(true),
46         resolver_id.eq(by_resolver_id),
47         updated.eq(naive_now()),
48       ))
49       .execute(conn)
50       .await
51   }
52
53   /// unresolve a comment report
54   ///
55   /// * `conn` - the postgres connection
56   /// * `report_id` - the id of the report to unresolve
57   /// * `by_resolver_id` - the id of the user unresolving the report
58   async fn unresolve(
59     pool: &DbPool,
60     report_id_: Self::IdType,
61     by_resolver_id: PersonId,
62   ) -> Result<usize, Error> {
63     let conn = &mut get_conn(pool).await?;
64     update(comment_report.find(report_id_))
65       .set((
66         resolved.eq(false),
67         resolver_id.eq(by_resolver_id),
68         updated.eq(naive_now()),
69       ))
70       .execute(conn)
71       .await
72   }
73 }