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},
8 use lemmy_apub::protocol::activities::community::report::Report;
10 source::comment_report::{CommentReport, CommentReportForm},
13 use lemmy_db_views::structs::{CommentReportView, CommentView};
14 use lemmy_utils::{error::LemmyError, ConnectionId};
15 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
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;
22 #[tracing::instrument(skip(context, websocket_id))]
25 context: &Data<LemmyContext>,
26 websocket_id: Option<ConnectionId>,
27 ) -> Result<CommentReportResponse, LemmyError> {
28 let data: &CreateCommentReport = self;
30 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
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"));
37 if reason.chars().count() > 1000 {
38 return Err(LemmyError::from_message("report_too_long"));
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)
48 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
50 let report_form = CommentReportForm {
51 creator_id: person_id,
53 original_comment_text: comment_view.comment.content,
54 reason: data.reason.to_owned(),
57 let report = blocking(context.pool(), move |conn| {
58 CommentReport::report(conn, &report_form)
61 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
63 let comment_report_view = blocking(context.pool(), move |conn| {
64 CommentReportView::read(conn, report.id, person_id)
68 let res = CommentReportResponse {
72 context.chat_server().do_send(SendModRoomMessage {
73 op: UserOperation::CreateCommentReport,
74 response: res.clone(),
75 community_id: comment_view.community.id,
80 ObjectId::new(comment_view.comment.ap_id),
81 &local_user_view.person.into(),
82 ObjectId::new(comment_view.community.actor_id),