2021-03-25 19:19:40 +00:00
|
|
|
use crate::PerformCrud;
|
|
|
|
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::{DeletePrivateMessage, PrivateMessageResponse},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::local_user_view_from_jwt,
|
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-06-06 16:27:22 +00:00
|
|
|
use lemmy_db_views::structs::PrivateMessageView;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for DeletePrivateMessage {
|
|
|
|
type Response = PrivateMessageResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(self, context))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> Result<PrivateMessageResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &DeletePrivateMessage = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Checking permissions
|
|
|
|
let private_message_id = data.private_message_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let orig_private_message =
|
|
|
|
PrivateMessage::read(&mut context.pool(), private_message_id).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
if local_user_view.person.id != orig_private_message.creator_id {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::EditPrivateMessageNotAllowed)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Doing the update
|
|
|
|
let private_message_id = data.private_message_id;
|
|
|
|
let deleted = data.deleted;
|
2022-11-28 14:29:33 +00:00
|
|
|
PrivateMessage::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
private_message_id,
|
|
|
|
&PrivateMessageUpdateForm::builder()
|
|
|
|
.deleted(Some(deleted))
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdatePrivateMessage)?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2023-07-11 13:09:59 +00:00
|
|
|
let view = PrivateMessageView::read(&mut context.pool(), private_message_id).await?;
|
2023-06-06 16:27:22 +00:00
|
|
|
Ok(PrivateMessageResponse {
|
|
|
|
private_message_view: view,
|
|
|
|
})
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|