2016-06-09 01:03:46 +03:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2020-01-23 16:54:28 +03:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2016-06-09 01:03:46 +03:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-11-14 01:19:34 +03:00
|
|
|
import { MatrixClient } from "matrix-js-sdk/src/client";
|
|
|
|
import { Room } from "matrix-js-sdk/src/models/room";
|
2021-07-20 00:43:11 +03:00
|
|
|
import { RoomMember } from "matrix-js-sdk/src/models/room-member";
|
2021-07-23 10:46:20 +03:00
|
|
|
import { EventType, RoomCreateTypeField, RoomType } from "matrix-js-sdk/src/@types/event";
|
2021-06-18 14:18:23 +03:00
|
|
|
import { ICreateRoomOpts } from "matrix-js-sdk/src/@types/requests";
|
2021-07-23 10:46:20 +03:00
|
|
|
import {
|
|
|
|
HistoryVisibility,
|
|
|
|
JoinRule,
|
|
|
|
Preset,
|
|
|
|
RestrictedAllowType,
|
|
|
|
Visibility,
|
|
|
|
} from "matrix-js-sdk/src/@types/partials";
|
2021-10-23 01:23:32 +03:00
|
|
|
import { logger } from "matrix-js-sdk/src/logger";
|
2022-02-22 15:18:08 +03:00
|
|
|
import { RoomStateEvent } from "matrix-js-sdk/src/models/room-state";
|
2020-07-03 00:38:11 +03:00
|
|
|
|
2020-11-14 01:19:34 +03:00
|
|
|
import { MatrixClientPeg } from './MatrixClientPeg';
|
2017-07-01 16:40:46 +03:00
|
|
|
import Modal from './Modal';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from './languageHandler';
|
2020-05-14 05:41:41 +03:00
|
|
|
import dis from "./dispatcher/dispatcher";
|
2017-07-05 17:00:50 +03:00
|
|
|
import * as Rooms from "./Rooms";
|
2019-12-19 19:43:10 +03:00
|
|
|
import DMRoomMap from "./utils/DMRoomMap";
|
2020-11-14 01:19:34 +03:00
|
|
|
import { getAddressType } from "./UserAddress";
|
2020-08-14 15:27:12 +03:00
|
|
|
import { getE2EEWellKnown } from "./utils/WellKnownUtils";
|
2020-11-14 01:19:34 +03:00
|
|
|
import { isJoinedOrNearlyJoined } from "./utils/membership";
|
2021-02-17 21:51:21 +03:00
|
|
|
import { VIRTUAL_ROOM_EVENT_TYPE } from "./CallHandler";
|
2021-11-11 16:07:41 +03:00
|
|
|
import SpaceStore from "./stores/spaces/SpaceStore";
|
2021-03-01 22:05:50 +03:00
|
|
|
import { makeSpaceParentEvent } from "./utils/space";
|
2022-03-23 01:14:11 +03:00
|
|
|
import { addVoiceChannel } from "./utils/VoiceChannelUtils";
|
2021-06-29 15:11:58 +03:00
|
|
|
import { Action } from "./dispatcher/actions";
|
2021-07-02 13:12:41 +03:00
|
|
|
import ErrorDialog from "./components/views/dialogs/ErrorDialog";
|
|
|
|
import Spinner from "./components/views/elements/Spinner";
|
2022-02-10 17:29:55 +03:00
|
|
|
import { ViewRoomPayload } from "./dispatcher/payloads/ViewRoomPayload";
|
2016-06-09 01:03:46 +03:00
|
|
|
|
2020-07-20 22:50:12 +03:00
|
|
|
// we define a number of interfaces which take their names from the js-sdk
|
2020-07-20 22:43:49 +03:00
|
|
|
/* eslint-disable camelcase */
|
|
|
|
|
2021-03-01 20:54:53 +03:00
|
|
|
export interface IOpts {
|
2020-07-03 00:38:11 +03:00
|
|
|
dmUserId?: string;
|
2021-06-14 22:31:58 +03:00
|
|
|
createOpts?: ICreateRoomOpts;
|
2020-07-03 00:38:11 +03:00
|
|
|
spinner?: boolean;
|
|
|
|
guestAccess?: boolean;
|
|
|
|
encryption?: boolean;
|
|
|
|
inlineErrors?: boolean;
|
|
|
|
andView?: boolean;
|
2021-07-23 10:46:20 +03:00
|
|
|
avatar?: File | string; // will upload if given file, else mxcUrl is needed
|
|
|
|
roomType?: RoomType | string;
|
|
|
|
historyVisibility?: HistoryVisibility;
|
2021-03-01 22:05:50 +03:00
|
|
|
parentSpace?: Room;
|
2021-08-31 18:42:21 +03:00
|
|
|
// contextually only makes sense if parentSpace is specified, if true then will be added to parentSpace as suggested
|
|
|
|
suggested?: boolean;
|
2021-07-02 16:51:55 +03:00
|
|
|
joinRule?: JoinRule;
|
2020-07-03 00:38:11 +03:00
|
|
|
}
|
|
|
|
|
2016-06-09 01:03:46 +03:00
|
|
|
/**
|
|
|
|
* Create a new room, and switch to it.
|
|
|
|
*
|
|
|
|
* @param {object=} opts parameters for creating the room
|
2016-09-09 21:25:00 +03:00
|
|
|
* @param {string=} opts.dmUserId If specified, make this a DM room for this user and invite them
|
2016-06-09 01:03:46 +03:00
|
|
|
* @param {object=} opts.createOpts set of options to pass to createRoom call.
|
2019-06-14 19:21:07 +03:00
|
|
|
* @param {bool=} opts.spinner True to show a modal spinner while the room is created.
|
|
|
|
* Default: True
|
2020-01-23 16:54:28 +03:00
|
|
|
* @param {bool=} opts.guestAccess Whether to enable guest access.
|
|
|
|
* Default: True
|
2020-01-23 17:05:38 +03:00
|
|
|
* @param {bool=} opts.encryption Whether to enable encryption.
|
|
|
|
* Default: False
|
2020-02-28 03:10:31 +03:00
|
|
|
* @param {bool=} opts.inlineErrors True to raise errors off the promise instead of resolving to null.
|
|
|
|
* Default: False
|
2020-07-03 00:38:11 +03:00
|
|
|
* @param {bool=} opts.andView True to dispatch an action to view the room once it has been created.
|
2017-07-01 16:40:46 +03:00
|
|
|
*
|
|
|
|
* @returns {Promise} which resolves to the room id, or null if the
|
|
|
|
* action was aborted or failed.
|
2016-06-09 01:03:46 +03:00
|
|
|
*/
|
2021-06-18 14:18:23 +03:00
|
|
|
export default async function createRoom(opts: IOpts): Promise<string | null> {
|
2016-09-09 21:25:00 +03:00
|
|
|
opts = opts || {};
|
2019-06-14 19:21:07 +03:00
|
|
|
if (opts.spinner === undefined) opts.spinner = true;
|
2020-01-23 16:54:28 +03:00
|
|
|
if (opts.guestAccess === undefined) opts.guestAccess = true;
|
2020-01-23 17:05:38 +03:00
|
|
|
if (opts.encryption === undefined) opts.encryption = false;
|
2016-06-09 01:03:46 +03:00
|
|
|
|
2016-09-09 21:25:00 +03:00
|
|
|
const client = MatrixClientPeg.get();
|
2016-06-09 01:03:46 +03:00
|
|
|
if (client.isGuest()) {
|
2021-06-29 15:11:58 +03:00
|
|
|
dis.dispatch({ action: 'require_registration' });
|
2021-06-18 14:18:23 +03:00
|
|
|
return null;
|
2016-06-09 01:03:46 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 00:38:11 +03:00
|
|
|
const defaultPreset = opts.dmUserId ? Preset.TrustedPrivateChat : Preset.PrivateChat;
|
2016-09-09 21:25:00 +03:00
|
|
|
|
2016-06-09 01:03:46 +03:00
|
|
|
// set some defaults for the creation
|
2021-06-14 22:31:58 +03:00
|
|
|
const createOpts: ICreateRoomOpts = opts.createOpts || {};
|
2016-09-09 21:25:00 +03:00
|
|
|
createOpts.preset = createOpts.preset || defaultPreset;
|
2020-07-03 00:38:11 +03:00
|
|
|
createOpts.visibility = createOpts.visibility || Visibility.Private;
|
2016-09-09 21:25:00 +03:00
|
|
|
if (opts.dmUserId && createOpts.invite === undefined) {
|
2018-01-25 12:54:31 +03:00
|
|
|
switch (getAddressType(opts.dmUserId)) {
|
|
|
|
case 'mx-user-id':
|
|
|
|
createOpts.invite = [opts.dmUserId];
|
|
|
|
break;
|
|
|
|
case 'email':
|
|
|
|
createOpts.invite_3pid = [{
|
|
|
|
id_server: MatrixClientPeg.get().getIdentityServerUrl(true),
|
|
|
|
medium: 'email',
|
|
|
|
address: opts.dmUserId,
|
|
|
|
}];
|
|
|
|
}
|
2016-09-09 21:25:00 +03:00
|
|
|
}
|
2016-09-12 20:32:44 +03:00
|
|
|
if (opts.dmUserId && createOpts.is_direct === undefined) {
|
|
|
|
createOpts.is_direct = true;
|
|
|
|
}
|
2016-06-09 01:03:46 +03:00
|
|
|
|
2021-07-23 10:46:20 +03:00
|
|
|
if (opts.roomType) {
|
|
|
|
createOpts.creation_content = {
|
|
|
|
...createOpts.creation_content,
|
|
|
|
[RoomCreateTypeField]: opts.roomType,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-05-24 18:56:13 +03:00
|
|
|
// By default, view the room after creating it
|
|
|
|
if (opts.andView === undefined) {
|
|
|
|
opts.andView = true;
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:05:38 +03:00
|
|
|
createOpts.initial_state = createOpts.initial_state || [];
|
|
|
|
|
2016-06-09 01:03:46 +03:00
|
|
|
// Allow guests by default since the room is private and they'd
|
|
|
|
// need an invite. This means clicking on a 3pid invite email can
|
|
|
|
// actually drop you right in to a chat.
|
2020-01-23 16:54:28 +03:00
|
|
|
if (opts.guestAccess) {
|
|
|
|
createOpts.initial_state.push({
|
2020-01-23 17:05:38 +03:00
|
|
|
type: 'm.room.guest_access',
|
|
|
|
state_key: '',
|
2016-06-09 01:03:46 +03:00
|
|
|
content: {
|
2017-07-01 16:40:46 +03:00
|
|
|
guest_access: 'can_join',
|
2016-06-09 01:03:46 +03:00
|
|
|
},
|
2020-01-23 17:05:38 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.encryption) {
|
|
|
|
createOpts.initial_state.push({
|
|
|
|
type: 'm.room.encryption',
|
2016-06-09 01:03:46 +03:00
|
|
|
state_key: '',
|
2020-01-23 17:05:38 +03:00
|
|
|
content: {
|
|
|
|
algorithm: 'm.megolm.v1.aes-sha2',
|
|
|
|
},
|
2020-01-23 16:54:28 +03:00
|
|
|
});
|
|
|
|
}
|
2016-06-09 01:03:46 +03:00
|
|
|
|
2021-03-01 22:05:50 +03:00
|
|
|
if (opts.parentSpace) {
|
2021-07-06 14:05:30 +03:00
|
|
|
createOpts.initial_state.push(makeSpaceParentEvent(opts.parentSpace, true));
|
2021-07-23 10:46:20 +03:00
|
|
|
if (!opts.historyVisibility) {
|
|
|
|
opts.historyVisibility = createOpts.preset === Preset.PublicChat
|
|
|
|
? HistoryVisibility.WorldReadable
|
|
|
|
: HistoryVisibility.Invited;
|
|
|
|
}
|
2021-06-18 14:18:23 +03:00
|
|
|
|
2021-07-02 16:51:55 +03:00
|
|
|
if (opts.joinRule === JoinRule.Restricted) {
|
2021-07-06 12:10:47 +03:00
|
|
|
if (SpaceStore.instance.restrictedJoinRuleSupport?.preferred) {
|
2021-07-06 14:05:30 +03:00
|
|
|
createOpts.room_version = SpaceStore.instance.restrictedJoinRuleSupport.preferred;
|
2021-06-18 14:18:23 +03:00
|
|
|
|
2021-07-06 14:05:30 +03:00
|
|
|
createOpts.initial_state.push({
|
2021-06-18 14:18:23 +03:00
|
|
|
type: EventType.RoomJoinRules,
|
|
|
|
content: {
|
|
|
|
"join_rule": JoinRule.Restricted,
|
|
|
|
"allow": [{
|
2021-06-22 18:07:05 +03:00
|
|
|
"type": RestrictedAllowType.RoomMembership,
|
2021-06-18 14:18:23 +03:00
|
|
|
"room_id": opts.parentSpace.roomId,
|
|
|
|
}],
|
|
|
|
},
|
2021-07-02 16:51:55 +03:00
|
|
|
});
|
2021-06-18 14:18:23 +03:00
|
|
|
}
|
|
|
|
}
|
2021-03-01 22:05:50 +03:00
|
|
|
}
|
|
|
|
|
2021-07-30 17:10:08 +03:00
|
|
|
// we handle the restricted join rule in the parentSpace handling block above
|
|
|
|
if (opts.joinRule && opts.joinRule !== JoinRule.Restricted) {
|
2021-07-06 14:05:30 +03:00
|
|
|
createOpts.initial_state.push({
|
2021-07-02 16:51:55 +03:00
|
|
|
type: EventType.RoomJoinRules,
|
|
|
|
content: { join_rule: opts.joinRule },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-23 10:46:20 +03:00
|
|
|
if (opts.avatar) {
|
|
|
|
let url = opts.avatar;
|
|
|
|
if (opts.avatar instanceof File) {
|
|
|
|
url = await client.uploadContent(opts.avatar);
|
|
|
|
}
|
|
|
|
|
|
|
|
createOpts.initial_state.push({
|
|
|
|
type: EventType.RoomAvatar,
|
|
|
|
content: { url },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.historyVisibility) {
|
|
|
|
createOpts.initial_state.push({
|
|
|
|
type: EventType.RoomHistoryVisibility,
|
|
|
|
content: {
|
|
|
|
"history_visibility": opts.historyVisibility,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-06-14 19:21:07 +03:00
|
|
|
let modal;
|
2021-07-02 13:12:41 +03:00
|
|
|
if (opts.spinner) modal = Modal.createDialog(Spinner, null, 'mx_Dialog_spinner');
|
2016-06-09 01:03:46 +03:00
|
|
|
|
2016-09-09 21:25:00 +03:00
|
|
|
let roomId;
|
2021-10-03 09:33:25 +03:00
|
|
|
return client.createRoom(createOpts).catch(function(err) {
|
2021-10-01 08:42:25 +03:00
|
|
|
// NB This checks for the Synapse-specific error condition of a room creation
|
|
|
|
// having been denied because the requesting user wanted to publish the room,
|
|
|
|
// but the server denies them that permission (via room_list_publication_rules).
|
|
|
|
// The check below responds by retrying without publishing the room.
|
2021-09-30 02:00:14 +03:00
|
|
|
if (err.httpStatus === 403 && err.errcode === "M_UNKNOWN" && err.data.error === "Not allowed to publish room") {
|
2021-10-15 17:31:29 +03:00
|
|
|
logger.warn("Failed to publish room, try again without publishing it");
|
2021-09-28 10:42:19 +03:00
|
|
|
createOpts.visibility = Visibility.Private;
|
|
|
|
return client.createRoom(createOpts);
|
|
|
|
} else {
|
|
|
|
return Promise.reject(err);
|
|
|
|
}
|
2021-10-03 09:33:25 +03:00
|
|
|
}).finally(function() {
|
|
|
|
if (modal) modal.close();
|
2016-06-09 01:03:46 +03:00
|
|
|
}).then(function(res) {
|
2016-09-09 21:25:00 +03:00
|
|
|
roomId = res.room_id;
|
|
|
|
if (opts.dmUserId) {
|
|
|
|
return Rooms.setDMRoom(roomId, opts.dmUserId);
|
|
|
|
} else {
|
2017-07-12 16:02:00 +03:00
|
|
|
return Promise.resolve();
|
2016-09-09 21:25:00 +03:00
|
|
|
}
|
2020-08-26 19:53:06 +03:00
|
|
|
}).then(() => {
|
2021-03-01 22:05:50 +03:00
|
|
|
if (opts.parentSpace) {
|
2021-08-31 18:42:21 +03:00
|
|
|
return SpaceStore.instance.addRoomToSpace(opts.parentSpace, roomId, [client.getDomain()], opts.suggested);
|
2021-03-01 22:05:50 +03:00
|
|
|
}
|
2022-03-23 01:14:11 +03:00
|
|
|
}).then(() => {
|
|
|
|
// Set up voice rooms with a Jitsi widget
|
|
|
|
if (opts.roomType === RoomType.UnstableCall) {
|
|
|
|
return addVoiceChannel(roomId, createOpts.name);
|
|
|
|
}
|
2016-09-09 21:25:00 +03:00
|
|
|
}).then(function() {
|
2016-07-17 20:32:48 +03:00
|
|
|
// NB createRoom doesn't block on the client seeing the echo that the
|
|
|
|
// room has been created, so we race here with the client knowing that
|
|
|
|
// the room exists, causing things like
|
|
|
|
// https://github.com/vector-im/vector-web/issues/1813
|
2021-03-01 20:54:53 +03:00
|
|
|
// Even if we were to block on the echo, servers tend to split the room
|
|
|
|
// state over multiple syncs so we can't atomically know when we have the
|
|
|
|
// entire thing.
|
2017-05-24 18:56:13 +03:00
|
|
|
if (opts.andView) {
|
2022-02-10 17:29:55 +03:00
|
|
|
dis.dispatch<ViewRoomPayload>({
|
2021-11-25 23:49:43 +03:00
|
|
|
action: Action.ViewRoom,
|
2017-05-24 18:56:13 +03:00
|
|
|
room_id: roomId,
|
2017-06-16 20:24:07 +03:00
|
|
|
should_peek: false,
|
2017-09-15 00:22:21 +03:00
|
|
|
// Creating a room will have joined us to the room,
|
|
|
|
// so we are expecting the room to come down the sync
|
|
|
|
// stream, if it hasn't already.
|
|
|
|
joining: true,
|
2021-03-01 20:54:53 +03:00
|
|
|
justCreatedOpts: opts,
|
2022-02-17 21:03:27 +03:00
|
|
|
metricsTrigger: "Created",
|
2017-05-24 18:56:13 +03:00
|
|
|
});
|
|
|
|
}
|
2016-09-09 21:25:00 +03:00
|
|
|
return roomId;
|
2016-06-09 01:03:46 +03:00
|
|
|
}, function(err) {
|
2020-02-28 03:10:31 +03:00
|
|
|
// Raise the error if the caller requested that we do so.
|
|
|
|
if (opts.inlineErrors) throw err;
|
|
|
|
|
2017-07-06 19:40:27 +03:00
|
|
|
// We also failed to join the room (this sets joining to false in RoomViewStore)
|
|
|
|
dis.dispatch({
|
2021-05-24 16:34:06 +03:00
|
|
|
action: Action.JoinRoomError,
|
|
|
|
roomId,
|
2017-07-06 19:40:27 +03:00
|
|
|
});
|
2021-10-15 17:30:53 +03:00
|
|
|
logger.error("Failed to create room " + roomId + " " + err);
|
2019-04-10 04:03:38 +03:00
|
|
|
let description = _t("Server may be unavailable, overloaded, or you hit a bug.");
|
|
|
|
if (err.errcode === "M_UNSUPPORTED_ROOM_VERSION") {
|
|
|
|
// Technically not possible with the UI as of April 2019 because there's no
|
|
|
|
// options for the user to change this. However, it's not a bad thing to report
|
|
|
|
// the error to the user for if/when the UI is available.
|
|
|
|
description = _t("The server does not support the room version specified.");
|
|
|
|
}
|
2017-08-10 17:17:52 +03:00
|
|
|
Modal.createTrackedDialog('Failure to create room', '', ErrorDialog, {
|
2017-05-23 17:16:31 +03:00
|
|
|
title: _t("Failure to create room"),
|
2019-04-10 04:03:38 +03:00
|
|
|
description,
|
2016-06-09 01:03:46 +03:00
|
|
|
});
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:38:11 +03:00
|
|
|
export function findDMForUser(client: MatrixClient, userId: string): Room {
|
2019-12-19 19:43:10 +03:00
|
|
|
const roomIds = DMRoomMap.shared().getDMRoomsForUserId(userId);
|
|
|
|
const rooms = roomIds.map(id => client.getRoom(id));
|
|
|
|
const suitableDMRooms = rooms.filter(r => {
|
2020-11-14 01:19:34 +03:00
|
|
|
// Validate that we are joined and the other person is also joined. We'll also make sure
|
|
|
|
// that the room also looks like a DM (until we have canonical DMs to tell us). For now,
|
|
|
|
// a DM is a room of two people that contains those two people exactly. This does mean
|
|
|
|
// that bots, assistants, etc will ruin a room's DM-ness, though this is a problem for
|
|
|
|
// canonical DMs to solve.
|
2019-12-19 19:43:10 +03:00
|
|
|
if (r && r.getMyMembership() === "join") {
|
2020-11-14 01:19:34 +03:00
|
|
|
const members = r.currentState.getMembers();
|
|
|
|
const joinedMembers = members.filter(m => isJoinedOrNearlyJoined(m.membership));
|
|
|
|
const otherMember = joinedMembers.find(m => m.userId === userId);
|
|
|
|
return otherMember && joinedMembers.length === 2;
|
2019-12-19 19:43:10 +03:00
|
|
|
}
|
|
|
|
return false;
|
2020-03-26 19:45:59 +03:00
|
|
|
}).sort((r1, r2) => {
|
|
|
|
return r2.getLastActiveTimestamp() -
|
|
|
|
r1.getLastActiveTimestamp();
|
2019-12-19 19:43:10 +03:00
|
|
|
});
|
|
|
|
if (suitableDMRooms.length) {
|
2020-02-13 16:34:44 +03:00
|
|
|
return suitableDMRooms[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 17:13:10 +03:00
|
|
|
/*
|
|
|
|
* Try to ensure the user is already in the megolm session before continuing
|
|
|
|
* NOTE: this assumes you've just created the room and there's not been an opportunity
|
|
|
|
* for other code to run, so we shouldn't miss RoomState.newMember when it comes by.
|
|
|
|
*/
|
2021-07-20 00:43:11 +03:00
|
|
|
export async function waitForMember(client: MatrixClient, roomId: string, userId: string, opts = { timeout: 1500 }) {
|
2020-02-13 17:13:10 +03:00
|
|
|
const { timeout } = opts;
|
|
|
|
let handler;
|
|
|
|
return new Promise((resolve) => {
|
2021-07-20 00:43:11 +03:00
|
|
|
handler = function(_, __, member: RoomMember) { // eslint-disable-line @typescript-eslint/naming-convention
|
2020-02-13 17:13:10 +03:00
|
|
|
if (member.userId !== userId) return;
|
|
|
|
if (member.roomId !== roomId) return;
|
|
|
|
resolve(true);
|
|
|
|
};
|
2022-02-22 15:18:08 +03:00
|
|
|
client.on(RoomStateEvent.NewMember, handler);
|
2020-02-13 17:13:10 +03:00
|
|
|
|
|
|
|
/* We don't want to hang if this goes wrong, so we proceed and hope the other
|
|
|
|
user is already in the megolm session */
|
|
|
|
setTimeout(resolve, timeout, false);
|
|
|
|
}).finally(() => {
|
2022-02-22 15:18:08 +03:00
|
|
|
client.removeListener(RoomStateEvent.NewMember, handler);
|
2020-02-13 17:13:10 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-18 14:25:19 +03:00
|
|
|
/*
|
|
|
|
* Ensure that for every user in a room, there is at least one device that we
|
|
|
|
* can encrypt to.
|
|
|
|
*/
|
2020-07-03 00:38:11 +03:00
|
|
|
export async function canEncryptToAllUsers(client: MatrixClient, userIds: string[]) {
|
2020-10-02 03:58:13 +03:00
|
|
|
try {
|
|
|
|
const usersDeviceMap = await client.downloadKeys(userIds);
|
|
|
|
// { "@user:host": { "DEVICE": {...}, ... }, ... }
|
|
|
|
return Object.values(usersDeviceMap).every((userDevices) =>
|
|
|
|
// { "DEVICE": {...}, ... }
|
|
|
|
Object.keys(userDevices).length > 0,
|
|
|
|
);
|
|
|
|
} catch (e) {
|
2021-10-15 17:30:53 +03:00
|
|
|
logger.error("Error determining if it's possible to encrypt to all users: ", e);
|
2020-10-02 03:58:13 +03:00
|
|
|
return false; // assume not
|
|
|
|
}
|
2020-02-18 14:25:19 +03:00
|
|
|
}
|
|
|
|
|
2021-02-17 21:51:21 +03:00
|
|
|
// Similar to ensureDMExists but also adds creation content
|
|
|
|
// without polluting ensureDMExists with unrelated stuff (also
|
|
|
|
// they're never encrypted).
|
|
|
|
export async function ensureVirtualRoomExists(
|
|
|
|
client: MatrixClient, userId: string, nativeRoomId: string,
|
|
|
|
): Promise<string> {
|
|
|
|
const existingDMRoom = findDMForUser(client, userId);
|
|
|
|
let roomId;
|
|
|
|
if (existingDMRoom) {
|
|
|
|
roomId = existingDMRoom.roomId;
|
|
|
|
} else {
|
|
|
|
roomId = await createRoom({
|
|
|
|
dmUserId: userId,
|
|
|
|
spinner: false,
|
|
|
|
andView: false,
|
|
|
|
createOpts: {
|
|
|
|
creation_content: {
|
|
|
|
// This allows us to recognise that the room is a virtual room
|
|
|
|
// when it comes down our sync stream (we also put the ID of the
|
|
|
|
// respective native room in there because why not?)
|
|
|
|
[VIRTUAL_ROOM_EVENT_TYPE]: nativeRoomId,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return roomId;
|
|
|
|
}
|
|
|
|
|
2020-07-03 00:38:11 +03:00
|
|
|
export async function ensureDMExists(client: MatrixClient, userId: string): Promise<string> {
|
2020-02-13 16:34:44 +03:00
|
|
|
const existingDMRoom = findDMForUser(client, userId);
|
|
|
|
let roomId;
|
|
|
|
if (existingDMRoom) {
|
|
|
|
roomId = existingDMRoom.roomId;
|
2019-12-19 19:43:10 +03:00
|
|
|
} else {
|
2020-10-01 17:11:01 +03:00
|
|
|
let encryption: boolean = undefined;
|
2020-05-19 13:36:44 +03:00
|
|
|
if (privateShouldBeEncrypted()) {
|
2020-10-01 17:11:01 +03:00
|
|
|
encryption = await canEncryptToAllUsers(client, [userId]);
|
2020-02-13 17:13:10 +03:00
|
|
|
}
|
2021-02-17 21:51:21 +03:00
|
|
|
|
2021-06-29 15:11:58 +03:00
|
|
|
roomId = await createRoom({ encryption, dmUserId: userId, spinner: false, andView: false });
|
2021-07-20 00:43:11 +03:00
|
|
|
await waitForMember(client, roomId, userId);
|
2019-12-19 19:43:10 +03:00
|
|
|
}
|
|
|
|
return roomId;
|
|
|
|
}
|
2020-05-19 13:36:44 +03:00
|
|
|
|
2020-08-14 15:27:12 +03:00
|
|
|
export function privateShouldBeEncrypted(): boolean {
|
|
|
|
const e2eeWellKnown = getE2EEWellKnown();
|
|
|
|
if (e2eeWellKnown) {
|
|
|
|
const defaultDisabled = e2eeWellKnown["default"] === false;
|
2020-08-14 15:01:43 +03:00
|
|
|
return !defaultDisabled;
|
|
|
|
}
|
2020-06-02 00:37:12 +03:00
|
|
|
return true;
|
2020-05-19 13:36:44 +03:00
|
|
|
}
|