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

Commit f410d52

Browse files
authored
Finish sending pending messages before leaving room (#7276)
1 parent 7dfdb06 commit f410d52

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

src/components/structures/MatrixChat.tsx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,12 +1180,8 @@ export default class MatrixChat extends React.PureComponent<IProps, IState> {
11801180
button: _t("Leave"),
11811181
onFinished: (shouldLeave) => {
11821182
if (shouldLeave) {
1183-
const d = leaveRoomBehaviour(roomId);
1183+
leaveRoomBehaviour(roomId);
11841184

1185-
// FIXME: controller shouldn't be loading a view :(
1186-
const modal = Modal.createDialog(Spinner, null, 'mx_Dialog_spinner');
1187-
1188-
d.finally(() => modal.close());
11891185
dis.dispatch({
11901186
action: "after_leave_room",
11911187
room_id: roomId,

src/utils/membership.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,16 @@ limitations under the License.
1616

1717
import { Room } from "matrix-js-sdk/src/models/room";
1818
import { sleep } from "matrix-js-sdk/src/utils";
19+
import { EventStatus } from "matrix-js-sdk/src/models/event";
1920

2021
import { MatrixClientPeg } from "../MatrixClientPeg";
2122
import { _t } from "../languageHandler";
22-
import Modal from "../Modal";
23+
import Modal, { IHandle } from "../Modal";
2324
import ErrorDialog from "../components/views/dialogs/ErrorDialog";
2425
import React from "react";
2526
import dis from "../dispatcher/dispatcher";
2627
import RoomViewStore from "../stores/RoomViewStore";
28+
import Spinner from "../components/views/elements/Spinner";
2729

2830
/**
2931
* Approximation of a membership status for a given room.
@@ -85,7 +87,12 @@ export function isJoinedOrNearlyJoined(membership: string): boolean {
8587
return effective === EffectiveMembership.Join || effective === EffectiveMembership.Invite;
8688
}
8789

88-
export async function leaveRoomBehaviour(roomId: string, retry = true) {
90+
export async function leaveRoomBehaviour(roomId: string, retry = true, spinner = true) {
91+
let spinnerModal: IHandle<any>;
92+
if (spinner) {
93+
spinnerModal = Modal.createDialog(Spinner, null, 'mx_Dialog_spinner');
94+
}
95+
8996
const cli = MatrixClientPeg.get();
9097
let leavingAllVersions = true;
9198
const history = cli.getRoomUpgradeHistory(roomId);
@@ -98,6 +105,26 @@ export async function leaveRoomBehaviour(roomId: string, retry = true) {
98105
}
99106
}
100107

108+
const room = cli.getRoom(roomId);
109+
// await any queued messages being sent so that they do not fail
110+
await Promise.all(room.getPendingEvents().filter(ev => {
111+
return [EventStatus.QUEUED, EventStatus.ENCRYPTING, EventStatus.SENDING].includes(ev.status);
112+
}).map(ev => new Promise<void>((resolve, reject) => {
113+
const handler = () => {
114+
if (ev.status === EventStatus.NOT_SENT) {
115+
spinnerModal?.close();
116+
reject(ev.error);
117+
}
118+
119+
if (!ev.status || ev.status === EventStatus.SENT) {
120+
ev.off("Event.status", handler);
121+
resolve();
122+
}
123+
};
124+
125+
ev.on("Event.status", handler);
126+
})));
127+
101128
let results: { [roomId: string]: Error & { errcode?: string, message: string, data?: Record<string, any> } } = {};
102129
if (!leavingAllVersions) {
103130
try {
@@ -118,10 +145,12 @@ export async function leaveRoomBehaviour(roomId: string, retry = true) {
118145
const limitExceededError = Object.values(results).find(e => e?.errcode === "M_LIMIT_EXCEEDED");
119146
if (limitExceededError) {
120147
await sleep(limitExceededError.data.retry_after_ms ?? 100);
121-
return leaveRoomBehaviour(roomId, false);
148+
return leaveRoomBehaviour(roomId, false, false);
122149
}
123150
}
124151

152+
spinnerModal?.close();
153+
125154
const errors = Object.entries(results).filter(r => !!r[1]);
126155
if (errors.length > 0) {
127156
const messages = [];

0 commit comments

Comments
 (0)