public function show($id) { $postStartId = Request::input('start'); $postEndId = get_int(Request::input('end')); $nthPost = get_int(Request::input('n')); $skipLayout = Request::input('skip_layout') === '1'; $jumpTo = null; $topic = Topic::with('forum.cover')->findOrFail($id); $this->authorizeView($topic->forum); $posts = $topic->posts(); if ($postStartId === 'unread') { $postStartId = Post::lastUnreadByUser($topic, Auth::user()); } else { $postStartId = get_int($postStartId); } if ($nthPost !== null) { $post = $topic->nthPost($nthPost); if ($post) { $postStartId = $post->post_id; } } if (!$skipLayout) { foreach ([$postStartId, $postEndId, 0] as $jumpPoint) { if ($jumpPoint === null) { continue; } $jumpTo = $jumpPoint; break; } } if ($postStartId !== null && !$skipLayout) { // move starting post up by ten to avoid hitting // page autoloader right after loading the page. $postPosition = $topic->postPosition($postStartId); $post = $topic->nthPost($postPosition - 10); $postStartId = $post->post_id; } if ($postStartId !== null) { $posts = $posts->where('post_id', '>=', $postStartId); } elseif ($postEndId !== null) { $posts = $posts->where('post_id', '<=', $postEndId)->orderBy('post_id', 'desc'); } $posts = $posts->take(20)->with('user.rank')->with('user.country')->with('user.supports')->get()->sortBy(function ($p) { return $p->post_id; }); if ($posts->count() === 0) { abort($skipLayout ? 204 : 404); } $postsPosition = $topic->postsPosition($posts); Event::fire(new TopicWasViewed($topic, $posts->last(), Auth::user())); $template = $skipLayout ? '_posts' : 'show'; $cover = fractal_item_array($topic->cover()->firstOrNew([]), new TopicCoverTransformer()); return view("forum.topics.{$template}", compact('topic', 'posts', 'postsPosition', 'jumpTo', 'cover')); }
public function show($id) { $postStartId = Request::input("start"); $postEndId = Request::input("end"); $nthPost = Request::input("n"); $skipLayout = Request::input("skip_layout") === "1"; $jumpTo = 0; $topic = Topic::findOrFail($id); $this->authorizeView($topic->forum); $posts = $topic->posts(); if ($postStartId === "unread") { $postStartId = Post::lastUnreadByUser($topic, Auth::user()); } if (is_numeric($nthPost)) { $post = $topic->nthPost($nthPost); if ($post) { $postStartId = $post->post_id; } } if (is_numeric($postStartId) && $skipLayout === false) { // move starting post up by ten to avoid hitting // page autoloader right after loading the page. $postPosition = $topic->postPosition($postStartId); $post = $topic->nthPost($postPosition - 10); $jumpTo = $postStartId; $postStartId = $post->post_id; } if (is_numeric($postStartId)) { $posts = $posts->where("post_id", ">=", $postStartId); } elseif (is_numeric($postEndId)) { $posts = $posts->where("post_id", "<=", $postEndId)->orderBy("post_id", "desc"); } $posts = $posts->take(20)->with("user.rank")->with("user.country")->get()->sortBy(function ($p) { return $p->post_id; }); if ($posts->count() === 0) { abort($skipLayout ? 204 : 404); } $postsPosition = $topic->postsPosition($posts); Event::fire(new TopicWasViewed($topic, $posts->last(), Auth::user())); $template = $skipLayout ? "_posts" : "show"; return view("forum.topics.{$template}", compact("topic", "posts", "postsPosition", "jumpTo")); }
public function show($id) { $postStartId = Request::input('start'); $postEndId = Request::input('end'); $nthPost = Request::input('n'); $skipLayout = Request::input('skip_layout') === '1'; $jumpTo = 0; $topic = Topic::findOrFail($id); $this->authorizeView($topic->forum); $posts = $topic->posts(); if ($postStartId === 'unread') { $postStartId = Post::lastUnreadByUser($topic, Auth::user()); } if (is_numeric($nthPost)) { $post = $topic->nthPost($nthPost); if ($post) { $postStartId = $post->post_id; } } if (is_numeric($postStartId) && $skipLayout === false) { // move starting post up by ten to avoid hitting // page autoloader right after loading the page. $postPosition = $topic->postPosition($postStartId); $post = $topic->nthPost($postPosition - 10); $jumpTo = $postStartId; $postStartId = $post->post_id; } if (is_numeric($postStartId)) { $posts = $posts->where('post_id', '>=', $postStartId); } elseif (is_numeric($postEndId)) { $posts = $posts->where('post_id', '<=', $postEndId)->orderBy('post_id', 'desc'); } $posts = $posts->take(20)->with('user.rank')->with('user.country')->get()->sortBy(function ($p) { return $p->post_id; }); if ($posts->count() === 0) { abort($skipLayout ? 204 : 404); } $postsPosition = $topic->postsPosition($posts); Event::fire(new TopicWasViewed($topic, $posts->last(), Auth::user())); $template = $skipLayout ? '_posts' : 'show'; return view("forum.topics.{$template}", compact('topic', 'posts', 'postsPosition', 'jumpTo')); }