From 77d1b9af041f49cec16a8276bb075e875a09685f Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 27 Oct 2015 09:58:55 +0000 Subject: [PATCH] Hook up aliases via listeners too. --- src/MatrixTools.js | 25 ++++++++++++++++++++++++ src/SlashCommands.js | 27 +++++++------------------- src/controllers/pages/MatrixChat.js | 22 ++++++++++++++++++++- src/linkify-matrix.js | 30 ++++++++++++++--------------- 4 files changed, 67 insertions(+), 37 deletions(-) diff --git a/src/MatrixTools.js b/src/MatrixTools.js index 5c6dca8b33..6c7a9ee9ba 100644 --- a/src/MatrixTools.js +++ b/src/MatrixTools.js @@ -31,6 +31,31 @@ module.exports = { } } return null; + }, + + /** + * Given a list of room objects, return the room which has the given alias, + * else null. + */ + getRoomForAlias: function(rooms, room_alias) { + var room; + 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) { + room = rooms[i]; + break; + } + } + if (room) { break; } + } + if (room) { break; } + } + return room || null; } } diff --git a/src/SlashCommands.js b/src/SlashCommands.js index 08d68331f8..e6ea7533dc 100644 --- a/src/SlashCommands.js +++ b/src/SlashCommands.js @@ -15,6 +15,7 @@ limitations under the License. */ var MatrixClientPeg = require("./MatrixClientPeg"); +var MatrixTools = require("./MatrixTools"); var dis = require("./dispatcher"); var encryption = require("./encryption"); @@ -98,28 +99,14 @@ var commands = { } // 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. + var foundRoom = MatrixTools.getRoomForAlias( + MatrixClientPeg.get().getRooms(), + room_alias + ); + if (foundRoom) { // we've already joined this room, view it. dis.dispatch({ action: 'view_room', - room_id: roomId + room_id: foundRoom.roomId }); return success(); } diff --git a/src/controllers/pages/MatrixChat.js b/src/controllers/pages/MatrixChat.js index 2a712b22f6..e56d835267 100644 --- a/src/controllers/pages/MatrixChat.js +++ b/src/controllers/pages/MatrixChat.js @@ -21,6 +21,7 @@ var dis = require("../../dispatcher"); var sdk = require('../../index'); var MatrixTools = require('../../MatrixTools'); +var linkifyMatrix = require("../../linkify-matrix"); var Cas = require("../../CasLogic"); @@ -66,6 +67,13 @@ module.exports = { } else { this.notifyNewScreen('login'); } + + // this can technically be done anywhere but doing this here keeps all + // the routing url path logic together. + linkifyMatrix.onAliasClick = function(event, alias) { + dis.dispatch({action: 'view_room_alias', room_alias: alias}); + event.preventDefault(); + }; }, componentWillUnmount: function() { @@ -212,7 +220,19 @@ module.exports = { } break; case 'view_room_alias': - MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias).done(function(result) { + var foundRoom = MatrixTools.getRoomForAlias( + MatrixClientPeg.get().getRooms(), payload.room_alias + ); + if (foundRoom) { + dis.dispatch({ + action: 'view_room', + room_id: foundRoom.roomId + }); + return; + } + // resolve the alias and *then* view it + MatrixClientPeg.get().getRoomIdForAlias(payload.room_alias).done( + function(result) { dis.dispatch({ action: 'view_room', room_id: result.room_id diff --git a/src/linkify-matrix.js b/src/linkify-matrix.js index 370bec7571..90b1938d51 100644 --- a/src/linkify-matrix.js +++ b/src/linkify-matrix.js @@ -95,26 +95,24 @@ function matrixLinkify(linkify) { S_AT_NAME_COLON_DOMAIN_DOT.on(TT.TLD, S_USERID); } -matrixLinkify.onUserClick = function(e, userId) {}; +matrixLinkify.onUserClick = function(e, userId) { e.preventDefault(); }; +matrixLinkify.onAliasClick = function(e, roomAlias) { e.preventDefault(); }; matrixLinkify.options = { events: function (href, type) { - if (type === "userid") { - return { - click: function(e) { - matrixLinkify.onUserClick(e, href); - e.preventDefault(); - } - }; - } - }, - - formatHref: function (href, type) { switch (type) { - case 'roomalias': - return "#/room/" + href; - default: - return href; + case "userid": + return { + click: function(e) { + matrixLinkify.onUserClick(e, href); + } + }; + case "roomalias": + return { + click: function(e) { + matrixLinkify.onAliasClick(e, href); + } + }; } } };