feat(push): add phase7 offline push queue, worker, APIs, and simulator inbox

This commit is contained in:
2026-01-24 15:20:00 +00:00
parent bccc049fc3
commit 6d6c77f77e
9 changed files with 392 additions and 6 deletions

View File

@@ -7,6 +7,7 @@ import { deviceLinks, devices, events, notifications } from '../db/schema';
import { requireAuth } from '../middleware/auth';
import { requireDeviceAuth } from '../middleware/device-auth';
import { sendRealtimeToDevice } from '../realtime/gateway';
import { enqueuePushNotification } from '../services/push';
const router = Router();
@@ -105,6 +106,19 @@ router.post('/motion/start', requireDeviceAuth, async (req, res) => {
isRead: false,
sentAt: now,
});
if (!delivered) {
await enqueuePushNotification({
ownerUserId: deviceAuth.userId,
recipientDeviceId: link.clientDeviceId,
type: 'motion_detected',
payload: {
eventId: event.id,
cameraDeviceId: cameraDevice.id,
startedAt: event.startedAt.toISOString(),
},
});
}
}
res.status(201).json({
@@ -172,13 +186,27 @@ router.post('/:eventId/motion/end', requireDeviceAuth, async (req, res) => {
});
for (const link of activeLinks) {
sendRealtimeToDevice(link.clientDeviceId, 'motion:ended', {
const delivered = sendRealtimeToDevice(link.clientDeviceId, 'motion:ended', {
eventId: event.id,
cameraDeviceId: deviceAuth.deviceId,
status: parsed.data.status,
endedAt: now,
videoUrl: parsed.data.videoUrl ?? event.videoUrl,
});
if (!delivered) {
await enqueuePushNotification({
ownerUserId: deviceAuth.userId,
recipientDeviceId: link.clientDeviceId,
type: 'motion_ended',
payload: {
eventId: event.id,
cameraDeviceId: deviceAuth.deviceId,
status: parsed.data.status,
endedAt: now.toISOString(),
},
});
}
}
res.json({ message: 'Motion event ended', event: updated, notifiedClients: activeLinks.length });