wip last see
This commit is contained in:
parent
d3994c5a00
commit
9bca444e99
20
app/Http/Middleware/UserLastSeen.php
Normal file
20
app/Http/Middleware/UserLastSeen.php
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Middleware;
|
||||||
|
|
||||||
|
use App\Services\UserService;
|
||||||
|
use Closure;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Auth;
|
||||||
|
|
||||||
|
class UserLastSeen
|
||||||
|
{
|
||||||
|
public function handle(Request $request, Closure $next)
|
||||||
|
{
|
||||||
|
if (Auth::check()) {
|
||||||
|
UserService::setLastSeen(Auth::id());
|
||||||
|
}
|
||||||
|
|
||||||
|
return $next($request);
|
||||||
|
}
|
||||||
|
}
|
@ -6,6 +6,8 @@
|
|||||||
use App\Models\Enums\Permission;
|
use App\Models\Enums\Permission;
|
||||||
use App\Models\Enums\UserRole;
|
use App\Models\Enums\UserRole;
|
||||||
use App\Models\Traits\HashableId;
|
use App\Models\Traits\HashableId;
|
||||||
|
use App\Services\UserService;
|
||||||
|
use Carbon\Carbon;
|
||||||
use Database\Factories\UserFactory;
|
use Database\Factories\UserFactory;
|
||||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||||
@ -68,6 +70,7 @@ class User extends Authenticatable
|
|||||||
*/
|
*/
|
||||||
protected $appends = [
|
protected $appends = [
|
||||||
'profile_photo_url',
|
'profile_photo_url',
|
||||||
|
'last_seen',
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,4 +105,14 @@ public function may(Permission|iterable|string $perm): bool
|
|||||||
{
|
{
|
||||||
return $this->isAdmin() || $this->can($perm);
|
return $this->isAdmin() || $this->can($perm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function lastSeen(): Carbon
|
||||||
|
{
|
||||||
|
return UserService::getLastSeen($this->id) ?? $this->created_at;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLastSeenAttribute(): Carbon
|
||||||
|
{
|
||||||
|
return $this->lastSeen();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
30
app/Providers/MenuServiceProvider.php
Normal file
30
app/Providers/MenuServiceProvider.php
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Providers;
|
||||||
|
|
||||||
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
|
||||||
|
class MenuServiceProvider extends ServiceProvider
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Register services.
|
||||||
|
*/
|
||||||
|
public function register(): void
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Bootstrap services.
|
||||||
|
*/
|
||||||
|
public function boot(): void
|
||||||
|
{
|
||||||
|
$verticalMenuJson = file_get_contents(base_path('resources/menu/verticalMenu.json'));
|
||||||
|
$verticalMenuData = json_decode($verticalMenuJson);
|
||||||
|
$horizontalMenuJson = file_get_contents(base_path('resources/menu/horizontalMenu.json'));
|
||||||
|
$horizontalMenuData = json_decode($horizontalMenuJson);
|
||||||
|
|
||||||
|
// Share all menuData to all the views
|
||||||
|
$this->app->make('view')->share('menuData', [$verticalMenuData, $horizontalMenuData]);
|
||||||
|
}
|
||||||
|
}
|
26
app/Services/UserService.php
Normal file
26
app/Services/UserService.php
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Redis;
|
||||||
|
|
||||||
|
final readonly class UserService
|
||||||
|
{
|
||||||
|
private static function lastSeenKey(int $userId): string
|
||||||
|
{
|
||||||
|
return sprintf('last_seen_%d', $userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function setLastSeen(int $userId, ?Carbon $seenAt = null): void
|
||||||
|
{
|
||||||
|
Redis::connection()->set(self::lastSeenKey($userId), ($seenAt ?? Carbon::now())->toISOString());
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getLastSeen(int $userId): ?Carbon
|
||||||
|
{
|
||||||
|
$lastSeen = Redis::connection()->get(self::lastSeenKey($userId));
|
||||||
|
|
||||||
|
return $lastSeen ? Carbon::parse($lastSeen) : null;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
use App\Http\Middleware\UserLastSeen;
|
||||||
use Illuminate\Foundation\Application;
|
use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Foundation\Configuration\Exceptions;
|
use Illuminate\Foundation\Configuration\Exceptions;
|
||||||
use Illuminate\Foundation\Configuration\Middleware;
|
use Illuminate\Foundation\Configuration\Middleware;
|
||||||
@ -12,7 +13,7 @@
|
|||||||
health: '/up',
|
health: '/up',
|
||||||
)
|
)
|
||||||
->withMiddleware(function (Middleware $middleware) {
|
->withMiddleware(function (Middleware $middleware) {
|
||||||
//
|
$middleware->append(UserLastSeen::class);
|
||||||
})
|
})
|
||||||
->withExceptions(function (Exceptions $exceptions) {
|
->withExceptions(function (Exceptions $exceptions) {
|
||||||
//
|
//
|
||||||
|
Loading…
Reference in New Issue
Block a user