From 27719075731e54c1eb68c0c56f940faba7b2f765 Mon Sep 17 00:00:00 2001 From: David Baker Date: Thu, 6 Aug 2015 14:58:52 +0100 Subject: [PATCH] Support room aliases in url bar and show them for rooms that have them --- examples/vector/index.js | 10 +++++++- src/controllers/organisms/RoomView.js | 1 + src/controllers/pages/MatrixChat.js | 34 +++++++++++++++++++++++---- 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/examples/vector/index.js b/examples/vector/index.js index 64e517ca8..1e1ec91b4 100644 --- a/examples/vector/index.js +++ b/examples/vector/index.js @@ -24,6 +24,8 @@ var React = require("react"); // maps cannot pass through two stages). var MatrixReactSdk = require("../../src/index"); +var lastLocationHashSet = null; + // Here, we do some crude URL analysis to allow // deep-linking. We only support registration // deep-links in this example. @@ -46,6 +48,10 @@ function routeUrl(location) { } function onHashChange(ev) { + if (decodeURIComponent(window.location.hash) == lastLocationHashSet) { + // we just set this: no need to route it! + return; + } routeUrl(window.location); } @@ -55,7 +61,9 @@ var loaded = false; // so a web page can update the URL bar appropriately. var onNewScreen = function(screen) { if (!loaded) return; - window.location.hash = '#/'+screen; + var hash = '#/' + screen; + lastLocationHashSet = hash; + window.location.hash = hash; } // We use this to work out what URL the SDK should diff --git a/src/controllers/organisms/RoomView.js b/src/controllers/organisms/RoomView.js index e03c9a972..2042885c6 100644 --- a/src/controllers/organisms/RoomView.js +++ b/src/controllers/organisms/RoomView.js @@ -202,6 +202,7 @@ module.exports = { fillSpace: function() { var messageWrapper = this.refs.messageWrapper.getDOMNode(); + if (!messageWrapper) return; if (messageWrapper.scrollTop < messageWrapper.clientHeight && this.state.room.oldState.paginationToken) { this.setState({paginating: true}); diff --git a/src/controllers/pages/MatrixChat.js b/src/controllers/pages/MatrixChat.js index ded72a097..065e83159 100644 --- a/src/controllers/pages/MatrixChat.js +++ b/src/controllers/pages/MatrixChat.js @@ -23,9 +23,11 @@ var MatrixClientPeg = require("../../MatrixClientPeg"); var RoomListSorter = require("../../RoomListSorter"); var Presence = require("../../Presence"); var dis = require("../../dispatcher"); +var q = require("q"); var ComponentBroker = require('../../ComponentBroker'); var Notifier = ComponentBroker.get('organisms/Notifier'); +var MatrixTools = require('../../MatrixTools'); module.exports = { PageTypes: { @@ -136,7 +138,13 @@ module.exports = { currentRoom: payload.room_id, page_type: this.PageTypes.RoomView, }); - this.notifyNewScreen('room/'+payload.room_id); + var presentedId = payload.room_id; + var room = MatrixClientPeg.get().getRoom(payload.room_id); + if (room) { + var theAlias = MatrixTools.getCanonicalAliasForRoom(room); + if (theAlias) presentedId = theAlias; + } + this.notifyNewScreen('room/'+presentedId); break; case 'view_prev_room': roomIndexDelta = -1; @@ -249,10 +257,26 @@ module.exports = { params: params }); } else if (screen.indexOf('room/') == 0) { - var roomId = screen.split('/')[1]; - dis.dispatch({ - action: 'view_room', - room_id: roomId + var roomString = screen.split('/')[1]; + var defer = q.defer(); + if (roomString[0] == '#') { + var self = this; + MatrixClientPeg.get().getRoomIdForAlias(roomString).done(function(result) { + self.setState({ready: true}); + defer.resolve(result.room_id); + }, function() { + self.setState({ready: true}); + defer.resolve(null); + }); + this.setState({ready: false}); + } else { + defer.resolve(roomString); + } + defer.promise.done(function(roomId) { + dis.dispatch({ + action: 'view_room', + room_id: roomId + }); }); } },