]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/create.rs
Sanitize html (#3708)
[lemmy.git] / crates / api / src / comment_report / create.rs
1 use crate::{check_report_reason, Perform};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   comment::{CommentReportResponse, CreateCommentReport},
5   context::LemmyContext,
6   utils::{
7     check_community_ban,
8     local_user_view_from_jwt,
9     sanitize_html,
10     send_new_report_email_to_admins,
11   },
12 };
13 use lemmy_db_schema::{
14   source::{
15     comment_report::{CommentReport, CommentReportForm},
16     local_site::LocalSite,
17   },
18   traits::Reportable,
19 };
20 use lemmy_db_views::structs::{CommentReportView, CommentView};
21 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
22
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;
27
28   #[tracing::instrument(skip(context))]
29   async fn perform(
30     &self,
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?;
36
37     let reason = sanitize_html(self.reason.trim());
38     check_report_reason(&reason, &local_site)?;
39
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?;
43
44     check_community_ban(person_id, comment_view.community.id, &mut context.pool()).await?;
45
46     let report_form = CommentReportForm {
47       creator_id: person_id,
48       comment_id,
49       original_comment_text: comment_view.comment.content,
50       reason,
51     };
52
53     let report = CommentReport::report(&mut context.pool(), &report_form)
54       .await
55       .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
56
57     let comment_report_view =
58       CommentReportView::read(&mut context.pool(), report.id, person_id).await?;
59
60     // Email the admins
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,
65         &mut context.pool(),
66         context.settings(),
67       )
68       .await?;
69     }
70
71     Ok(CommentReportResponse {
72       comment_report_view,
73     })
74   }
75 }