1 use crate::{check_report_reason, Perform};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
5 post::{CreatePostReport, PostReportResponse},
6 utils::{check_community_ban, local_user_view_from_jwt, send_new_report_email_to_admins},
10 local_site::LocalSite,
11 post_report::{PostReport, PostReportForm},
15 use lemmy_db_views::structs::{PostReportView, PostView};
16 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
18 /// Creates a post report and notifies the moderators of the community
19 #[async_trait::async_trait(?Send)]
20 impl Perform for CreatePostReport {
21 type Response = PostReportResponse;
23 #[tracing::instrument(skip(context))]
24 async fn perform(&self, context: &Data<LemmyContext>) -> Result<PostReportResponse, LemmyError> {
25 let data: &CreatePostReport = self;
26 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
27 let local_site = LocalSite::read(&mut context.pool()).await?;
29 let reason = self.reason.trim();
30 check_report_reason(reason, &local_site)?;
32 let person_id = local_user_view.person.id;
33 let post_id = data.post_id;
34 let post_view = PostView::read(&mut context.pool(), post_id, None, None).await?;
36 check_community_ban(person_id, post_view.community.id, &mut context.pool()).await?;
38 let report_form = PostReportForm {
39 creator_id: person_id,
41 original_post_name: post_view.post.name,
42 original_post_url: post_view.post.url,
43 original_post_body: post_view.post.body,
44 reason: reason.to_owned(),
47 let report = PostReport::report(&mut context.pool(), &report_form)
49 .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
51 let post_report_view = PostReportView::read(&mut context.pool(), report.id, person_id).await?;
54 if local_site.reports_email_admins {
55 send_new_report_email_to_admins(
56 &post_report_view.creator.name,
57 &post_report_view.post_creator.name,
64 Ok(PostReportResponse { post_report_view })