From 9d0efedaee1e522e0fbc1300aee0c27e3ebf3eae Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Wed, 12 Nov 2014 11:14:19 +0000 Subject: [PATCH] Move room alias/id mapping logic from matrixService to modelService. --- .../matrix/event-handler-service.js | 2 +- .../components/matrix/matrix-filter.js | 2 +- .../components/matrix/matrix-service.js | 60 ------------------- .../components/matrix/model-service.js | 60 ++++++++++++++++++- syweb/webclient/home/home-controller.js | 8 +-- syweb/webclient/room/room-controller.js | 4 +- syweb/webclient/test/unit/filters.spec.js | 10 ++-- 7 files changed, 72 insertions(+), 74 deletions(-) diff --git a/syweb/webclient/components/matrix/event-handler-service.js b/syweb/webclient/components/matrix/event-handler-service.js index f51031f4cd..7b2a75507d 100644 --- a/syweb/webclient/components/matrix/event-handler-service.js +++ b/syweb/webclient/components/matrix/event-handler-service.js @@ -92,7 +92,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati }; var handleRoomAliases = function(event, isLiveEvent) { - matrixService.createRoomIdToAliasMapping(event.room_id, event.content.aliases[0]); + modelService.createRoomIdToAliasMapping(event.room_id, event.content.aliases[0]); }; var displayNotification = function(event) { diff --git a/syweb/webclient/components/matrix/matrix-filter.js b/syweb/webclient/components/matrix/matrix-filter.js index 69de97b055..20a29c4ab1 100644 --- a/syweb/webclient/components/matrix/matrix-filter.js +++ b/syweb/webclient/components/matrix/matrix-filter.js @@ -28,7 +28,7 @@ function($rootScope, matrixService, eventHandlerService, modelService) { // If there is an alias, use it // TODO: only one alias is managed for now - var alias = matrixService.getRoomIdToAliasMapping(room_id); + var alias = modelService.getRoomIdToAliasMapping(room_id); var room = modelService.getRoom(room_id).current_room_state; var room_name_event = room.state("m.room.name"); diff --git a/syweb/webclient/components/matrix/matrix-service.js b/syweb/webclient/components/matrix/matrix-service.js index 63051c4f47..70747155f9 100644 --- a/syweb/webclient/components/matrix/matrix-service.js +++ b/syweb/webclient/components/matrix/matrix-service.js @@ -36,9 +36,6 @@ angular.module('matrixService', []) */ var config; - var roomIdToAlias = {}; - var aliasToRoomId = {}; - // Current version of permanent storage var configVersion = 0; var prefixPath = "/_matrix/client/api/v1"; @@ -671,63 +668,6 @@ angular.module('matrixService', []) config.version = configVersion; localStorage.setItem("config", JSON.stringify(config)); }, - - - /****** Room aliases management ******/ - - /** - * Get the room_alias & room_display_name which are computed from data - * already retrieved from the server. - * @param {Room object} room one element of the array returned by the response - * of rooms() and publicRooms() - * @returns {Object} {room_alias: "...", room_display_name: "..."} - */ - getRoomAliasAndDisplayName: function(room) { - var result = { - room_alias: undefined, - room_display_name: undefined - }; - var alias = this.getRoomIdToAliasMapping(room.room_id); - if (alias) { - // use the existing alias from storage - result.room_alias = alias; - result.room_display_name = alias; - } - // XXX: this only lets us learn aliases from our local HS - we should - // make the client stop returning this if we can trust m.room.aliases state events - else if (room.aliases && room.aliases[0]) { - // save the mapping - // TODO: select the smarter alias from the array - this.createRoomIdToAliasMapping(room.room_id, room.aliases[0]); - result.room_display_name = room.aliases[0]; - result.room_alias = room.aliases[0]; - } - else if (room.membership === "invite" && "inviter" in room) { - result.room_display_name = room.inviter + "'s room"; - } - else { - // last resort use the room id - result.room_display_name = room.room_id; - } - return result; - }, - - createRoomIdToAliasMapping: function(roomId, alias) { - roomIdToAlias[roomId] = alias; - aliasToRoomId[alias] = roomId; - }, - - getRoomIdToAliasMapping: function(roomId) { - var alias = roomIdToAlias[roomId]; - //console.log("looking for alias for " + roomId + "; found: " + alias); - return alias; - }, - - getAliasToRoomIdMapping: function(alias) { - var roomId = aliasToRoomId[alias]; - //console.log("looking for roomId for " + alias + "; found: " + roomId); - return roomId; - }, /** * Change or reset the power level of a user diff --git a/syweb/webclient/components/matrix/model-service.js b/syweb/webclient/components/matrix/model-service.js index 8e0ce8d1a9..c6f0defb61 100644 --- a/syweb/webclient/components/matrix/model-service.js +++ b/syweb/webclient/components/matrix/model-service.js @@ -27,6 +27,10 @@ dependency. // $rootScope. angular.module('modelService', []) .factory('modelService', ['matrixService', function(matrixService) { + + // alias / id lookups + var roomIdToAlias = {}; + var aliasToRoomId = {}; /***** Room Object *****/ var Room = function Room(room_id) { @@ -166,7 +170,61 @@ angular.module('modelService', []) getMember: function(room_id, user_id) { var room = this.getRoom(room_id); return room.current_room_state.members[user_id]; - } + }, + + /** + * Get the room_alias & room_display_name which are computed from data + * already retrieved from the server. + * @param {Room object} room one element of the array returned by the response + * of rooms() and publicRooms() + * @returns {Object} {room_alias: "...", room_display_name: "..."} + */ + getRoomAliasAndDisplayName: function(room) { + var result = { + room_alias: undefined, + room_display_name: undefined + }; + var alias = this.getRoomIdToAliasMapping(room.room_id); + if (alias) { + // use the existing alias from storage + result.room_alias = alias; + result.room_display_name = alias; + } + // XXX: this only lets us learn aliases from our local HS - we should + // make the client stop returning this if we can trust m.room.aliases state events + else if (room.aliases && room.aliases[0]) { + // save the mapping + // TODO: select the smarter alias from the array + this.createRoomIdToAliasMapping(room.room_id, room.aliases[0]); + result.room_display_name = room.aliases[0]; + result.room_alias = room.aliases[0]; + } + else if (room.membership === "invite" && "inviter" in room) { + result.room_display_name = room.inviter + "'s room"; + } + else { + // last resort use the room id + result.room_display_name = room.room_id; + } + return result; + }, + + createRoomIdToAliasMapping: function(roomId, alias) { + roomIdToAlias[roomId] = alias; + aliasToRoomId[alias] = roomId; + }, + + getRoomIdToAliasMapping: function(roomId) { + var alias = roomIdToAlias[roomId]; + //console.log("looking for alias for " + roomId + "; found: " + alias); + return alias; + }, + + getAliasToRoomIdMapping: function(alias) { + var roomId = aliasToRoomId[alias]; + //console.log("looking for roomId for " + alias + "; found: " + roomId); + return roomId; + }, }; }]); diff --git a/syweb/webclient/home/home-controller.js b/syweb/webclient/home/home-controller.js index 3a48e64ab4..467db09b3a 100644 --- a/syweb/webclient/home/home-controller.js +++ b/syweb/webclient/home/home-controller.js @@ -17,8 +17,8 @@ limitations under the License. 'use strict'; angular.module('HomeController', ['matrixService', 'eventHandlerService', 'RecentsController']) -.controller('HomeController', ['$scope', '$location', 'matrixService', 'eventHandlerService', - function($scope, $location, matrixService, eventHandlerService) { +.controller('HomeController', ['$scope', '$location', 'matrixService', 'eventHandlerService', 'modelService', + function($scope, $location, matrixService, eventHandlerService, modelService) { $scope.config = matrixService.config(); $scope.public_rooms = []; @@ -56,7 +56,7 @@ angular.module('HomeController', ['matrixService', 'eventHandlerService', 'Recen var room = $scope.public_rooms[i]; // Add room_alias & room_display_name members - angular.extend(room, matrixService.getRoomAliasAndDisplayName(room)); + angular.extend(room, modelService.getRoomAliasAndDisplayName(room)); } } @@ -75,7 +75,7 @@ angular.module('HomeController', ['matrixService', 'eventHandlerService', 'Recen // This room has been created. Refresh the rooms list console.log("Created room " + response.data.room_alias + " with id: "+ response.data.room_id); - matrixService.createRoomIdToAliasMapping( + modelService.createRoomIdToAliasMapping( response.data.room_id, response.data.room_alias); }, function(error) { diff --git a/syweb/webclient/room/room-controller.js b/syweb/webclient/room/room-controller.js index be433d6e80..2a79fbfbbc 100644 --- a/syweb/webclient/room/room-controller.js +++ b/syweb/webclient/room/room-controller.js @@ -490,7 +490,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a // with or without port as is appropriate and append it at this point } - var room_id = matrixService.getAliasToRoomIdMapping(room_alias); + var room_id = modelService.getAliasToRoomIdMapping(room_alias); console.log("joining " + room_alias + " id=" + room_id); if ($scope.room) { // TODO actually check that you = join // don't send a join event for a room you're already in. @@ -677,7 +677,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a if (room_id_or_alias && '!' === room_id_or_alias[0]) { // Yes. We can go on right now $scope.room_id = room_id_or_alias; - $scope.room_alias = matrixService.getRoomIdToAliasMapping($scope.room_id); + $scope.room_alias = modelService.getRoomIdToAliasMapping($scope.room_id); onInit2(); } else { diff --git a/syweb/webclient/test/unit/filters.spec.js b/syweb/webclient/test/unit/filters.spec.js index 7324a8e028..ef33812939 100644 --- a/syweb/webclient/test/unit/filters.spec.js +++ b/syweb/webclient/test/unit/filters.spec.js @@ -8,10 +8,6 @@ describe('mRoomName filter', function() { // mocked services which return the test values above. var matrixService = { - getRoomIdToAliasMapping: function(room_id) { - return testAlias; - }, - config: function() { return { user_id: testUserId @@ -33,7 +29,11 @@ describe('mRoomName filter', function() { return { current_room_state: testRoomState }; - } + }, + + getRoomIdToAliasMapping: function(room_id) { + return testAlias; + }, }; beforeEach(function() {