2022-07-30 03:55:59 +00:00
|
|
|
use crate::Perform;
|
|
|
|
use actix_web::web::Data;
|
|
|
|
use lemmy_api_common::{
|
2022-11-28 14:29:33 +00:00
|
|
|
context::LemmyContext,
|
2022-07-30 03:55:59 +00:00
|
|
|
person::{CommentReplyResponse, MarkCommentReplyAsRead},
|
2023-05-25 14:50:07 +00:00
|
|
|
utils::local_user_view_from_jwt,
|
2022-07-30 03:55:59 +00:00
|
|
|
};
|
2022-10-27 09:24:07 +00:00
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::comment_reply::{CommentReply, CommentReplyUpdateForm},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2022-07-30 03:55:59 +00:00
|
|
|
use lemmy_db_views_actor::structs::CommentReplyView;
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2022-07-30 03:55:59 +00:00
|
|
|
|
|
|
|
#[async_trait::async_trait(?Send)]
|
|
|
|
impl Perform for MarkCommentReplyAsRead {
|
|
|
|
type Response = CommentReplyResponse;
|
|
|
|
|
2023-06-06 16:27:22 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
2022-07-30 03:55:59 +00:00
|
|
|
async fn perform(
|
|
|
|
&self,
|
|
|
|
context: &Data<LemmyContext>,
|
|
|
|
) -> Result<CommentReplyResponse, LemmyError> {
|
|
|
|
let data = self;
|
2023-05-25 14:50:07 +00:00
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
|
2022-07-30 03:55:59 +00:00
|
|
|
|
|
|
|
let comment_reply_id = data.comment_reply_id;
|
2023-07-11 13:09:59 +00:00
|
|
|
let read_comment_reply = CommentReply::read(&mut context.pool(), comment_reply_id).await?;
|
2022-07-30 03:55:59 +00:00
|
|
|
|
|
|
|
if local_user_view.person.id != read_comment_reply.recipient_id {
|
2023-07-10 14:50:07 +00:00
|
|
|
return Err(LemmyErrorType::CouldntUpdateComment)?;
|
2022-07-30 03:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let comment_reply_id = read_comment_reply.id;
|
2022-10-27 09:24:07 +00:00
|
|
|
let read = Some(data.read);
|
2022-11-09 10:05:00 +00:00
|
|
|
|
|
|
|
CommentReply::update(
|
2023-07-11 13:09:59 +00:00
|
|
|
&mut context.pool(),
|
2022-11-09 10:05:00 +00:00
|
|
|
comment_reply_id,
|
|
|
|
&CommentReplyUpdateForm { read },
|
|
|
|
)
|
|
|
|
.await
|
2023-07-10 14:50:07 +00:00
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
|
2022-07-30 03:55:59 +00:00
|
|
|
|
|
|
|
let comment_reply_id = read_comment_reply.id;
|
|
|
|
let person_id = local_user_view.person.id;
|
2022-11-09 10:05:00 +00:00
|
|
|
let comment_reply_view =
|
2023-07-11 13:09:59 +00:00
|
|
|
CommentReplyView::read(&mut context.pool(), comment_reply_id, Some(person_id)).await?;
|
2022-07-30 03:55:59 +00:00
|
|
|
|
|
|
|
Ok(CommentReplyResponse { comment_reply_view })
|
|
|
|
}
|
|
|
|
}
|