]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/resolve.rs
Make functions work with both connection and pool (#3420)
[lemmy.git] / crates / api / src / comment_report / resolve.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   comment::{CommentReportResponse, ResolveCommentReport},
5   context::LemmyContext,
6   utils::{is_mod_or_admin, local_user_view_from_jwt},
7 };
8 use lemmy_db_schema::{source::comment_report::CommentReport, traits::Reportable};
9 use lemmy_db_views::structs::CommentReportView;
10 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
11
12 /// Resolves or unresolves a comment report and notifies the moderators of the community
13 #[async_trait::async_trait(?Send)]
14 impl Perform for ResolveCommentReport {
15   type Response = CommentReportResponse;
16
17   #[tracing::instrument(skip(context))]
18   async fn perform(
19     &self,
20     context: &Data<LemmyContext>,
21   ) -> Result<CommentReportResponse, LemmyError> {
22     let data: &ResolveCommentReport = self;
23     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
24
25     let report_id = data.report_id;
26     let person_id = local_user_view.person.id;
27     let report = CommentReportView::read(&mut context.pool(), report_id, person_id).await?;
28
29     let person_id = local_user_view.person.id;
30     is_mod_or_admin(&mut context.pool(), person_id, report.community.id).await?;
31
32     if data.resolved {
33       CommentReport::resolve(&mut context.pool(), report_id, person_id)
34         .await
35         .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?;
36     } else {
37       CommentReport::unresolve(&mut context.pool(), report_id, person_id)
38         .await
39         .with_lemmy_type(LemmyErrorType::CouldntResolveReport)?;
40     }
41
42     let report_id = data.report_id;
43     let comment_report_view =
44       CommentReportView::read(&mut context.pool(), report_id, person_id).await?;
45
46     Ok(CommentReportResponse {
47       comment_report_view,
48     })
49   }
50 }