]> Untitled Git - lemmy.git/blob - crates/api/src/comment/distinguish.rs
add enable_federated_downvotes site option
[lemmy.git] / crates / api / src / comment / distinguish.rs
1 use actix_web::web::{Data, Json};
2 use lemmy_api_common::{
3   comment::{CommentResponse, DistinguishComment},
4   context::LemmyContext,
5   utils::{check_community_ban, is_mod_or_admin, local_user_view_from_jwt},
6 };
7 use lemmy_db_schema::{
8   source::comment::{Comment, CommentUpdateForm},
9   traits::Crud,
10 };
11 use lemmy_db_views::structs::CommentView;
12 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
13
14 #[tracing::instrument(skip(context))]
15 pub async fn distinguish_comment(
16   data: Json<DistinguishComment>,
17   context: Data<LemmyContext>,
18 ) -> Result<Json<CommentResponse>, LemmyError> {
19   let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
20
21   let comment_id = data.comment_id;
22   let orig_comment = CommentView::read(&mut context.pool(), comment_id, None).await?;
23
24   check_community_ban(
25     local_user_view.person.id,
26     orig_comment.community.id,
27     &mut context.pool(),
28   )
29   .await?;
30
31   // Verify that only a mod or admin can distinguish a comment
32   is_mod_or_admin(
33     &mut context.pool(),
34     local_user_view.person.id,
35     orig_comment.community.id,
36   )
37   .await?;
38
39   // Update the Comment
40   let comment_id = data.comment_id;
41   let form = CommentUpdateForm {
42     distinguished: Some(data.distinguished),
43     ..Default::default()
44   };
45   Comment::update(&mut context.pool(), comment_id, &form)
46     .await
47     .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
48
49   let comment_id = data.comment_id;
50   let person_id = local_user_view.person.id;
51   let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)).await?;
52
53   Ok(Json(CommentResponse {
54     comment_view,
55     recipient_ids: Vec::new(),
56   }))
57 }