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