/**
  * @api            {delete} /users/:username/notifications/:notificationId Delete Notification(s)
  * @apiGroup       User Notifications
  * @apiDescription Deletes a notification and others that were grouped together.
  * @apiUse         RequiresAuthentication
  *
  * @param Request      $request
  * @param User         $user
  * @param Notification $notification
  *
  * @return \Illuminate\Http\Response
  */
 public function destroy(Request $request, User $user, Notification $notification)
 {
     $this->requireUser($notification->userId);
     $nm = new NotificationManager();
     $rows = $nm->deleteNotification($notification);
     return $this->response(['success' => $rows > 0, 'deleted' => $rows]);
 }
 /**
  * Handle the event.
  *
  * @param  SubmissionRejected $event
  *
  * @return void
  */
 public function handle(SubmissionRejected $event)
 {
     $nm = new NotificationManager();
     $n = $nm->createNotification($event->submission->userId, Notification::TYPE_SUBMISSION_REJECTED);
     $n->submissionId = $event->submission->id;
     $n->taskId = $event->submission->taskId;
     $nm->saveNotification($n);
 }
 /**
  * Handle the event.
  *
  * @param  StickerEarnt $event
  *
  * @return void
  */
 public function handle(StickerEarnt $event)
 {
     EventLog::create(['type' => EventLog::TYPE_STICKER_EARNT, 'userId' => $event->user->id, 'stickerId' => $event->sticker->id, 'earntId' => $event->earntId]);
     $nm = new NotificationManager();
     $n = $nm->createNotification($event->user->id, Notification::TYPE_STICKER_EARNT);
     $n->stickerId = $event->sticker->id;
     $nm->saveNotification($n);
     $sticker = $event->sticker;
     $sticker->earntCount += 1;
     $sticker->save();
 }
 /**
  * - Send notification to the user
  * - Check if the user has earnt the sticker
  *
  * @param  SubmissionApproved $event
  *
  * @return void
  */
 public function handle(SubmissionApproved $event)
 {
     /** @var Post|null $post */
     $post = Post::where('submissionId', $event->submission->id)->first();
     if ($post) {
         EventLog::create(['type' => EventLog::TYPE_SUBMISSION_APPROVED, 'userId' => $event->submission->userId, 'taskId' => $event->submission->taskId, 'postId' => $post->id]);
     }
     $submission = $event->submission;
     $nm = new NotificationManager();
     $notification = $nm->createNotification($submission->userId, Notification::TYPE_SUBMISSION_APPROVED);
     $notification->submissionId = $submission->id;
     $notification->taskId = $submission->taskId;
     $nm->saveNotification($notification);
     $task = $submission->task;
     $task->completedCount += 1;
     $task->save();
     $pm = new ProgressManager($submission->user, $submission->task->sticker);
     if ($pm->completedSticker()) {
         $pm->giveSticker();
     }
     $submission->getTask()->updateRating();
 }
 /**
  * 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);
     }
 }
 /**
  * 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);
     }
 }