1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
5 private_message::{DeletePrivateMessage, PrivateMessageResponse},
6 send_activity::{ActivityChannel, SendActivityData},
7 utils::local_user_view_from_jwt,
10 source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
13 use lemmy_db_views::structs::PrivateMessageView;
14 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
16 #[tracing::instrument(skip(context))]
17 pub async fn delete_private_message(
18 data: Json<DeletePrivateMessage>,
19 context: Data<LemmyContext>,
20 ) -> Result<Json<PrivateMessageResponse>, LemmyError> {
21 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
23 // Checking permissions
24 let private_message_id = data.private_message_id;
25 let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
26 if local_user_view.person.id != orig_private_message.creator_id {
27 return Err(LemmyErrorType::EditPrivateMessageNotAllowed)?;
31 let private_message_id = data.private_message_id;
32 let deleted = data.deleted;
33 let private_message = PrivateMessage::update(
36 &PrivateMessageUpdateForm {
37 deleted: Some(deleted),
42 .with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?;
44 ActivityChannel::submit_activity(
45 SendActivityData::DeletePrivateMessage(local_user_view.person, private_message, data.deleted),
50 let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?;
51 Ok(Json(PrivateMessageResponse {
52 private_message_view: view,