/** * @param User $user * * @return int * @throws Exception */ public function unlike(User $user) { if (!$this->userLikes($user)) { throw new Exception("User does not like that post"); } $post = $this->objectOrId instanceof Post ? $this->objectOrId : Post::find($this->objectOrId); $likes = Like::where('userId', $user->id)->where('postId', $post->id)->get(); $deleted = 0; foreach ($likes as $like) { if ($like->delete()) { ++$deleted; } } if ($deleted) { $post->likeCount -= $deleted; $post->save(); } return $deleted; }
/** * Handle the event. * * @param NewComment $event * * @return void */ public function handle(NewComment $event) { /** @var Post $post */ $post = Post::find($event->comment->postId); EventLog::create(['type' => EventLog::TYPE_NEW_COMMENT, 'commentId' => $event->comment->id, 'postId' => $event->comment->postId, 'userId' => $event->comment->userId, 'taskId' => $post->taskId]); $nm = new NotificationManager(); // Notify OP of post if ($post = $event->comment->post) { $n = $nm->createNotification($post->userId, Notification::TYPE_COMMENT_ON_POST); $n->postId = $event->comment->postId; $n->newCommentId = $event->comment->id; $n->fromUserId = $event->comment->userId; $nm->saveNotification($n); } // Notify OP of comment if reply if ($parentComment = $event->comment->parentComment) { $n = $nm->createNotification($parentComment->userId, Notification::TYPE_COMMENT_REPLY); $n->postId = $event->comment->postId; $n->commentId = $parentComment->id; $n->newCommentId = $event->comment->id; $n->fromUserId = $event->comment->userId; $nm->saveNotification($n); } }
public function getText() { $usernames = $this->getUsernameText(); $postTitle = null; if ($this->postId) { $post = Post::find($this->postId); $postTitle = $post ? $post->title : ''; } elseif ($this->commentId) { if ($comment = Comment::find($this->commentId)) { $post = Post::find($comment->postId); $postTitle = $post ? $post->title : ''; } } $stickerName = null; if ($this->stickerId) { $stickerRepository = new StickerRepository(); $sticker = $stickerRepository->find($this->stickerId); $stickerName = $sticker ? $sticker->name : ''; } $taskName = null; if ($this->taskId) { $taskRepository = new TaskRepository(); $task = $taskRepository->find($this->taskId); $taskName = $task ? $task->name : ''; } return Lang::get('notifications.types.' . $this->type, ['usernames' => $usernames, 'stickerName' => $stickerName, 'taskName' => $taskName, 'postTitle' => $postTitle]); }