]> Untitled Git - lemmy.git/blobdiff - crates/api/src/comment.rs
Rewrite fetcher (#1792)
[lemmy.git] / crates / api / src / comment.rs
index 2237204f45d8fc6602d9abba4c5be0cdba1b4b6b..0af3324fae0b082159496a614a384ff858081944 100644 (file)
@@ -4,15 +4,23 @@ use lemmy_api_common::{
   blocking,
   check_community_ban,
   check_downvotes_enabled,
+  check_person_block,
   comment::*,
   get_local_user_view_from_jwt,
 };
-use lemmy_apub::ApubLikeableType;
+use lemmy_apub::{
+  activities::voting::{
+    undo_vote::UndoVote,
+    vote::{Vote, VoteType},
+  },
+  fetcher::post_or_comment::PostOrComment,
+};
 use lemmy_db_queries::{source::comment::Comment_, Likeable, Saveable};
 use lemmy_db_schema::{source::comment::*, LocalUserId};
 use lemmy_db_views::{comment_view::CommentView, local_user_view::LocalUserView};
 use lemmy_utils::{ApiError, ConnectionId, LemmyError};
-use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
+use lemmy_websocket::{send::send_comment_ws_message, LemmyContext, UserOperation};
+use std::convert::TryInto;
 
 #[async_trait::async_trait(?Send)]
 impl Perform for MarkCommentAsRead {
@@ -23,12 +31,12 @@ impl Perform for MarkCommentAsRead {
     context: &Data<LemmyContext>,
     _websocket_id: Option<ConnectionId>,
   ) -> Result<CommentResponse, LemmyError> {
-    let data: &MarkCommentAsRead = &self;
+    let data: &MarkCommentAsRead = self;
     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
 
     let comment_id = data.comment_id;
     let orig_comment = blocking(context.pool(), move |conn| {
-      CommentView::read(&conn, comment_id, None)
+      CommentView::read(conn, comment_id, None)
     })
     .await??;
 
@@ -46,14 +54,11 @@ impl Perform for MarkCommentAsRead {
 
     // Do the mark as read
     let read = data.read;
-    match blocking(context.pool(), move |conn| {
+    blocking(context.pool(), move |conn| {
       Comment::update_read(conn, comment_id, read)
     })
     .await?
-    {
-      Ok(comment) => comment,
-      Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
-    };
+    .map_err(|_| ApiError::err("couldnt_update_comment"))?;
 
     // Refetch it
     let comment_id = data.comment_id;
@@ -82,7 +87,7 @@ impl Perform for SaveComment {
     context: &Data<LemmyContext>,
     _websocket_id: Option<ConnectionId>,
   ) -> Result<CommentResponse, LemmyError> {
-    let data: &SaveComment = &self;
+    let data: &SaveComment = self;
     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
 
     let comment_saved_form = CommentSavedForm {
@@ -126,7 +131,7 @@ impl Perform for CreateCommentLike {
     context: &Data<LemmyContext>,
     websocket_id: Option<ConnectionId>,
   ) -> Result<CommentResponse, LemmyError> {
-    let data: &CreateCommentLike = &self;
+    let data: &CreateCommentLike = self;
     let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
 
     let mut recipient_ids = Vec::<LocalUserId>::new();
@@ -136,7 +141,7 @@ impl Perform for CreateCommentLike {
 
     let comment_id = data.comment_id;
     let orig_comment = blocking(context.pool(), move |conn| {
-      CommentView::read(&conn, comment_id, None)
+      CommentView::read(conn, comment_id, None)
     })
     .await??;
 
@@ -147,6 +152,13 @@ impl Perform for CreateCommentLike {
     )
     .await?;
 
+    check_person_block(
+      local_user_view.person.id,
+      orig_comment.get_recipient_id(),
+      context.pool(),
+    )
+    .await?;
+
     // Add parent user to recipients
     let recipient_id = orig_comment.get_recipient_id();
     if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
@@ -173,6 +185,7 @@ impl Perform for CreateCommentLike {
 
     // Only add the like if the score isnt 0
     let comment = orig_comment.comment;
+    let object = PostOrComment::Comment(Box::new(comment));
     let do_add = like_form.score != 0 && (like_form.score == 1 || like_form.score == -1);
     if do_add {
       let like_form2 = like_form.clone();
@@ -181,39 +194,35 @@ impl Perform for CreateCommentLike {
         return Err(ApiError::err("couldnt_like_comment").into());
       }
 
-      if like_form.score == 1 {
-        comment.send_like(&local_user_view.person, context).await?;
-      } else if like_form.score == -1 {
-        comment
-          .send_dislike(&local_user_view.person, context)
-          .await?;
-      }
+      Vote::send(
+        &object,
+        &local_user_view.person,
+        orig_comment.community.id,
+        like_form.score.try_into()?,
+        context,
+      )
+      .await?;
     } else {
-      comment
-        .send_undo_like(&local_user_view.person, context)
-        .await?;
+      // API doesn't distinguish between Undo/Like and Undo/Dislike
+      UndoVote::send(
+        &object,
+        &local_user_view.person,
+        orig_comment.community.id,
+        VoteType::Like,
+        context,
+      )
+      .await?;
     }
 
-    // Have to refetch the comment to get the current state
-    let comment_id = data.comment_id;
-    let person_id = local_user_view.person.id;
-    let liked_comment = blocking(context.pool(), move |conn| {
-      CommentView::read(conn, comment_id, Some(person_id))
-    })
-    .await??;
-
-    let res = CommentResponse {
-      comment_view: liked_comment,
-      recipient_ids,
-      form_id: None,
-    };
-
-    context.chat_server().do_send(SendComment {
-      op: UserOperation::CreateCommentLike,
-      comment: res.clone(),
+    send_comment_ws_message(
+      data.comment_id,
+      UserOperation::CreateCommentLike,
       websocket_id,
-    });
-
-    Ok(res)
+      None,
+      Some(local_user_view.person.id),
+      recipient_ids,
+      context,
+    )
+    .await
   }
 }