// Copyright (c) 2026 Xessenger // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. package bridgekit import ( "context" "fmt" "net/http" "github.com/rs/zerolog" "go.mau.fi/util/dbutil" "maunium.net/go/mautrix/id" ) // The API asks the bridge to forget files it is about to delete (QA-289, docs/privacy-deletion.md): // // POST /_xessenger/v1/media/forget {mxc: ["mxc:///", ...]} -> {forgotten: n} // // The Telegram bridge uploads a Telegram file once and hands that upload to every chat that gets the // same file again (its telegram_file table); a deleted upload it handed out would show as broken. // The other bridges keep no such cache, so they forget nothing. // maxForgetMedia bounds one request; the API asks about one file at a time. const maxForgetMedia = 1000 func (k *Kit) handleForgetMedia(w http.ResponseWriter, r *http.Request) { var body struct { MXC []id.ContentURIString `json:"mxc"` } if !readJSON(w, r, &body) { return } if len(body.MXC) > maxForgetMedia { writeError(w, http.StatusBadRequest, "M_INVALID_PARAM", fmt.Sprintf("At most %d files at a time", maxForgetMedia)) return } for _, uri := range body.MXC { parsed, err := uri.Parse() if err != nil || parsed.Homeserver != k.main.Config.Homeserver.Domain { writeError(w, http.StatusBadRequest, "M_INVALID_PARAM", "Every file must be an mxc:// URI of this homeserver") return } } ctx := k.requestContext(r) forgotten, err := forgetMedia(ctx, k.db, k.network, body.MXC) if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to forget media") writeError(w, http.StatusInternalServerError, "M_UNKNOWN", "Failed to forget the files") return } writeJSON(w, http.StatusOK, map[string]int{"forgotten": forgotten}) } // forgetMedia drops the Telegram bridge's cached uploads of the files, and reports how many it held. func forgetMedia(ctx context.Context, db *dbutil.Database, network string, uris []id.ContentURIString) (int, error) { if network != "telegram" { return 0, nil } forgotten := 0 for _, uri := range uris { result, err := db.Exec(ctx, "DELETE FROM telegram_file WHERE mxc=$1", uri) if err != nil { return forgotten, err } if count, err := result.RowsAffected(); err == nil { forgotten += int(count) } } return forgotten, nil }