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},
8 fetcher::post_or_comment::PostOrComment,
9 protocol::activities::voting::{
11 vote::{Vote, VoteType},
14 use lemmy_db_schema::{
15 newtypes::LocalUserId,
17 comment::{CommentLike, CommentLikeForm},
18 comment_reply::CommentReply,
19 local_site::LocalSite,
23 use lemmy_db_views::structs::{CommentView, LocalUserView};
24 use lemmy_utils::{error::LemmyError, ConnectionId};
25 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation};
26 use std::convert::TryInto;
28 #[async_trait::async_trait(?Send)]
29 impl Perform for CreateCommentLike {
30 type Response = CommentResponse;
32 #[tracing::instrument(skip(context, websocket_id))]
35 context: &Data<LemmyContext>,
36 websocket_id: Option<ConnectionId>,
37 ) -> Result<CommentResponse, LemmyError> {
38 let data: &CreateCommentLike = self;
39 let local_site = LocalSite::read(context.pool()).await?;
41 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
43 let mut recipient_ids = Vec::<LocalUserId>::new();
45 // Don't do a downvote if site has downvotes disabled
46 check_downvotes_enabled(data.score, &local_site)?;
48 let comment_id = data.comment_id;
49 let orig_comment = CommentView::read(context.pool(), comment_id, None).await?;
52 local_user_view.person.id,
53 orig_comment.community.id,
58 // Add parent poster or commenter to recipients
59 let comment_reply = CommentReply::read_by_comment(context.pool(), comment_id).await;
60 if let Ok(reply) = comment_reply {
61 let recipient_id = reply.recipient_id;
62 if let Ok(local_recipient) = LocalUserView::read_person(context.pool(), recipient_id).await {
63 recipient_ids.push(local_recipient.local_user.id);
67 let like_form = CommentLikeForm {
68 comment_id: data.comment_id,
69 post_id: orig_comment.post.id,
70 person_id: local_user_view.person.id,
74 // Remove any likes first
75 let person_id = local_user_view.person.id;
77 CommentLike::remove(context.pool(), person_id, comment_id).await?;
79 // Only add the like if the score isnt 0
80 let comment = orig_comment.comment;
81 let object = PostOrComment::Comment(Box::new(comment.into()));
82 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
84 let like_form2 = like_form.clone();
85 CommentLike::like(context.pool(), &like_form2)
87 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
91 &local_user_view.person.clone().into(),
92 orig_comment.community.id,
93 like_form.score.try_into()?,
98 // API doesn't distinguish between Undo/Like and Undo/Dislike
101 &local_user_view.person.clone().into(),
102 orig_comment.community.id,
109 send_comment_ws_message(
111 UserOperation::CreateCommentLike,
114 Some(local_user_view.person.id),