]> Untitled Git - lemmy.git/blobdiff - crates/api/src/comment/like.rs
Moving settings to Database. (#2492)
[lemmy.git] / crates / api / src / comment / like.rs
index 48302ac6509ce955df10e0334899a262434a5a49..754072ee204daab38f3dc1ca6beb5bdb808d7188 100644 (file)
@@ -13,7 +13,11 @@ use lemmy_apub::{
 };
 use lemmy_db_schema::{
   newtypes::LocalUserId,
-  source::comment::{CommentLike, CommentLikeForm},
+  source::{
+    comment::{CommentLike, CommentLikeForm},
+    comment_reply::CommentReply,
+    local_site::LocalSite,
+  },
   traits::Likeable,
 };
 use lemmy_db_views::structs::{CommentView, LocalUserView};
@@ -32,13 +36,14 @@ impl Perform for CreateCommentLike {
     websocket_id: Option<ConnectionId>,
   ) -> Result<CommentResponse, LemmyError> {
     let data: &CreateCommentLike = self;
+    let local_site = blocking(context.pool(), LocalSite::read).await??;
     let local_user_view =
       get_local_user_view_from_jwt(&data.auth, context.pool(), context.secret()).await?;
 
     let mut recipient_ids = Vec::<LocalUserId>::new();
 
     // Don't do a downvote if site has downvotes disabled
-    check_downvotes_enabled(data.score, context.pool()).await?;
+    check_downvotes_enabled(data.score, &local_site)?;
 
     let comment_id = data.comment_id;
     let orig_comment = blocking(context.pool(), move |conn| {
@@ -53,14 +58,20 @@ impl Perform for CreateCommentLike {
     )
     .await?;
 
-    // Add parent user to recipients
-    let recipient_id = orig_comment.get_recipient_id();
-    if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
-      LocalUserView::read_person(conn, recipient_id)
+    // Add parent poster or commenter to recipients
+    let comment_reply = blocking(context.pool(), move |conn| {
+      CommentReply::read_by_comment(conn, comment_id)
     })
-    .await?
-    {
-      recipient_ids.push(local_recipient.local_user.id);
+    .await?;
+    if let Ok(reply) = comment_reply {
+      let recipient_id = reply.recipient_id;
+      if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
+        LocalUserView::read_person(conn, recipient_id)
+      })
+      .await?
+      {
+        recipient_ids.push(local_recipient.local_user.id);
+      }
     }
 
     let like_form = CommentLikeForm {
@@ -83,7 +94,7 @@ impl Perform for CreateCommentLike {
     let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
     if do_add {
       let like_form2 = like_form.clone();
-      let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
+      let like = move |conn: &mut _| CommentLike::like(conn, &like_form2);
       blocking(context.pool(), like)
         .await?
         .map_err(|e| LemmyError::from_error_message(e, "couldnt_like_comment"))?;