mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-27 23:27:31 +00:00
fix(admin): show when users were last active and keep the list current
The column showed the last sign-in, which a renewing cookie leaves weeks behind for someone using TREK daily. It now shows the later of the sign-in and the last request. Saving an edit no longer blanks the row's avatar, activity and online dot, and the list refreshes every 30 seconds while open.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import type { UserSessions } from '../entities/UserSessions.entity';
|
||||
import { Users } from '../entities/Users.entity';
|
||||
import type { DB } from '../kysely/db';
|
||||
import type { AssertRowKeys } from './_shared/rows';
|
||||
import { TrekRepository } from './_shared/trek-repository';
|
||||
|
||||
@@ -154,6 +155,20 @@ export class UserSessionsRepository extends TrekRepository<UserSessions> {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* `SELECT user_id, MAX(last_seen_at) FROM user_sessions GROUP BY user_id`: when
|
||||
* each account last made an authenticated request, over every row the nightly
|
||||
* purge has kept, revoked ones included (the activity still happened).
|
||||
*/
|
||||
async lastSeenByUser(): Promise<Map<number, string>> {
|
||||
const rows = await this.kysely<Pick<DB, 'user_sessions'>>()
|
||||
.selectFrom('user_sessions')
|
||||
.select((eb) => ['user_id', eb.fn.max('last_seen_at').as('last_seen_at')])
|
||||
.groupBy('user_id')
|
||||
.execute();
|
||||
return new Map(rows.map((row) => [Number(row.user_id), String(row.last_seen_at)]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Every active session with its owner's email, read before the database
|
||||
* file is swapped (a demo reset, a backup restore):
|
||||
|
||||
Reference in New Issue
Block a user