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