]> Untitled Git - lemmy.git/blob - crates/api/src/post/like.rs
Diesel 2.0.0 upgrade (#2452)
[lemmy.git] / crates / api / src / post / like.rs
1 use crate::Perform;
2 use actix_web::web::Data;
3 use lemmy_api_common::{
4   post::{CreatePostLike, PostResponse},
5   utils::{
6     blocking,
7     check_community_ban,
8     check_community_deleted_or_removed,
9     check_downvotes_enabled,
10     get_local_user_view_from_jwt,
11     mark_post_as_read,
12   },
13 };
14 use lemmy_apub::{
15   fetcher::post_or_comment::PostOrComment,
16   objects::post::ApubPost,
17   protocol::activities::voting::{
18     undo_vote::UndoVote,
19     vote::{Vote, VoteType},
20   },
21 };
22 use lemmy_db_schema::{
23   source::post::{Post, PostLike, PostLikeForm},
24   traits::{Crud, Likeable},
25 };
26 use lemmy_utils::{error::LemmyError, ConnectionId};
27 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
28
29 #[async_trait::async_trait(?Send)]
30 impl Perform for CreatePostLike {
31   type Response = PostResponse;
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<PostResponse, LemmyError> {
39     let data: &CreatePostLike = self;
40     let local_user_view =
41       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
42
43     // Don't do a downvote if site has downvotes disabled
44     check_downvotes_enabled(data.score, context.pool()).await?;
45
46     // Check for a community ban
47     let post_id = data.post_id;
48     let post: ApubPost = blocking(context.pool(), move |conn| Post::read(conn, post_id))
49       .await??
50       .into();
51
52     check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
53     check_community_deleted_or_removed(post.community_id, context.pool()).await?;
54
55     let like_form = PostLikeForm {
56       post_id: data.post_id,
57       person_id: local_user_view.person.id,
58       score: data.score,
59     };
60
61     // Remove any likes first
62     let person_id = local_user_view.person.id;
63     blocking(context.pool(), move |conn| {
64       PostLike::remove(conn, person_id, post_id)
65     })
66     .await??;
67
68     let community_id = post.community_id;
69     let object = PostOrComment::Post(Box::new(post));
70
71     // Only add the like if the score isnt 0
72     let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
73     if do_add {
74       let like_form2 = like_form.clone();
75       let like = move |conn: &mut _| PostLike::like(conn, &like_form2);
76       blocking(context.pool(), like)
77         .await?
78         .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
79
80       Vote::send(
81         &object,
82         &local_user_view.person.clone().into(),
83         community_id,
84         like_form.score.try_into()?,
85         context,
86       )
87       .await?;
88     } else {
89       // API doesn't distinguish between Undo/Like and Undo/Dislike
90       UndoVote::send(
91         &object,
92         &local_user_view.person.clone().into(),
93         community_id,
94         VoteType::Like,
95         context,
96       )
97       .await?;
98     }
99
100     // Mark the post as read
101     mark_post_as_read(person_id, post_id, context.pool()).await?;
102
103     send_post_ws_message(
104       data.post_id,
105       UserOperation::CreatePostLike,
106       websocket_id,
107       Some(local_user_view.person.id),
108       context,
109     )
110     .await
111   }
112 }