]> Untitled Git - lemmy.git/blob - crates/api/src/comment/like.rs
48302ac6509ce955df10e0334899a262434a5a49
[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   utils::{blocking, check_community_ban, check_downvotes_enabled, get_local_user_view_from_jwt},
6 };
7 use lemmy_apub::{
8   fetcher::post_or_comment::PostOrComment,
9   protocol::activities::voting::{
10     undo_vote::UndoVote,
11     vote::{Vote, VoteType},
12   },
13 };
14 use lemmy_db_schema::{
15   newtypes::LocalUserId,
16   source::comment::{CommentLike, CommentLikeForm},
17   traits::Likeable,
18 };
19 use lemmy_db_views::structs::{CommentView, LocalUserView};
20 use lemmy_utils::{error::LemmyError, ConnectionId};
21 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation};
22 use std::convert::TryInto;
23
24 #[async_trait::async_trait(?Send)]
25 impl Perform for CreateCommentLike {
26   type Response = CommentResponse;
27
28   #[tracing::instrument(skip(context, websocket_id))]
29   async fn perform(
30     &self,
31     context: &Data<LemmyContext>,
32     websocket_id: Option<ConnectionId>,
33   ) -> Result<CommentResponse, LemmyError> {
34     let data: &CreateCommentLike = self;
35     let local_user_view =
36       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
37
38     let mut recipient_ids = Vec::<LocalUserId>::new();
39
40     // Don't do a downvote if site has downvotes disabled
41     check_downvotes_enabled(data.score, context.pool()).await?;
42
43     let comment_id = data.comment_id;
44     let orig_comment = blocking(context.pool(), move |conn| {
45       CommentView::read(conn, comment_id, None)
46     })
47     .await??;
48
49     check_community_ban(
50       local_user_view.person.id,
51       orig_comment.community.id,
52       context.pool(),
53     )
54     .await?;
55
56     // Add parent user to recipients
57     let recipient_id = orig_comment.get_recipient_id();
58     if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
59       LocalUserView::read_person(conn, recipient_id)
60     })
61     .await?
62     {
63       recipient_ids.push(local_recipient.local_user.id);
64     }
65
66     let like_form = CommentLikeForm {
67       comment_id: data.comment_id,
68       post_id: orig_comment.post.id,
69       person_id: local_user_view.person.id,
70       score: data.score,
71     };
72
73     // Remove any likes first
74     let person_id = local_user_view.person.id;
75     blocking(context.pool(), move |conn| {
76       CommentLike::remove(conn, person_id, comment_id)
77     })
78     .await??;
79
80     // Only add the like if the score isnt 0
81     let comment = orig_comment.comment;
82     let object = PostOrComment::Comment(Box::new(comment.into()));
83     let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
84     if do_add {
85       let like_form2 = like_form.clone();
86       let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
87       blocking(context.pool(), like)
88         .await?
89         .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
90
91       Vote::send(
92         &object,
93         &local_user_view.person.clone().into(),
94         orig_comment.community.id,
95         like_form.score.try_into()?,
96         context,
97       )
98       .await?;
99     } else {
100       // API doesn't distinguish between Undo/Like and Undo/Dislike
101       UndoVote::send(
102         &object,
103         &local_user_view.person.clone().into(),
104         orig_comment.community.id,
105         VoteType::Like,
106         context,
107       )
108       .await?;
109     }
110
111     send_comment_ws_message(
112       data.comment_id,
113       UserOperation::CreateCommentLike,
114       websocket_id,
115       None,
116       Some(local_user_view.person.id),
117       recipient_ids,
118       context,
119     )
120     .await
121   }
122 }