Explorar o código

Add basic notification support

Daniel Supernault %!s(int64=7) %!d(string=hai) anos
pai
achega
1406f467a0

+ 29 - 4
app/Http/Controllers/AccountController.php

@@ -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;
     }
 }

+ 11 - 1
app/Notification.php

@@ -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');
+  }
+
 }

+ 9 - 0
resources/assets/sass/custom.scss

@@ -76,4 +76,13 @@ 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;
 }

+ 29 - 0
resources/views/account/activity.blade.php

@@ -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