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::{blocking, check_community_ban, get_local_user_view_from_jwt},
8 use lemmy_apub::protocol::activities::community::report::Report;
11 comment_report::{CommentReport, CommentReportForm},
12 local_site::LocalSite,
16 use lemmy_db_views::structs::{CommentReportView, CommentView};
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
20 /// Creates a comment report and notifies the moderators of the community
21 #[async_trait::async_trait(?Send)]
22 impl Perform for CreateCommentReport {
23 type Response = CommentReportResponse;
25 #[tracing::instrument(skip(context, websocket_id))]
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<CommentReportResponse, LemmyError> {
31 let data: &CreateCommentReport = self;
33 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
34 let local_site = blocking(context.pool(), LocalSite::read).await??;
36 let reason = self.reason.trim();
37 check_report_reason(reason, &local_site)?;
39 let person_id = local_user_view.person.id;
40 let comment_id = data.comment_id;
41 let comment_view = blocking(context.pool(), move |conn| {
42 CommentView::read(conn, comment_id, None)
46 check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
48 let report_form = CommentReportForm {
49 creator_id: person_id,
51 original_comment_text: comment_view.comment.content,
52 reason: reason.to_owned(),
55 let report = blocking(context.pool(), move |conn| {
56 CommentReport::report(conn, &report_form)
59 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
61 let comment_report_view = blocking(context.pool(), move |conn| {
62 CommentReportView::read(conn, report.id, person_id)
66 let res = CommentReportResponse {
70 context.chat_server().do_send(SendModRoomMessage {
71 op: UserOperation::CreateCommentReport,
72 response: res.clone(),
73 community_id: comment_view.community.id,
78 ObjectId::new(comment_view.comment.ap_id),
79 &local_user_view.person.into(),
80 ObjectId::new(comment_view.community.actor_id),