refactor _startDm invite flow to use async/await

pull/21833/head
Germain Souquet 2021-04-14 08:44:33 +01:00
parent d0dfc5df2b
commit 8d95c012ef
1 changed files with 18 additions and 23 deletions

View File

@ -656,35 +656,30 @@ export default class InviteDialog extends React.PureComponent<IInviteDialogProps
// Check if it's a traditional DM and create the room if required. // Check if it's a traditional DM and create the room if required.
// TODO: [Canonical DMs] Remove this check and instead just create the multi-person DM // TODO: [Canonical DMs] Remove this check and instead just create the multi-person DM
let createRoomPromise = Promise.resolve(null) as Promise<string | null | boolean>;
try {
const isSelf = targetIds.length === 1 && targetIds[0] === MatrixClientPeg.get().getUserId(); const isSelf = targetIds.length === 1 && targetIds[0] === MatrixClientPeg.get().getUserId();
if (targetIds.length === 1 && !isSelf) { if (targetIds.length === 1 && !isSelf) {
createRoomOptions.dmUserId = targetIds[0]; createRoomOptions.dmUserId = targetIds[0];
createRoomPromise = createRoom(createRoomOptions); await createRoom(createRoomOptions);
} else if (isSelf) { } else if (isSelf) {
createRoomPromise = createRoom(createRoomOptions); await createRoom(createRoomOptions);
} else { } else {
// Create a boring room and try to invite the targets manually. const roomId = await createRoom(createRoomOptions);
createRoomPromise = createRoom(createRoomOptions).then(roomId => { const invitesState = await inviteMultipleToRoom(roomId, targetIds);
return inviteMultipleToRoom(roomId, targetIds);
}).then(result => {
if (this._shouldAbortAfterInviteError(result)) {
return true; // abort
}
});
}
// the createRoom call will show the room for us, so we don't need to worry about that. const abort = this._shouldAbortAfterInviteError(invitesState);
createRoomPromise.then(abort => { if (abort === false) {
if (abort === true) return; // only abort on true booleans, not roomIds or something
this.props.onFinished(); this.props.onFinished();
}).catch(err => { }
}
} catch (err) {
console.error(err); console.error(err);
this.setState({ this.setState({
busy: false, busy: false,
errorText: _t("We couldn't create your DM."), errorText: _t("We couldn't create your DM."),
}); });
}); }
}; };
_inviteUsers = async () => { _inviteUsers = async () => {