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