]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/create.rs
Implement reports for private messages (#2433)
[lemmy.git] / crates / api / src / comment_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   comment::{CommentReportResponse, CreateCommentReport},
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::comment_report::{CommentReport, CommentReportForm},
11   traits::Reportable,
12 };
13 use lemmy_db_views::structs::{CommentReportView, CommentView};
14 use lemmy_utils::{error::LemmyError, ConnectionId};
15 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
16
17 /// Creates a comment report and notifies the moderators of the community
18 #[async_trait::async_trait(?Send)]
19 impl Perform for CreateCommentReport {
20   type Response = CommentReportResponse;
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<CommentReportResponse, LemmyError> {
28     let data: &CreateCommentReport = 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 comment_id = data.comment_id;
37     let comment_view = blocking(context.pool(), move |conn| {
38       CommentView::read(conn, comment_id, None)
39     })
40     .await??;
41
42     check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
43
44     let report_form = CommentReportForm {
45       creator_id: person_id,
46       comment_id,
47       original_comment_text: comment_view.comment.content,
48       reason: reason.to_owned(),
49     };
50
51     let report = blocking(context.pool(), move |conn| {
52       CommentReport::report(conn, &report_form)
53     })
54     .await?
55     .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
56
57     let comment_report_view = blocking(context.pool(), move |conn| {
58       CommentReportView::read(conn, report.id, person_id)
59     })
60     .await??;
61
62     let res = CommentReportResponse {
63       comment_report_view,
64     };
65
66     context.chat_server().do_send(SendModRoomMessage {
67       op: UserOperation::CreateCommentReport,
68       response: res.clone(),
69       community_id: comment_view.community.id,
70       websocket_id,
71     });
72
73     Report::send(
74       ObjectId::new(comment_view.comment.ap_id),
75       &local_user_view.person.into(),
76       ObjectId::new(comment_view.community.actor_id),
77       reason.to_string(),
78       context,
79     )
80     .await?;
81
82     Ok(res)
83   }
84 }