From 2bb22954995535d483261f2db24a51cd56de72f0 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 17 Jul 2015 11:48:40 +0100 Subject: [PATCH] Implement /join --- src/SlashCommands.js | 51 ++++++++++++++++---- src/controllers/molecules/MessageComposer.js | 5 +- 2 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/SlashCommands.js b/src/SlashCommands.js index de9f5c5be..a34263476 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -61,15 +61,48 @@ var commands = { var matches = args.match(/^(\S+)$/); if (matches) { var room_alias = matches[1]; - // TODO: - // Map alias to room ID - // Find the room (if joined, then just view the room) - // else join the room alias - /* - dis.dispatch({ - action: 'view_room', - room_id: roomId - }); */ + // Try to find a room with this alias + var rooms = MatrixClientPeg.get().getRooms(); + var roomId; + for (var i = 0; i < rooms.length; i++) { + var aliasEvents = rooms[i].currentState.getStateEvents( + "m.room.aliases" + ); + for (var j = 0; j < aliasEvents.length; j++) { + var aliases = aliasEvents[j].getContent().aliases || []; + for (var k = 0; k < aliases.length; k++) { + if (aliases[k] === room_alias) { + roomId = rooms[i].roomId; + break; + } + } + if (roomId) { break; } + } + if (roomId) { break; } + } + if (roomId) { // we've already joined this room, view it. + dis.dispatch({ + action: 'view_room', + room_id: roomId + }); + return success(); + } + else { + // attempt to join this alias. + return success( + MatrixClientPeg.get().joinRoom(room_alias).done( + function(room) { + dis.dispatch({ + action: 'view_room', + room_id: room.roomId + }); + }, function(err) { + console.error( + "Failed to join room: %s", JSON.stringify(err) + ); + }) + ); + } } } return reject("Usage: /join [NOT IMPLEMENTED]"); diff --git a/src/controllers/molecules/MessageComposer.js b/src/controllers/molecules/MessageComposer.js index db1f45500..34183083d 100644 --- a/src/controllers/molecules/MessageComposer.js +++ b/src/controllers/molecules/MessageComposer.js @@ -176,18 +176,19 @@ module.exports = { var cmd = SlashCommands.processInput(this.props.room.roomId, contentText); if (cmd) { ev.preventDefault(); + if (!cmd.error) { + this.refs.textarea.getDOMNode().value = ''; + } if (cmd.promise) { cmd.promise.done(function() { console.log("Command success."); }, function(err) { console.error("Command failure: %s", err); }); - this.refs.textarea.getDOMNode().value = ''; } else if (cmd.error) { console.error(cmd.error); } - return; }