source: Klonkt/src/services/NotificationService.js@ fe6aac9

main
Last change on this file since fe6aac9 was 9a34d31, checked in by roboburr <roboburr@…>, 3 months ago

fix(notifications): table renamed to user_notifications (collision with stale table)

Older DBs still have an unused 'notifications' table (different schema, no
read column) -> CREATE TABLE IF NOT EXISTS became a no-op and the index on
read crashed the boot (demo + demohub in crash loop). Table + queries now
use user_notifications.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 1.6 KB
Line 
1/**
2 * Meldingen — antwoord op je reactie, reactie op je post, like op je post.
3 * Voor élke ingelogde gebruiker (Google-bezoekers/fans én admin). Snapshots van
4 * actor-naam + post-titel zodat de lijst zonder joins te tonen is.
5 */
6import { randomUUID } from 'crypto';
7import db from '../config/database.js';
8
9// Maakt een melding aan. Doet niets als er geen ontvanger is of als je jezelf
10// zou notificeren (eigen reactie/like op eigen post/reactie).
11export function notify({ userId, actorId, actorName, type, postSlug, postTitle, url }) {
12 if (!userId || userId === actorId) return;
13 try {
14 db.prepare(`
15 INSERT INTO user_notifications (id, user_id, type, actor_id, actor_name, post_slug, post_title, url, read)
16 VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)
17 `).run(randomUUID(), userId, type, actorId || null, actorName || null, postSlug || null, postTitle || null, url || null);
18 } catch { /* meldingen zijn niet-fataal */ }
19}
20
21export function unreadCount(userId) {
22 if (!userId) return 0;
23 try { return db.prepare('SELECT COUNT(*) AS c FROM user_notifications WHERE user_id = ? AND read = 0').get(userId).c; }
24 catch { return 0; }
25}
26
27export function list(userId, limit = 50) {
28 if (!userId) return [];
29 try { return db.prepare('SELECT * FROM user_notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT ?').all(userId, limit); }
30 catch { return []; }
31}
32
33export function markAllRead(userId) {
34 if (!userId) return;
35 try { db.prepare('UPDATE user_notifications SET read = 1 WHERE user_id = ? AND read = 0').run(userId); } catch { /* noop */ }
36}
37
38export default { notify, unreadCount, list, markAllRead };
Note: See TracBrowser for help on using the repository browser.