package migrate import ( "context" "errors" "net/http" "github.com/rs/zerolog" "maunium.net/go/mautrix" "maunium.net/go/mautrix/id" ) // handleRelease lets go of a room whose history was moved, right before the API queues the old // room for deletion: in a portal the bot and the contacts' ghosts leave the old room, so that the // server deletes it whole without waiting for the bridge; in two Xessenger users' room the bot // leaves the new room, which it joined only to write the history. func (m *mover) handleRelease(w http.ResponseWriter, r *http.Request) { userID, ok := m.pathUser(w, r) if !ok { return } oldRoom, ok := pathRoom(w, r) if !ok { return } ctx := m.requestContext(r) lock := m.roomLock(oldRoom) lock.Lock() defer lock.Unlock() row, err := m.store.room(ctx, oldRoom) if err != nil { answerError(ctx, w, err, "Failed to read the room's move") return } else if row == nil { writeError(w, http.StatusNotFound, "M_NOT_FOUND", "The room was not moved") return } else if !row.owns(userID) { writeError(w, http.StatusForbidden, "M_FORBIDDEN", "The room was moved for another user") return } else if row.State != stateDone { writeError(w, http.StatusConflict, "XSG_NOT_MIGRATED", "The room's history was not moved yet") return } var left int if row.Kind == kindXessenger { left, err = m.leave(ctx, m.conn.Bot.UserID, row.NewRoom) } else { left, err = m.releasePortalRoom(ctx, row) } if err != nil { answerError(ctx, w, err, "Failed to let the room go") return } writeJSON(w, http.StatusOK, map[string]any{"left": left}) } // releasePortalRoom has the bot and the contacts' ghosts leave a portal's old room. A moved group no // start looked at (name_checked) and without a name the app shows takes the old room's first // (fillMovedName), as the room holds the name's last copy: what fails keeps the room, which the API lets // go of again later. A group a start looked at keeps what it has, as it would at the next start: a name // taken off on the messenger since stays off, and its old room goes unread. // A group whose owner is out of its new room (ownerAway) does not take its metadata until they are back, // if ever: its old room goes without the name, as keeping it would keep it for good (the user's switch // would never finish, the room's plaintext staying with it). The two rules meet only in a group no start // looked at: one a start looked at is not filled in, so whether its owner is away is not asked (the // start marked it once its metadata went, or once it needed none). func (m *mover) releasePortalRoom(ctx context.Context, row *room) (int, error) { oldRoom := row.OldRoom portal, err := m.br.GetPortalByMXID(ctx, oldRoom) if err != nil { return 0, err } else if portal != nil { return 0, failWith(http.StatusConflict, "XSG_STILL_PORTAL", "The old room is still a portal") } checked, err := m.store.nameWasChecked(ctx, oldRoom) if err != nil { return 0, err } if !checked { if _, err = m.fillMovedName(ctx, row, false); ownerAway(err) { zerolog.Ctx(ctx).Warn().Err(err).Stringer("old_room_id", oldRoom).Stringer("room_id", row.NewRoom).Msg("Letting the old room go without filling in its group's name: the group's owner is out of its room") } else if err != nil { return 0, err } } members, err := m.conn.Bot.JoinedMembers(ctx, oldRoom) if errors.Is(err, mautrix.MForbidden) || isNotFound(err) { // The bot already left: released before. return 0, nil } else if err != nil { return 0, err } left := 0 for userID := range members.Joined { if userID == m.conn.Bot.UserID || !m.br.IsGhostMXID(userID) { continue } count, err := m.leave(ctx, userID, oldRoom) if err != nil { return left, err } left += count } count, err := m.leave(ctx, m.conn.Bot.UserID, oldRoom) return left + count, err } // leave makes the bot or a ghost (by its own appservice identity) leave a room it may have left. func (m *mover) leave(ctx context.Context, userID id.UserID, roomID id.RoomID) (int, error) { intent := m.conn.Bot if userID != m.conn.Bot.UserID { intent = m.conn.AS.Intent(userID) } _, err := intent.LeaveRoom(ctx, roomID) if errors.Is(err, mautrix.MForbidden) || isNotFound(err) { return 0, nil } else if err != nil { zerolog.Ctx(ctx).Err(err).Stringer("room_id", roomID).Msg("Failed to leave a moved room") return 0, err } return 1, nil } // handleForget makes the bridge forget a bot management room before the API deletes it: the user's // management room is cleared (bridgev2 makes a new one when the user writes to the bot again) and // the bot leaves. func (m *mover) handleForget(w http.ResponseWriter, r *http.Request) { userID, ok := m.pathUser(w, r) if !ok { return } var req struct { RoomID id.RoomID `json:"roomId"` } if !readBody(w, r, &req) { return } if len(req.RoomID) < 3 || req.RoomID[0] != '!' { writeError(w, http.StatusBadRequest, "M_INVALID_PARAM", "Not a room ID") return } ctx := m.requestContext(r) forgotten, err := m.forgetManagementRoom(ctx, userID, req.RoomID) if err != nil { answerError(ctx, w, err, "Failed to forget the management room") return } writeJSON(w, http.StatusOK, map[string]any{"forgotten": forgotten}) } func (m *mover) forgetManagementRoom(ctx context.Context, userID id.UserID, roomID id.RoomID) (bool, error) { portal, err := m.br.GetPortalByMXID(ctx, roomID) if err != nil { return false, err } else if portal != nil { return false, failWith(http.StatusConflict, "XSG_STILL_PORTAL", "The room is a portal") } forgotten := false user, err := m.br.GetExistingUserByMXID(ctx, userID) if err != nil { return false, err } if user != nil && user.ManagementRoom == roomID { user.ManagementRoom = "" if err = m.br.DB.User.Update(ctx, user.User); err != nil { return false, err } forgotten = true } if _, err = m.leave(ctx, m.conn.Bot.UserID, roomID); err != nil { return forgotten, err } return forgotten, nil }