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::{
24 local_site::LocalSite,
25 post::{Post, PostLike, PostLikeForm},
27 traits::{Crud, Likeable},
29 use lemmy_utils::{error::LemmyError, ConnectionId};
30 use lemmy_websocket::{send::send_post_ws_message, LemmyContext, UserOperation};
32 #[async_trait::async_trait(?Send)]
33 impl Perform for CreatePostLike {
34 type Response = PostResponse;
36 #[tracing::instrument(skip(context, websocket_id))]
39 context: &Data<LemmyContext>,
40 websocket_id: Option<ConnectionId>,
41 ) -> Result<PostResponse, LemmyError> {
42 let data: &CreatePostLike = self;
44 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
45 let local_site = blocking(context.pool(), LocalSite::read).await??;
47 // Don't do a downvote if site has downvotes disabled
48 check_downvotes_enabled(data.score, &local_site)?;
50 // Check for a community ban
51 let post_id = data.post_id;
52 let post: ApubPost = blocking(context.pool(), move |conn| Post::read(conn, post_id))
56 check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
57 check_community_deleted_or_removed(post.community_id, context.pool()).await?;
59 let like_form = PostLikeForm {
60 post_id: data.post_id,
61 person_id: local_user_view.person.id,
65 // Remove any likes first
66 let person_id = local_user_view.person.id;
67 blocking(context.pool(), move |conn| {
68 PostLike::remove(conn, person_id, post_id)
72 let community_id = post.community_id;
73 let object = PostOrComment::Post(Box::new(post));
75 // Only add the like if the score isnt 0
76 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
78 let like_form2 = like_form.clone();
79 let like = move |conn: &mut _| PostLike::like(conn, &like_form2);
80 blocking(context.pool(), like)
82 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_post"))?;
86 &local_user_view.person.clone().into(),
88 like_form.score.try_into()?,
93 // API doesn't distinguish between Undo/Like and Undo/Dislike
96 &local_user_view.person.clone().into(),
104 // Mark the post as read
105 mark_post_as_read(person_id, post_id, context.pool()).await?;
107 send_post_ws_message(
109 UserOperation::CreatePostLike,
111 Some(local_user_view.person.id),