2023-08-02 16:52:41 +00:00
|
|
|
use activitypub_federation::config::Data;
|
|
|
|
use actix_web::web::Json;
|
2021-03-25 19:19:40 +00:00
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-09-19 22:58:42 +00:00
|
|
|
private_message::{DeletePrivateMessage, PrivateMessageResponse},
|
2023-08-02 16:52:41 +00:00
|
|
|
send_activity::{ActivityChannel, SendActivityData},
|
2021-03-25 19:19:40 +00:00
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::private_message::{PrivateMessage, PrivateMessageUpdateForm},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::{LocalUserView, PrivateMessageView};
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn delete_private_message(
|
|
|
|
data: Json<DeletePrivateMessage>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-08-02 16:52:41 +00:00
|
|
|
) -> Result<Json<PrivateMessageResponse>, LemmyError> {
|
|
|
|
// Checking permissions
|
|
|
|
let private_message_id = data.private_message_id;
|
|
|
|
let orig_private_message = PrivateMessage::read(&mut context.pool(), private_message_id).await?;
|
|
|
|
if local_user_view.person.id != orig_private_message.creator_id {
|
2023-08-31 13:01:08 +00:00
|
|
|
Err(LemmyErrorType::EditPrivateMessageNotAllowed)?
|
2023-08-02 16:52:41 +00:00
|
|
|
}
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
// Doing the update
|
|
|
|
let private_message_id = data.private_message_id;
|
|
|
|
let deleted = data.deleted;
|
|
|
|
let private_message = PrivateMessage::update(
|
|
|
|
&mut context.pool(),
|
|
|
|
private_message_id,
|
2023-08-08 09:41:41 +00:00
|
|
|
&PrivateMessageUpdateForm {
|
|
|
|
deleted: Some(deleted),
|
|
|
|
..Default::default()
|
|
|
|
},
|
2023-08-02 16:52:41 +00:00
|
|
|
)
|
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
ActivityChannel::submit_activity(
|
|
|
|
SendActivityData::DeletePrivateMessage(local_user_view.person, private_message, data.deleted),
|
|
|
|
&context,
|
|
|
|
)
|
|
|
|
.await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-08-02 16:52:41 +00:00
|
|
|
let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?;
|
|
|
|
Ok(Json(PrivateMessageResponse {
|
|
|
|
private_message_view: view,
|
|
|
|
}))
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|