2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{CreatePostLike, PostResponse},
8 check_community_deleted_or_removed,
9 check_downvotes_enabled,
10 get_local_user_view_from_jwt,
15 fetcher::post_or_comment::PostOrComment,
16 objects::post::ApubPost,
17 protocol::activities::voting::{
19 vote::{Vote, VoteType},
22 use lemmy_db_schema::{
23 source::post::{Post, PostLike, PostLikeForm},
24 traits::{Crud, Likeable},
26 use lemmy_utils::{error::LemmyError, ConnectionId};
27 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
29 #[async_trait::async_trait(?Send)]
30 impl Perform for CreatePostLike {
31 type Response = PostResponse;
33 #[tracing::instrument(skip(context, websocket_id))]
36 context: &Data<LemmyContext>,
37 websocket_id: Option<ConnectionId>,
38 ) -> Result<PostResponse, LemmyError> {
39 let data: &CreatePostLike = self;
41 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
43 // Don't do a downvote if site has downvotes disabled
44 check_downvotes_enabled(data.score, context.pool()).await?;
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))
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?;
55 let like_form = PostLikeForm {
56 post_id: data.post_id,
57 person_id: local_user_view.person.id,
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)
68 let community_id = post.community_id;
69 let object = PostOrComment::Post(Box::new(post));
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);
74 let like_form2 = like_form.clone();
75 let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
76 blocking(context.pool(), like)
78 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
82 &local_user_view.person.clone().into(),
84 like_form.score.try_into()?,
89 // API doesn't distinguish between Undo/Like and Undo/Dislike
92 &local_user_view.person.clone().into(),
100 // Mark the post as read
101 mark_post_as_read(person_id, post_id, context.pool()).await?;
103 send_post_ws_message(
105 UserOperation::CreatePostLike,
107 Some(local_user_view.person.id),