package migrate import ( "context" "errors" "fmt" "net/http" "slices" "github.com/rs/zerolog" "maunium.net/go/mautrix" "maunium.net/go/mautrix/id" ) // maxRedacted bounds the event IDs one call names. What the bridge found deleted stays marked, so the // API names more of them in the next call. const maxRedacted = 1000 // handleRedacted leaves the moved events deleted in the new room since they were written out of what // the app checks the move against (docs/e2ee/work-plan.md 4.10 ④). The app reads the new room before // the old room may go, and a moved event deleted meanwhile no longer decrypts to anything: the messenger // took the message back (bridgev2's record of it names the moved event, repoint), a reaction was taken // back, or the person deleted their message. Counted, its numbers could never be the bridge's, and the // old room would stay for good. // // The API names the events the app saw deleted. The ones this bridge wrote into the room that the // homeserver shows deleted are marked, once; any other ID (another room's event, an event not moved, an // event not deleted) changes nothing, so no ID makes the numbers match a history the app did not read. // The answer is the tally of the room's written events without every one marked so far, and how many // those are: // // POST …/rooms/{roomId}/migrate/redacted {eventIds} -> {events, files, digest, redacted} func (m *mover) handleRedacted(w http.ResponseWriter, r *http.Request) { oldRoom, ok := pathRoom(w, r) if !ok { return } // One call of a room at a time, under the room's lock as a move: each reads the marks every call // before it kept, so calls naming parts of the list at once each answer with all marked so far. lock := m.roomLock(oldRoom) lock.Lock() defer lock.Unlock() row, ok := m.ownedRoom(w, r) if !ok { return } var req struct { EventIDs []id.EventID `json:"eventIds"` } if !readBody(w, r, &req) { return } if len(req.EventIDs) > maxRedacted { writeError(w, http.StatusBadRequest, "M_INVALID_PARAM", fmt.Sprintf("At most %d event IDs", maxRedacted)) return } if row.State != stateDone { writeError(w, http.StatusConflict, "XSG_NOT_DONE", "The room's history is not moved yet") return } ctx := m.requestContext(r) answer, err := m.redacted(ctx, row, req.EventIDs) if err != nil { answerError(ctx, w, err, "Failed to leave the deleted events out") return } writeJSON(w, http.StatusOK, answer) } func (m *mover) redacted(ctx context.Context, row *room, named []id.EventID) (map[string]any, error) { written, err := m.store.events(ctx, row.OldRoom) if err != nil { return nil, err } byID := make(map[id.EventID]*movedEvent, len(written)) for _, e := range written { byID[e.NewEventID] = e } var marked []int for _, eventID := range named { e := byID[eventID] if e == nil || e.Redacted { continue } evt, err := m.conn.Bot.GetEvent(ctx, row.NewRoom, eventID) if errors.Is(err, mautrix.MNotFound) { // Not deleted but gone from the room (the retention period emptied it): not the app's to say. continue } else if err != nil { zerolog.Ctx(ctx).Err(err).Msg("Failed to read a moved event the API named as deleted") return nil, failWith(http.StatusServiceUnavailable, "XSG_READ", "Failed to read a moved event") } if evt.Unsigned.RedactedBecause == nil { continue } e.Redacted = true marked = append(marked, e.Seq) } if len(marked) > 0 { if err = m.store.markRedacted(ctx, row.OldRoom, marked); err != nil { return nil, err } zerolog.Ctx(ctx).Info().Int("marked", len(marked)).Msg("Left moved events deleted since out of the room's numbers") } if !slices.ContainsFunc(written, func(e *movedEvent) bool { return e.Redacted }) { // Nothing moved went (or nothing was moved): the move's own numbers. return map[string]any{"events": row.Events, "files": row.Files, "digest": row.Digest, "redacted": 0}, nil } sealer, err := m.sealer() if err != nil { return nil, err } key, err := m.digestKey(ctx, row, sealer, true) if err != nil { return nil, err } t, err := m.tallyOf(row, written, sealer, key, func(e *movedEvent) bool { return !e.Redacted }) if err != nil { return nil, err } return map[string]any{"events": t.Events, "files": t.Files, "digest": t.Digest, "redacted": len(written) - t.Events}, nil }