/**
  * @api            {delete} /posts/:postId/likes Unlike A Post
  * @apiGroup       Likes
  * @apiDescription Remove the current user's like of a post.
  * @apiUse         RequiresAuthentication
  *
  * @param Post $post
  *
  * @return \Illuminate\Http\Response
  * @throws \Exception
  */
 public function destroy(Post $post)
 {
     $user = $this->requireAuthentication();
     $postLikeManager = $post->getLikeManager();
     $success = !!$postLikeManager->unlike($user);
     return $this->response(['success' => $success, 'post' => $post->fresh()]);
 }
 /**
  * @api            {post} /tasks/:slug/posts Create A Post
  * @apiGroup       Task Posts
  * @apiDescription Add a post (tip/question) on a task. For submissions use the "Add A Task Submission" endpoint
  *                 and a post will be generated.
  *
  * @param Request $request
  * @param Task    $task
  *
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request, Task $task)
 {
     $this->requireAuthentication();
     $this->validate($request, ['title' => 'required', 'text' => 'string', 'image' => '', 'type' => 'in:question,tip']);
     $post = new Post(['taskId' => $task->id, 'userId' => $this->user->id, 'title' => $request->input('title'), 'text' => $request->input('text'), 'type' => $request->input('type')]);
     $post->save();
     $post = $post->fresh();
     return $this->response(['post' => $post]);
 }
示例#3
0
 public static function boot()
 {
     parent::boot();
     static::deleting(function (Submission $submission) {
         Post::where('submissionId', $submission->id)->delete();
     });
 }
 public function getIndex()
 {
     $pendingSubmissions = Submission::notApproved()->count();
     $ordersNeedPrinting = Order::whereNull('printedAt')->count();
     $ordersNeedShipping = Order::whereNull('shippedAt')->count();
     $newMembers = User::where('createdAt', '>=', date('Y-m-d 00:00:00'))->count();
     $newPosts = Post::where('createdAt', '>=', date('Y-m-d 00:00:00'))->count();
     $newPosts += Comment::where('createdAt', '>=', date('Y-m-d 00:00:00'))->count();
     return $this->view('admin::dashboard', ['pendingSubmissions' => $pendingSubmissions, 'ordersNeedPrinting' => $ordersNeedPrinting, 'ordersNeedShipping' => $ordersNeedShipping, 'newMembers' => $newMembers, 'newPosts' => $newPosts]);
 }
 /**
  * @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;
 }
 /**
  * - 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();
 }
 /**
  * Define your route model bindings, pattern filters, etc.
  *
  * @param  \Illuminate\Routing\Router $router
  *
  * @return void
  */
 public function boot(Router $router)
 {
     /**
      * Route model binding
      */
     $router->bind('category', function ($slug) {
         return app('CategoryRepository')->findBySlugOrFail($slug);
     });
     $router->bind('user', function ($username) {
         return app('UserRepository')->findBySlugOrFail($username);
     });
     $router->bind('sticker', function ($slug) {
         return app('StickerRepository')->findBySlugOrFail($slug);
     });
     $router->bind('task', function ($slug) {
         return app('TaskRepository')->findBySlugOrFail($slug);
     });
     $router->bind('submission', function ($id) {
         return \Stickable\Models\Submission::findOrFail($id);
     });
     $router->bind('post', function ($id) {
         return \Stickable\Models\Post::findOrFail($id);
     });
     $router->bind('comment', function ($id) {
         return \Stickable\Models\Comment::findOrFail($id);
     });
     $router->bind('notification', function ($id) {
         return \Stickable\Models\Notification::findOrFail($id);
     });
     $router->bind('event', function ($id) {
         return \Stickable\Models\EventLog::findOrFail($id);
     });
     $router->bind('todo', function ($id) {
         return \Stickable\Models\ToDo::findOrFail($id);
     });
     parent::boot($router);
 }
 /**
  * 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);
     }
 }
 /**
  * Returns the top 3 levels of comments on a post.
  *
  * @param Post $post
  *
  * @return Comment[]
  */
 public function getPostCommentsWithReplies(Post $post)
 {
     $comments = $post->comments()->where('depth', '<=', 2)->orderBy('depth', 'ASC')->orderBy('likeCount', 'DESC')->orderBy('createdAt', 'ASC')->get()->getDictionary();
     //Get a dictionary keyed by primary keys
     return $this->sortCommentReplies($comments);
 }
 /**
  * @api            {delete} /posts/:postId Delete A Post
  * @apiGroup       Task Posts
  * @apiDescription Delete a post on a task.
  * @apiUse         RequiresAuthentication
  * @apiUse         GenericSuccessResponse
  *
  * @param Post $post
  *
  * @return \Illuminate\Http\Response
  */
 public function destroy(Post $post)
 {
     $this->requireUserOrRole($post->getUser(), Role::ROLE_MANAGE_POSTS);
     $success = $post->delete();
     return $this->successResponse($success);
 }
示例#11
0
 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]);
 }