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.
|
||||
|
||||
Reference in New Issue
Block a user