/** * Display the specified resource. * * @param int $id * @return Response */ public function show($topic_id) { $posts = \Nexus\Post::with('author')->where('topic_id', $topic_id)->orderBy('id', 'dsc'); $topic = \Nexus\Topic::findOrFail($topic_id); // is this topic readonly to the authenticated user? $readonly = true; if ($topic->readonly == false) { $readonly = false; } if ($topic->section->moderator->id === \Auth::user()->id) { $readonly = false; } if (\Auth::user()->administrator) { $readonly = false; } // is this topic secret to the authenticated user? $userCanSeeSecrets = false; if ($topic->section->moderator->id === \Auth::user()->id) { $userCanSeeSecrets = true; } if (\Auth::user()->administrator) { $userCanSeeSecrets = true; } // get the previously read progress so we can indicate this in the view $readProgress = \Nexus\Helpers\ViewHelper::getReadProgress(\Auth::user(), $topic); // get the subscription status $topicStatus = \Nexus\Helpers\ViewHelper::getTopicStatus(\Auth::user(), $topic); $unsubscribed = $topicStatus['unsubscribed']; \Nexus\Helpers\ViewHelper::updateReadProgress(\Auth::user(), $topic); \Nexus\Helpers\ActivityHelper::updateActivity(\Auth::user()->id, "Reading <em>{$topic->title}</em>", action('Nexus\\TopicController@show', ['id' => $topic->id])); $breadcrumbs = \Nexus\Helpers\BreadcrumbHelper::breadcrumbForTopic($topic); return view('topics.index', compact('topic', 'posts', 'readonly', 'userCanSeeSecrets', 'readProgress', 'breadcrumbs', 'unsubscribed')); }
/** * reports on the status of a given topic for a user * * @param \Nexus\User $user - the user * @param \Nexus\Topic $topic - a topic * @return array - of status values */ public static function getTopicStatus(\Nexus\User $user, \Nexus\Topic $topic) { $status = ['new_posts' => false, 'never_read' => false, 'unsubscribed' => false]; $mostRecentlyReadPostDate = \Nexus\Helpers\ViewHelper::getReadProgress($user, $topic); $mostRecentPostDate = $topic->most_recent_post_time; $view = \Nexus\View::where('topic_id', $topic->id)->where('user_id', $user->id)->first(); if ($view !== null) { if ($view->unsubscribed != 0) { $status['unsubscribed'] = true; } if ($mostRecentPostDate !== false && $mostRecentlyReadPostDate !== false) { if ($mostRecentPostDate->gt($mostRecentlyReadPostDate)) { $status['new_posts'] = true; } } } else { $status['never_read'] = true; } return $status; }
public function test_getReadProgress_returns_time_of_most_recently_read_post_time() { $faker = \Faker\Factory::create(); // GIVEN we have a user $user = factory(User::class, 1)->create(); // AND we have a topic with posts $topic = factory(Topic::class, 1)->create(); factory(Post::class, 20)->create(['topic_id' => $topic->id, 'time' => $faker->dateTimeThisMonth('-2 days')]); // AND the most recent post being from yesterday $newPost = factory(Post::class, 1)->create(['topic_id' => $topic->id, 'time' => $faker->dateTimeThisMonth('-1 days')]); // WHEN the user reads the topic \Nexus\Helpers\ViewHelper::updateReadProgress($user, $topic); // THEN the date of the most recent post in the topic matches // the one most recently read by the user $this->assertEquals($topic->most_recent_post_time, \Nexus\Helpers\ViewHelper::getReadProgress($user, $topic)); // WHEN a new post is added now $anotherPost = factory(Post::class, 1)->create(['topic_id' => $topic->id, 'time' => new \DateTime('now')]); // THEN the date of the most recent post in the topic does not matches // the one most recently read by the user $this->assertNotEquals($topic->most_recent_post_time, \Nexus\Helpers\ViewHelper::getReadProgress($user, $topic)); }