2023-07-28 14:39:38 +00:00
|
|
|
use actix_web::web::{Data, Json};
|
2023-02-18 14:46:34 +00:00
|
|
|
use lemmy_api_common::{
|
|
|
|
comment::{CommentResponse, DistinguishComment},
|
|
|
|
context::LemmyContext,
|
2023-10-13 13:48:18 +00:00
|
|
|
utils::{check_community_mod_action, check_community_user_action},
|
2023-02-18 14:46:34 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::comment::{Comment, CommentUpdateForm},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
2023-09-21 10:42:28 +00:00
|
|
|
use lemmy_db_views::structs::{CommentView, LocalUserView};
|
2023-07-10 14:50:07 +00:00
|
|
|
use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
|
2023-02-18 14:46:34 +00:00
|
|
|
|
2023-07-28 14:39:38 +00:00
|
|
|
#[tracing::instrument(skip(context))]
|
|
|
|
pub async fn distinguish_comment(
|
|
|
|
data: Json<DistinguishComment>,
|
|
|
|
context: Data<LemmyContext>,
|
2023-09-21 10:42:28 +00:00
|
|
|
local_user_view: LocalUserView,
|
2023-07-28 14:39:38 +00:00
|
|
|
) -> Result<Json<CommentResponse>, LemmyError> {
|
2023-09-04 09:06:54 +00:00
|
|
|
let orig_comment = CommentView::read(&mut context.pool(), data.comment_id, None).await?;
|
2023-07-28 14:39:38 +00:00
|
|
|
|
2023-10-13 13:48:18 +00:00
|
|
|
check_community_user_action(
|
|
|
|
&local_user_view.person,
|
2023-07-28 14:39:38 +00:00
|
|
|
orig_comment.community.id,
|
|
|
|
&mut context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Verify that only a mod or admin can distinguish a comment
|
2023-10-13 13:48:18 +00:00
|
|
|
check_community_mod_action(
|
|
|
|
&local_user_view.person,
|
2023-07-28 14:39:38 +00:00
|
|
|
orig_comment.community.id,
|
2023-10-13 13:48:18 +00:00
|
|
|
false,
|
|
|
|
&mut context.pool(),
|
2023-07-28 14:39:38 +00:00
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Update the Comment
|
2023-08-08 09:41:41 +00:00
|
|
|
let form = CommentUpdateForm {
|
|
|
|
distinguished: Some(data.distinguished),
|
|
|
|
..Default::default()
|
|
|
|
};
|
2023-09-04 09:06:54 +00:00
|
|
|
Comment::update(&mut context.pool(), data.comment_id, &form)
|
2023-07-28 14:39:38 +00:00
|
|
|
.await
|
|
|
|
.with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
|
|
|
|
|
2023-09-04 09:06:54 +00:00
|
|
|
let comment_view = CommentView::read(
|
|
|
|
&mut context.pool(),
|
|
|
|
data.comment_id,
|
|
|
|
Some(local_user_view.person.id),
|
|
|
|
)
|
|
|
|
.await?;
|
2023-07-28 14:39:38 +00:00
|
|
|
|
|
|
|
Ok(Json(CommentResponse {
|
|
|
|
comment_view,
|
|
|
|
recipient_ids: Vec::new(),
|
|
|
|
}))
|
2023-02-18 14:46:34 +00:00
|
|
|
}
|