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