2022-09-19 22:58:42 +00:00
|
|
|
use crate::{check_report_reason, Perform};
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::{local_user_view_from_jwt, send_new_report_email_to_admins},
|
2022-09-19 22:58:42 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::{
|
2022-10-27 09:24:07 +00:00
|
|
|
local_site::LocalSite,
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message::PrivateMessage,
|
|
|
|
private_message_report::{PrivateMessageReport, PrivateMessageReportForm},
|
|
|
|
},
|
|
|
|
traits::{Crud, Reportable},
|
|
|
|
};
|
|
|
|
use lemmy_db_views::structs::PrivateMessageReportView;
|
2023-06-06 16:27:22 +00:00
|
|
|
use lemmy_utils::error::LemmyError;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for CreatePrivateMessageReport {
|
|
|
|
type Response = PrivateMessageReportResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&self.auth, context).await?;
|
2022-11-09 10:05:00 +00:00
|
|
|
let local_site = LocalSite::read(context.pool()).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
let reason = self.reason.trim();
|
2022-10-27 09:24:07 +00:00
|
|
|
check_report_reason(reason, &local_site)?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
let person_id = local_user_view.person.id;
|
|
|
|
let private_message_id = self.private_message_id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let private_message = PrivateMessage::read(context.pool(), private_message_id).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
|
|
|
let report_form = PrivateMessageReportForm {
|
|
|
|
creator_id: person_id,
|
|
|
|
private_message_id,
|
|
|
|
original_pm_text: private_message.content,
|
|
|
|
reason: reason.to_owned(),
|
|
|
|
};
|
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let report = PrivateMessageReport::report(context.pool(), &report_form)
|
|
|
|
.await
|
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2022-11-09 10:05:00 +00:00
|
|
|
let private_message_report_view =
|
|
|
|
PrivateMessageReportView::read(context.pool(), report.id).await?;
|
2022-09-19 22:58:42 +00:00
|
|
|
|
2023-02-14 15:57:08 +00:00
|
|
|
// Email the admins
|
|
|
|
if local_site.reports_email_admins {
|
|
|
|
send_new_report_email_to_admins(
|
|
|
|
&private_message_report_view.creator.name,
|
|
|
|
&private_message_report_view.private_message_creator.name,
|
|
|
|
context.pool(),
|
|
|
|
context.settings(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2022-09-19 22:58:42 +00:00
|
|
|
// TODO: consider federating this
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
Ok(PrivateMessageReportResponse {
|
|
|
|
private_message_report_view,
|
|
|
|
})
|
2022-09-19 22:58:42 +00:00
|
|
|
}
|
|
|
|
}
|