1 use crate::{check_report_reason, Perform};
2 use activitypub_federation::core::object_id::ObjectId;
3 use actix_web::web::Data;
4 use lemmy_api_common::{
5 post::{CreatePostReport, PostReportResponse},
6 utils::{check_community_ban, get_local_user_view_from_jwt},
8 use lemmy_apub::protocol::activities::community::report::Report;
11 local_site::LocalSite,
12 post_report::{PostReport, PostReportForm},
16 use lemmy_db_views::structs::{PostReportView, PostView};
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
20 /// Creates a post report and notifies the moderators of the community
21 #[async_trait::async_trait(?Send)]
22 impl Perform for CreatePostReport {
23 type Response = PostReportResponse;
25 #[tracing::instrument(skip(context, websocket_id))]
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<PostReportResponse, LemmyError> {
31 let data: &CreatePostReport = self;
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 let local_site = LocalSite::read(context.pool()).await?;
36 let reason = self.reason.trim();
37 check_report_reason(reason, &local_site)?;
39 let person_id = local_user_view.person.id;
40 let post_id = data.post_id;
41 let post_view = PostView::read(context.pool(), post_id, None).await?;
43 check_community_ban(person_id, post_view.community.id, context.pool()).await?;
45 let report_form = PostReportForm {
46 creator_id: person_id,
48 original_post_name: post_view.post.name,
49 original_post_url: post_view.post.url,
50 original_post_body: post_view.post.body,
51 reason: reason.to_owned(),
54 let report = PostReport::report(context.pool(), &report_form)
56 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
58 let post_report_view = PostReportView::read(context.pool(), report.id, person_id).await?;
60 let res = PostReportResponse { post_report_view };
62 context.chat_server().do_send(SendModRoomMessage {
63 op: UserOperation::CreatePostReport,
64 response: res.clone(),
65 community_id: post_view.community.id,
70 ObjectId::new(post_view.post.ap_id),
71 &local_user_view.person.into(),
72 ObjectId::new(post_view.community.actor_id),