Changeset c9c6a2d in Klonkt for src/routes


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@…>

Location:
src/routes
Files:
1 added
2 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:
  • src/routes/posts.js

    rd41f4be rc9c6a2d  
    1010import { renderPage } from '../middleware/render.js';
    1111import { recordPageview, recordPostView } from '../services/StatsService.js';
     12import { notify } from '../services/NotificationService.js';
    1213import PermissionsService from '../services/PermissionsService.js';
    1314import MarkdownService from '../services/MarkdownService.js';
     
    405406router.post('/posts/:id/like', requireAuth, (req, res) => {
    406407  const userId = req.session.user.id;
    407   const post = db.prepare('SELECT id, status FROM posts WHERE id = ?').get(req.params.id);
     408  const post = db.prepare('SELECT id, status, slug, title, author_id FROM posts WHERE id = ?').get(req.params.id);
    408409  if (!post) return res.status(404).send('Post niet gevonden');
    409410  if (post.status !== 'published') return res.status(403).send('Niet beschikbaar');
     
    414415  } else {
    415416    db.prepare('INSERT OR IGNORE INTO post_likes (post_id, user_id) VALUES (?, ?)').run(post.id, userId);
     417    // Melding voor de post-auteur (notify slaat jezelf-liken over).
     418    notify({
     419      userId: post.author_id, actorId: userId, actorName: req.session.user.username, type: 'like',
     420      postSlug: post.slug, postTitle: post.title, url: (res.locals.siteUrlBase || '') + '/' + post.slug,
     421    });
    416422  }
    417423  const likeCount = db.prepare('SELECT COUNT(*) AS c FROM post_likes WHERE post_id = ?').get(post.id).c;
Note: See TracChangeset for help on using the changeset viewer.