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-05-25 14:50:07 +00:00
|
|
|
utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
|
2023-02-18 14:46:34 +00:00
|
|
|
};
|
|
|
|
use lemmy_db_schema::{
|
|
|
|
source::comment::{Comment, CommentUpdateForm},
|
|
|
|
traits::Crud,
|
|
|
|
};
|
|
|
|
use lemmy_db_views::structs::CommentView;
|
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>,
|
|
|
|
) -> Result<Json<CommentResponse>, LemmyError> {
|
|
|
|
let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
|
|
|
|
|
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
|
|
|
|
|
|
|
check_community_ban(
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
&mut context.pool(),
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
// Verify that only a mod or admin can distinguish a comment
|
|
|
|
is_mod_or_admin(
|
|
|
|
&mut context.pool(),
|
|
|
|
local_user_view.person.id,
|
|
|
|
orig_comment.community.id,
|
|
|
|
)
|
|
|
|
.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
|
|
|
}
|