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