]> Untitled Git - lemmy.git/blob - crates/api/src/comment_report/create.rs
Add SendActivity trait so that api crates compile in parallel with lemmy_apub
[lemmy.git] / crates / api / src / comment_report / create.rs
1 use crate::{check_report_reason, Perform};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   comment::{CommentReportResponse, CreateCommentReport},
5   context::LemmyContext,
6   utils::{check_community_ban, get_local_user_view_from_jwt},
7   websocket::{messages::SendModRoomMessage, UserOperation},
8 };
9 use lemmy_db_schema::{
10   source::{
11     comment_report::{CommentReport, CommentReportForm},
12     local_site::LocalSite,
13   },
14   traits::Reportable,
15 };
16 use lemmy_db_views::structs::{CommentReportView, CommentView};
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18
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;
23
24   #[tracing::instrument(skip(context, websocket_id))]
25   async fn perform(
26     &self,
27     context: &Data<LemmyContext>,
28     websocket_id: Option<ConnectionId>,
29   ) -> Result<CommentReportResponse, LemmyError> {
30     let data: &CreateCommentReport = self;
31     let local_user_view =
32       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
33     let local_site = LocalSite::read(context.pool()).await?;
34
35     let reason = self.reason.trim();
36     check_report_reason(reason, &local_site)?;
37
38     let person_id = local_user_view.person.id;
39     let comment_id = data.comment_id;
40     let comment_view = CommentView::read(context.pool(), comment_id, None).await?;
41
42     check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
43
44     let report_form = CommentReportForm {
45       creator_id: person_id,
46       comment_id,
47       original_comment_text: comment_view.comment.content,
48       reason: reason.to_owned(),
49     };
50
51     let report = CommentReport::report(context.pool(), &report_form)
52       .await
53       .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
54
55     let comment_report_view = CommentReportView::read(context.pool(), report.id, person_id).await?;
56
57     let res = CommentReportResponse {
58       comment_report_view,
59     };
60
61     context.chat_server().do_send(SendModRoomMessage {
62       op: UserOperation::CreateCommentReport,
63       response: res.clone(),
64       community_id: comment_view.community.id,
65       websocket_id,
66     });
67
68     Ok(res)
69   }
70 }