/** * Handle the event. * * @param NewLike $event * * @return void */ public function handle(NewLike $event) { $nm = new NotificationManager(); if ($event->like->commentId && ($comment = $event->like->comment)) { $notif = $nm->createNotification($comment->userId, Notification::TYPE_LIKED_COMMENT); $notif->commentId = $comment->id; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } if ($event->like->postId && ($post = $event->like->post)) { $notif = $nm->createNotification($post->userId, Notification::TYPE_LIKED_POST); $notif->postId = $post->id; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } if ($event->like->earntId) { if ($earn = DB::selectOne('SELECT * FROM user_earnt_stickers WHERE id = ?', [$event->like->earntId])) { $notif = $nm->createNotification($earn->userId, Notification::TYPE_LIKED_EARNT); $notif->stickerId = $earn->stickerId; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } } if ($event->like->toDoId) { if ($toDo = ToDo::find($event->like->toDoId)) { /** @var ToDo $toDo */ $type = $toDo->taskId ? Notification::TYPE_LIKED_TODO_TASK : Notification::TYPE_LIKED_TODO_STICKER; $notif = $nm->createNotification($toDo->userId, $type); $notif->stickerId = $toDo->stickerId; $notif->taskId = $toDo->taskId; $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } } if ($event->like->joinId) { $notif = $nm->createNotification($event->like->joinId, Notification::TYPE_LIKED_JOIN); $notif->likeId = $event->like->id; $notif->fromUserId = $event->like->userId; $nm->saveNotification($notif); } }
/** * @param User $user * * @return int * @throws Exception */ public function unlike(User $user) { if (!$this->userLikes($user)) { throw new Exception("User does not like that to do"); } $toDo = $this->objectOrId instanceof ToDo ? $this->objectOrId : ToDo::find($this->objectOrId); $likes = Like::where('userId', $user->id)->where('toDoId', $toDo->id)->get(); $deleted = 0; foreach ($likes as $like) { if ($like->delete()) { ++$deleted; } } if ($deleted) { $toDo->likeCount -= $deleted; $toDo->save(); } return $deleted; }