]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/create.rs
Add cargo feature for building lemmy_api_common with mininum deps (#2243)
[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   comment::{CommentReportResponse, CreateCommentReport},
5   utils::{blocking, check_community_ban, get_local_user_view_from_jwt},
6 };
7 use lemmy_apub::protocol::activities::community::report::Report;
8 use lemmy_apub_lib::object_id::ObjectId;
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::{ConnectionId, LemmyError};
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     // check size of report and check for whitespace
33     let reason = data.reason.trim();
34     if reason.is_empty() {
35       return Err(LemmyError::from_message("report_reason_required"));
36     }
37     if reason.chars().count() > 1000 {
38       return Err(LemmyError::from_message("report_too_long"));
39     }
40
41     let person_id = local_user_view.person.id;
42     let comment_id = data.comment_id;
43     let comment_view = blocking(context.pool(), move |conn| {
44       CommentView::read(conn, comment_id, None)
45     })
46     .await??;
47
48     check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
49
50     let report_form = CommentReportForm {
51       creator_id: person_id,
52       comment_id,
53       original_comment_text: comment_view.comment.content,
54       reason: data.reason.to_owned(),
55     };
56
57     let report = blocking(context.pool(), move |conn| {
58       CommentReport::report(conn, &report_form)
59     })
60     .await?
61     .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
62
63     let comment_report_view = blocking(context.pool(), move |conn| {
64       CommentReportView::read(conn, report.id, person_id)
65     })
66     .await??;
67
68     let res = CommentReportResponse {
69       comment_report_view,
70     };
71
72     context.chat_server().do_send(SendModRoomMessage {
73       op: UserOperation::CreateCommentReport,
74       response: res.clone(),
75       community_id: comment_view.community.id,
76       websocket_id,
77     });
78
79     Report::send(
80       ObjectId::new(comment_view.comment.ap_id),
81       &local_user_view.person.into(),
82       ObjectId::new(comment_view.community.actor_id),
83       reason.to_string(),
84       context,
85     )
86     .await?;
87
88     Ok(res)
89   }
90 }