Changeset c9c6a2d in Klonkt for src/routes/comments.js


Ignore:
Timestamp:
06/21/2026 06:38:24 PM (3 months ago)
Author:
roboburr <roboburr@…>
Branches:
main
Children:
9a34d31
Parents:
d41f4be
Message:

feat(meldingen): notifications — reply on comment, comment on post, like on post

For every logged-in user (Google visitors/fans and admin). Bell icon in the
header with unread counter + notifications page /notifications (via user menu
desktop + profile sheet mobile; badge also on the mobile Profile tab). Opening =
read. Triggers in comments (reply→comment author, top-level→post author) and
like (→post author); notify() skips notifying yourself. Table notifications
+ NotificationService. NL/EN/DE.

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/routes/comments.js

    rd41f4be rc9c6a2d  
    1919import { requireAuth } from '../middleware/auth.js';
    2020import PermissionsService from '../services/PermissionsService.js';
     21import { notify } from '../services/NotificationService.js';
    2122
    2223const router = express.Router();
     
    3940
    4041  const post = db.prepare(
    41     'SELECT id, slug FROM posts WHERE site_id = ? AND slug = ? AND status = ?'
     42    'SELECT id, slug, title, author_id FROM posts WHERE site_id = ? AND slug = ? AND status = ?'
    4243  ).get(site.id, postSlug, 'published');
    4344  if (!post) return res.status(404).send('Post not found');
     
    5051  // up to the top-level parent so we never go deeper than 1)
    5152  let resolvedParent = null;
     53  let parentAuthorId = null;
    5254  if (parentId) {
    5355    const parent = db.prepare(
    54       'SELECT id, parent_comment_id FROM comments WHERE id = ? AND post_id = ?'
     56      'SELECT id, parent_comment_id, author_id FROM comments WHERE id = ? AND post_id = ?'
    5557    ).get(parentId, post.id);
    5658    if (!parent) return res.status(400).send('Invalid parent comment');
    5759    resolvedParent = parent.parent_comment_id || parent.id;
     60    parentAuthorId = parent.author_id; // ontvanger van de "antwoord"-melding
    5861  }
    5962
     
    7376    VALUES (?, ?, ?, ?, ?, ?)
    7477  `).run(commentId, post.id, req.session.user.id, resolvedParent, rawContent, status);
     78
     79  // Melding (alleen bij een zichtbare reactie): antwoord → de auteur van de reactie
     80  // waarop gereageerd is; top-level reactie → de auteur van de post. notify() slaat
     81  // jezelf-notificeren over.
     82  if (status === 'approved') {
     83    const url = `${res.locals.siteUrlBase || ''}/${post.slug}#comment-${commentId}`;
     84    const actorId = req.session.user.id;
     85    const actorName = req.session.user.username;
     86    if (parentId) {
     87      notify({ userId: parentAuthorId, actorId, actorName, type: 'reply', postSlug: post.slug, postTitle: post.title, url });
     88    } else {
     89      notify({ userId: post.author_id, actorId, actorName, type: 'comment', postSlug: post.slug, postTitle: post.title, url });
     90    }
     91  }
    7592
    7693  // Where to land after submit:
Note: See TracChangeset for help on using the changeset viewer.