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:
@@ -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.
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -19,8 +19,13 @@ import (
|
||||
"path/filepath"
|
||||
|
||||
xdraw "golang.org/x/image/draw"
|
||||
_ "golang.org/x/image/webp" // WebP-Decoder für image.Decode
|
||||
)
|
||||
|
||||
// Angenommen werden JPEG, PNG, GIF und WebP; die Decoder registrieren sich
|
||||
// über die Importe oben bei image.Decode. Gespeichert wird nie im
|
||||
// Eingangsformat (außer animierte GIFs, siehe SaveUpload), WebP also als JPEG.
|
||||
|
||||
const (
|
||||
// Dir ist das Medienverzeichnis relativ zu Root.
|
||||
Dir = "static/media"
|
||||
@@ -121,11 +126,6 @@ func (s Store) create(dir, pattern string, write func(io.Writer) error) (string,
|
||||
return dir + "/" + filepath.Base(name), nil
|
||||
}
|
||||
|
||||
// isWebP erkennt den RIFF/WEBP-Header.
|
||||
func isWebP(data []byte) bool {
|
||||
return len(data) >= 12 && string(data[:4]) == "RIFF" && string(data[8:12]) == "WEBP"
|
||||
}
|
||||
|
||||
var errGIFTooLarge = errors.New("animiertes GIF zu groß")
|
||||
|
||||
// gifFrames prüft ein GIF gegen Frame- und Pixelbudget und meldet, ob es
|
||||
|
||||
+4
-19
@@ -4,11 +4,8 @@ import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
"io"
|
||||
"os"
|
||||
"path"
|
||||
|
||||
"golang.org/x/image/webp"
|
||||
)
|
||||
|
||||
// ThumbSize ist die Zielkante eines Vorschaubilds (klein, steht neben dem Text).
|
||||
@@ -37,29 +34,17 @@ func (s Store) SaveThumb(data []byte, dest string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// decodeChecked dekodiert data nach der Größenprüfung. WebP wird gezielt
|
||||
// dekodiert statt global registriert -- sonst nähme auch SaveUpload WebP an.
|
||||
// decodeChecked dekodiert data nach der Größenprüfung (alle in media.go
|
||||
// registrierten Formate).
|
||||
func decodeChecked(data []byte) (image.Image, error) {
|
||||
decodeConfig := image.DecodeConfig
|
||||
decode := func(r *bytes.Reader) (image.Image, error) {
|
||||
img, _, err := image.Decode(r)
|
||||
return img, err
|
||||
}
|
||||
if isWebP(data) {
|
||||
decodeConfig = func(r io.Reader) (image.Config, string, error) {
|
||||
c, err := webp.DecodeConfig(r)
|
||||
return c, "webp", err
|
||||
}
|
||||
decode = func(r *bytes.Reader) (image.Image, error) { return webp.Decode(r) }
|
||||
}
|
||||
cfg, _, err := decodeConfig(bytes.NewReader(data))
|
||||
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bildformat: %w", err)
|
||||
}
|
||||
if err := checkSize(cfg); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
img, err := decode(bytes.NewReader(data))
|
||||
img, _, err := image.Decode(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("bild dekodieren: %w", err)
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
|
||||
// SaveUpload speichert ein hochgeladenes Bild und liefert seinen Pfad.
|
||||
//
|
||||
// Standbilder (JPEG/PNG/Einzelbild-GIF) werden auf maxImageSize skaliert und
|
||||
// Standbilder (JPEG/PNG/WebP/Einzelbild-GIF) werden auf maxImageSize skaliert und
|
||||
// dabei durch Decode+Re-Encode sanitisiert. Animierte GIFs würden durch das
|
||||
// Re-Encode auf das erste Frame reduziert -> sie werden stattdessen unverändert
|
||||
// durchgereicht. Die Bytes sind dann NICHT sanitisiert; deshalb liefert der
|
||||
|
||||
Reference in New Issue
Block a user