]> Untitled Git - lemmy.git/blob - crates/api/src/post_report/create.rs
Replace Option<bool> with bool for PostQuery and CommentQuery (#3819) (#3857)
[lemmy.git] / crates / api / src / post_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   context::LemmyContext,
6   post::{CreatePostReport, PostReportResponse},
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     local_site::LocalSite,
18     post_report::{PostReport, PostReportForm},
19   },
20   traits::Reportable,
21 };
22 use lemmy_db_views::structs::{PostReportView, PostView};
23 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
24
25 /// Creates a post report and notifies the moderators of the community
26 #[tracing::instrument(skip(context))]
27 pub async fn create_post_report(
28   data: Json<CreatePostReport>,
29   context: Data<LemmyContext>,
30 ) -> Result<Json<PostReportResponse>, 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 post_id = data.post_id;
39   let post_view = PostView::read(&mut context.pool(), post_id, None, false).await?;
40
41   check_community_ban(person_id, post_view.community.id, &mut context.pool()).await?;
42
43   let report_form = PostReportForm {
44     creator_id: person_id,
45     post_id,
46     original_post_name: post_view.post.name,
47     original_post_url: post_view.post.url,
48     original_post_body: post_view.post.body,
49     reason,
50   };
51
52   let report = PostReport::report(&mut context.pool(), &report_form)
53     .await
54     .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
55
56   let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id).await?;
57
58   // Email the admins
59   if local_site.reports_email_admins {
60     send_new_report_email_to_admins(
61       &post_report_view.creator.name,
62       &post_report_view.post_creator.name,
63       &mut context.pool(),
64       context.settings(),
65     )
66     .await?;
67   }
68
69   ActivityChannel::submit_activity(
70     SendActivityData::CreateReport(
71       post_view.post.ap_id.inner().clone(),
72       local_user_view.person,
73       post_view.community,
74       data.reason.clone(),
75     ),
76     &context,
77   )
78   .await?;
79
80   Ok(Json(PostReportResponse { post_report_view }))
81 }