State data now provides up-to-date users displaynames. So use it first.

Continue to use presence data as fallback solution which is required when users do not join the room yet.
Created eventHandlerService.getUserDisplayName() as a single point to compute display name.
pull/10/head
Emmanuel ROHEE 2014-09-24 11:04:27 +02:00
parent 7b8e24a588
commit ef5b39c410
2 changed files with 36 additions and 50 deletions

View File

@ -544,6 +544,34 @@ function(matrixService, $rootScope, $q, $timeout, mPresence) {
return member;
},
/**
* 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
* @returns {String} the user displayname or user_id if not available
*/
getUserDisplayName: function(room_id, user_id) {
var displayName;
// Get the user display name from the member list of the room
var member = this.getMember(room_id, user_id);
if (member) {
displayName = member.content.displayname;
}
// 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 && user_id in $rootScope.presence) {
displayName = $rootScope.presence[user_id].content.displayname;
}
if (undefined === displayName) {
// By default, use the user ID
displayName = user_id;
}
return displayName;
},
setRoomVisibility: function(room_id, visible) {
if (!visible) {
return;

View File

@ -19,7 +19,7 @@
angular.module('matrixFilter', [])
// Compute the room name according to information we have
.filter('mRoomName', ['$rootScope', 'matrixService', function($rootScope, matrixService) {
.filter('mRoomName', ['$rootScope', 'matrixService', 'eventHandlerService', function($rootScope, matrixService, eventHandlerService) {
return function(room_id) {
var roomName;
@ -47,16 +47,8 @@ angular.module('matrixFilter', [])
for (var i in room.members) {
var member = room.members[i];
if (member.state_key !== user_id) {
if (member.state_key in $rootScope.presence) {
// If the user is listed in presence, use the displayname there
// as it is the most uptodate
// XXX: is this true nowadays?
roomName = $rootScope.presence[member.state_key].content.displayname || member.state_key;
}
else {
roomName = member.content.displayname || member.state_key;
}
roomName = eventHandlerService.getUserDisplayName(room_id, member.state_key);
break;
}
}
}
@ -102,14 +94,8 @@ angular.module('matrixFilter', [])
*/
}
// Try to resolve his displayname in presence global data
// XXX: should we be looking in the room state instead, given it should be accurate nowadays?
if (otherUserId in $rootScope.presence) {
roomName = $rootScope.presence[otherUserId].content.displayname || otherUserId;
}
else {
roomName = otherUserId;
}
// Get the user display name
roomName = eventHandlerService.getUserDisplayName(room_id, otherUserId);
}
}
}
@ -136,37 +122,9 @@ angular.module('matrixFilter', [])
};
}])
// Compute the user display name in a room according to the data already downloaded
.filter('mUserDisplayName', ['$rootScope', function($rootScope) {
// Return the user display name
.filter('mUserDisplayName', ['eventHandlerService', function(eventHandlerService) {
return function(user_id, room_id) {
var displayName;
// Try to find the user name among presence data
// Warning: that means we have received before a presence event for this
// user which cannot be guaranted.
// However, if we get the info by this way, we are sure this is the latest user display name
// See FIXME comment below
if (user_id in $rootScope.presence) {
displayName = $rootScope.presence[user_id].content.displayname;
}
// FIXME: Would like to use the display name as defined in room members of the room.
// But this information is the display name of the user when he has joined the room.
// It does not take into account user display name update
if (room_id) {
var room = $rootScope.events.rooms[room_id];
if (room && (user_id in room.members)) {
var member = room.members[user_id];
if (member.content.displayname) {
displayName = member.content.displayname;
}
}
}
if (undefined === displayName) {
// By default, use the user ID
displayName = user_id;
}
return displayName;
return eventHandlerService.getUserDisplayName(room_id, user_id);
};
}]);