1 use crate::{check_report_reason, Perform};
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse},
5 utils::get_local_user_view_from_jwt,
10 local_site::LocalSite,
11 private_message::PrivateMessage,
12 private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
14 traits::{Crud, Reportable},
16 use lemmy_db_views::structs::PrivateMessageReportView;
17 use lemmy_utils::{error::LemmyError, ConnectionId};
18 use lemmy_websocket::{messages::SendModRoomMessage, LemmyContext, UserOperation};
20 #[async_trait::async_trait(?Send)]
21 impl Perform for CreatePrivateMessageReport {
22 type Response = PrivateMessageReportResponse;
24 #[tracing::instrument(skip(context, websocket_id))]
27 context: &Data<LemmyContext>,
28 websocket_id: Option<ConnectionId>,
29 ) -> Result<Self::Response, LemmyError> {
31 get_local_user_view_from_jwt(&self.auth, context.pool(), context.secret()).await?;
32 let local_site = LocalSite::read(context.pool()).await?;
34 let reason = self.reason.trim();
35 check_report_reason(reason, &local_site)?;
37 let person_id = local_user_view.person.id;
38 let private_message_id = self.private_message_id;
39 let private_message = PrivateMessage::read(context.pool(), private_message_id).await?;
41 let report_form = PrivateMessageReportForm {
42 creator_id: person_id,
44 original_pm_text: private_message.content,
45 reason: reason.to_owned(),
48 let report = PrivateMessageReport::report(context.pool(), &report_form)
50 .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
52 let private_message_report_view =
53 PrivateMessageReportView::read(context.pool(), report.id).await?;
55 let res = PrivateMessageReportResponse {
56 private_message_report_view,
59 context.chat_server().do_send(SendModRoomMessage {
60 op: UserOperation::CreatePrivateMessageReport,
61 response: res.clone(),
62 community_id: CommunityId(0),
66 // TODO: consider federating this