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,
17 comment::{CommentLike, CommentLikeForm},
18 comment_reply::CommentReply,
22 use lemmy_db_views::structs::{CommentView, LocalUserView};
23 use lemmy_utils::{error::LemmyError, ConnectionId};
24 use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation};
25 use std::convert::TryInto;
27 #[async_trait::async_trait(?Send)]
28 impl Perform for CreateCommentLike {
29 type Response = CommentResponse;
31 #[tracing::instrument(skip(context, websocket_id))]
34 context: &Data<LemmyContext>,
35 websocket_id: Option<ConnectionId>,
36 ) -> Result<CommentResponse, LemmyError> {
37 let data: &CreateCommentLike = self;
39 get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
41 let mut recipient_ids = Vec::<LocalUserId>::new();
43 // Don't do a downvote if site has downvotes disabled
44 check_downvotes_enabled(data.score, context.pool()).await?;
46 let comment_id = data.comment_id;
47 let orig_comment = blocking(context.pool(), move |conn| {
48 CommentView::read(conn, comment_id, None)
53 local_user_view.person.id,
54 orig_comment.community.id,
59 // Add parent poster or commenter to recipients
60 let comment_reply = blocking(context.pool(), move |conn| {
61 CommentReply::read_by_comment(conn, comment_id)
64 if let Ok(reply) = comment_reply {
65 let recipient_id = reply.recipient_id;
66 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
67 LocalUserView::read_person(conn, recipient_id)
71 recipient_ids.push(local_recipient.local_user.id);
75 let like_form = CommentLikeForm {
76 comment_id: data.comment_id,
77 post_id: orig_comment.post.id,
78 person_id: local_user_view.person.id,
82 // Remove any likes first
83 let person_id = local_user_view.person.id;
84 blocking(context.pool(), move |conn| {
85 CommentLike::remove(conn, person_id, comment_id)
89 // Only add the like if the score isnt 0
90 let comment = orig_comment.comment;
91 let object = PostOrComment::Comment(Box::new(comment.into()));
92 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
94 let like_form2 = like_form.clone();
95 let like = move |conn: &mut _| CommentLike::like(conn, &like_form2);
96 blocking(context.pool(), like)
98 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
102 &local_user_view.person.clone().into(),
103 orig_comment.community.id,
104 like_form.score.try_into()?,
109 // API doesn't distinguish between Undo/Like and Undo/Dislike
112 &local_user_view.person.clone().into(),
113 orig_comment.community.id,
120 send_comment_ws_message(
122 UserOperation::CreateCommentLike,
125 Some(local_user_view.person.id),