Merge pull request #458 from matrix-org/dbkr/createdoom_dm

Update createRoom to support creating DM rooms
pull/21833/head
David Baker 2016-09-12 18:29:43 +01:00 committed by GitHub
commit f6478f111a
4 changed files with 38 additions and 23 deletions

View File

@ -93,14 +93,12 @@ export function setDMRoom(roomId, userId) {
if (mDirectEvent !== undefined) dmRoomMap = mDirectEvent.getContent();
// remove it from the lists of any others users
// (it can only be a DM room for one person)
for (const thisUserId of Object.keys(dmRoomMap)) {
const roomList = dmRoomMap[thisUserId];
if (thisUserId == userId) {
if (roomList.indexOf(roomId) == -1) {
roomList.push(roomId);
}
} else {
if (thisUserId != userId) {
const indexOfRoom = roomList.indexOf(roomId);
if (indexOfRoom > -1) {
roomList.splice(indexOfRoom, 1);
@ -108,6 +106,14 @@ export function setDMRoom(roomId, userId) {
}
}
// now add it, if it's not already there
const roomList = dmRoomMap[userId] || [];
if (roomList.indexOf(roomId) == -1) {
roomList.push(roomId);
}
dmRoomMap[userId] = roomList;
return MatrixClientPeg.get().setAccountData('m.direct', dmRoomMap);
}

View File

@ -257,9 +257,7 @@ module.exports = React.createClass({
_startChat: function(addr) {
// Start the chat
createRoom().then(function(roomId) {
return Invite.inviteToRoom(roomId, addr);
})
createRoom({dmUserId: addr})
.catch(function(err) {
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
Modal.createDialog(ErrorDialog, {

View File

@ -421,11 +421,7 @@ module.exports = React.createClass({
onNewDMClick: function() {
this.setState({ updating: this.state.updating + 1 });
createRoom({
createOpts: {
invite: [this.props.member.userId],
},
}).finally(() => {
createRoom({dmUserId: this.props.member.userId}).finally(() => {
this.props.onFinished();
this.setState({ updating: this.state.updating - 1 });
}).done();

View File

@ -18,6 +18,7 @@ var MatrixClientPeg = require('./MatrixClientPeg');
var Modal = require('./Modal');
var sdk = require('./index');
var dis = require("./dispatcher");
var Rooms = require("./Rooms");
var q = require('q');
@ -28,16 +29,17 @@ var q = require('q');
* 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) {
var opts = opts || {};
opts = opts || {};
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
var NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
var Loader = sdk.getComponent("elements.Spinner");
const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
const NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
const Loader = sdk.getComponent("elements.Spinner");
var client = MatrixClientPeg.get();
const client = MatrixClientPeg.get();
if (client.isGuest()) {
Modal.createDialog(NeedToRegisterDialog, {
title: "Please Register",
@ -46,10 +48,15 @@ function createRoom(opts) {
return q(null);
}
const defaultPreset = opts.dmUserId ? 'trusted_private_chat' : 'private_chat';
// set some defaults for the creation
var createOpts = opts.createOpts || {};
createOpts.preset = createOpts.preset || 'private_chat';
const createOpts = opts.createOpts || {};
createOpts.preset = createOpts.preset || defaultPreset;
createOpts.visibility = createOpts.visibility || 'private';
if (opts.dmUserId && createOpts.invite === undefined) {
createOpts.invite = [opts.dmUserId];
}
// Allow guests by default since the room is private and they'd
// need an invite. This means clicking on a 3pid invite email can
@ -64,20 +71,28 @@ function createRoom(opts) {
}
];
var modal = Modal.createDialog(Loader, null, 'mx_Dialog_spinner');
const modal = Modal.createDialog(Loader, null, 'mx_Dialog_spinner');
let roomId;
return client.createRoom(createOpts).finally(function() {
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
dis.dispatch({
action: 'view_room',
room_id: res.room_id
room_id: roomId
});
return res.room_id;
return roomId;
}, function(err) {
Modal.createDialog(ErrorDialog, {
title: "Failure to create room",