Benachrichtigungen: Handle bei Antworten, neu auch Reposts
Bei Antworten steht statt "Jemand" der Handle. Damit er nach einer Umbenennung stimmt, speichert die Benachrichtigung nur die uid (actor) und den Auszug; den Text setzt der Abruf zusammen. Die Tabelle war noch nirgends ausgerollt, daher direkt in neuer Form statt per ALTER. Neu ist der Repost eines eigenen Beitrags (kind bump), in derselben Transaktion wie der Bump. Wer repostet, wird nicht gespeichert, die Meldung sagt daher "Jemand". Eigene Reposts lösen nichts aus. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -14,8 +14,8 @@ import (
|
||||
const (
|
||||
// maxNotifications begrenzt die Liste auf die jüngsten Einträge.
|
||||
maxNotifications = 50
|
||||
// snippetRunes: so viel vom Beitrag steht in der Benachrichtigung.
|
||||
snippetRunes = 50
|
||||
// excerptRunes: so viel vom Beitrag steht in der Benachrichtigung.
|
||||
excerptRunes = 50
|
||||
)
|
||||
|
||||
// notificationJSON ist eine Benachrichtigung in der Liste. Seen meldet den
|
||||
@@ -30,29 +30,63 @@ type notificationJSON struct {
|
||||
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
|
||||
// excerpt ist der Anfang eines Beitrags für die Benachrichtigung, Leerraum
|
||||
// zusammengefasst.
|
||||
func excerpt(content string) string {
|
||||
e := strings.Join(strings.Fields(content), " ")
|
||||
if short := runes.Truncate(e, excerptRunes); short != e {
|
||||
e = strings.TrimSpace(short) + "…"
|
||||
}
|
||||
if short := runes.Truncate(snippet, snippetRunes); short != snippet {
|
||||
snippet = strings.TrimSpace(short) + "…"
|
||||
return e
|
||||
}
|
||||
|
||||
// notificationMessage setzt den Text zusammen. actor ist der Handle dessen,
|
||||
// der geantwortet hat; leer (Reposts, unbekannt) heißt "Jemand".
|
||||
func notificationMessage(kind, actor, excerpt string) string {
|
||||
who := "Jemand"
|
||||
if actor != "" {
|
||||
who = "@" + actor
|
||||
}
|
||||
return msg + " (" + snippet + ")"
|
||||
msg := who + " hat deinen Beitrag repostet."
|
||||
if kind == "reply" {
|
||||
msg = who + " hat auf deinen Beitrag geantwortet."
|
||||
}
|
||||
if excerpt != "" {
|
||||
msg += " (" + excerpt + ")"
|
||||
}
|
||||
return msg
|
||||
}
|
||||
|
||||
// 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.
|
||||
// (uid 0). Zitiert wird die Antwort. Läuft in der Transaktion, die sie 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 {
|
||||
`INSERT INTO notification (uid, kind, actor, excerpt, link, pid, created_at)
|
||||
SELECT uid, 'reply', $1, $2, $3, $4, $5 FROM entry
|
||||
WHERE pid = $6 AND uid <> 0 AND uid <> $1`,
|
||||
in.uid, excerpt(in.content), fmt.Sprintf("/e/%d", pid), pid, in.created, in.replyTo); err != nil {
|
||||
return fmt.Errorf("benachrichtigung anlegen: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// notifyBump benachrichtigt den Autor über einen Repost seines Beitrags pid,
|
||||
// außer er hat ihn selbst repostet. Wer repostet, wird nicht gespeichert.
|
||||
func notifyBump(ctx context.Context, tx *sql.Tx, pid, bumper, now int64) error {
|
||||
var author int64
|
||||
var content string
|
||||
if err := tx.QueryRowContext(ctx,
|
||||
`SELECT uid, content FROM entry WHERE pid = $1`, pid).Scan(&author, &content); err != nil {
|
||||
return fmt.Errorf("beitrag lesen: %w", err)
|
||||
}
|
||||
if author == 0 || author == bumper {
|
||||
return nil
|
||||
}
|
||||
if _, err := tx.ExecContext(ctx,
|
||||
`INSERT INTO notification (uid, kind, excerpt, link, pid, created_at)
|
||||
VALUES ($1, 'bump', $2, $3, $4, $5)`,
|
||||
author, excerpt(content), fmt.Sprintf("/e/%d", pid), pid, now); err != nil {
|
||||
return fmt.Errorf("benachrichtigung anlegen: %w", err)
|
||||
}
|
||||
return nil
|
||||
@@ -63,8 +97,9 @@ func notifyReply(ctx context.Context, tx *sql.Tx, in newEntry, pid int64) error
|
||||
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)
|
||||
`SELECT n.id, n.kind, COALESCE(a.handle, ''), n.excerpt, n.link, n.created_at, n.seen_at > 0
|
||||
FROM notification n LEFT JOIN account a ON a.uid = n.actor AND n.actor <> 0
|
||||
WHERE n.uid = $1 ORDER BY n.id DESC LIMIT $2`, uid, maxNotifications)
|
||||
if err != nil {
|
||||
return apperr.Internal(err)
|
||||
}
|
||||
@@ -72,9 +107,11 @@ func (s *Server) handleNotifications(w http.ResponseWriter, r *http.Request) err
|
||||
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 {
|
||||
var actor, excerpt string
|
||||
if err := rows.Scan(&n.ID, &n.Kind, &actor, &excerpt, &n.Link, &n.CreatedAt, &n.Seen); err != nil {
|
||||
return apperr.Internal(err)
|
||||
}
|
||||
n.Message = notificationMessage(n.Kind, actor, excerpt)
|
||||
list = append(list, n)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user