fix: correct shown flag behavior

This commit is contained in:
KernelDeimos 2024-06-16 02:19:29 -04:00
parent c35ecec5a4
commit 632c536616
2 changed files with 46 additions and 2 deletions

View File

@ -1,6 +1,7 @@
const APIError = require("../api/APIError"); const APIError = require("../api/APIError");
const auth2 = require("../middleware/auth2"); const auth2 = require("../middleware/auth2");
const { Endpoint } = require("../util/expressutil"); const { Endpoint } = require("../util/expressutil");
const { TeePromise } = require("../util/promise");
const BaseService = require("./BaseService"); const BaseService = require("./BaseService");
const { DB_WRITE } = require("./database/consts"); const { DB_WRITE } = require("./database/consts");
@ -37,6 +38,11 @@ class NotificationService extends BaseService {
svc_event.on('web.socket.user-connected', (_, { user }) => { svc_event.on('web.socket.user-connected', (_, { user }) => {
this.on_user_connected({ user }); this.on_user_connected({ user });
}); });
svc_event.on('sent-to-user.notif.message', (_, o) => {
this.on_sent_to_user(o);
})
this.notifs_pending_write = {};
} }
['__on_install.routes'] (_, { app }) { ['__on_install.routes'] (_, { app }) {
@ -83,6 +89,16 @@ class NotificationService extends BaseService {
'ORDER BY created_at ASC', 'ORDER BY created_at ASC',
[user.id] [user.id]
); );
// set all the notifications to "shown"
const shown_ts = Math.floor(Date.now() / 1000);
await this.db.write(
'UPDATE `notification` ' +
'SET shown = ? ' +
'WHERE user_id=? AND shown IS NULL AND acknowledged IS NULL ',
[shown_ts, user.id]
);
for ( const n of notifications ) { for ( const n of notifications ) {
n.value = this.db.case({ n.value = this.db.case({
mysql: () => n.value, mysql: () => n.value,
@ -108,10 +124,25 @@ class NotificationService extends BaseService {
}); });
} }
async on_sent_to_user ({ user_id, response }) {
console.log('GOT IT AND IT WORKED!!!', user_id, response);
const shown_ts = Math.floor(Date.now() / 1000);
if ( this.notifs_pending_write[response.uid] ) {
await this.notifs_pending_write[response.uid];
}
await this.db.write(...ll([
'UPDATE `notification` ' +
'SET shown = ? ' +
'WHERE user_id=? AND uid=?',
[shown_ts, user_id, response.uid]
]));
}
async notify (selector, notification) { async notify (selector, notification) {
const uid = this.modules.uuidv4(); const uid = this.modules.uuidv4();
const svc_event = this.services.get('event'); const svc_event = this.services.get('event');
const user_id_list = await selector(this); const user_id_list = await selector(this);
this.notifs_pending_write[uid] = new TeePromise();
svc_event.emit('outer.gui.notif.message', { svc_event.emit('outer.gui.notif.message', {
user_id_list, user_id_list,
response: { response: {
@ -122,13 +153,16 @@ class NotificationService extends BaseService {
(async () => { (async () => {
for ( const user_id of user_id_list ) { for ( const user_id of user_id_list ) {
await this.db.write(...ll([ await this.db.write(
'INSERT INTO `notification` ' + 'INSERT INTO `notification` ' +
'(`user_id`, `uid`, `value`) ' + '(`user_id`, `uid`, `value`) ' +
'VALUES (?, ?, ?)', 'VALUES (?, ?, ?)',
[user_id, uid, JSON.stringify(notification)], [user_id, uid, JSON.stringify(notification)],
])); );
} }
const p = this.notifs_pending_write[uid];
delete this.notifs_pending_write[uid];
p.resolve()
svc_event.emit('outer.gui.notif.persisted', { svc_event.emit('outer.gui.notif.persisted', {
user_id_list, user_id_list,
response: { response: {

View File

@ -234,8 +234,18 @@ class WSPushService extends AdvancedBase {
const { socketio } = this.modules; const { socketio } = this.modules;
const io = socketio.getio(); const io = socketio.getio();
for ( const user_id of user_id_list ) { for ( const user_id of user_id_list ) {
const room = io.sockets.adapter.rooms.get(user_id);
if ( ! room || room.size <= 0 ) {
continue;
}
io.to(user_id).emit(key, response); io.to(user_id).emit(key, response);
this.svc_event.emit(`sent-to-user.${key}`, {
user_id,
response,
meta,
});
} }
} }
} }