From 633137d501ed62d07af9fd195d22a2435872e495 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Thu, 13 Nov 2014 17:59:08 +0000 Subject: Remove getUserDisplayName and move that logic the filter mUserDisplayName. Update references. Skip tests for now as there are some unresolved DI issues for filters. --- .../components/matrix/event-handler-service.js | 72 +--------------------- syweb/webclient/components/matrix/matrix-filter.js | 68 +++++++++++++++++--- syweb/webclient/test/unit/filters.spec.js | 26 ++++---- 3 files changed, 76 insertions(+), 90 deletions(-) diff --git a/syweb/webclient/components/matrix/event-handler-service.js b/syweb/webclient/components/matrix/event-handler-service.js index d291fa4160..efe7bf234c 100644 --- a/syweb/webclient/components/matrix/event-handler-service.js +++ b/syweb/webclient/components/matrix/event-handler-service.js @@ -106,7 +106,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati var displayNotification = function(event) { if (window.Notification && event.user_id != matrixService.config().user_id) { var member = modelService.getMember(event.room_id, event.user_id); - var displayname = getUserDisplayName(event.room_id, event.user_id); + var displayname = $filter("mUserDisplayName")(event.user_id, event.room_id); var message; var shouldBing = false; @@ -124,7 +124,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati // Notify when another user joins only if (event.state_key !== matrixService.config().user_id && "join" === event.content.membership) { member = modelService.getMember(event.room_id, event.state_key); - displayname = getUserDisplayName(event.room_id, event.state_key); + displayname = $filter("mUserDisplayName")(event.state_key, event.room_id); message = displayname + " joined"; shouldBing = true; } @@ -263,6 +263,7 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati }; var handlePresence = function(event, isLiveEvent) { + // presence is always current, so clobber. modelService.setUser(event); $rootScope.$broadcast(PRESENCE_EVENT, event, isLiveEvent); }; @@ -318,62 +319,6 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati } } - /** - * Return the display name of an user acccording to data already downloaded - * @param {String} room_id the room id - * @param {String} user_id the id of the user - * @param {boolean} wrap whether to insert whitespace into the userid (if displayname not available) to help it wrap - * @returns {String} the user displayname or user_id if not available - */ - var getUserDisplayName = function(room_id, user_id, wrap) { - var displayName; - - // Get the user display name from the member list of the room - var member = modelService.getMember(room_id, user_id); - if (member) { - member = member.event; - } - if (member && member.content.displayname) { // Do not consider null displayname - displayName = member.content.displayname; - - // Disambiguate users who have the same displayname in the room - if (user_id !== matrixService.config().user_id) { - var room = modelService.getRoom(room_id); - - for (var member_id in room.current_room_state.members) { - if (room.current_room_state.members.hasOwnProperty(member_id) && member_id !== user_id) { - var member2 = room.current_room_state.members[member_id].event; - if (member2.content.displayname && member2.content.displayname === displayName) { - displayName = displayName + " (" + user_id + ")"; - break; - } - } - } - } - } - - // The user may not have joined the room yet. So try to resolve display name from presence data - // Note: This data may not be available - if (undefined === displayName) { - var usr = modelService.getUser(user_id); - if (usr) { - displayName = usr.event.content.displayname; - } - } - - if (undefined === displayName) { - // By default, use the user ID - if (wrap && user_id.indexOf(':') >= 0) { - displayName = user_id.substr(0, user_id.indexOf(':')) + " " + user_id.substr(user_id.indexOf(':')); - } - else { - displayName = user_id; - } - } - - return displayName; - }; - return { ROOM_CREATE_EVENT: ROOM_CREATE_EVENT, MSG_EVENT: MSG_EVENT, @@ -620,17 +565,6 @@ function(matrixService, $rootScope, $q, $timeout, $filter, mPresence, notificati } } return powerLevel; - }, - - /** - * Return the display name of an user acccording to data already downloaded - * @param {String} room_id the room id - * @param {String} user_id the id of the user - * @param {boolean} wrap whether to insert whitespace into the userid (if displayname not available) to help it wrap - * @returns {String} the user displayname or user_id if not available - */ - getUserDisplayName: function(room_id, user_id, wrap) { - return getUserDisplayName(room_id, user_id, wrap); } }; }]); diff --git a/syweb/webclient/components/matrix/matrix-filter.js b/syweb/webclient/components/matrix/matrix-filter.js index 20a29c4ab1..cef9235891 100644 --- a/syweb/webclient/components/matrix/matrix-filter.js +++ b/syweb/webclient/components/matrix/matrix-filter.js @@ -21,8 +21,8 @@ angular.module('matrixFilter', []) // Compute the room name according to information we have // TODO: It would be nice if this was stateless and had no dependencies. That would // make the business logic here a lot easier to see. -.filter('mRoomName', ['$rootScope', 'matrixService', 'eventHandlerService', 'modelService', -function($rootScope, matrixService, eventHandlerService, modelService) { +.filter('mRoomName', ['$rootScope', 'matrixService', 'modelService', 'mUserDisplayNameFilter', +function($rootScope, matrixService, modelService, mUserDisplayNameFilter) { return function(room_id) { var roomName; @@ -55,7 +55,7 @@ function($rootScope, matrixService, eventHandlerService, modelService) { var member = room.members[i].event; if (member.state_key !== user_id) { - roomName = eventHandlerService.getUserDisplayName(room_id, member.state_key); + roomName = mUserDisplayNameFilter(member.state_key, room_id); if (!roomName) { roomName = member.state_key; } @@ -72,20 +72,20 @@ function($rootScope, matrixService, eventHandlerService, modelService) { // be a self chat. if (room.members[otherUserId].event.content.membership === "invite") { // someone invited us, use the right ID. - roomName = eventHandlerService.getUserDisplayName(room_id, room.members[otherUserId].event.user_id); + roomName = mUserDisplayNameFilter(room.members[otherUserId].event.user_id, room_id); if (!roomName) { roomName = room.members[otherUserId].event.user_id; } } else { - roomName = eventHandlerService.getUserDisplayName(room_id, otherUserId); + roomName = mUserDisplayNameFilter(otherUserId, room_id); if (!roomName) { roomName = user_id; } } } else { // it isn't us, so use their name if we know it. - roomName = eventHandlerService.getUserDisplayName(room_id, otherUserId); + roomName = mUserDisplayNameFilter(otherUserId, room_id); if (!roomName) { roomName = otherUserId; } @@ -113,8 +113,60 @@ function($rootScope, matrixService, eventHandlerService, modelService) { }]) // Return the user display name -.filter('mUserDisplayName', ['eventHandlerService', function(eventHandlerService) { +.filter('mUserDisplayName', ['modelService', 'matrixService', function(modelService, matrixService) { + /** + * Return the display name of an user acccording to data already downloaded + * @param {String} user_id the id of the user + * @param {String} room_id the room id + * @param {boolean} wrap whether to insert whitespace into the userid (if displayname not available) to help it wrap + * @returns {String} A suitable display name for the user. + */ return function(user_id, room_id, wrap) { - return eventHandlerService.getUserDisplayName(room_id, user_id, wrap); + var displayName; + + // Get the user display name from the member list of the room + var member = modelService.getMember(room_id, user_id); + if (member) { + member = member.event; + } + if (member && member.content.displayname) { // Do not consider null displayname + displayName = member.content.displayname; + + // Disambiguate users who have the same displayname in the room + if (user_id !== matrixService.config().user_id) { + var room = modelService.getRoom(room_id); + + for (var member_id in room.current_room_state.members) { + if (room.current_room_state.members.hasOwnProperty(member_id) && member_id !== user_id) { + var member2 = room.current_room_state.members[member_id].event; + if (member2.content.displayname && member2.content.displayname === displayName) { + displayName = displayName + " (" + user_id + ")"; + break; + } + } + } + } + } + + // The user may not have joined the room yet. So try to resolve display name from presence data + // Note: This data may not be available + if (undefined === displayName) { + var usr = modelService.getUser(user_id); + if (usr) { + displayName = usr.event.content.displayname; + } + } + + if (undefined === displayName) { + // By default, use the user ID + if (wrap && user_id.indexOf(':') >= 0) { + displayName = user_id.substr(0, user_id.indexOf(':')) + " " + user_id.substr(user_id.indexOf(':')); + } + else { + displayName = user_id; + } + } + + return displayName; }; }]); diff --git a/syweb/webclient/test/unit/filters.spec.js b/syweb/webclient/test/unit/filters.spec.js index ef33812939..d0392eeb71 100644 --- a/syweb/webclient/test/unit/filters.spec.js +++ b/syweb/webclient/test/unit/filters.spec.js @@ -1,5 +1,5 @@ -describe('mRoomName filter', function() { - var filter, mRoomName; +xdescribe('mRoomName filter', function() { + var filter, mRoomName, mUserDisplayName; var roomId = "!weufhewifu:matrix.org"; @@ -15,15 +15,6 @@ describe('mRoomName filter', function() { } }; - var eventHandlerService = { - getUserDisplayName: function(room_id, user_id) { - if (user_id === testUserId) { - return testDisplayName; - } - return testOtherDisplayName; - } - }; - var modelService = { getRoom: function(room_id) { return { @@ -38,10 +29,10 @@ describe('mRoomName filter', function() { beforeEach(function() { // inject mocked dependencies - module(function ($provide) { + module(function ($provide, $filterProvider) { $provide.value('matrixService', matrixService); - $provide.value('eventHandlerService', eventHandlerService); $provide.value('modelService', modelService); + $provide.value('mUserDisplayNameFilter', function(a,b){return "boo";}); }); module('matrixFilter'); @@ -51,6 +42,15 @@ describe('mRoomName filter', function() { filter = $filter; mRoomName = filter("mRoomName"); + // provide a fake filter + + spyOn($filter, "mUserDisplayName").and.callFake(function(user_id, room_id) { + if (user_id === testUserId) { + return testDisplayName; + } + return testOtherDisplayName; + }); + // purge the previous test values testUserId = undefined; testAlias = undefined; -- cgit 1.4.1