]> Untitled Git - lemmy.git/blob - crates/api/src/post_report/create.rs
Implement reports for private messages (#2433)
[lemmy.git] / crates / api / src / post_report / create.rs
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},
7 };
8 use lemmy_apub::protocol::activities::community::report::Report;
9 use lemmy_db_schema::{
10   source::post_report::{PostReport, PostReportForm},
11   traits::Reportable,
12 };
13 use lemmy_db_views::structs::{PostReportView, PostView};
14 use lemmy_utils::{error::LemmyError, ConnectionId};
15 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
16
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;
21
22   #[tracing::instrument(skip(context, websocket_id))]
23   async fn perform(
24     &self,
25     context: &Data<LemmyContext>,
26     websocket_id: Option<ConnectionId>,
27   ) -> Result<PostReportResponse, LemmyError> {
28     let data: &CreatePostReport = self;
29     let local_user_view =
30       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
31
32     let reason = self.reason.trim();
33     check_report_reason(reason, context)?;
34
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)
39     })
40     .await??;
41
42     check_community_ban(person_id, post_view.community.id, context.pool()).await?;
43
44     let report_form = PostReportForm {
45       creator_id: person_id,
46       post_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(),
51     };
52
53     let report = blocking(context.pool(), move |conn| {
54       PostReport::report(conn, &report_form)
55     })
56     .await?
57     .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
58
59     let post_report_view = blocking(context.pool(), move |conn| {
60       PostReportView::read(conn, report.id, person_id)
61     })
62     .await??;
63
64     let res = PostReportResponse { post_report_view };
65
66     context.chat_server().do_send(SendModRoomMessage {
67       op: UserOperation::CreatePostReport,
68       response: res.clone(),
69       community_id: post_view.community.id,
70       websocket_id,
71     });
72
73     Report::send(
74       ObjectId::new(post_view.post.ap_id),
75       &local_user_view.person.into(),
76       ObjectId::new(post_view.community.actor_id),
77       reason.to_string(),
78       context,
79     )
80     .await?;
81
82     Ok(res)
83   }
84 }