2 use actix_web::web::Data;
3 use lemmy_api_common::{
6 comment::{CommentReportResponse, CreateCommentReport},
7 get_local_user_view_from_jwt,
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},
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};
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;
24 #[tracing::instrument(skip(context, websocket_id))]
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<CommentReportResponse, LemmyError> {
30 let data: &CreateCommentReport = self;
32 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
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"));
39 if reason.chars().count() > 1000 {
40 return Err(LemmyError::from_message("report_too_long"));
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)
50 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
52 let report_form = CommentReportForm {
53 creator_id: person_id,
55 original_comment_text: comment_view.comment.content,
56 reason: data.reason.to_owned(),
59 let report = blocking(context.pool(), move |conn| {
60 CommentReport::report(conn, &report_form)
63 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
65 let comment_report_view = blocking(context.pool(), move |conn| {
66 CommentReportView::read(conn, report.id, person_id)
70 let res = CommentReportResponse {
74 context.chat_server().do_send(SendModRoomMessage {
75 op: UserOperation::CreateCommentReport,
76 response: res.clone(),
77 community_id: comment_view.community.id,
82 ObjectId::new(comment_view.comment.ap_id),
83 &local_user_view.person.into(),
84 ObjectId::new(comment_view.community.actor_id),