From 96fabe09d26a304bbde56adc6f37a31134d5603e Mon Sep 17 00:00:00 2001 From: David Baker Date: Fri, 9 Sep 2016 19:25:00 +0100 Subject: [PATCH] Update createRoom to support creating DM rooms * Make ChatInviteDialog and MemberInfo createRoom use it * Fix bug in setDMRoom --- src/Rooms.js | 16 ++++++--- .../views/dialogs/ChatInviteDialog.js | 4 +-- src/components/views/rooms/MemberInfo.js | 6 +--- src/createRoom.js | 35 +++++++++++++------ 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/src/Rooms.js b/src/Rooms.js index 436580371e..281ff842e9 100644 --- a/src/Rooms.js +++ b/src/Rooms.js @@ -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); } diff --git a/src/components/views/dialogs/ChatInviteDialog.js b/src/components/views/dialogs/ChatInviteDialog.js index e322127135..3e4af5bc50 100644 --- a/src/components/views/dialogs/ChatInviteDialog.js +++ b/src/components/views/dialogs/ChatInviteDialog.js @@ -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, { diff --git a/src/components/views/rooms/MemberInfo.js b/src/components/views/rooms/MemberInfo.js index b6944a3d4e..4a01ffd536 100644 --- a/src/components/views/rooms/MemberInfo.js +++ b/src/components/views/rooms/MemberInfo.js @@ -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(); diff --git a/src/createRoom.js b/src/createRoom.js index 51e3ff8a96..4c282ef485 100644 --- a/src/createRoom.js +++ b/src/createRoom.js @@ -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",