2016-06-09 00:03:46 +02:00
|
|
|
/*
|
|
|
|
Copyright 2015, 2016 OpenMarket Ltd
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2017-07-01 15:40:46 +02:00
|
|
|
import MatrixClientPeg from './MatrixClientPeg';
|
|
|
|
import Modal from './Modal';
|
|
|
|
import sdk from './index';
|
2017-05-25 12:39:08 +02:00
|
|
|
import { _t } from './languageHandler';
|
2017-07-01 15:40:46 +02:00
|
|
|
import dis from "./dispatcher";
|
2017-07-05 16:00:50 +02:00
|
|
|
import * as Rooms from "./Rooms";
|
2016-06-09 00:03:46 +02:00
|
|
|
|
2017-07-12 14:58:14 +02:00
|
|
|
import Promise from 'bluebird';
|
2016-06-09 00:03:46 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new room, and switch to it.
|
|
|
|
*
|
|
|
|
* @param {object=} opts parameters for creating the room
|
2016-09-09 20:25:00 +02:00
|
|
|
* @param {string=} opts.dmUserId If specified, make this a DM room for this user and invite them
|
2016-06-09 00:03:46 +02:00
|
|
|
* @param {object=} opts.createOpts set of options to pass to createRoom call.
|
2017-07-01 15:40:46 +02:00
|
|
|
*
|
|
|
|
* @returns {Promise} which resolves to the room id, or null if the
|
|
|
|
* action was aborted or failed.
|
2016-06-09 00:03:46 +02:00
|
|
|
*/
|
|
|
|
function createRoom(opts) {
|
2016-09-09 20:25:00 +02:00
|
|
|
opts = opts || {};
|
2016-06-09 00:03:46 +02:00
|
|
|
|
2016-09-09 20:25:00 +02:00
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
2016-06-09 00:03:46 +02:00
|
|
|
|
2016-09-09 20:25:00 +02:00
|
|
|
const client = MatrixClientPeg.get();
|
2016-06-09 00:03:46 +02:00
|
|
|
if (client.isGuest()) {
|
2017-05-15 18:31:26 +02:00
|
|
|
dis.dispatch({action: 'view_set_mxid'});
|
2017-07-12 15:02:00 +02:00
|
|
|
return Promise.resolve(null);
|
2016-06-09 00:03:46 +02:00
|
|
|
}
|
|
|
|
|
2016-09-09 20:25:00 +02:00
|
|
|
const defaultPreset = opts.dmUserId ? 'trusted_private_chat' : 'private_chat';
|
|
|
|
|
2016-06-09 00:03:46 +02:00
|
|
|
// set some defaults for the creation
|
2016-09-09 20:25:00 +02:00
|
|
|
const createOpts = opts.createOpts || {};
|
|
|
|
createOpts.preset = createOpts.preset || defaultPreset;
|
2016-06-09 00:03:46 +02:00
|
|
|
createOpts.visibility = createOpts.visibility || 'private';
|
2016-09-09 20:25:00 +02:00
|
|
|
if (opts.dmUserId && createOpts.invite === undefined) {
|
|
|
|
createOpts.invite = [opts.dmUserId];
|
|
|
|
}
|
2016-09-12 19:32:44 +02:00
|
|
|
if (opts.dmUserId && createOpts.is_direct === undefined) {
|
|
|
|
createOpts.is_direct = true;
|
|
|
|
}
|
2016-06-09 00:03:46 +02:00
|
|
|
|
2017-05-24 17:56:13 +02:00
|
|
|
// By default, view the room after creating it
|
|
|
|
if (opts.andView === undefined) {
|
|
|
|
opts.andView = true;
|
|
|
|
}
|
|
|
|
|
2016-06-09 00:03:46 +02: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.
|
|
|
|
createOpts.initial_state = createOpts.initial_state || [
|
|
|
|
{
|
|
|
|
content: {
|
2017-07-01 15:40:46 +02:00
|
|
|
guest_access: 'can_join',
|
2016-06-09 00:03:46 +02:00
|
|
|
},
|
|
|
|
type: 'm.room.guest_access',
|
|
|
|
state_key: '',
|
2017-07-01 15:40:46 +02:00
|
|
|
},
|
2016-06-09 00:03:46 +02:00
|
|
|
];
|
|
|
|
|
2017-06-19 17:40:33 +02:00
|
|
|
const modal = Modal.createDialog(Loader, null, 'mx_Dialog_spinner');
|
2016-06-09 00:03:46 +02:00
|
|
|
|
2016-09-09 20:25:00 +02:00
|
|
|
let roomId;
|
2017-07-06 18:40:27 +02:00
|
|
|
if (opts.andView) {
|
|
|
|
// We will possibly have a successful join, indicate as such
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'will_join',
|
|
|
|
});
|
|
|
|
}
|
2016-06-09 00:03:46 +02:00
|
|
|
return client.createRoom(createOpts).finally(function() {
|
2017-06-19 17:45:40 +02:00
|
|
|
modal.close();
|
2016-06-09 00:03:46 +02:00
|
|
|
}).then(function(res) {
|
2016-09-09 20:25:00 +02:00
|
|
|
roomId = res.room_id;
|
|
|
|
if (opts.dmUserId) {
|
|
|
|
return Rooms.setDMRoom(roomId, opts.dmUserId);
|
|
|
|
} else {
|
2017-07-12 15:02:00 +02:00
|
|
|
return Promise.resolve();
|
2016-09-09 20:25:00 +02:00
|
|
|
}
|
|
|
|
}).then(function() {
|
2016-07-17 19:32:48 +02: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 17:56:13 +02:00
|
|
|
if (opts.andView) {
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'view_room',
|
|
|
|
room_id: roomId,
|
2017-06-16 19:24:07 +02:00
|
|
|
should_peek: false,
|
2017-07-06 18:40:27 +02:00
|
|
|
// Creating a room will have joined us to the room
|
|
|
|
joined: true,
|
2017-05-24 17:56:13 +02:00
|
|
|
});
|
|
|
|
}
|
2016-09-09 20:25:00 +02:00
|
|
|
return roomId;
|
2016-06-09 00:03:46 +02:00
|
|
|
}, function(err) {
|
2017-07-06 18:40:27 +02:00
|
|
|
// We also failed to join the room (this sets joining to false in RoomViewStore)
|
|
|
|
dis.dispatch({
|
|
|
|
action: 'join_room_error',
|
|
|
|
});
|
2017-03-12 23:59:41 +01:00
|
|
|
console.error("Failed to create room " + roomId + " " + err);
|
2017-07-27 18:19:18 +02:00
|
|
|
Modal.createTrackedDialog('Failure to create room', err.message, ErrorDialog, {
|
2017-05-23 16:16:31 +02:00
|
|
|
title: _t("Failure to create room"),
|
2017-06-01 16:18:06 +02:00
|
|
|
description: _t("Server may be unavailable, overloaded, or you hit a bug."),
|
2016-06-09 00:03:46 +02:00
|
|
|
});
|
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = createRoom;
|