]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/create.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / api / src / comment_report / create.rs
1 use crate::check_report_reason;
2 use activitypub_federation::config::Data;
3 use actix_web::web::Json;
4 use lemmy_api_common::{
5   comment::{CommentReportResponse, CreateCommentReport},
6   context::LemmyContext,
7   send_activity::{ActivityChannel, SendActivityData},
8   utils::{
9     check_community_ban,
10     local_user_view_from_jwt,
11     sanitize_html,
12     send_new_report_email_to_admins,
13   },
14 };
15 use lemmy_db_schema::{
16   source::{
17     comment_report::{CommentReport, CommentReportForm},
18     local_site::LocalSite,
19   },
20   traits::Reportable,
21 };
22 use lemmy_db_views::structs::{CommentReportView, CommentView};
23 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
24
25 /// Creates a comment report and notifies the moderators of the community
26 #[tracing::instrument(skip(context))]
27 pub async fn create_comment_report(
28   data: Json<CreateCommentReport>,
29   context: Data<LemmyContext>,
30 ) -> Result<Json<CommentReportResponse>, LemmyError> {
31   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
32   let local_site = LocalSite::read(&mut context.pool()).await?;
33
34   let reason = sanitize_html(data.reason.trim());
35   check_report_reason(&reason, &local_site)?;
36
37   let person_id = local_user_view.person.id;
38   let comment_id = data.comment_id;
39   let comment_view = CommentView::read(&mut context.pool(), comment_id, None).await?;
40
41   check_community_ban(person_id, comment_view.community.id, &mut context.pool()).await?;
42
43   let report_form = CommentReportForm {
44     creator_id: person_id,
45     comment_id,
46     original_comment_text: comment_view.comment.content,
47     reason,
48   };
49
50   let report = CommentReport::report(&mut context.pool(), &report_form)
51     .await
52     .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
53
54   let comment_report_view =
55     CommentReportView::read(&mut context.pool(), report.id, person_id).await?;
56
57   // Email the admins
58   if local_site.reports_email_admins {
59     send_new_report_email_to_admins(
60       &comment_report_view.creator.name,
61       &comment_report_view.comment_creator.name,
62       &mut context.pool(),
63       context.settings(),
64     )
65     .await?;
66   }
67
68   ActivityChannel::submit_activity(
69     SendActivityData::CreateReport(
70       comment_view.comment.ap_id.inner().clone(),
71       local_user_view.person,
72       comment_view.community,
73       data.reason.clone(),
74     ),
75     &context,
76   )
77   .await?;
78
79   Ok(Json(CommentReportResponse {
80     comment_report_view,
81   }))
82 }