1 use crate::{check_report_reason, Perform};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentReportResponse, CreateCommentReport},
8 local_user_view_from_jwt,
10 send_new_report_email_to_admins,
13 use lemmy_db_schema::{
15 comment_report::{CommentReport, CommentReportForm},
16 local_site::LocalSite,
20 use lemmy_db_views::structs::{CommentReportView, CommentView};
21 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
23 /// Creates a comment report and notifies the moderators of the community
24 #[async_trait::async_trait(?Send)]
25 impl Perform for CreateCommentReport {
26 type Response = CommentReportResponse;
28 #[tracing::instrument(skip(context))]
31 context: &Data<LemmyContext>,
32 ) -> Result<CommentReportResponse, LemmyError> {
33 let data: &CreateCommentReport = self;
34 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
35 let local_site = LocalSite::read(&mut context.pool()).await?;
37 let reason = sanitize_html(self.reason.trim());
38 check_report_reason(&reason, &local_site)?;
40 let person_id = local_user_view.person.id;
41 let comment_id = data.comment_id;
42 let comment_view = CommentView::read(&mut context.pool(), comment_id, None).await?;
44 check_community_ban(person_id, comment_view.community.id, &mut context.pool()).await?;
46 let report_form = CommentReportForm {
47 creator_id: person_id,
49 original_comment_text: comment_view.comment.content,
53 let report = CommentReport::report(&mut context.pool(), &report_form)
55 .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
57 let comment_report_view =
58 CommentReportView::read(&mut context.pool(), report.id, person_id).await?;
61 if local_site.reports_email_admins {
62 send_new_report_email_to_admins(
63 &comment_report_view.creator.name,
64 &comment_report_view.comment_creator.name,
71 Ok(CommentReportResponse {