2 use actix_web::web::Data;
3 use lemmy_api_common::{
4 comment::{CommentResponse, CreateCommentLike},
5 utils::{check_community_ban, check_downvotes_enabled, get_local_user_view_from_jwt},
6 websocket::{send::send_comment_ws_message, UserOperation},
10 fetcher::post_or_comment::PostOrComment,
11 protocol::activities::voting::{
13 vote::{Vote, VoteType},
16 use lemmy_db_schema::{
17 newtypes::LocalUserId,
19 comment::{CommentLike, CommentLikeForm},
20 comment_reply::CommentReply,
21 local_site::LocalSite,
25 use lemmy_db_views::structs::{CommentView, LocalUserView};
26 use lemmy_utils::{error::LemmyError, ConnectionId};
27 use std::convert::TryInto;
29 #[async_trait::async_trait(?Send)]
30 impl Perform for CreateCommentLike {
31 type Response = CommentResponse;
33 #[tracing::instrument(skip(context, websocket_id))]
36 context: &Data<LemmyContext>,
37 websocket_id: Option<ConnectionId>,
38 ) -> Result<CommentResponse, LemmyError> {
39 let data: &CreateCommentLike = self;
40 let local_site = LocalSite::read(context.pool()).await?;
42 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
44 let mut recipient_ids = Vec::<LocalUserId>::new();
46 // Don't do a downvote if site has downvotes disabled
47 check_downvotes_enabled(data.score, &local_site)?;
49 let comment_id = data.comment_id;
50 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
53 local_user_view.person.id,
54 orig_comment.community.id,
59 // Add parent poster or commenter to recipients
60 let comment_reply = CommentReply::read_by_comment(context.pool(), comment_id).await;
61 if let Ok(reply) = comment_reply {
62 let recipient_id = reply.recipient_id;
63 if let Ok(local_recipient) = LocalUserView::read_person(context.pool(), recipient_id).await {
64 recipient_ids.push(local_recipient.local_user.id);
68 let like_form = CommentLikeForm {
69 comment_id: data.comment_id,
70 post_id: orig_comment.post.id,
71 person_id: local_user_view.person.id,
75 // Remove any likes first
76 let person_id = local_user_view.person.id;
78 CommentLike::remove(context.pool(), person_id, comment_id).await?;
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 CommentLike::like(context.pool(), &like_form2)
88 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
92 &local_user_view.person.clone().into(),
93 orig_comment.community.id,
94 like_form.score.try_into()?,
99 // API doesn't distinguish between Undo/Like and Undo/Dislike
102 &local_user_view.person.clone().into(),
103 orig_comment.community.id,
110 send_comment_ws_message(
112 UserOperation::CreateCommentLike,
115 Some(local_user_view.person.id),