source: Klonkt/src/services/NotificationService.js@ 2279f80

main
Last change on this file since 2279f80 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

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

  • Property mode set to 100644
File size: 1.6 KB
RevLine 
[c9c6a2d]1/**
[834bcc3]2 * Notifications — reply to your comment, comment on your post, like on your post.
3 * For every logged-in user (Google visitors/fans and admins). Snapshots of
4 * actor name + post title so the list can be rendered without joins.
[c9c6a2d]5 */
6import { randomUUID } from 'crypto';
7import db from '../config/database.js';
8
[834bcc3]9// Creates a notification. Does nothing if there is no recipient or if you
10// would notify yourself (your own comment/like on your own post/comment).
[c9c6a2d]11export function notify({ userId, actorId, actorName, type, postSlug, postTitle, url }) {
12 if (!userId || userId === actorId) return;
13 try {
14 db.prepare(`
[9a34d31]15 INSERT INTO user_notifications (id, user_id, type, actor_id, actor_name, post_slug, post_title, url, read)
[c9c6a2d]16 VALUES (?, ?, ?, ?, ?, ?, ?, ?, 0)
17 `).run(randomUUID(), userId, type, actorId || null, actorName || null, postSlug || null, postTitle || null, url || null);
[834bcc3]18 } catch { /* notifications are non-fatal */ }
[c9c6a2d]19}
20
21export function unreadCount(userId) {
22 if (!userId) return 0;
[9a34d31]23 try { return db.prepare('SELECT COUNT(*) AS c FROM user_notifications WHERE user_id = ? AND read = 0').get(userId).c; }
[c9c6a2d]24 catch { return 0; }
25}
26
27export function list(userId, limit = 50) {
28 if (!userId) return [];
[9a34d31]29 try { return db.prepare('SELECT * FROM user_notifications WHERE user_id = ? ORDER BY created_at DESC LIMIT ?').all(userId, limit); }
[c9c6a2d]30 catch { return []; }
31}
32
33export function markAllRead(userId) {
34 if (!userId) return;
[834bcc3]35 try { db.prepare('UPDATE user_notifications SET read = 1 WHERE user_id = ? AND read = 0').run(userId); } catch { /* no-op */ }
[c9c6a2d]36}
37
38export default { notify, unreadCount, list, markAllRead };
Note: See TracBrowser for help on using the repository browser.