]> Untitled Git - lemmy.git/blob - crates/api/src/comment/like.rs
Dont return error in case optional auth is invalid (#2879)
[lemmy.git] / crates / api / src / comment / like.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   comment::{CommentResponse, CreateCommentLike},
5   context::LemmyContext,
6   utils::{check_community_ban, check_downvotes_enabled, local_user_view_from_jwt},
7   websocket::UserOperation,
8 };
9 use lemmy_db_schema::{
10   newtypes::LocalUserId,
11   source::{
12     comment::{CommentLike, CommentLikeForm},
13     comment_reply::CommentReply,
14     local_site::LocalSite,
15   },
16   traits::Likeable,
17 };
18 use lemmy_db_views::structs::{CommentView, LocalUserView};
19 use lemmy_utils::{error::LemmyError, ConnectionId};
20
21 #[async_trait::async_trait(?Send)]
22 impl Perform for CreateCommentLike {
23   type Response = CommentResponse;
24
25   #[tracing::instrument(skip(context, websocket_id))]
26   async fn perform(
27     &self,
28     context: &Data<LemmyContext>,
29     websocket_id: Option<ConnectionId>,
30   ) -> Result<CommentResponse, LemmyError> {
31     let data: &CreateCommentLike = self;
32     let local_site = LocalSite::read(context.pool()).await?;
33     let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
34
35     let mut recipient_ids = Vec::<LocalUserId>::new();
36
37     // Don't do a downvote if site has downvotes disabled
38     check_downvotes_enabled(data.score, &local_site)?;
39
40     let comment_id = data.comment_id;
41     let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
42
43     check_community_ban(
44       local_user_view.person.id,
45       orig_comment.community.id,
46       context.pool(),
47     )
48     .await?;
49
50     // Add parent poster or commenter to recipients
51     let comment_reply = CommentReply::read_by_comment(context.pool(), comment_id).await;
52     if let Ok(reply) = comment_reply {
53       let recipient_id = reply.recipient_id;
54       if let Ok(local_recipient) = LocalUserView::read_person(context.pool(), recipient_id).await {
55         recipient_ids.push(local_recipient.local_user.id);
56       }
57     }
58
59     let like_form = CommentLikeForm {
60       comment_id: data.comment_id,
61       post_id: orig_comment.post.id,
62       person_id: local_user_view.person.id,
63       score: data.score,
64     };
65
66     // Remove any likes first
67     let person_id = local_user_view.person.id;
68
69     CommentLike::remove(context.pool(), person_id, comment_id).await?;
70
71     // Only add the like if the score isnt 0
72     let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
73     if do_add {
74       CommentLike::like(context.pool(), &like_form)
75         .await
76         .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
77     }
78
79     context
80       .send_comment_ws_message(
81         &UserOperation::CreateCommentLike,
82         data.comment_id,
83         websocket_id,
84         None,
85         Some(local_user_view.person.id),
86         recipient_ids,
87       )
88       .await
89   }
90 }