Permalink
Browse files

Episode 48

  • Loading branch information...
JeffreyWay committed Jun 6, 2017
1 parent 7c9cee4 commit 10dd72acf5b990f6871d78bd0a13a8ac2b8ac1f2
@@ -79,6 +79,10 @@ public function store(Request $request)
*/
public function show($channel, Thread $thread)
{
if (auth()->check()) {
auth()->user()->read($thread);
}
return view('threads.show', compact('thread'));
}
@@ -171,4 +171,17 @@ public function getIsSubscribedToAttribute()
->where('user_id', auth()->id())
->exists();
}
/**
* Determine if the thread has been updated since the user last read it.
*
* @param User $user
* @return bool
*/
public function hasUpdatesFor($user)
{
$key = $user->visitedThreadCacheKey($this);
return $this->updated_at > cache($key);
}
}
@@ -2,6 +2,7 @@
namespace App;
use Carbon\Carbon;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
@@ -56,4 +57,28 @@ public function activity()
{
return $this->hasMany(Activity::class);
}
/**
* Record that the user has read the given thread.
*
* @param Thread $thread
*/
public function read($thread)
{
cache()->forever(
$this->visitedThreadCacheKey($thread),
Carbon::now()
);
}
/**
* Get the cache key for when a user reads a thread.
*
* @param Thread $thread
* @return string
*/
public function visitedThreadCacheKey($thread)
{
return sprintf("users.%s.visits.%s", $this->id, $thread->id);
}
}
@@ -10,7 +10,13 @@
<div class="level">
<h4 class="flex">
<a href="{{ $thread->path() }}">
{{ $thread->title }}
@if ($thread->hasUpdatesFor(auth()->user()))
<strong>
{{ $thread->title }}
</strong>
@else
{{ $thread->title }}
@endif
</a>
</h4>
@@ -117,4 +117,20 @@ function it_knows_if_the_authenticated_user_is_subscribed_to_it()
$this->assertTrue($thread->isSubscribedTo);
}
/** @test */
function a_thread_can_check_if_the_authenticated_user_has_read_all_replies()
{
$this->signIn();
$thread = create('App\Thread');
tap(auth()->user(), function ($user) use ($thread) {
$this->assertTrue($thread->hasUpdatesFor($user));
$user->read($thread);
$this->assertFalse($thread->hasUpdatesFor($user));
});
}
}

0 comments on commit 10dd72a

Please sign in to comment.