Repository navigation
Expand file tree
/
Copy pathUpdateUserLastSeenAt.php
More file actions
51 lines (43 loc) · 2.27 KB
/
Copy pathUpdateUserLastSeenAt.php
File metadata and controls
51 lines (43 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace App\Actions;
use App\Enums\ActiveDayApp;
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
final readonly class UpdateUserLastSeenAt
{
/**
* Remembers the day someone was last seen, not the time, so it is written at most once a day. The first time they
* are seen in a day also counts them in that day's active users. Only the request whose update moves the day does,
* so two requests at once can't count someone twice. It leaves updated_at alone, which the sitemap reads as the day
* the profile last changed.
*
* Each request also records the day and the app, for retention and to know which apps people use. A row is only
* written the first time an app is seen in a day: `not exists` makes every other request a read, as an upsert that
* only skips its update would still lock the row. Selecting from users means an account deleted by this very request
* gets no row. The count comes first, so failing to record an app can't lose someone from the active users.
*/
public function handle(User $user, ActiveDayApp $app): void
{
$today = today()->toDateString();
$moved = 0;
if ($user->last_seen_at?->isToday() !== true) {
$moved = User::query()
->whereKey($user->id)
->where(fn (Builder $query): Builder => $query->whereNull('last_seen_at')->orWhere('last_seen_at', '<', $today))
->toBase()
->update(['last_seen_at' => $today]);
$user->forceFill(['last_seen_at' => $today])->syncOriginalAttribute('last_seen_at');
}
if ($moved > 0) {
DB::table('active_user_counts')->upsert([['seen_at' => $today, 'count' => 1]], ['seen_at'], ['count' => DB::raw('active_user_counts.count + 1')]);
}
DB::statement(<<<SQL
insert into user_active_days (user_id, seen_at, {$app->value})
select id, ?::date, true from users
where id = ? and not exists (select 1 from user_active_days where user_id = ? and seen_at = ?::date and {$app->value})
on conflict (user_id, seen_at) do update set {$app->value} = true
SQL, [$today, $user->id, $user->id, $today]);
}
}