2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, CreateCommentLike},
6 utils::{check_community_ban, check_downvotes_enabled, get_local_user_view_from_jwt},
7 websocket::UserOperation,
10 newtypes::LocalUserId,
12 comment::{CommentLike, CommentLikeForm},
13 comment_reply::CommentReply,
14 local_site::LocalSite,
18 use lemmy_db_views::structs::{CommentView, LocalUserView};
19 use lemmy_utils::{error::LemmyError, ConnectionId};
21 #[async_trait::async_trait(?Send)]
22 impl Perform for CreateCommentLike {
23 type Response = CommentResponse;
25 #[tracing::instrument(skip(context, websocket_id))]
28 context: &Data<LemmyContext>,
29 websocket_id: Option<ConnectionId>,
30 ) -> Result<CommentResponse, LemmyError> {
31 let data: &CreateCommentLike = self;
32 let local_site = LocalSite::read(context.pool()).await?;
34 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
36 let mut recipient_ids = Vec::<LocalUserId>::new();
38 // Don't do a downvote if site has downvotes disabled
39 check_downvotes_enabled(data.score, &local_site)?;
41 let comment_id = data.comment_id;
42 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
45 local_user_view.person.id,
46 orig_comment.community.id,
51 // Add parent poster or commenter to recipients
52 let comment_reply = CommentReply::read_by_comment(context.pool(), comment_id).await;
53 if let Ok(reply) = comment_reply {
54 let recipient_id = reply.recipient_id;
55 if let Ok(local_recipient) = LocalUserView::read_person(context.pool(), recipient_id).await {
56 recipient_ids.push(local_recipient.local_user.id);
60 let like_form = CommentLikeForm {
61 comment_id: data.comment_id,
62 post_id: orig_comment.post.id,
63 person_id: local_user_view.person.id,
67 // Remove any likes first
68 let person_id = local_user_view.person.id;
70 CommentLike::remove(context.pool(), person_id, comment_id).await?;
72 // Only add the like if the score isnt 0
73 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
75 let like_form2 = like_form.clone();
76 CommentLike::like(context.pool(), &like_form2)
78 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
82 .send_comment_ws_message(
83 &UserOperation::CreateCommentLike,
87 Some(local_user_view.person.id),