2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 build_response::build_post_response,
6 post::{CreatePostLike, PostResponse},
9 check_community_deleted_or_removed,
10 check_downvotes_enabled,
11 local_user_view_from_jwt,
15 use lemmy_db_schema::{
17 local_site::LocalSite,
18 post::{Post, PostLike, PostLikeForm},
20 traits::{Crud, Likeable},
22 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
24 #[async_trait::async_trait(?Send)]
25 impl Perform for CreatePostLike {
26 type Response = PostResponse;
28 #[tracing::instrument(skip(context))]
29 async fn perform(&self, context: &Data<LemmyContext>) -> Result<PostResponse, LemmyError> {
30 let data: &CreatePostLike = self;
31 let local_user_view = local_user_view_from_jwt(&data.auth, context).await?;
32 let local_site = LocalSite::read(context.pool()).await?;
34 // Don't do a downvote if site has downvotes disabled
35 check_downvotes_enabled(data.score, &local_site)?;
37 // Check for a community ban
38 let post_id = data.post_id;
39 let post = Post::read(context.pool(), post_id).await?;
41 check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
42 check_community_deleted_or_removed(post.community_id, context.pool()).await?;
44 let like_form = PostLikeForm {
45 post_id: data.post_id,
46 person_id: local_user_view.person.id,
50 // Remove any likes first
51 let person_id = local_user_view.person.id;
53 PostLike::remove(context.pool(), person_id, post_id).await?;
55 // Only add the like if the score isnt 0
56 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
58 PostLike::like(context.pool(), &like_form)
60 .with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
63 // Mark the post as read
64 mark_post_as_read(person_id, post_id, context.pool()).await?;
69 local_user_view.person.id,