update presence times in realtime through the magic of two-way binding

This commit is contained in:
Matthew Hodgson 2014-08-29 17:21:57 +01:00
parent d4145abd33
commit b86d2a2d4f
2 changed files with 15 additions and 35 deletions

View File

@ -176,6 +176,8 @@ angular.module('RoomController', ['ngSanitize', 'mFileInput'])
// set target_user_id to keep things clear
var target_user_id = chunk.state_key;
var now = new Date().getTime();
var isNewMember = !(target_user_id in $scope.members);
if (isNewMember) {
// FIXME: why are we copying these fields around inside chunk?
@ -185,45 +187,15 @@ angular.module('RoomController', ['ngSanitize', 'mFileInput'])
if ("mtime_age" in chunk.content) {
chunk.mtime_age = chunk.content.mtime_age;
}
// Once the HS reliably returns the displaynames & avatar_urls for both
// local and remote users, we should use this rather than the evalAsync block
// below
if ("displayname" in chunk.content) {
chunk.displayname = chunk.content.displayname;
}
if ("avatar_url" in chunk.content) {
chunk.avatar_url = chunk.content.avatar_url;
}
chunk.last_updated = now;
$scope.members[target_user_id] = chunk;
/*
// Stale code for explicitly hammering the homeserver for every displayname & avatar_url
// get their display name and profile picture and set it to their
// member entry in $scope.members. We HAVE to use $timeout with 0 delay
// to make this function run AFTER the current digest cycle, else the
// response may update a STALE VERSION of the member list (manifesting
// as no member names appearing, or appearing sporadically).
$scope.$evalAsync(function() {
matrixService.getDisplayName(chunk.target_user_id).then(
function(response) {
var member = $scope.members[chunk.target_user_id];
if (member !== undefined) {
member.displayname = response.data.displayname;
}
}
);
matrixService.getProfilePictureUrl(chunk.target_user_id).then(
function(response) {
var member = $scope.members[chunk.target_user_id];
if (member !== undefined) {
member.avatar_url = response.data.avatar_url;
}
}
);
});
*/
if (target_user_id in $rootScope.presence) {
updatePresence($rootScope.presence[target_user_id]);
}
@ -235,6 +207,12 @@ angular.module('RoomController', ['ngSanitize', 'mFileInput'])
}
};
var updateMemberListPresenceAge = function() {
$scope.now = new Date().getTime();
//console.log("updateMemberListPresenceAge() - now = " + $scope.now);
$timeout(updateMemberListPresenceAge, 5 * 1000);
};
var updatePresence = function(chunk) {
if (!(chunk.content.user_id in $scope.members)) {
console.log("updatePresence: Unknown member for chunk " + JSON.stringify(chunk));
@ -397,6 +375,8 @@ angular.module('RoomController', ['ngSanitize', 'mFileInput'])
$scope.recentsSelectedRoomID = $scope.room_id;
paginate(MESSAGES_PER_PAGINATION);
updateMemberListPresenceAge();
};
$scope.inviteUser = function(user_id) {

View File

@ -25,7 +25,7 @@
<div class="userName">{{ member.displayname || member.id.substr(0, member.id.indexOf(':')) }}<br/>{{ member.displayname ? "" : member.id.substr(member.id.indexOf(':')) }}</div>
</td>
<td class="userPresence" ng-class="member.presenceState === 'online' ? 'online' : (member.presenceState === 'unavailable' ? 'unavailable' : '')">
{{ member.mtime_age | duration }}<br/>{{ member.mtime_age ? "ago" : "" }}
{{ member.mtime_age + (now - member.last_updated) | duration }}<br/>{{ member.mtime_age ? "ago" : "" }}
</td>
</table>
</div>