2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, CreateCommentLike},
5 utils::{blocking, check_community_ban, check_downvotes_enabled, get_local_user_view_from_jwt},
8 fetcher::post_or_comment::PostOrComment,
9 protocol::activities::voting::{
11 vote::{Vote, VoteType},
14 use lemmy_db_schema::{
15 newtypes::LocalUserId,
16 source::comment::{CommentLike, CommentLikeForm},
19 use lemmy_db_views::structs::{CommentView, LocalUserView};
20 use lemmy_utils::{ConnectionId, LemmyError};
21 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation};
22 use std::convert::TryInto;
24 #[async_trait::async_trait(?Send)]
25 impl Perform for CreateCommentLike {
26 type Response = CommentResponse;
28 #[tracing::instrument(skip(context, websocket_id))]
31 context: &Data<LemmyContext>,
32 websocket_id: Option<ConnectionId>,
33 ) -> Result<CommentResponse, LemmyError> {
34 let data: &CreateCommentLike = self;
36 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
38 let mut recipient_ids = Vec::<LocalUserId>::new();
40 // Don't do a downvote if site has downvotes disabled
41 check_downvotes_enabled(data.score, context.pool()).await?;
43 let comment_id = data.comment_id;
44 let orig_comment = blocking(context.pool(), move |conn| {
45 CommentView::read(conn, comment_id, None)
50 local_user_view.person.id,
51 orig_comment.community.id,
56 // Add parent user to recipients
57 let recipient_id = orig_comment.get_recipient_id();
58 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
59 LocalUserView::read_person(conn, recipient_id)
63 recipient_ids.push(local_recipient.local_user.id);
66 let like_form = CommentLikeForm {
67 comment_id: data.comment_id,
68 post_id: orig_comment.post.id,
69 person_id: local_user_view.person.id,
73 // Remove any likes first
74 let person_id = local_user_view.person.id;
75 blocking(context.pool(), move |conn| {
76 CommentLike::remove(conn, person_id, comment_id)
80 // Only add the like if the score isnt 0
81 let comment = orig_comment.comment;
82 let object = PostOrComment::Comment(Box::new(comment.into()));
83 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
85 let like_form2 = like_form.clone();
86 let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
87 blocking(context.pool(), like)
89 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
93 &local_user_view.person.clone().into(),
94 orig_comment.community.id,
95 like_form.score.try_into()?,
100 // API doesn't distinguish between Undo/Like and Undo/Dislike
103 &local_user_view.person.clone().into(),
104 orig_comment.community.id,
111 send_comment_ws_message(
113 UserOperation::CreateCommentLike,
116 Some(local_user_view.person.id),