]> Untitled Git - lemmy.git/blob - crates/api/src/comment/like.rs
Moving settings to Database. (#2492)
[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     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 = blocking(context.pool(), LocalSite::read).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 = blocking(context.pool(), move |conn| {
50       CommentView::read(conn, comment_id, None)
51     })
52     .await??;
53
54     check_community_ban(
55       local_user_view.person.id,
56       orig_comment.community.id,
57       context.pool(),
58     )
59     .await?;
60
61     // Add parent poster or commenter to recipients
62     let comment_reply = blocking(context.pool(), move |conn| {
63       CommentReply::read_by_comment(conn, comment_id)
64     })
65     .await?;
66     if let Ok(reply) = comment_reply {
67       let recipient_id = reply.recipient_id;
68       if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
69         LocalUserView::read_person(conn, recipient_id)
70       })
71       .await?
72       {
73         recipient_ids.push(local_recipient.local_user.id);
74       }
75     }
76
77     let like_form = CommentLikeForm {
78       comment_id: data.comment_id,
79       post_id: orig_comment.post.id,
80       person_id: local_user_view.person.id,
81       score: data.score,
82     };
83
84     // Remove any likes first
85     let person_id = local_user_view.person.id;
86     blocking(context.pool(), move |conn| {
87       CommentLike::remove(conn, person_id, comment_id)
88     })
89     .await??;
90
91     // Only add the like if the score isnt 0
92     let comment = orig_comment.comment;
93     let object = PostOrComment::Comment(Box::new(comment.into()));
94     let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
95     if do_add {
96       let like_form2 = like_form.clone();
97       let like = move |conn: &mut _| CommentLike::like(conn, &like_form2);
98       blocking(context.pool(), like)
99         .await?
100         .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
101
102       Vote::send(
103         &object,
104         &local_user_view.person.clone().into(),
105         orig_comment.community.id,
106         like_form.score.try_into()?,
107         context,
108       )
109       .await?;
110     } else {
111       // API doesn't distinguish between Undo/Like and Undo/Dislike
112       UndoVote::send(
113         &object,
114         &local_user_view.person.clone().into(),
115         orig_comment.community.id,
116         VoteType::Like,
117         context,
118       )
119       .await?;
120     }
121
122     send_comment_ws_message(
123       data.comment_id,
124       UserOperation::CreateCommentLike,
125       websocket_id,
126       None,
127       Some(local_user_view.person.id),
128       recipient_ids,
129       context,
130     )
131     .await
132   }
133 }