]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/create.rs
4f4ef3bb80499b25f92425798110a24dc18abaf9
[lemmy.git] / crates / api / src / comment_report / create.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   blocking,
5   check_community_ban,
6   comment::{CommentReportResponse, CreateCommentReport},
7   get_local_user_view_from_jwt,
8 };
9 use lemmy_apub::protocol::activities::community::report::Report;
10 use lemmy_apub_lib::object_id::ObjectId;
11 use lemmy_db_schema::{
12   source::comment_report::{CommentReport, CommentReportForm},
13   traits::Reportable,
14 };
15 use lemmy_db_views::{comment_report_view::CommentReportView, comment_view::CommentView};
16 use lemmy_utils::{ConnectionId, LemmyError};
17 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
18
19 /// Creates a comment report and notifies the moderators of the community
20 #[async_trait::async_trait(?Send)]
21 impl Perform for CreateCommentReport {
22   type Response = CommentReportResponse;
23
24   #[tracing::instrument(skip(context, websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     websocket_id: Option<ConnectionId>,
29   ) -> Result<CommentReportResponse, LemmyError> {
30     let data: &CreateCommentReport = self;
31     let local_user_view =
32       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33
34     // check size of report and check for whitespace
35     let reason = data.reason.trim();
36     if reason.is_empty() {
37       return Err(LemmyError::from_message("report_reason_required"));
38     }
39     if reason.chars().count() > 1000 {
40       return Err(LemmyError::from_message("report_too_long"));
41     }
42
43     let person_id = local_user_view.person.id;
44     let comment_id = data.comment_id;
45     let comment_view = blocking(context.pool(), move |conn| {
46       CommentView::read(conn, comment_id, None)
47     })
48     .await??;
49
50     check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
51
52     let report_form = CommentReportForm {
53       creator_id: person_id,
54       comment_id,
55       original_comment_text: comment_view.comment.content,
56       reason: data.reason.to_owned(),
57     };
58
59     let report = blocking(context.pool(), move |conn| {
60       CommentReport::report(conn, &report_form)
61     })
62     .await?
63     .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
64
65     let comment_report_view = blocking(context.pool(), move |conn| {
66       CommentReportView::read(conn, report.id, person_id)
67     })
68     .await??;
69
70     let res = CommentReportResponse {
71       comment_report_view,
72     };
73
74     context.chat_server().do_send(SendModRoomMessage {
75       op: UserOperation::CreateCommentReport,
76       response: res.clone(),
77       community_id: comment_view.community.id,
78       websocket_id,
79     });
80
81     Report::send(
82       ObjectId::new(comment_view.comment.ap_id),
83       &local_user_view.person.into(),
84       ObjectId::new(comment_view.community.actor_id),
85       reason.to_string(),
86       context,
87     )
88     .await?;
89
90     Ok(res)
91   }
92 }