1 use crate::check_report_reason;
2 use activitypub_federation::config::Data;
3 use actix_web::web::Json;
4 use lemmy_api_common::{
6 post::{CreatePostReport, PostReportResponse},
7 send_activity::{ActivityChannel, SendActivityData},
10 local_user_view_from_jwt,
12 send_new_report_email_to_admins,
15 use lemmy_db_schema::{
17 local_site::LocalSite,
18 post_report::{PostReport, PostReportForm},
22 use lemmy_db_views::structs::{PostReportView, PostView};
23 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
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?;
34 let reason = sanitize_html(data.reason.trim());
35 check_report_reason(&reason, &local_site)?;
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?;
41 check_community_ban(person_id, post_view.community.id, &mut context.pool()).await?;
43 let report_form = PostReportForm {
44 creator_id: person_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,
52 let report = PostReport::report(&mut context.pool(), &report_form)
54 .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
56 let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id).await?;
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,
69 ActivityChannel::submit_activity(
70 SendActivityData::CreateReport(
71 post_view.post.ap_id.inner().clone(),
72 local_user_view.person,
80 Ok(Json(PostReportResponse { post_report_view }))