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 {
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??;
// 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;
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 {
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();
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??;
)
.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| {
// 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();
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
}
}