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 websocket::{messages::SendModRoomMessage, UserOperation},
10 use lemmy_apub::protocol::activities::community::report::Report;
11 use lemmy_db_schema::{
13 comment_report::{CommentReport, CommentReportForm},
14 local_site::LocalSite,
18 use lemmy_db_views::structs::{CommentReportView, CommentView};
19 use lemmy_utils::{error::LemmyError, ConnectionId};
21 /// Creates a comment report and notifies the moderators of the community
22 #[async_trait::async_trait(?Send)]
23 impl Perform for CreateCommentReport {
24 type Response = CommentReportResponse;
26 #[tracing::instrument(skip(context, websocket_id))]
29 context: &Data<LemmyContext>,
30 websocket_id: Option<ConnectionId>,
31 ) -> Result<CommentReportResponse, LemmyError> {
32 let data: &CreateCommentReport = self;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
35 let local_site = LocalSite::read(context.pool()).await?;
37 let reason = self.reason.trim();
38 check_report_reason(reason, &local_site)?;
40 let person_id = local_user_view.person.id;
41 let comment_id = data.comment_id;
42 let comment_view = CommentView::read(context.pool(), comment_id, None).await?;
44 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
46 let report_form = CommentReportForm {
47 creator_id: person_id,
49 original_comment_text: comment_view.comment.content,
50 reason: reason.to_owned(),
53 let report = CommentReport::report(context.pool(), &report_form)
55 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
57 let comment_report_view = CommentReportView::read(context.pool(), report.id, person_id).await?;
59 let res = CommentReportResponse {
63 context.chat_server().do_send(SendModRoomMessage {
64 op: UserOperation::CreateCommentReport,
65 response: res.clone(),
66 community_id: comment_view.community.id,
71 ObjectId::new(comment_view.comment.ap_id),
72 &local_user_view.person.into(),
73 ObjectId::new(comment_view.community.actor_id),