2016-06-09 06:03:46 +08:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
2020-01-23 21:54:28 +08:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2016-06-09 06:03:46 +08: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.
|
|
|
|
*/
|
|
|
|
|
2019-12-21 05:13:46 +08:00
|
|
|
import {MatrixClientPeg} from './MatrixClientPeg';
|
2017-07-01 21:40:46 +08:00
|
|
|
import Modal from './Modal';
|
2019-12-20 09:19:56 +08:00
|
|
|
import * as sdk from './index';
|
2017-05-25 18:39:08 +08:00
|
|
|
import { _t } from './languageHandler';
|
2017-07-01 21:40:46 +08:00
|
|
|
import dis from "./dispatcher";
|
2017-07-05 22:00:50 +08:00
|
|
|
import * as Rooms from "./Rooms";
|
2019-12-20 00:43:10 +08:00
|
|
|
import DMRoomMap from "./utils/DMRoomMap";
|
2018-01-25 17:54:31 +08:00
|
|
|
import {getAddressType} from "./UserAddress";
|
2020-02-13 22:13:10 +08:00
|
|
|
import SettingsStore from "./settings/SettingsStore";
|
2016-06-09 06:03:46 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new room, and switch to it.
|
|
|
|
*
|
|
|
|
* @param {object=} opts parameters for creating the room
|
2016-09-10 02:25:00 +08:00
|
|
|
* @param {string=} opts.dmUserId If specified, make this a DM room for this user and invite them
|
2016-06-09 06:03:46 +08:00
|
|
|
* @param {object=} opts.createOpts set of options to pass to createRoom call.
|
2019-06-15 00:21:07 +08:00
|
|
|
* @param {bool=} opts.spinner True to show a modal spinner while the room is created.
|
|
|
|
* Default: True
|
2020-01-23 21:54:28 +08:00
|
|
|
* @param {bool=} opts.guestAccess Whether to enable guest access.
|
|
|
|
* Default: True
|
2020-01-23 22:05:38 +08:00
|
|
|
* @param {bool=} opts.encryption Whether to enable encryption.
|
|
|
|
* Default: False
|
2020-02-28 08:10:31 +08:00
|
|
|
* @param {bool=} opts.inlineErrors True to raise errors off the promise instead of resolving to null.
|
|
|
|
* Default: False
|
2017-07-01 21:40:46 +08:00
|
|
|
*
|
|
|
|
* @returns {Promise} which resolves to the room id, or null if the
|
|
|
|
* action was aborted or failed.
|
2016-06-09 06:03:46 +08:00
|
|
|
*/
|
2019-12-20 00:43:10 +08:00
|
|
|
export default function createRoom(opts) {
|
2016-09-10 02:25:00 +08:00
|
|
|
opts = opts || {};
|
2019-06-15 00:21:07 +08:00
|
|
|
if (opts.spinner === undefined) opts.spinner = true;
|
2020-01-23 21:54:28 +08:00
|
|
|
if (opts.guestAccess === undefined) opts.guestAccess = true;
|
2020-01-23 22:05:38 +08:00
|
|
|
if (opts.encryption === undefined) opts.encryption = false;
|
2016-06-09 06:03:46 +08:00
|
|
|
|
2016-09-10 02:25:00 +08:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
2016-06-09 06:03:46 +08:00
|
|
|
|
2016-09-10 02:25:00 +08:00
|
|
|
const client = MatrixClientPeg.get();
|
2016-06-09 06:03:46 +08:00
|
|
|
if (client.isGuest()) {
|
2018-09-06 01:08:49 +08:00
|
|
|
dis.dispatch({action: 'require_registration'});
|
2017-07-12 21:02:00 +08:00
|
|
|
return Promise.resolve(null);
|
2016-06-09 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
2016-09-10 02:25:00 +08:00
|
|
|
const defaultPreset = opts.dmUserId ? 'trusted_private_chat' : 'private_chat';
|
|
|
|
|
2016-06-09 06:03:46 +08:00
|
|
|
// set some defaults for the creation
|
2016-09-10 02:25:00 +08:00
|
|
|
const createOpts = opts.createOpts || {};
|
|
|
|
createOpts.preset = createOpts.preset || defaultPreset;
|
2016-06-09 06:03:46 +08:00
|
|
|
createOpts.visibility = createOpts.visibility || 'private';
|
2016-09-10 02:25:00 +08:00
|
|
|
if (opts.dmUserId && createOpts.invite === undefined) {
|
2018-01-25 17:54:31 +08: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-10 02:25:00 +08:00
|
|
|
}
|
2016-09-13 01:32:44 +08:00
|
|
|
if (opts.dmUserId && createOpts.is_direct === undefined) {
|
|
|
|
createOpts.is_direct = true;
|
|
|
|
}
|
2016-06-09 06:03:46 +08:00
|
|
|
|
2017-05-24 23:56:13 +08:00
|
|
|
// By default, view the room after creating it
|
|
|
|
if (opts.andView === undefined) {
|
|
|
|
opts.andView = true;
|
|
|
|
}
|
|
|
|
|
2020-01-23 22:05:38 +08:00
|
|
|
createOpts.initial_state = createOpts.initial_state || [];
|
|
|
|
|
2016-06-09 06:03:46 +08: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 21:54:28 +08:00
|
|
|
if (opts.guestAccess) {
|
|
|
|
createOpts.initial_state.push({
|
2020-01-23 22:05:38 +08:00
|
|
|
type: 'm.room.guest_access',
|
|
|
|
state_key: '',
|
2016-06-09 06:03:46 +08:00
|
|
|
content: {
|
2017-07-01 21:40:46 +08:00
|
|
|
guest_access: 'can_join',
|
2016-06-09 06:03:46 +08:00
|
|
|
},
|
2020-01-23 22:05:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts.encryption) {
|
|
|
|
createOpts.initial_state.push({
|
|
|
|
type: 'm.room.encryption',
|
2016-06-09 06:03:46 +08:00
|
|
|
state_key: '',
|
2020-01-23 22:05:38 +08:00
|
|
|
content: {
|
|
|
|
algorithm: 'm.megolm.v1.aes-sha2',
|
|
|
|
},
|
2020-01-23 21:54:28 +08:00
|
|
|
});
|
|
|
|
}
|
2016-06-09 06:03:46 +08:00
|
|
|
|
2019-06-15 00:21:07 +08:00
|
|
|
let modal;
|
|
|
|
if (opts.spinner) modal = Modal.createDialog(Loader, null, 'mx_Dialog_spinner');
|
2016-06-09 06:03:46 +08:00
|
|
|
|
2016-09-10 02:25:00 +08:00
|
|
|
let roomId;
|
2016-06-09 06:03:46 +08:00
|
|
|
return client.createRoom(createOpts).finally(function() {
|
2019-06-15 00:21:07 +08:00
|
|
|
if (modal) modal.close();
|
2016-06-09 06:03:46 +08:00
|
|
|
}).then(function(res) {
|
2016-09-10 02:25:00 +08:00
|
|
|
roomId = res.room_id;
|
|
|
|
if (opts.dmUserId) {
|
|
|
|
return Rooms.setDMRoom(roomId, opts.dmUserId);
|
|
|
|
} else {
|
2017-07-12 21:02:00 +08:00
|
|
|
return Promise.resolve();
|
2016-09-10 02:25:00 +08:00
|
|
|
}
|
|
|
|
}).then(function() {
|
2016-07-18 01:32:48 +08: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
|
2017-05-24 23:56:13 +08:00
|
|
|
if (opts.andView) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: roomId,
|
2017-06-17 01:24:07 +08:00
|
|
|
should_peek: false,
|
2017-09-15 05:22:21 +08: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,
|
2017-05-24 23:56:13 +08:00
|
|
|
});
|
|
|
|
}
|
2016-09-10 02:25:00 +08:00
|
|
|
return roomId;
|
2016-06-09 06:03:46 +08:00
|
|
|
}, function(err) {
|
2020-02-28 08:10:31 +08:00
|
|
|
// Raise the error if the caller requested that we do so.
|
|
|
|
if (opts.inlineErrors) throw err;
|
|
|
|
|
2017-07-07 00:40:27 +08:00
|
|
|
// We also failed to join the room (this sets joining to false in RoomViewStore)
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'join_room_error',
|
|
|
|
});
|
2017-03-13 06:59:41 +08:00
|
|
|
console.error("Failed to create room " + roomId + " " + err);
|
2019-04-10 09:03:38 +08: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 22:17:52 +08:00
|
|
|
Modal.createTrackedDialog('Failure to create room', '', ErrorDialog, {
|
2017-05-23 22:16:31 +08:00
|
|
|
title: _t("Failure to create room"),
|
2019-04-10 09:03:38 +08:00
|
|
|
description,
|
2016-06-09 06:03:46 +08:00
|
|
|
});
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:34:44 +08:00
|
|
|
export function findDMForUser(client, userId) {
|
2019-12-20 00:43:10 +08:00
|
|
|
const roomIds = DMRoomMap.shared().getDMRoomsForUserId(userId);
|
|
|
|
const rooms = roomIds.map(id => client.getRoom(id));
|
|
|
|
const suitableDMRooms = rooms.filter(r => {
|
|
|
|
if (r && r.getMyMembership() === "join") {
|
|
|
|
const member = r.getMember(userId);
|
|
|
|
return member && (member.membership === "invite" || member.membership === "join");
|
|
|
|
}
|
|
|
|
return false;
|
2020-03-27 00:45:59 +08:00
|
|
|
}).sort((r1, r2) => {
|
|
|
|
return r2.getLastActiveTimestamp() -
|
|
|
|
r1.getLastActiveTimestamp();
|
2019-12-20 00:43:10 +08:00
|
|
|
});
|
|
|
|
if (suitableDMRooms.length) {
|
2020-02-13 21:34:44 +08:00
|
|
|
return suitableDMRooms[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-13 22:13:10 +08: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.
|
|
|
|
*/
|
|
|
|
export async function _waitForMember(client, roomId, userId, opts = { timeout: 1500 }) {
|
|
|
|
const { timeout } = opts;
|
|
|
|
let handler;
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
handler = function(_event, _roomstate, member) {
|
|
|
|
if (member.userId !== userId) return;
|
|
|
|
if (member.roomId !== roomId) return;
|
|
|
|
resolve(true);
|
|
|
|
};
|
|
|
|
client.on("RoomState.newMember", handler);
|
|
|
|
|
|
|
|
/* 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(() => {
|
|
|
|
client.removeListener("RoomState.newMember", handler);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-02-18 19:25:19 +08:00
|
|
|
/*
|
|
|
|
* Ensure that for every user in a room, there is at least one device that we
|
|
|
|
* can encrypt to.
|
|
|
|
*/
|
|
|
|
export async function canEncryptToAllUsers(client, userIds) {
|
2020-05-08 23:36:13 +08:00
|
|
|
const usersDeviceMap = await client.downloadKeys(userIds);
|
2020-02-18 19:25:19 +08:00
|
|
|
// { "@user:host": { "DEVICE": {...}, ... }, ... }
|
|
|
|
return Object.values(usersDeviceMap).every((userDevices) =>
|
|
|
|
// { "DEVICE": {...}, ... }
|
|
|
|
Object.keys(userDevices).length > 0,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-13 21:34:44 +08:00
|
|
|
export async function ensureDMExists(client, userId) {
|
|
|
|
const existingDMRoom = findDMForUser(client, userId);
|
|
|
|
let roomId;
|
|
|
|
if (existingDMRoom) {
|
|
|
|
roomId = existingDMRoom.roomId;
|
2019-12-20 00:43:10 +08:00
|
|
|
} else {
|
2020-02-13 22:13:10 +08:00
|
|
|
let encryption;
|
2020-04-16 03:18:42 +08:00
|
|
|
if (SettingsStore.getValue("feature_cross_signing")) {
|
2020-02-18 19:25:19 +08:00
|
|
|
encryption = canEncryptToAllUsers(client, [userId]);
|
2020-02-13 22:13:10 +08:00
|
|
|
}
|
|
|
|
roomId = await createRoom({encryption, dmUserId: userId, spinner: false, andView: false});
|
|
|
|
await _waitForMember(client, roomId, userId);
|
2019-12-20 00:43:10 +08:00
|
|
|
}
|
|
|
|
return roomId;
|
|
|
|
}
|