checkComment($id); $user = $this->getLoginUser(); $validator = new UserLimitValidator(); $validator->checkDailyCommentLikeLimit($user); $likeRepo = new CommentLikeRepo(); $commentLike = $likeRepo->findCommentLike($comment->id, $user->id); if (!$commentLike) { $commentLike = new CommentLikeModel(); $commentLike->comment_id = $comment->id; $commentLike->user_id = $user->id; $commentLike->create(); } else { $commentLike->comment_id = $commentLike->deleted == 1 ? 0 : 1; $commentLike->update(); $this->decrCommentLikeCount($comment); } if ($commentLike->deleted == 0) { $action = 'do'; $this->incrCommentLikeCount($comment); $this->eventsManager->fire('Comment:afterLike', $this, $comment); } else { $action = 'undo'; $this->decrCommentLikeCount($comment); $this->eventsManager->fire('Comment:afterUndoLike', $this, $comment); } $isOwner = $user->id == $comment->owner_id; /** * 仅首次点赞发送通知 */ if (!$commentLike && $isOwner) { $this->handleCommentLikedNotice($comment, $user); } return [ 'action' => $action, 'count' => $comment->like_count, ]; } protected function incrCommentLikeCount(CommentModel $comment) { $comment->like_count += 1; $comment->update(); } protected function decrCommentLikeCount(CommentModel $comment) { if ($comment->like_count > 0) { $comment->like_count -= 1; $comment->update(); } } protected function incrUserDailyCommentLikeCount(UserModel $user) { $this->eventsManager->fire('UserDailyCounter:incrCommentLikeCount', $this, $user); } protected function handleCommentLikedNotice(CommentModel $comment, UserModel $sender) { $notice = new CommentLikedNotice(); $notice->handle($comment, $sender); } }