Skip to content

feat:(LAR-89) add tracking for user #259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/Http/Middleware/TrackLastActivity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Carbon\Carbon;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;

final class TrackLastActivity
{
public function handle(Request $request, Closure $next): Response
{
if ($user = $request->user()) {
$lastActive = $user->last_active_at;

if (! $lastActive || $lastActive->diffInMinutes(Carbon::now()) >= 3) {
$user->update([
'last_active_at' => now(),
]);
}
}

return $next($request);
}
}
4 changes: 4 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @property Carbon | null $banned_at
* @property Carbon $created_at
* @property Carbon $updated_at
* @property Carbon | null $last_active_at
* @property Collection | Activity[] $activities
* @property Collection | Article[] $articles
* @property Collection | Thread[] $threads
Expand Down Expand Up @@ -96,20 +97,23 @@ final class User extends Authenticatable implements FilamentUser, HasAvatar, Has
'banned_at',
'banned_reason',
'opt_in',
'last_active_at',
];

protected $hidden = [
'password',
'remember_token',
'two_factor_recovery_codes',
'two_factor_secret',
'last_active_at',
];

protected $casts = [
'email_verified_at' => 'datetime',
'last_login_at' => 'datetime',
'banned_at' => 'datetime',
'settings' => 'array',
'last_active_at' => 'datetime',
];

public function hasProvider(string $provider): bool
Expand Down
1 change: 1 addition & 0 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
]);
$middleware->web(append: [
LocaleMiddleware::class,
\App\Http\Middleware\TrackLastActivity::class,
]);
})
->withExceptions(function (Exceptions $exceptions): void {
Expand Down
8 changes: 4 additions & 4 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
public function up(): void
{
Schema::table('users', static function (Blueprint $table): void {
$table->timestamp('last_active_at')->nullable()->after('last_login_ip');
});
}
};
10 changes: 10 additions & 0 deletions tests/Feature/UserActivitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Activity;
use App\Models\Article;
use Carbon\Carbon;
use Illuminate\Support\Facades\Route;

it('records activity when an article is created', function (): void {
$user = $this->login();
Expand Down Expand Up @@ -39,3 +40,12 @@
Carbon::now()->subWeek()->format('Y-m-d')
));
});

it('does not update the last activity for unauthenticated users', function (): void {
Route::middleware(\App\Http\Middleware\TrackLastActivity::class)
->get('/activity-user', fn () => 'ok');

$this->get('/activity-user')->assertOk();

$this->assertDatabaseMissing('users', ['last_active_at' => now()]);
});
Loading