1 use crate::{check_report_reason, Perform};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentReportResponse, CreateCommentReport},
6 utils::{check_community_ban, local_user_view_from_jwt, send_new_report_email_to_admins},
10 comment_report::{CommentReport, CommentReportForm},
11 local_site::LocalSite,
15 use lemmy_db_views::structs::{CommentReportView, CommentView};
16 use lemmy_utils::error::LemmyError;
18 /// Creates a comment report and notifies the moderators of the community
19 #[async_trait::async_trait(?Send)]
20 impl Perform for CreateCommentReport {
21 type Response = CommentReportResponse;
23 #[tracing::instrument(skip(context))]
26 context: &Data<LemmyContext>,
27 ) -> Result<CommentReportResponse, LemmyError> {
28 let data: &CreateCommentReport = self;
29 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
30 let local_site = LocalSite::read(context.pool()).await?;
32 let reason = self.reason.trim();
33 check_report_reason(reason, &local_site)?;
35 let person_id = local_user_view.person.id;
36 let comment_id = data.comment_id;
37 let comment_view = CommentView::read(context.pool(), comment_id, None).await?;
39 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
41 let report_form = CommentReportForm {
42 creator_id: person_id,
44 original_comment_text: comment_view.comment.content,
45 reason: reason.to_owned(),
48 let report = CommentReport::report(context.pool(), &report_form)
50 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
52 let comment_report_view = CommentReportView::read(context.pool(), report.id, person_id).await?;
55 if local_site.reports_email_admins {
56 send_new_report_email_to_admins(
57 &comment_report_view.creator.name,
58 &comment_report_view.comment_creator.name,
65 Ok(CommentReportResponse {