1 use activitypub_federation::config::Data;
2 use actix_web::web::Json;
3 use lemmy_api_common::{
4 build_response::build_post_response,
6 post::{CreatePostLike, PostResponse},
7 send_activity::{ActivityChannel, SendActivityData},
10 check_community_deleted_or_removed,
11 check_downvotes_enabled,
12 local_user_view_from_jwt,
16 use lemmy_db_schema::{
19 local_site::LocalSite,
20 post::{Post, PostLike, PostLikeForm},
22 traits::{Crud, Likeable},
24 use lemmy_utils::error::{LemmyError, LemmyErrorExt, LemmyErrorType};
27 #[tracing::instrument(skip(context))]
28 pub async fn like_post(
29 data: Json<CreatePostLike>,
30 context: Data<LemmyContext>,
31 ) -> Result<Json<PostResponse>, LemmyError> {
32 let local_user_view = local_user_view_from_jwt(&data.auth, &context).await?;
33 let local_site = LocalSite::read(&mut context.pool()).await?;
35 // Don't do a downvote if site has downvotes disabled
36 check_downvotes_enabled(data.score, &local_site)?;
38 // Check for a community ban
39 let post_id = data.post_id;
40 let post = Post::read(&mut context.pool(), post_id).await?;
43 local_user_view.person.id,
48 check_community_deleted_or_removed(post.community_id, &mut context.pool()).await?;
50 let like_form = PostLikeForm {
51 post_id: data.post_id,
52 person_id: local_user_view.person.id,
56 // Remove any likes first
57 let person_id = local_user_view.person.id;
59 PostLike::remove(&mut context.pool(), person_id, post_id).await?;
61 // Only add the like if the score isnt 0
62 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
64 PostLike::like(&mut context.pool(), &like_form)
66 .with_lemmy_type(LemmyErrorType::CouldntLikePost)?;
69 // Mark the post as read
70 mark_post_as_read(person_id, post_id, &mut context.pool()).await?;
72 ActivityChannel::submit_activity(
73 SendActivityData::LikePostOrComment(
75 local_user_view.person.clone(),
76 Community::read(&mut context.pool(), post.community_id).await?,
87 local_user_view.person.id,