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,
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 = blocking(context.pool(), LocalSite::read).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 = blocking(context.pool(), move |conn| {
50 CommentView::read(conn, comment_id, None)
55 local_user_view.person.id,
56 orig_comment.community.id,
61 // Add parent poster or commenter to recipients
62 let comment_reply = blocking(context.pool(), move |conn| {
63 CommentReply::read_by_comment(conn, comment_id)
66 if let Ok(reply) = comment_reply {
67 let recipient_id = reply.recipient_id;
68 if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
69 LocalUserView::read_person(conn, recipient_id)
73 recipient_ids.push(local_recipient.local_user.id);
77 let like_form = CommentLikeForm {
78 comment_id: data.comment_id,
79 post_id: orig_comment.post.id,
80 person_id: local_user_view.person.id,
84 // Remove any likes first
85 let person_id = local_user_view.person.id;
86 blocking(context.pool(), move |conn| {
87 CommentLike::remove(conn, person_id, comment_id)
91 // Only add the like if the score isnt 0
92 let comment = orig_comment.comment;
93 let object = PostOrComment::Comment(Box::new(comment.into()));
94 let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
96 let like_form2 = like_form.clone();
97 let like = move |conn: &mut _| CommentLike::like(conn, &like_form2);
98 blocking(context.pool(), like)
100 .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;
104 &local_user_view.person.clone().into(),
105 orig_comment.community.id,
106 like_form.score.try_into()?,
111 // API doesn't distinguish between Undo/Like and Undo/Dislike
114 &local_user_view.person.clone().into(),
115 orig_comment.community.id,
122 send_comment_ws_message(
124 UserOperation::CreateCommentLike,
127 Some(local_user_view.person.id),