From c51cf4efcaad26695382924e142d9a1ad2658754 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Fri, 15 Aug 2014 12:51:20 +0100 Subject: Store messages in $rootScope so they can be accessed from multiple controllers without duplicated storage for each. This also gives updates. --- .../components/matrix/event-handler-service.js | 26 ++++++++++++++++++++-- webclient/room/room-controller.js | 7 +----- webclient/room/room.html | 8 +++---- 3 files changed, 29 insertions(+), 12 deletions(-) diff --git a/webclient/components/matrix/event-handler-service.js b/webclient/components/matrix/event-handler-service.js index 30d7ab35ca..f7411fd80a 100644 --- a/webclient/components/matrix/event-handler-service.js +++ b/webclient/components/matrix/event-handler-service.js @@ -32,15 +32,37 @@ angular.module('eventHandlerService', []) var MEMBER_EVENT = "MEMBER_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) { 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 // scroll to the bottom, etc which cannot be expressed via simple $scope // updates. - console.log("Bcast " + JSON.stringify(event)); $rootScope.$broadcast(MSG_EVENT, event, isLiveEvent); }; diff --git a/webclient/room/room-controller.js b/webclient/room/room-controller.js index 301d3d6447..4c5415eace 100644 --- a/webclient/room/room-controller.js +++ b/webclient/room/room-controller.js @@ -28,7 +28,6 @@ angular.module('RoomController', []) can_paginate: true, // this is toggled off when we run out of items stream_failure: undefined // the response when the stream fails }; - $scope.messages = []; $scope.members = {}; $scope.stopPoll = false; @@ -43,13 +42,9 @@ angular.module('RoomController', []) }; $scope.$on(eventHandlerService.MSG_EVENT, function(ngEvent, event, isLive) { - if (isLive) { - $scope.messages.push(event); + if (isLive && event.room_id === $scope.room_id) { scrollToBottom(); } - else { - $scope.messages.unshift(event); - } }); $scope.$on(eventHandlerService.MEMBER_EVENT, function(ngEvent, event, isLive) { diff --git a/webclient/room/room.html b/webclient/room/room.html index 3b9ba713de..93917071d9 100644 --- a/webclient/room/room.html +++ b/webclient/room/room.html @@ -22,14 +22,14 @@
- +
-
{{ members[msg.user_id].displayname || msg.user_id }}
+
{{ members[msg.user_id].displayname || msg.user_id }}
{{ msg.content.hsob_ts | date:'MMM d HH:mm:ss' }}
+ ng-hide="events.rooms[room_id].messages[$index - 1].user_id === msg.user_id || msg.user_id === state.user_id"/>
@@ -40,7 +40,7 @@
+ ng-hide="events.rooms[room_id].messages[$index - 1].user_id === msg.user_id || msg.user_id !== state.user_id"/>
-- cgit 1.4.1