2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 post::{CreatePostLike, PostResponse},
7 check_community_deleted_or_removed,
8 check_downvotes_enabled,
9 get_local_user_view_from_jwt,
14 fetcher::post_or_comment::PostOrComment,
15 objects::post::ApubPost,
16 protocol::activities::voting::{
18 vote::{Vote, VoteType},
21 use lemmy_db_schema::{
23 local_site::LocalSite,
24 post::{Post, PostLike, PostLikeForm},
26 traits::{Crud, Likeable},
28 use lemmy_utils::{error::LemmyError, ConnectionId};
29 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
31 #[async_trait::async_trait(?Send)]
32 impl Perform for CreatePostLike {
33 type Response = PostResponse;
35 #[tracing::instrument(skip(context, websocket_id))]
38 context: &Data<LemmyContext>,
39 websocket_id: Option<ConnectionId>,
40 ) -> Result<PostResponse, LemmyError> {
41 let data: &CreatePostLike = self;
43 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
44 let local_site = LocalSite::read(context.pool()).await?;
46 // Don't do a downvote if site has downvotes disabled
47 check_downvotes_enabled(data.score, &local_site)?;
49 // Check for a community ban
50 let post_id = data.post_id;
51 let post: ApubPost = Post::read(context.pool(), post_id).await?.into();
53 check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
54 check_community_deleted_or_removed(post.community_id, context.pool()).await?;
56 let like_form = PostLikeForm {
57 post_id: data.post_id,
58 person_id: local_user_view.person.id,
62 // Remove any likes first
63 let person_id = local_user_view.person.id;
65 PostLike::remove(context.pool(), person_id, post_id).await?;
67 let community_id = post.community_id;
68 let object = PostOrComment::Post(Box::new(post));
70 // Only add the like if the score isnt 0
71 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
73 let like_form2 = like_form.clone();
74 PostLike::like(context.pool(), &like_form2)
76 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
80 &local_user_view.person.clone().into(),
82 like_form.score.try_into()?,
87 // API doesn't distinguish between Undo/Like and Undo/Dislike
90 &local_user_view.person.clone().into(),
98 // Mark the post as read
99 mark_post_as_read(person_id, post_id, context.pool()).await?;
101 send_post_ws_message(
103 UserOperation::CreatePostLike,
105 Some(local_user_view.person.id),