From e5257b21b3cb6a1823159273b07a0ada25cdf8a8 Mon Sep 17 00:00:00 2001 From: Emmanuel ROHEE Date: Mon, 18 Aug 2014 17:11:08 +0200 Subject: [PATCH] Support room alias in rooms URL (ex: http://127.0.0.1:8000/#/room/#public:localhost:8080) --- webclient/app.js | 6 +++ webclient/components/matrix/matrix-service.js | 10 +++++ webclient/room/room-controller.js | 43 ++++++++++++++++++- 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/webclient/app.js b/webclient/app.js index 8d64db92d..193c11d46 100644 --- a/webclient/app.js +++ b/webclient/app.js @@ -37,6 +37,12 @@ matrixWebClient.config(['$routeProvider', '$provide', '$httpProvider', templateUrl: 'room/room.html', controller: 'RoomController' }). + when('/room/', { // room URL with room alias in it (ex: http://127.0.0.1:8000/#/room/#public:localhost:8080) will come here. + // The reason is that 2nd hash key breaks routeProvider parameters cutting so that the URL will not match with + // the previous '/room/:room_id' URL rule + templateUrl: 'room/room.html', + controller: 'RoomController' + }). when('/rooms', { templateUrl: 'rooms/rooms.html', controller: 'RoomsController' diff --git a/webclient/components/matrix/matrix-service.js b/webclient/components/matrix/matrix-service.js index c52c94c31..47828993a 100644 --- a/webclient/components/matrix/matrix-service.js +++ b/webclient/components/matrix/matrix-service.js @@ -165,6 +165,16 @@ angular.module('matrixService', []) return doRequest("DELETE", path, undefined, undefined); }, + // Retrieves the room ID corresponding to a room alias + resolveRoomAlias:function(room_alias) { + var path = "/matrix/client/api/v1/ds/room/$room_alias"; + room_alias = encodeURIComponent(room_alias); + + path = path.replace("$room_alias", room_alias); + + return doRequest("GET", path, undefined, {}); + }, + sendMessage: function(room_id, msg_id, content) { // The REST path spec var path = "/rooms/$room_id/messages/$from/$msg_id"; diff --git a/webclient/room/room-controller.js b/webclient/room/room-controller.js index b859f3d7e..999c48f02 100644 --- a/webclient/room/room-controller.js +++ b/webclient/room/room-controller.js @@ -108,8 +108,11 @@ angular.module('RoomController', ['ngSanitize']) function($scope, $http, $timeout, $routeParams, $location, matrixService, eventStreamService, eventHandlerService) { 'use strict'; var MESSAGES_PER_PAGINATION = 30; + + // Room ids. Checked, computed and resolved in onInit $scope.room_id = $routeParams.room_id; - $scope.room_alias = matrixService.getRoomIdToAliasMapping($scope.room_id); + $scope.room_alias = undefined; + $scope.state = { user_id: matrixService.config().user_id, events_from: "END", // when to start the event stream from. @@ -342,7 +345,45 @@ angular.module('RoomController', ['ngSanitize']) $scope.onInit = function() { // $timeout(function() { document.getElementById('textInput').focus() }, 0); console.log("onInit"); + + // Does the room ID provided in the URL? + if ($scope.room_id) { + // Yes, we can start right now + $scope.room_alias = matrixService.getRoomIdToAliasMapping($scope.room_id); + onInit2(); + } + else { + // No, the URL contains the room alias. Get this alias. + // ie: extract #public:localhost:8080 from http://127.0.0.1:8000/#/room/#public:localhost:8080 + if (3 === location.hash.split("#").length) { + $scope.room_alias = "#" + location.hash.split("#")[2]; + } + else { + // In case of issue, go to the default page + console.log("Error: cannot extract room alias"); + $location.path("/"); + return; + } + + console.log("Resolving alias: " + $scope.room_alias); + // Need a room ID required in Matrix API requests + matrixService.resolveRoomAlias($scope.room_alias).then(function(response) { + $scope.room_id = response.data.room_id; + console.log(" -> Room ID: " + $scope.room_id); + + // Now, we can start + onInit2(); + }, + function () { + // In case of issue, go to the default page + console.log("Error: cannot resolve room alias"); + $location.path("/"); + }); + } + }; + + var onInit2 = function() { // Join the room matrixService.join($scope.room_id).then( function() {