2018-05-26 23:00:07 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2018-06-09 23:30:54 +00:00
|
|
|
use Carbon\Carbon;
|
2018-05-27 03:25:04 +00:00
|
|
|
use Auth, Cache, Redis;
|
2018-05-26 23:00:07 +00:00
|
|
|
use App\{Notification, Profile, User};
|
|
|
|
|
|
|
|
class AccountController extends Controller
|
|
|
|
{
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->middleware('auth');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function notifications(Request $request)
|
|
|
|
{
|
2018-06-09 23:30:54 +00:00
|
|
|
$this->validate($request, [
|
|
|
|
'page' => 'nullable|min:1|max:3'
|
|
|
|
]);
|
2018-05-27 03:25:04 +00:00
|
|
|
$profile = Auth::user()->profile;
|
2018-06-09 23:30:54 +00:00
|
|
|
$timeago = Carbon::now()->subMonths(6);
|
2018-06-04 08:16:33 +00:00
|
|
|
$notifications = Notification::whereProfileId($profile->id)
|
2018-06-09 23:30:54 +00:00
|
|
|
->whereDate('created_at', '>', $timeago)
|
|
|
|
->orderBy('id','desc')
|
|
|
|
->take(30)
|
|
|
|
->simplePaginate();
|
|
|
|
|
2018-05-27 03:25:04 +00:00
|
|
|
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.';
|
2018-05-30 03:03:11 +00:00
|
|
|
$notifications = collect([]);
|
2018-05-27 03:25:04 +00:00
|
|
|
foreach($keys as $key) {
|
2018-05-30 03:03:11 +00:00
|
|
|
$notifications->push(Cache::get("{$prefix}{$key}"));
|
2018-05-27 03:25:04 +00:00
|
|
|
}
|
|
|
|
return $notifications;
|
2018-05-26 23:00:07 +00:00
|
|
|
}
|
|
|
|
}
|