use lemmy_api_common::{
context::LemmyContext,
private_message::{CreatePrivateMessageReport, PrivateMessageReportResponse},
- utils::get_local_user_view_from_jwt,
- websocket::UserOperation,
+ utils::{local_user_view_from_jwt, send_new_report_email_to_admins},
};
use lemmy_db_schema::{
- newtypes::CommunityId,
source::{
local_site::LocalSite,
private_message::PrivateMessage,
traits::{Crud, Reportable},
};
use lemmy_db_views::structs::PrivateMessageReportView;
-use lemmy_utils::{error::LemmyError, ConnectionId};
+use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
#[async_trait::async_trait(?Send)]
impl Perform for CreatePrivateMessageReport {
type Response = PrivateMessageReportResponse;
- #[tracing::instrument(skip(context, websocket_id))]
- async fn perform(
- &self,
- context: &Data<LemmyContext>,
- websocket_id: Option<ConnectionId>,
- ) -> Result<Self::Response, LemmyError> {
- let local_user_view =
- get_local_user_view_from_jwt(&self.auth, context.pool(), context.secret()).await?;
- let local_site = LocalSite::read(context.pool()).await?;
+ #[tracing::instrument(skip(context))]
+ async fn perform(&self, context: &Data<LemmyContext>) -> Result<Self::Response, LemmyError> {
+ let local_user_view = local_user_view_from_jwt(&self.auth, context).await?;
+ let local_site = LocalSite::read(&mut context.pool()).await?;
let reason = self.reason.trim();
check_report_reason(reason, &local_site)?;
let person_id = local_user_view.person.id;
let private_message_id = self.private_message_id;
- let private_message = PrivateMessage::read(context.pool(), private_message_id).await?;
+ let private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
let report_form = PrivateMessageReportForm {
creator_id: person_id,
reason: reason.to_owned(),
};
- let report = PrivateMessageReport::report(context.pool(), &report_form)
+ let report = PrivateMessageReport::report(&mut context.pool(), &report_form)
.await
- .map_err(|e| LemmyError::from_error_message(e, "couldnt_create_report"))?;
+ .with_lemmy_type(LemmyErrorType::CouldntCreateReport)?;
let private_message_report_view =
- PrivateMessageReportView::read(context.pool(), report.id).await?;
+ PrivateMessageReportView::read(&mut context.pool(), report.id).await?;
- let res = PrivateMessageReportResponse {
- private_message_report_view,
- };
-
- context
- .chat_server()
- .send_mod_room_message(
- UserOperation::CreatePrivateMessageReport,
- &res,
- CommunityId(0),
- websocket_id,
+ // 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,
+ &mut context.pool(),
+ context.settings(),
)
.await?;
+ }
// TODO: consider federating this
- Ok(res)
+ Ok(PrivateMessageReportResponse {
+ private_message_report_view,
+ })
}
}