Move room alias/id mapping logic from matrixService to modelService.

This commit is contained in:
Kegan Dougal 2014-11-12 11:14:19 +00:00
parent 33e9e0fb2d
commit 9d0efedaee
7 changed files with 72 additions and 74 deletions

View File

@ -92,7 +92,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati
}; };
var handleRoomAliases = function(event, isLiveEvent) { 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) { var displayNotification = function(event) {

View File

@ -28,7 +28,7 @@ function($rootScope, matrixService, eventHandlerService, modelService) {
// If there is an alias, use it // If there is an alias, use it
// TODO: only one alias is managed for now // 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 = modelService.getRoom(room_id).current_room_state;
var room_name_event = room.state("m.room.name"); var room_name_event = room.state("m.room.name");

View File

@ -36,9 +36,6 @@ angular.module('matrixService', [])
*/ */
var config; var config;
var roomIdToAlias = {};
var aliasToRoomId = {};
// Current version of permanent storage // Current version of permanent storage
var configVersion = 0; var configVersion = 0;
var prefixPath = "/_matrix/client/api/v1"; var prefixPath = "/_matrix/client/api/v1";
@ -672,63 +669,6 @@ angular.module('matrixService', [])
localStorage.setItem("config", JSON.stringify(config)); 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 * Change or reset the power level of a user
* @param {String} room_id the room id * @param {String} room_id the room id

View File

@ -28,6 +28,10 @@ dependency.
angular.module('modelService', []) angular.module('modelService', [])
.factory('modelService', ['matrixService', function(matrixService) { .factory('modelService', ['matrixService', function(matrixService) {
// alias / id lookups
var roomIdToAlias = {};
var aliasToRoomId = {};
/***** Room Object *****/ /***** Room Object *****/
var Room = function Room(room_id) { var Room = function Room(room_id) {
this.room_id = room_id; this.room_id = room_id;
@ -166,7 +170,61 @@ angular.module('modelService', [])
getMember: function(room_id, user_id) { getMember: function(room_id, user_id) {
var room = this.getRoom(room_id); var room = this.getRoom(room_id);
return room.current_room_state.members[user_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;
},
}; };
}]); }]);

View File

@ -17,8 +17,8 @@ limitations under the License.
'use strict'; 'use strict';
angular.module('HomeController', ['matrixService', 'eventHandlerService', 'RecentsController']) angular.module('HomeController', ['matrixService', 'eventHandlerService', 'RecentsController'])
.controller('HomeController', ['$scope', '$location', 'matrixService', 'eventHandlerService', .controller('HomeController', ['$scope', '$location', 'matrixService', 'eventHandlerService', 'modelService',
function($scope, $location, matrixService, eventHandlerService) { function($scope, $location, matrixService, eventHandlerService, modelService) {
$scope.config = matrixService.config(); $scope.config = matrixService.config();
$scope.public_rooms = []; $scope.public_rooms = [];
@ -56,7 +56,7 @@ angular.module('HomeController', ['matrixService', 'eventHandlerService', 'Recen
var room = $scope.public_rooms[i]; var room = $scope.public_rooms[i];
// Add room_alias & room_display_name members // 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 // This room has been created. Refresh the rooms list
console.log("Created room " + response.data.room_alias + " with id: "+ console.log("Created room " + response.data.room_alias + " with id: "+
response.data.room_id); response.data.room_id);
matrixService.createRoomIdToAliasMapping( modelService.createRoomIdToAliasMapping(
response.data.room_id, response.data.room_alias); response.data.room_id, response.data.room_alias);
}, },
function(error) { function(error) {

View File

@ -490,7 +490,7 @@ angular.module('RoomController', ['ngSanitize', 'matrixFilter', 'mFileInput', 'a
// with or without port as is appropriate and append it at this point // 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); console.log("joining " + room_alias + " id=" + room_id);
if ($scope.room) { // TODO actually check that you = join if ($scope.room) { // TODO actually check that you = join
// don't send a join event for a room you're already in. // 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]) { if (room_id_or_alias && '!' === room_id_or_alias[0]) {
// Yes. We can go on right now // Yes. We can go on right now
$scope.room_id = room_id_or_alias; $scope.room_id = room_id_or_alias;
$scope.room_alias = matrixService.getRoomIdToAliasMapping($scope.room_id); $scope.room_alias = modelService.getRoomIdToAliasMapping($scope.room_id);
onInit2(); onInit2();
} }
else { else {

View File

@ -8,10 +8,6 @@ describe('mRoomName filter', function() {
// mocked services which return the test values above. // mocked services which return the test values above.
var matrixService = { var matrixService = {
getRoomIdToAliasMapping: function(room_id) {
return testAlias;
},
config: function() { config: function() {
return { return {
user_id: testUserId user_id: testUserId
@ -33,7 +29,11 @@ describe('mRoomName filter', function() {
return { return {
current_room_state: testRoomState current_room_state: testRoomState
}; };
} },
getRoomIdToAliasMapping: function(room_id) {
return testAlias;
},
}; };
beforeEach(function() { beforeEach(function() {