From 8d95c012ef7bba98795322742756f80da81bd9bf Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Wed, 14 Apr 2021 08:44:33 +0100 Subject: [PATCH 1/5] refactor _startDm invite flow to use async/await --- src/components/views/dialogs/InviteDialog.tsx | 41 ++++++++----------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/components/views/dialogs/InviteDialog.tsx b/src/components/views/dialogs/InviteDialog.tsx index 60f783e889..aabc8b59e3 100644 --- a/src/components/views/dialogs/InviteDialog.tsx +++ b/src/components/views/dialogs/InviteDialog.tsx @@ -656,35 +656,30 @@ export default class InviteDialog extends React.PureComponent; - const isSelf = targetIds.length === 1 && targetIds[0] === MatrixClientPeg.get().getUserId(); - if (targetIds.length === 1 && !isSelf) { - createRoomOptions.dmUserId = targetIds[0]; - createRoomPromise = createRoom(createRoomOptions); - } else if (isSelf) { - createRoomPromise = createRoom(createRoomOptions); - } else { - // Create a boring room and try to invite the targets manually. - createRoomPromise = createRoom(createRoomOptions).then(roomId => { - 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. - createRoomPromise.then(abort => { - if (abort === true) return; // only abort on true booleans, not roomIds or something - this.props.onFinished(); - }).catch(err => { + try { + const isSelf = targetIds.length === 1 && targetIds[0] === MatrixClientPeg.get().getUserId(); + if (targetIds.length === 1 && !isSelf) { + createRoomOptions.dmUserId = targetIds[0]; + await createRoom(createRoomOptions); + } else if (isSelf) { + await createRoom(createRoomOptions); + } else { + const roomId = await createRoom(createRoomOptions); + const invitesState = await inviteMultipleToRoom(roomId, targetIds); + + const abort = this._shouldAbortAfterInviteError(invitesState); + if (abort === false) { + this.props.onFinished(); + } + } + } catch (err) { console.error(err); this.setState({ busy: false, errorText: _t("We couldn't create your DM."), }); - }); + } }; _inviteUsers = async () => { From f89bbea3f1678754f2ef9b6e1eb2f8f178350f04 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Wed, 14 Apr 2021 09:03:47 +0100 Subject: [PATCH 2/5] Ensure room is synced with account before sending invites --- src/components/views/dialogs/InviteDialog.tsx | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/components/views/dialogs/InviteDialog.tsx b/src/components/views/dialogs/InviteDialog.tsx index aabc8b59e3..cbdfdcbdc8 100644 --- a/src/components/views/dialogs/InviteDialog.tsx +++ b/src/components/views/dialogs/InviteDialog.tsx @@ -618,13 +618,14 @@ export default class InviteDialog extends React.PureComponent { this.setState({busy: true}); + const client = MatrixClientPeg.get(); const targets = this._convertFilter(); const targetIds = targets.map(t => t.userId); // Check if there is already a DM with these people and reuse it if possible. let existingRoom: Room; if (targetIds.length === 1) { - existingRoom = findDMForUser(MatrixClientPeg.get(), targetIds[0]); + existingRoom = findDMForUser(client, targetIds[0]); } else { existingRoom = DMRoomMap.shared().getDMRoomForIdentifiers(targetIds); } @@ -646,7 +647,6 @@ export default class InviteDialog extends React.PureComponent t instanceof ThreepidMember); if (!has3PidMembers) { - const client = MatrixClientPeg.get(); const allHaveDeviceKeys = await canEncryptToAllUsers(client, targetIds); if (allHaveDeviceKeys) { createRoomOptions.encryption = true; @@ -656,9 +656,8 @@ export default class InviteDialog extends React.PureComponent Date: Wed, 14 Apr 2021 09:37:06 +0100 Subject: [PATCH 3/5] fix closing modal when finished --- src/components/views/dialogs/InviteDialog.tsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/components/views/dialogs/InviteDialog.tsx b/src/components/views/dialogs/InviteDialog.tsx index cbdfdcbdc8..c89e61bd18 100644 --- a/src/components/views/dialogs/InviteDialog.tsx +++ b/src/components/views/dialogs/InviteDialog.tsx @@ -656,6 +656,7 @@ export default class InviteDialog extends React.PureComponent Date: Wed, 14 Apr 2021 19:59:17 +0100 Subject: [PATCH 4/5] move DM invite responsiblity to the server side --- src/components/views/dialogs/InviteDialog.tsx | 39 +++++++++++-------- src/createRoom.ts | 6 +++ 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/src/components/views/dialogs/InviteDialog.tsx b/src/components/views/dialogs/InviteDialog.tsx index c89e61bd18..5ca7e3aec2 100644 --- a/src/components/views/dialogs/InviteDialog.tsx +++ b/src/components/views/dialogs/InviteDialog.tsx @@ -31,6 +31,7 @@ import Modal from "../../../Modal"; import {humanizeTime} from "../../../utils/humanize"; import createRoom, { canEncryptToAllUsers, ensureDMExists, findDMForUser, privateShouldBeEncrypted, + IInvite3PID, } from "../../../createRoom"; import {inviteMultipleToRoom, showCommunityInviteDialog} from "../../../RoomInvite"; import {Key} from "../../../Keyboard"; @@ -656,29 +657,33 @@ export default class InviteDialog extends React.PureComponent 1) { + createRoomOptions.createOpts = targetIds.reduce( + (roomOptions, address) => { + if (getAddressType(address) === 'email') { + const invite: IInvite3PID = { + id_server: client.getIdentityServerUrl(true), + medium: 'email', + address, + }; + roomOptions.invite_3pid.push(invite); + } else { + roomOptions.invite.push(address); + } + return roomOptions; + }, + { invite: [], invite_3pid: [] }, + ) } + + await createRoom(createRoomOptions); + this.props.onFinished(); } catch (err) { console.error(err); this.setState({ diff --git a/src/createRoom.ts b/src/createRoom.ts index a5343076ac..310d894266 100644 --- a/src/createRoom.ts +++ b/src/createRoom.ts @@ -90,6 +90,12 @@ export interface IOpts { parentSpace?: Room; } +export interface IInvite3PID { + id_server: string, + medium: 'email', + address: string, +} + /** * Create a new room, and switch to it. * From 3a91dff7112f4b1e653e76ab0df460cd5723c883 Mon Sep 17 00:00:00 2001 From: Germain Souquet Date: Thu, 15 Apr 2021 14:13:37 +0100 Subject: [PATCH 5/5] Check if address type is mx-user-id --- src/components/views/dialogs/InviteDialog.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/views/dialogs/InviteDialog.tsx b/src/components/views/dialogs/InviteDialog.tsx index 5ca7e3aec2..2ebc84ec7c 100644 --- a/src/components/views/dialogs/InviteDialog.tsx +++ b/src/components/views/dialogs/InviteDialog.tsx @@ -666,14 +666,15 @@ export default class InviteDialog extends React.PureComponent 1) { createRoomOptions.createOpts = targetIds.reduce( (roomOptions, address) => { - if (getAddressType(address) === 'email') { + const type = getAddressType(address); + if (type === 'email') { const invite: IInvite3PID = { id_server: client.getIdentityServerUrl(true), medium: 'email', address, }; roomOptions.invite_3pid.push(invite); - } else { + } else if (type === 'mx-user-id') { roomOptions.invite.push(address); } return roomOptions;