]> Untitled Git - lemmy.git/blob - crates/api/src/post_report/create.rs
Moving settings to Database. (#2492)
[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::{
11     local_site::LocalSite,
12     post_report::{PostReport, PostReportForm},
13   },
14   traits::Reportable,
15 };
16 use lemmy_db_views::structs::{PostReportView, PostView};
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
19
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;
24
25   #[tracing::instrument(skip(context, websocket_id))]
26   async fn perform(
27     &self,
28     context: &Data<LemmyContext>,
29     websocket_id: Option<ConnectionId>,
30   ) -> Result<PostReportResponse, LemmyError> {
31     let data: &CreatePostReport = self;
32     let local_user_view =
33       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34     let local_site = blocking(context.pool(), LocalSite::read).await??;
35
36     let reason = self.reason.trim();
37     check_report_reason(reason, &local_site)?;
38
39     let person_id = local_user_view.person.id;
40     let post_id = data.post_id;
41     let post_view = blocking(context.pool(), move |conn| {
42       PostView::read(conn, post_id, None)
43     })
44     .await??;
45
46     check_community_ban(person_id, post_view.community.id, context.pool()).await?;
47
48     let report_form = PostReportForm {
49       creator_id: person_id,
50       post_id,
51       original_post_name: post_view.post.name,
52       original_post_url: post_view.post.url,
53       original_post_body: post_view.post.body,
54       reason: reason.to_owned(),
55     };
56
57     let report = blocking(context.pool(), move |conn| {
58       PostReport::report(conn, &report_form)
59     })
60     .await?
61     .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
62
63     let post_report_view = blocking(context.pool(), move |conn| {
64       PostReportView::read(conn, report.id, person_id)
65     })
66     .await??;
67
68     let res = PostReportResponse { post_report_view };
69
70     context.chat_server().do_send(SendModRoomMessage {
71       op: UserOperation::CreatePostReport,
72       response: res.clone(),
73       community_id: post_view.community.id,
74       websocket_id,
75     });
76
77     Report::send(
78       ObjectId::new(post_view.post.ap_id),
79       &local_user_view.person.into(),
80       ObjectId::new(post_view.community.actor_id),
81       reason.to_string(),
82       context,
83     )
84     .await?;
85
86     Ok(res)
87   }
88 }