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::{blocking, check_community_ban, get_local_user_view_from_jwt},
8 use lemmy_apub::protocol::activities::community::report::Report;
10 source::post_report::{PostReport, PostReportForm},
13 use lemmy_db_views::structs::{PostReportView, PostView};
14 use lemmy_utils::{error::LemmyError, ConnectionId};
15 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
17 /// Creates a post report and notifies the moderators of the community
18 #[async_trait::async_trait(?Send)]
19 impl Perform for CreatePostReport {
20 type Response = PostReportResponse;
22 #[tracing::instrument(skip(context, websocket_id))]
25 context: &Data<LemmyContext>,
26 websocket_id: Option<ConnectionId>,
27 ) -> Result<PostReportResponse, LemmyError> {
28 let data: &CreatePostReport = self;
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
32 let reason = self.reason.trim();
33 check_report_reason(reason, context)?;
35 let person_id = local_user_view.person.id;
36 let post_id = data.post_id;
37 let post_view = blocking(context.pool(), move |conn| {
38 PostView::read(conn, post_id, None)
42 check_community_ban(person_id, post_view.community.id, context.pool()).await?;
44 let report_form = PostReportForm {
45 creator_id: person_id,
47 original_post_name: post_view.post.name,
48 original_post_url: post_view.post.url,
49 original_post_body: post_view.post.body,
50 reason: reason.to_owned(),
53 let report = blocking(context.pool(), move |conn| {
54 PostReport::report(conn, &report_form)
57 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
59 let post_report_view = blocking(context.pool(), move |conn| {
60 PostReportView::read(conn, report.id, person_id)
64 let res = PostReportResponse { post_report_view };
66 context.chat_server().do_send(SendModRoomMessage {
67 op: UserOperation::CreatePostReport,
68 response: res.clone(),
69 community_id: post_view.community.id,
74 ObjectId::new(post_view.post.ap_id),
75 &local_user_view.person.into(),
76 ObjectId::new(post_view.community.actor_id),