mirror of
https://github.com/vector-im/element-web.git
synced 2025-08-18 12:17:03 +02:00
This allows for a truely flux-y way of storing the currently viewed room, making some callbacks (like onRoomIdResolved) redundant and making sure that the currently viewed room (ID) is only stored in one place as opposed to the previous many places. This was required for the `join_room` action which can be dispatched to join the currently viewed room. Another change was to introduce `LifeCycleStore` which is a start at encorporating state related to the lifecycle of the app into a flux store. Currently it only contains an action which will be dispatched when the sync state has become PREPARED. This was necessary to do a deferred dispatch of `join_room` following the registration of a PWLU (PassWord-Less User). The following actions are introduced: - RoomViewStore: - `view_room`: dispatch to change the currently viewed room ID - `join_room`: dispatch to join the currently viewed room - LifecycleStore: - `do_after_sync_prepared`: dispatch to store an action which will be dispatched when `sync_state` is dispatched with `state = 'PREPARED'` - MatrixChat: - `sync_state`: dispatched when the sync state changes. Ideally there'd be a SyncStateStore that emitted an `update` upon receiving this, but for now the `LifecycleStore` will listen for `sync_state` directly.
116 lines
3.7 KiB
JavaScript
116 lines
3.7 KiB
JavaScript
/*
|
|
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.
|
|
*/
|
|
|
|
var MatrixClientPeg = require('./MatrixClientPeg');
|
|
var Modal = require('./Modal');
|
|
var sdk = require('./index');
|
|
var dis = require("./dispatcher");
|
|
var Rooms = require("./Rooms");
|
|
|
|
var q = require('q');
|
|
|
|
/**
|
|
* Create a new room, and switch to it.
|
|
*
|
|
* Returns a promise which resolves to the room id, or null if the
|
|
* action was aborted or failed.
|
|
*
|
|
* @param {object=} opts parameters for creating the room
|
|
* @param {string=} opts.dmUserId If specified, make this a DM room for this user and invite them
|
|
* @param {object=} opts.createOpts set of options to pass to createRoom call.
|
|
*/
|
|
function createRoom(opts) {
|
|
opts = opts || {};
|
|
|
|
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
|
|
const Loader = sdk.getComponent("elements.Spinner");
|
|
|
|
const client = MatrixClientPeg.get();
|
|
if (client.isGuest()) {
|
|
dis.dispatch({action: 'view_set_mxid'});
|
|
return q(null);
|
|
}
|
|
|
|
const defaultPreset = opts.dmUserId ? 'trusted_private_chat' : 'private_chat';
|
|
|
|
// set some defaults for the creation
|
|
const createOpts = opts.createOpts || {};
|
|
createOpts.preset = createOpts.preset || defaultPreset;
|
|
createOpts.visibility = createOpts.visibility || 'private';
|
|
if (opts.dmUserId && createOpts.invite === undefined) {
|
|
createOpts.invite = [opts.dmUserId];
|
|
}
|
|
if (opts.dmUserId && createOpts.is_direct === undefined) {
|
|
createOpts.is_direct = true;
|
|
}
|
|
|
|
// By default, view the room after creating it
|
|
if (opts.andView === undefined) {
|
|
opts.andView = true;
|
|
}
|
|
|
|
// 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: {
|
|
guest_access: 'can_join'
|
|
},
|
|
type: 'm.room.guest_access',
|
|
state_key: '',
|
|
}
|
|
];
|
|
|
|
let modal;
|
|
setTimeout(()=>{
|
|
modal = Modal.createDialog(Loader, null, 'mx_Dialog_spinner');
|
|
}, 0);
|
|
|
|
let roomId;
|
|
return client.createRoom(createOpts).finally(function() {
|
|
if (modal) modal.close();
|
|
}).then(function(res) {
|
|
roomId = res.room_id;
|
|
if (opts.dmUserId) {
|
|
return Rooms.setDMRoom(roomId, opts.dmUserId);
|
|
} else {
|
|
return q();
|
|
}
|
|
}).then(function() {
|
|
// 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
|
|
if (opts.andView) {
|
|
dis.dispatch({
|
|
action: 'view_room',
|
|
room_id: roomId,
|
|
});
|
|
}
|
|
return roomId;
|
|
}, function(err) {
|
|
console.error("Failed to create room " + roomId + " " + err);
|
|
Modal.createDialog(ErrorDialog, {
|
|
title: "Failure to create room",
|
|
description: "Server may be unavailable, overloaded, or you hit a bug.",
|
|
});
|
|
return null;
|
|
});
|
|
}
|
|
|
|
module.exports = createRoom;
|