Aufräumen: Soft-Delete, Benachrichtigungsarten, Antwort-Structs, WebP

Das Soft-Delete-SQL stand zweimal (Beitrag löschen, Konto löschen) und
beide Stellen mussten dieselben Nebentabellen kennen; jetzt macht
softDeleteEntries das für eine WHERE-Bedingung, die Kontolöschung räumt
nur noch ab, was am Nutzer selbst hängt.

Die Benachrichtigungsarten waren an fünf Stellen als Literal im SQL und
im Go-Code; jetzt Konstanten, die als Parameter mitgehen. Antworten auf
Anlegen/Bearbeiten und sessioninfo sind Structs wie der Rest.

media: der Import von x/image/webp registriert den Decoder global, die
Sonderbehandlung in decodeChecked war daher tot und ihr Kommentar falsch
(SaveUpload nahm WebP längst an). Jetzt ein Import mit Kommentar, ein
Pfad für alle Formate.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
2026-11-22 13:12:27 +01:00
co-authored by Claude Fable 5.1
parent a93e10956b
commit b2bb1f176f
8 changed files with 86 additions and 70 deletions
+39 -22
View File
@@ -50,15 +50,24 @@ func (s *Server) handleCreateEntry(w http.ResponseWriter, r *http.Request) error
return apperr.Internal(err)
}
s.Previews.Request(r.Context(), in.linkURL)
writeJSON(w, http.StatusCreated, map[string]any{
"pid": pid,
"content": in.content,
"filepath": in.filepath,
"reply_to": in.replyTo,
})
writeJSON(w, http.StatusCreated, createdJSON{PID: pid, Content: in.content, Filepath: in.filepath, ReplyTo: in.replyTo})
return nil
}
// createdJSON ist die Antwort auf einen angelegten Beitrag.
type createdJSON struct {
PID int64 `json:"pid"`
Content string `json:"content"`
Filepath string `json:"filepath"`
ReplyTo int64 `json:"reply_to"`
}
// editedJSON ist die Antwort auf einen bearbeiteten Beitrag.
type editedJSON struct {
PID int64 `json:"pid"`
Content string `json:"content"`
}
// readNewEntry prüft das Formular und speichert ein angehängtes Bild.
func (s *Server) readNewEntry(r *http.Request) (newEntry, error) {
in := newEntry{
@@ -191,7 +200,7 @@ func (s *Server) handleEditEntry(w http.ResponseWriter, r *http.Request) error {
return apperr.Internal(err)
}
s.Previews.Request(r.Context(), linkURL)
writeJSON(w, http.StatusOK, map[string]any{"pid": e.PID, "content": content})
writeJSON(w, http.StatusOK, editedJSON{PID: e.PID, Content: content})
return nil
}
@@ -221,22 +230,8 @@ func (s *Server) handleDeleteEntry(w http.ResponseWriter, r *http.Request) error
if err != nil {
return err
}
// Mit dem Beitrag verschwinden seine Erwähnungen und die Benachrichtigungen,
// die ihn zitieren.
if err := database.InTx(r.Context(), s.DB, func(tx *sql.Tx) error {
if _, err := tx.ExecContext(r.Context(),
`UPDATE entry SET deleted = 1, content = '', filepath = '', image_warning = '', image_cover = '', link_url = '', uid = 0 WHERE pid = $1`,
e.PID); err != nil {
return err
}
if _, err := tx.ExecContext(r.Context(), `DELETE FROM mention WHERE pid = $1`, e.PID); err != nil {
return err
}
if _, err := tx.ExecContext(r.Context(), `DELETE FROM entry_tag WHERE pid = $1`, e.PID); err != nil {
return err
}
_, err := tx.ExecContext(r.Context(), `DELETE FROM notification WHERE pid = $1`, e.PID)
return err
return softDeleteEntries(r.Context(), tx, "pid = $1", e.PID)
}); err != nil {
return apperr.Internal(err)
}
@@ -246,6 +241,28 @@ func (s *Server) handleDeleteEntry(w http.ResponseWriter, r *http.Request) error
return nil
}
// softDeleteEntries löscht alle Beiträge weich, auf die cond (eine
// WHERE-Bedingung über entry mit $1 = arg, fest im Code) zutrifft: Inhalt,
// Bild, Link und Autor werden geleert, die Zeile bleibt als
// [deleted]-Platzhalter. Mit den Beiträgen verschwinden ihre Erwähnungen,
// Hashtags und die Benachrichtigungen, die sie zitieren. Die Dateien
// (filepath, image_cover) löscht der Aufrufer.
func softDeleteEntries(ctx context.Context, tx *sql.Tx, cond string, arg any) error {
pids := `(SELECT pid FROM entry WHERE ` + cond + `)`
for _, q := range []string{
`DELETE FROM mention WHERE pid IN ` + pids,
`DELETE FROM entry_tag WHERE pid IN ` + pids,
`DELETE FROM notification WHERE pid IN ` + pids,
`UPDATE entry SET deleted = 1, content = '', filepath = '', image_warning = '', image_cover = '',
link_url = '', uid = 0 WHERE ` + cond,
} {
if _, err := tx.ExecContext(ctx, q, arg); err != nil {
return fmt.Errorf("beiträge löschen: %w", err)
}
}
return nil
}
// handleReport meldet einen Beitrag, optional mit Grund. Melden darf jeder;
// ohne Session ist der Melder anonym (uid 0). Angemeldete legen für denselben
// Beitrag keine zweite Meldung an, solange ihre erste offen ist.
+4 -4
View File
@@ -68,8 +68,8 @@ func syncMentions(ctx context.Context, tx *sql.Tx, pid, author, replyTo int64, c
return fmt.Errorf("erwähnungen speichern: %w", err)
}
if _, err := tx.ExecContext(ctx,
`DELETE FROM notification WHERE pid = $1 AND kind = 'mention'
AND uid NOT IN (SELECT uid FROM mention WHERE pid = $1)`, pid); err != nil {
`DELETE FROM notification WHERE pid = $1 AND kind = $2
AND uid NOT IN (SELECT uid FROM mention WHERE pid = $1)`, pid, kindMention); err != nil {
return fmt.Errorf("erwähnungen abgleichen: %w", err)
}
after, err := mentionedUIDs(ctx, tx, pid)
@@ -89,8 +89,8 @@ func syncMentions(ctx context.Context, tx *sql.Tx, pid, author, replyTo int64, c
}
if _, err := tx.ExecContext(ctx,
`INSERT INTO notification (uid, kind, actor, excerpt, link, pid, created_at)
VALUES ($1, 'mention', $2, $3, $4, $5, $6)`,
uid, author, excerpt(content), fmt.Sprintf("/e/%d", pid), pid, now); err != nil {
VALUES ($1, $7, $2, $3, $4, $5, $6)`,
uid, author, excerpt(content), entryLink(pid), pid, now, kindMention); err != nil {
return fmt.Errorf("benachrichtigung anlegen: %w", err)
}
}
+17 -6
View File
@@ -18,6 +18,13 @@ const (
excerptRunes = 50
)
// Arten von Benachrichtigungen (Spalte notification.kind, Feld kind der API).
const (
kindReply = "reply" // Antwort auf einen eigenen Beitrag
kindMention = "mention" // in einem Beitrag erwähnt
kindBump = "bump" // eigener Beitrag repostet
)
// 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.
@@ -41,6 +48,10 @@ func excerpt(content string) string {
return e
}
// entryLink ist der interne Pfad zu einem Beitrag, wie ihn eine
// Benachrichtigung trägt.
func entryLink(pid int64) string { return fmt.Sprintf("/e/%d", pid) }
// notificationMessage ist die Zeile, die sagt, was passiert ist. actor ist
// der Handle dessen, der geantwortet hat; leer (Reposts, unbekannt) heißt
// "Jemand". Den Auszug liefert die API getrennt.
@@ -50,9 +61,9 @@ func notificationMessage(kind, actor string) string {
who = "@" + actor
}
switch kind {
case "reply":
case kindReply:
return who + " hat auf deinen Beitrag geantwortet."
case "mention":
case kindMention:
return who + " hat dich erwähnt."
}
return who + " hat deinen Beitrag repostet."
@@ -64,9 +75,9 @@ func notificationMessage(kind, actor string) string {
func notifyReply(ctx context.Context, tx *sql.Tx, in newEntry, pid int64) error {
if _, err := tx.ExecContext(ctx,
`INSERT INTO notification (uid, kind, actor, excerpt, link, pid, created_at)
SELECT uid, 'reply', $1, $2, $3, $4, $5 FROM entry
SELECT uid, $7, $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 {
in.uid, excerpt(in.content), entryLink(pid), pid, in.created, in.replyTo, kindReply); err != nil {
return fmt.Errorf("benachrichtigung anlegen: %w", err)
}
return nil
@@ -86,8 +97,8 @@ func notifyBump(ctx context.Context, tx *sql.Tx, pid, bumper, now int64) error {
}
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 {
VALUES ($1, $6, $2, $3, $4, $5)`,
author, excerpt(content), entryLink(pid), pid, now, kindBump); err != nil {
return fmt.Errorf("benachrichtigung anlegen: %w", err)
}
return nil
+10 -5
View File
@@ -181,15 +181,20 @@ func (s *Server) handleSessionInfo(w http.ResponseWriter, r *http.Request) error
if !ok {
return apperr.Unauth("auth.no_session", "Keine gültige Sitzung.")
}
writeJSON(w, http.StatusOK, map[string]any{
"uid": sess.UID,
"created_at": sess.CreatedAt,
"expires": sess.Expires,
"description": sess.Description,
writeJSON(w, http.StatusOK, sessionJSON{
UID: sess.UID, CreatedAt: sess.CreatedAt, Expires: sess.Expires, Description: sess.Description,
})
return nil
}
// sessionJSON ist die Sitzung im Drahtformat (ohne den Token).
type sessionJSON struct {
UID int64 `json:"uid"`
CreatedAt int64 `json:"created_at"`
Expires int64 `json:"expires"`
Description string `json:"description"`
}
// handleNewUser sammelt alle Validierungsfehler ein, statt beim ersten
// abzubrechen -- der Nutzer soll nicht Feld für Feld durchprobieren müssen.
func (s *Server) handleNewUser(w http.ResponseWriter, r *http.Request) error {
+6 -8
View File
@@ -198,20 +198,18 @@ func (s *Server) userMedia(ctx context.Context, uid int64) ([]string, error) {
}
// deleteUserContent räumt ab, was von uid im Forum steht: Votes,
// Benachrichtigungen an und von ihm, Erwähnungen; die Beiträge werden weich
// gelöscht.
// Benachrichtigungen an und von ihm, Erwähnungen seiner Person; die Beiträge
// werden weich gelöscht (softDeleteEntries nimmt deren Erwähnungen, Tags und
// Benachrichtigungen mit).
func deleteUserContent(ctx context.Context, tx *sql.Tx, uid int64) error {
for _, q := range []string{
`DELETE FROM vote WHERE uid = $1`,
// Vor dem UPDATE auf entry: danach stehen die Beiträge auf uid 0.
`DELETE FROM notification WHERE uid = $1 OR actor = $1 OR pid IN (SELECT pid FROM entry WHERE uid = $1)`,
`DELETE FROM mention WHERE uid = $1 OR pid IN (SELECT pid FROM entry WHERE uid = $1)`,
`DELETE FROM entry_tag WHERE pid IN (SELECT pid FROM entry WHERE uid = $1)`,
`UPDATE entry SET deleted = 1, content = '', filepath = '', image_warning = '', image_cover = '', link_url = '', uid = 0 WHERE uid = $1`,
`DELETE FROM notification WHERE uid = $1 OR actor = $1`,
`DELETE FROM mention WHERE uid = $1`,
} {
if _, err := tx.ExecContext(ctx, q, uid); err != nil {
return fmt.Errorf("beiträge löschen: %w", err)
}
}
return nil
return softDeleteEntries(ctx, tx, "uid = $1", uid)
}