Benachrichtigungen: Antworten auf eigene Beiträge

Bisher erfuhr man nur durch Nachsehen, ob jemand geantwortet hat. Jetzt
legt jede Antwort eine Benachrichtigung für den Autor des Elternbeitrags an
(Zeit, Typ, Text mit Auszug, interner Link, gesehen-Flag). Die Glocke in
der Navigation zählt die ungesehenen; die Liste markiert beim Ansehen alles
als gesehen.

Der Auszug zitiert die Antwort, deshalb hängt die Benachrichtigung an deren
pid: Bearbeiten zieht den Text nach, Löschen (auch des Kontos) nimmt sie mit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 21:14:54 +01:00
co-authored by Claude Opus 5.5
parent bed563d369
commit d6b6baedc1
13 changed files with 349 additions and 13 deletions
+106
View File
@@ -0,0 +1,106 @@
package httpapi
import (
"context"
"database/sql"
"fmt"
"net/http"
"strings"
"kver/internal/apperr"
"kver/internal/runes"
)
const (
// maxNotifications begrenzt die Liste auf die jüngsten Einträge.
maxNotifications = 50
// snippetRunes: so viel vom Beitrag steht in der Benachrichtigung.
snippetRunes = 50
)
// notificationJSON ist eine Benachrichtigung in der Liste. Seen meldet den
// Stand vor dem Abruf: so sieht die Liste, was neu war, obwohl der Abruf
// selbst alles als gesehen markiert.
type notificationJSON struct {
ID int64 `json:"id"`
Kind string `json:"kind"`
Message string `json:"message"`
Link string `json:"link"`
CreatedAt int64 `json:"created_at"`
Seen bool `json:"seen"`
}
// replyMessage ist der Text der Benachrichtigung über eine Antwort, mit den
// ersten Zeichen der Antwort (Leerraum zusammengefasst).
func replyMessage(content string) string {
const msg = "Jemand hat auf deinen Beitrag geantwortet."
snippet := strings.Join(strings.Fields(content), " ")
if snippet == "" {
return msg
}
if short := runes.Truncate(snippet, snippetRunes); short != snippet {
snippet = strings.TrimSpace(short) + "…"
}
return msg + " (" + snippet + ")"
}
// notifyReply benachrichtigt den Autor des Elternbeitrags über die Antwort
// pid, nicht aber bei Antworten auf sich selbst oder auf gelöschte Beiträge
// (uid 0). Läuft in der Transaktion, die die Antwort anlegt.
func notifyReply(ctx context.Context, tx *sql.Tx, in newEntry, pid int64) error {
if _, err := tx.ExecContext(ctx,
`INSERT INTO notification (uid, kind, message, link, pid, created_at)
SELECT uid, 'reply', $1, $2, $3, $4 FROM entry
WHERE pid = $5 AND uid <> 0 AND uid <> $6`,
replyMessage(in.content), fmt.Sprintf("/e/%d", pid), pid, in.created, in.replyTo, in.uid); err != nil {
return fmt.Errorf("benachrichtigung anlegen: %w", err)
}
return nil
}
// handleNotifications liefert die jüngsten Benachrichtigungen, neueste
// zuerst, und markiert die gelieferten als gesehen.
func (s *Server) handleNotifications(w http.ResponseWriter, r *http.Request) error {
ctx, uid := r.Context(), currentUID(r)
rows, err := s.DB.QueryContext(ctx,
`SELECT id, kind, message, link, created_at, seen_at > 0 FROM notification
WHERE uid = $1 ORDER BY id DESC LIMIT $2`, uid, maxNotifications)
if err != nil {
return apperr.Internal(err)
}
defer rows.Close()
list := []notificationJSON{}
for rows.Next() {
var n notificationJSON
if err := rows.Scan(&n.ID, &n.Kind, &n.Message, &n.Link, &n.CreatedAt, &n.Seen); err != nil {
return apperr.Internal(err)
}
list = append(list, n)
}
if err := rows.Err(); err != nil {
return apperr.Internal(err)
}
// Nur bis zur jüngsten gelieferten: was während des Abrufs neu kam, hat
// noch niemand gesehen.
if len(list) > 0 {
if _, err := s.DB.ExecContext(ctx,
`UPDATE notification SET seen_at = $1 WHERE uid = $2 AND seen_at = 0 AND id <= $3`,
s.Now().Unix(), uid, list[0].ID); err != nil {
return apperr.Internal(err)
}
}
writeJSON(w, http.StatusOK, map[string]any{"notifications": list})
return nil
}
// handleNotificationsUnread zählt die ungesehenen Benachrichtigungen (für den
// Zähler am Icon).
func (s *Server) handleNotificationsUnread(w http.ResponseWriter, r *http.Request) error {
var n int64
if err := s.DB.QueryRowContext(r.Context(),
`SELECT COUNT(*) FROM notification WHERE uid = $1 AND seen_at = 0`, currentUID(r)).Scan(&n); err != nil {
return apperr.Internal(err)
}
writeJSON(w, http.StatusOK, map[string]int64{"unread": n})
return nil
}