40 lines
1 KiB
JavaScript
40 lines
1 KiB
JavaScript
// ws-server/ws-events-photos.js
|
|
const { randomUUID } = require("crypto");
|
|
const dbWs = require("../db/dbWs");
|
|
const { wss, wsBySession } = require("./ws-core");
|
|
|
|
async function broadcastPhotoEvent(user, payload) {
|
|
const event_id = randomUUID();
|
|
const now = Date.now();
|
|
|
|
const sessions = await dbWs.getSessionsByUser(user);
|
|
if (!sessions || sessions.length === 0) return;
|
|
|
|
for (const s of sessions) {
|
|
const deviceRows = await dbWs.getDevicesForSession(s.session_id);
|
|
|
|
for (const device_id of deviceRows) {
|
|
await dbWs.insertPendingEvent({
|
|
user_id: user,
|
|
session_id: s.session_id,
|
|
device_id,
|
|
event_id,
|
|
payload: JSON.stringify(payload),
|
|
created_at: now,
|
|
});
|
|
}
|
|
}
|
|
|
|
for (const s of sessions) {
|
|
const devices = wsBySession.get(s.session_id);
|
|
if (!devices) continue;
|
|
|
|
for (const [device_id, ws] of devices) {
|
|
if (ws.readyState === 1 && ws.authenticated) {
|
|
ws.send(JSON.stringify({ ...payload, event_id }));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = { broadcastPhotoEvent };
|