]> Untitled Git - lemmy.git/blob - crates/api/src/comment/distinguish.rs
540c19a3dd3af8519df18b45bd48b44704b47bc5
[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::builder()
42     .distinguished(Some(data.distinguished))
43     .build();
44   Comment::update(&mut context.pool(), comment_id, &form)
45     .await
46     .with_lemmy_type(LemmyErrorType::CouldntUpdateComment)?;
47
48   let comment_id = data.comment_id;
49   let person_id = local_user_view.person.id;
50   let comment_view = CommentView::read(&mut context.pool(), comment_id, Some(person_id)).await?;
51
52   Ok(Json(CommentResponse {
53     comment_view,
54     recipient_ids: Vec::new(),
55     form_id: None,
56   }))
57 }