move DM invite responsiblity to the server side

This commit is contained in:
Germain Souquet 2021-04-14 19:59:17 +01:00
parent 8b2dc22fe1
commit 3b66821258
2 changed files with 28 additions and 17 deletions

View file

@ -31,6 +31,7 @@ import Modal from "../../../Modal";
import {humanizeTime} from "../../../utils/humanize"; import {humanizeTime} from "../../../utils/humanize";
import createRoom, { import createRoom, {
canEncryptToAllUsers, ensureDMExists, findDMForUser, privateShouldBeEncrypted, canEncryptToAllUsers, ensureDMExists, findDMForUser, privateShouldBeEncrypted,
IInvite3PID,
} from "../../../createRoom"; } from "../../../createRoom";
import {inviteMultipleToRoom, showCommunityInviteDialog} from "../../../RoomInvite"; import {inviteMultipleToRoom, showCommunityInviteDialog} from "../../../RoomInvite";
import {Key} from "../../../Keyboard"; import {Key} from "../../../Keyboard";
@ -656,29 +657,33 @@ 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 abort = false;
try { try {
const isSelf = targetIds.length === 1 && targetIds[0] === client.getUserId(); const isSelf = targetIds.length === 1 && targetIds[0] === client.getUserId();
if (targetIds.length === 1 && !isSelf) { if (targetIds.length === 1 && !isSelf) {
createRoomOptions.dmUserId = targetIds[0]; createRoomOptions.dmUserId = targetIds[0];
await createRoom(createRoomOptions); }
} else if (isSelf) {
await createRoom(createRoomOptions);
} else {
const roomId = await createRoom(createRoomOptions);
/**
* To avoid race condition we want to make sure the room information
* is properly synced with the account before sending invites to targets
* It can otherwise result in a "Cannot find room" error
*/
await client.peekInRoom(roomId);
const invitesState = await inviteMultipleToRoom(roomId, targetIds);
abort = this._shouldAbortAfterInviteError(invitesState); if (targetIds.length > 1) {
} createRoomOptions.createOpts = targetIds.reduce(
if (!abort) { (roomOptions, address) => {
this.props.onFinished(); 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) { } catch (err) {
console.error(err); console.error(err);
this.setState({ this.setState({

View file

@ -90,6 +90,12 @@ export interface IOpts {
parentSpace?: Room; parentSpace?: Room;
} }
export interface IInvite3PID {
id_server: string,
medium: 'email',
address: string,
}
/** /**
* Create a new room, and switch to it. * Create a new room, and switch to it.
* *