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},
|
2022-11-09 10:05:00 +00:00
|
|
|
utils::get_local_user_view_from_jwt,
|
2022-11-26 02:04:46 +00:00
|
|
|
websocket::{send::send_pm_ws_message, UserOperationCrud},
|
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,
|
|
|
|
};
|
2022-06-02 14:33:41 +00:00
|
|
|
use lemmy_utils::{error::LemmyError, ConnectionId};
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl PerformCrud for DeletePrivateMessage {
|
|
|
|
type Response = PrivateMessageResponse;
|
|
|
|
|
2021-12-06 14:54:47 +00:00
|
|
|
#[tracing::instrument(skip(self, context, websocket_id))]
|
2021-03-25 19:19:40 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
websocket_id: Option<ConnectionId>,
|
|
|
|
) -> Result<PrivateMessageResponse, LemmyError> {
|
2021-07-05 16:07:26 +00:00
|
|
|
let data: &DeletePrivateMessage = self;
|
2021-09-22 15:57:09 +00:00
|
|
|
let local_user_view =
|
|
|
|
get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
|
|
|
// Checking permissions
|
|
|
|
let private_message_id = data.private_message_id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let orig_private_message = PrivateMessage::read(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 {
|
2021-12-06 14:54:47 +00:00
|
|
|
return Err(LemmyError::from_message("no_private_message_edit_allowed"));
|
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(
|
2022-11-09 10:05:00 +00:00
|
|
|
context.pool(),
|
|
|
|
private_message_id,
|
|
|
|
&PrivateMessageUpdateForm::builder()
|
|
|
|
.deleted(Some(deleted))
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
.await
|
2022-03-16 20:11:49 +00:00
|
|
|
.map_err(|e| LemmyError::from_error_message(e, "couldnt_update_private_message"))?;
|
2021-03-25 19:19:40 +00:00
|
|
|
|
2021-08-17 18:04:58 +00:00
|
|
|
let op = UserOperationCrud::DeletePrivateMessage;
|
|
|
|
send_pm_ws_message(data.private_message_id, op, websocket_id, context).await
|
2021-03-25 19:19:40 +00:00
|
|
|
}
|
|
|
|
}
|