Store messages in $rootScope so they can be accessed from multiple controllers without duplicated storage for each. This also gives updates.

This commit is contained in:
Kegan Dougal 2014-08-15 12:51:20 +01:00
parent 5dbceaf5a4
commit c51cf4efca
3 changed files with 29 additions and 12 deletions

View File

@ -32,15 +32,37 @@ angular.module('eventHandlerService', [])
var MEMBER_EVENT = "MEMBER_EVENT"; var MEMBER_EVENT = "MEMBER_EVENT";
var PRESENCE_EVENT = "PRESENCE_EVENT"; var PRESENCE_EVENT = "PRESENCE_EVENT";
$rootScope.events = {
rooms: {}, // will contain roomId: { messages:[], members:[] }
};
var initRoom = function(room_id) {
console.log("Creating new handler entry for " + room_id);
$rootScope.events.rooms[room_id] = {};
$rootScope.events.rooms[room_id].messages = [];
$rootScope.events.rooms[room_id].members = [];
}
var handleMessage = function(event, isLiveEvent) { var handleMessage = function(event, isLiveEvent) {
if ("membership_target" in event.content) { if ("membership_target" in event.content) {
// event.user_id = event.content.membership_target; event.user_id = event.content.membership_target;
} }
if (!(event.room_id in $rootScope.events.rooms)) {
initRoom(event.room_id);
}
if (isLiveEvent) {
$rootScope.events.rooms[event.room_id].messages.push(event);
}
else {
$rootScope.events.rooms[event.room_id].messages.unshift(event);
}
// TODO send delivery receipt if isLiveEvent
// $broadcast this, as controllers may want to do funky things such as // $broadcast this, as controllers may want to do funky things such as
// scroll to the bottom, etc which cannot be expressed via simple $scope // scroll to the bottom, etc which cannot be expressed via simple $scope
// updates. // updates.
console.log("Bcast " + JSON.stringify(event));
$rootScope.$broadcast(MSG_EVENT, event, isLiveEvent); $rootScope.$broadcast(MSG_EVENT, event, isLiveEvent);
}; };

View File

@ -28,7 +28,6 @@ angular.module('RoomController', [])
can_paginate: true, // this is toggled off when we run out of items can_paginate: true, // this is toggled off when we run out of items
stream_failure: undefined // the response when the stream fails stream_failure: undefined // the response when the stream fails
}; };
$scope.messages = [];
$scope.members = {}; $scope.members = {};
$scope.stopPoll = false; $scope.stopPoll = false;
@ -43,13 +42,9 @@ angular.module('RoomController', [])
}; };
$scope.$on(eventHandlerService.MSG_EVENT, function(ngEvent, event, isLive) { $scope.$on(eventHandlerService.MSG_EVENT, function(ngEvent, event, isLive) {
if (isLive) { if (isLive && event.room_id === $scope.room_id) {
$scope.messages.push(event);
scrollToBottom(); scrollToBottom();
} }
else {
$scope.messages.unshift(event);
}
}); });
$scope.$on(eventHandlerService.MEMBER_EVENT, function(ngEvent, event, isLive) { $scope.$on(eventHandlerService.MEMBER_EVENT, function(ngEvent, event, isLive) {

View File

@ -22,14 +22,14 @@
<div class="messageTableWrapper"> <div class="messageTableWrapper">
<table class="messageTable"> <table class="messageTable">
<tr ng-repeat="msg in messages" ng-class="msg.user_id === state.user_id ? 'mine' : ''"> <tr ng-repeat="msg in events.rooms[room_id].messages" ng-class="msg.user_id === state.user_id ? 'mine' : ''">
<td class="leftBlock"> <td class="leftBlock">
<div class="sender" ng-hide="messages[$index - 1].user_id === msg.user_id">{{ members[msg.user_id].displayname || msg.user_id }}</div> <div class="sender" ng-hide="events.rooms[room_id].messages[$index - 1].user_id === msg.user_id">{{ members[msg.user_id].displayname || msg.user_id }}</div>
<div class="timestamp">{{ msg.content.hsob_ts | date:'MMM d HH:mm:ss' }}</div> <div class="timestamp">{{ msg.content.hsob_ts | date:'MMM d HH:mm:ss' }}</div>
</td> </td>
<td class="avatar"> <td class="avatar">
<img ng-src="{{ members[msg.user_id].avatar_url || 'img/default-profile.jpg' }}" width="32" height="32" <img ng-src="{{ members[msg.user_id].avatar_url || 'img/default-profile.jpg' }}" width="32" height="32"
ng-hide="messages[$index - 1].user_id === msg.user_id || msg.user_id === state.user_id"/> ng-hide="events.rooms[room_id].messages[$index - 1].user_id === msg.user_id || msg.user_id === state.user_id"/>
</td> </td>
<td ng-class="!msg.content.membership_target ? (msg.content.msgtype === 'm.emote' ? 'emote text' : 'text') : ''"> <td ng-class="!msg.content.membership_target ? (msg.content.msgtype === 'm.emote' ? 'emote text' : 'text') : ''">
<div class="bubble"> <div class="bubble">
@ -40,7 +40,7 @@
</td> </td>
<td class="rightBlock"> <td class="rightBlock">
<img ng-src="{{ members[msg.user_id].avatar_url || 'img/default-profile.jpg' }}" width="32" height="32" <img ng-src="{{ members[msg.user_id].avatar_url || 'img/default-profile.jpg' }}" width="32" height="32"
ng-hide="messages[$index - 1].user_id === msg.user_id || msg.user_id !== state.user_id"/> ng-hide="events.rooms[room_id].messages[$index - 1].user_id === msg.user_id || msg.user_id !== state.user_id"/>
</td> </td>
</tr> </tr>
</table> </table>