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(); 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)) { for (const thisUserId of Object.keys(dmRoomMap)) {
const roomList = dmRoomMap[thisUserId]; const roomList = dmRoomMap[thisUserId];
if (thisUserId == userId) { if (thisUserId != userId) {
if (roomList.indexOf(roomId) == -1) {
roomList.push(roomId);
}
} else {
const indexOfRoom = roomList.indexOf(roomId); const indexOfRoom = roomList.indexOf(roomId);
if (indexOfRoom > -1) { if (indexOfRoom > -1) {
roomList.splice(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); return MatrixClientPeg.get().setAccountData('m.direct', dmRoomMap);
} }

View File

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

View File

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

View File

@ -18,6 +18,7 @@ var MatrixClientPeg = require('./MatrixClientPeg');
var Modal = require('./Modal'); var Modal = require('./Modal');
var sdk = require('./index'); var sdk = require('./index');
var dis = require("./dispatcher"); var dis = require("./dispatcher");
var Rooms = require("./Rooms");
var q = require('q'); var q = require('q');
@ -28,16 +29,17 @@ var q = require('q');
* action was aborted or failed. * action was aborted or failed.
* *
* @param {object=} opts parameters for creating the room * @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. * @param {object=} opts.createOpts set of options to pass to createRoom call.
*/ */
function createRoom(opts) { function createRoom(opts) {
var opts = opts || {}; opts = opts || {};
var ErrorDialog = sdk.getComponent("dialogs.ErrorDialog"); const ErrorDialog = sdk.getComponent("dialogs.ErrorDialog");
var NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog"); const NeedToRegisterDialog = sdk.getComponent("dialogs.NeedToRegisterDialog");
var Loader = sdk.getComponent("elements.Spinner"); const Loader = sdk.getComponent("elements.Spinner");
var client = MatrixClientPeg.get(); const client = MatrixClientPeg.get();
if (client.isGuest()) { if (client.isGuest()) {
Modal.createDialog(NeedToRegisterDialog, { Modal.createDialog(NeedToRegisterDialog, {
title: "Please Register", title: "Please Register",
@ -46,10 +48,15 @@ function createRoom(opts) {
return q(null); return q(null);
} }
const defaultPreset = opts.dmUserId ? 'trusted_private_chat' : 'private_chat';
// set some defaults for the creation // set some defaults for the creation
var createOpts = opts.createOpts || {}; const createOpts = opts.createOpts || {};
createOpts.preset = createOpts.preset || 'private_chat'; createOpts.preset = createOpts.preset || defaultPreset;
createOpts.visibility = createOpts.visibility || 'private'; 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 // Allow guests by default since the room is private and they'd
// need an invite. This means clicking on a 3pid invite email can // 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() { return client.createRoom(createOpts).finally(function() {
modal.close(); modal.close();
}).then(function(res) { }).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 // 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 // room has been created, so we race here with the client knowing that
// the room exists, causing things like // the room exists, causing things like
// https://github.com/vector-im/vector-web/issues/1813 // https://github.com/vector-im/vector-web/issues/1813
dis.dispatch({ dis.dispatch({
action: 'view_room', action: 'view_room',
room_id: res.room_id room_id: roomId
}); });
return res.room_id; return roomId;
}, function(err) { }, function(err) {
Modal.createDialog(ErrorDialog, { Modal.createDialog(ErrorDialog, {
title: "Failure to create room", title: "Failure to create room",