Add basic notification support

This commit is contained in:
Daniel Supernault 2018-05-26 21:25:04 -06:00
parent 3ec93d096c
commit 1406f467a0
4 changed files with 78 additions and 5 deletions

View file

@ -3,7 +3,7 @@
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth, Cache;
use Auth, Cache, Redis;
use App\{Notification, Profile, User};
class AccountController extends Controller
@ -15,8 +15,33 @@ class AccountController extends Controller
public function notifications(Request $request)
{
$user = Auth::user();
$profile = $user->profile;
return view('account.activity', compact('profile'));
$profile = Auth::user()->profile;
$notifications = $this->fetchNotifications($profile->id);
return view('account.activity', compact('profile', 'notifications'));
}
public function fetchNotifications($id)
{
$key = config('cache.prefix') . ":user.{$id}.notifications";
$redis = Redis::connection();
$notifications = $redis->lrange($key, 0, 30);
if(empty($notifications)) {
$notifications = Notification::whereProfileId($id)
->orderBy('id','desc')->take(30)->get();
} else {
$notifications = $this->hydrateNotifications($notifications);
}
return $notifications;
}
public function hydrateNotifications($keys)
{
$prefix = 'notification.';
$notifications = [];
foreach($keys as $key) {
$notifications[] = Cache::get($prefix . $key);
}
return $notifications;
}
}

View file

@ -6,5 +6,15 @@ use Illuminate\Database\Eloquent\Model;
class Notification extends Model
{
//
public function actor()
{
return $this->belongsTo(Profile::class, 'actor_id', 'id');
}
public function profile()
{
return $this->belongsTo(Profile::class, 'profile_id', 'id');
}
}

View file

@ -77,3 +77,12 @@ body, button, input, textarea {
.no-caret.dropdown-toggle::after {
display:none;
}
.notification-page .profile-link {
color: #212529;
font-weight: bold;
}
.notification-page .list-group-item:first-child {
border-top: none;
}

View file

@ -0,0 +1,29 @@
@extends('layouts.app')
@section('content')
<div class="container notification-page" style="min-height: 60vh;">
<div class="col-12 col-md-8 offset-2">
<ul class="list-group">
@foreach($notifications as $notification)
<li class="list-group-item notification">
@switch($notification->action)
@case('like')
<span class="notification-icon pr-3">
<img src="{{$notification->actor->avatarUrl()}}" width="32px" class="rounded-circle">
</span>
<span class="notification-text">
{!! $notification->rendered !!}
<span class="text-muted notification-timestamp pl-1">{{$notification->created_at->diffForHumans(null, true, true, true)}}</span>
</span>
<span class="float-right notification-action">
</span>
@break
@endswitch
</li>
@endforeach
</ul>
</div>
</div>
@endsection