package migrate import ( "net/http" "strconv" "maunium.net/go/mautrix/id" ) // handleMedia lists the encrypted copies put in the new room and their ciphertext SHA-256, which // the API checks against the server's copies before it deletes the old files. A copy only moved // events found deleted since show (handleRedacted), as their files or a mail body's attachments // (deletedFiles), is `deleted`: the new room let go of it, and its copy on the server goes with the // deletion queue. func (m *mover) handleMedia(w http.ResponseWriter, r *http.Request) { row, ok := m.ownedRoom(w, r) if !ok { return } limit := 500 if value := r.URL.Query().Get("limit"); value != "" { parsed, err := strconv.Atoi(value) if err != nil || parsed < 1 || parsed > 1000 { writeError(w, http.StatusBadRequest, "M_INVALID_PARAM", "limit must be 1 to 1000") return } limit = parsed } ctx := m.requestContext(r) list, err := m.store.copiedMedia(ctx, row.OldRoom, r.URL.Query().Get("after"), limit) if err != nil { answerError(ctx, w, err, "Failed to list the copied files") return } deleted, err := m.store.deletedFiles(ctx, row.OldRoom) if err != nil { answerError(ctx, w, err, "Failed to list the copied files") return } type copied struct { MXC string `json:"mxc"` SHA256 string `json:"sha256"` Deleted bool `json:"deleted,omitempty"` } media := make([]copied, len(list)) for i, item := range list { media[i] = copied{MXC: item.NewMXC, SHA256: item.SHA256, Deleted: deleted[item.NewMXC]} } next := "" if len(list) == limit { next = list[len(list)-1].NewMXC } writeJSON(w, http.StatusOK, map[string]any{"media": media, "next": next}) } // handleMap gives the new events of old ones, for the API to carry the user's read marker and hidden // messages over. With before (an old timestamp, in milliseconds) it also gives the last moved event // sent at or before it, for a marker on an event that was not moved. func (m *mover) handleMap(w http.ResponseWriter, r *http.Request) { row, ok := m.ownedRoom(w, r) if !ok { return } var req struct { EventIDs []id.EventID `json:"eventIds"` Before int64 `json:"before"` } if !readBody(w, r, &req) { return } if len(req.EventIDs) > 1000 { writeError(w, http.StatusBadRequest, "M_INVALID_PARAM", "At most 1000 event IDs") return } ctx := m.requestContext(r) mapping, err := m.store.mapEvents(ctx, row.OldRoom, req.EventIDs) if err != nil { answerError(ctx, w, err, "Failed to map the events") return } answer := map[string]any{"map": mapping} if req.Before > 0 { floor, err := m.store.floorEvent(ctx, row.OldRoom, req.Before) if err != nil { answerError(ctx, w, err, "Failed to map the events") return } if floor != "" { answer["floor"] = floor } } writeJSON(w, http.StatusOK, answer) } // ownedRoom is the move of the path's room, which must be the path user's. func (m *mover) ownedRoom(w http.ResponseWriter, r *http.Request) (*room, bool) { userID, ok := m.pathUser(w, r) if !ok { return nil, false } oldRoom, ok := pathRoom(w, r) if !ok { return nil, false } row, err := m.store.room(m.requestContext(r), oldRoom) if err != nil { answerError(m.requestContext(r), w, err, "Failed to read the room's move") return nil, false } else if row == nil { writeError(w, http.StatusNotFound, "M_NOT_FOUND", "The room was not moved") return nil, false } else if !row.owns(userID) { writeError(w, http.StatusForbidden, "M_FORBIDDEN", "The room was moved for another user") return nil, false } return row, true }