Fix last_activity_ts

This commit is contained in:
KernelDeimos 2024-04-16 19:30:30 -04:00
parent 19c49db538
commit 7e0c6c6470

View File

@ -114,6 +114,9 @@ class SessionService extends BaseService {
this.log.tick('UPDATING SESSIONS');
const now = Date.now();
const keys = Object.keys(this.sessions);
const user_updates = {};
for ( const key of keys ) {
const session = this.sessions[key];
// if ( now - session.last_store > 5 * MINUTE ) {
@ -127,9 +130,29 @@ class SessionService extends BaseService {
[JSON.stringify(session.meta), unix_ts, session.uuid],
);
session.last_store = now;
if (
! user_updates[session.user_id] ||
user_updates[session.user_id][1] < session.last_touch
) {
user_updates[session.user_id] = [session.user_id, session.last_touch];
}
}
}
for ( const [user_id, last_touch] of Object.values(user_updates) ) {
const sql_ts = (date =>
date.toISOString().split('T')[0] + ' '
+ date.toTimeString().split(' ')[0]
)(new Date(last_touch));
await this.db.write(
'UPDATE `user` ' +
'SET `last_activity_ts` = ? ' +
'WHERE `id` = ? LIMIT 1',
[sql_ts, user_id],
);
}
}
}
module.exports = { SessionService };