Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Merge pull request #6737 from matrix-org/t3chguy/fix/18789
Browse files Browse the repository at this point in the history
Serialize and retry mass-leave when leaving space
  • Loading branch information
t3chguy authored Sep 3, 2021
2 parents 0b07a70 + acb89d3 commit b5abaff
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/utils/membership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ limitations under the License.
*/

import { Room } from "matrix-js-sdk/src/models/room";
import { sleep } from "matrix-js-sdk/src/utils";

import { MatrixClientPeg } from "../MatrixClientPeg";
import { _t } from "../languageHandler";
import Modal from "../Modal";
Expand Down Expand Up @@ -83,9 +85,10 @@ export function isJoinedOrNearlyJoined(membership: string): boolean {
return effective === EffectiveMembership.Join || effective === EffectiveMembership.Invite;
}

export async function leaveRoomBehaviour(roomId: string) {
export async function leaveRoomBehaviour(roomId: string, retry = true) {
const cli = MatrixClientPeg.get();
let leavingAllVersions = true;
const history = await MatrixClientPeg.get().getRoomUpgradeHistory(roomId);
const history = cli.getRoomUpgradeHistory(roomId);
if (history && history.length > 0) {
const currentRoom = history[history.length - 1];
if (currentRoom.roomId !== roomId) {
Expand All @@ -95,20 +98,28 @@ export async function leaveRoomBehaviour(roomId: string) {
}
}

let results: { [roomId: string]: Error & { errcode: string, message: string } } = {};
let results: { [roomId: string]: Error & { errcode?: string, message: string, data?: Record<string, any> } } = {};
if (!leavingAllVersions) {
try {
await MatrixClientPeg.get().leave(roomId);
await cli.leave(roomId);
} catch (e) {
if (e && e.data && e.data.errcode) {
if (e?.data?.errcode) {
const message = e.data.error || _t("Unexpected server error trying to leave the room");
results[roomId] = Object.assign(new Error(message), { errcode: e.data.errcode });
results[roomId] = Object.assign(new Error(message), { errcode: e.data.errcode, data: e.data });
} else {
results[roomId] = e || new Error("Failed to leave room for unknown causes");
}
}
} else {
results = await MatrixClientPeg.get().leaveRoomChain(roomId);
results = await cli.leaveRoomChain(roomId, retry);
}

if (retry) {
const limitExceededError = Object.values(results).find(e => e?.errcode === "M_LIMIT_EXCEEDED");
if (limitExceededError) {
await sleep(limitExceededError.data.retry_after_ms ?? 100);
return leaveRoomBehaviour(roomId, false);
}
}

const errors = Object.entries(results).filter(r => !!r[1]);
Expand Down
4 changes: 3 additions & 1 deletion src/utils/space.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,9 @@ export const leaveSpace = (space: Room) => {
if (!leave) return;
const modal = Modal.createDialog(Spinner, null, "mx_Dialog_spinner");
try {
await Promise.all(rooms.map(r => leaveRoomBehaviour(r.roomId)));
for (const room of rooms) {
await leaveRoomBehaviour(room.roomId);
}
await leaveRoomBehaviour(space.roomId);
} finally {
modal.close();
Expand Down

0 comments on commit b5abaff

Please sign in to comment.