summary refs log tree commit diff
path: root/syweb/webclient
diff options
context:
space:
mode:
authorKegan Dougal <kegan@matrix.org>2014-11-12 15:11:34 +0000
committerKegan Dougal <kegan@matrix.org>2014-11-12 15:11:34 +0000
commit99c445a6d69f0560c08c0264d9b844a4b03599b6 (patch)
tree65b08409b51b5126c9c462f5b19d29240a253cce /syweb/webclient
parentAdd recents-service to store shared state between recents-controllers. (diff)
downloadsynapse-99c445a6d69f0560c08c0264d9b844a4b03599b6.tar.xz
Migrate unread messages logic to recentsService.
Diffstat (limited to 'syweb/webclient')
-rw-r--r--syweb/webclient/components/matrix/recents-service.js31
-rw-r--r--syweb/webclient/recents/recents-controller.js26
2 files changed, 36 insertions, 21 deletions
diff --git a/syweb/webclient/components/matrix/recents-service.js b/syweb/webclient/components/matrix/recents-service.js
index 64d1ab93b4..237f1e3912 100644
--- a/syweb/webclient/components/matrix/recents-service.js
+++ b/syweb/webclient/components/matrix/recents-service.js
@@ -27,15 +27,31 @@ This is preferable to polluting the $rootScope with recents specific info, and
 makes the dependency on this shared state *explicit*.
 */
 angular.module('recentsService', [])
-.factory('recentsService', ['$rootScope', function($rootScope) {
+.factory('recentsService', ['$rootScope', 'eventHandlerService', function($rootScope, eventHandlerService) {
     // notify listeners when variables in the service are updated. We need to do
     // this since we do not tie them to any scope.
-    var BROADCAST_SELECTED_ROOM_ID = "recentsService:BROADCAST_SELECTED_ROOM_ID";
+    var BROADCAST_SELECTED_ROOM_ID = "recentsService:BROADCAST_SELECTED_ROOM_ID(room_id)";
     var selectedRoomId = undefined;
     
+    var BROADCAST_UNREAD_MESSAGES = "recentsService:BROADCAST_UNREAD_MESSAGES(room_id, unreadCount)";
+    var unreadMessages = {
+        // room_id: <number>
+    };
+    
+    // listen for new unread messages
+    $rootScope.$on(eventHandlerService.MSG_EVENT, function(ngEvent, event, isLive) {
+        if (isLive && event.room_id !== selectedRoomId) {
+            if (!unreadMessages[event.room_id]) {
+                unreadMessages[event.room_id] = 0;
+            }
+            unreadMessages[event.room_id] += 1;
+            $rootScope.$broadcast(BROADCAST_UNREAD_MESSAGES, event.room_id, unreadMessages[event.room_id]);
+        }
+    });
     
     return {
         BROADCAST_SELECTED_ROOM_ID: BROADCAST_SELECTED_ROOM_ID,
+        BROADCAST_UNREAD_MESSAGES: BROADCAST_UNREAD_MESSAGES,
     
         getSelectedRoomId: function() {
             return selectedRoomId;
@@ -44,6 +60,17 @@ angular.module('recentsService', [])
         setSelectedRoomId: function(room_id) {
             selectedRoomId = room_id;
             $rootScope.$broadcast(BROADCAST_SELECTED_ROOM_ID, room_id);
+        },
+        
+        getUnreadMessages: function() {
+            return unreadMessages;
+        },
+        
+        markAsRead: function(room_id) {
+            if (unreadMessages[room_id]) {
+                unreadMessages[room_id] = 0;
+            }
+            $rootScope.$broadcast(BROADCAST_UNREAD_MESSAGES, room_id, 0);
         }
     
     };
diff --git a/syweb/webclient/recents/recents-controller.js b/syweb/webclient/recents/recents-controller.js
index 78aeca128d..5c2cf270c3 100644
--- a/syweb/webclient/recents/recents-controller.js
+++ b/syweb/webclient/recents/recents-controller.js
@@ -26,34 +26,22 @@ angular.module('RecentsController', ['matrixService', 'matrixFilter'])
     // retrieve all rooms and expose them
     $scope.rooms = modelService.getRooms();
     
-    if (!$rootScope.unreadMessages) {
-        $rootScope.unreadMessages = {
-            // room_id: <number>
-        };
-    }
-    
+    // track the selected room ID: the html will use this
     $scope.recentsSelectedRoomID = recentsService.getSelectedRoomId();
     $scope.$on(recentsService.BROADCAST_SELECTED_ROOM_ID, function(ngEvent, room_id) {
         $scope.recentsSelectedRoomID = room_id;
     });
     
+    // track the list of unread messages: the html will use this
+    $scope.unreadMessages = recentsService.getUnreadMessages();
+    $scope.$on(recentsService.BROADCAST_UNREAD_MESSAGES, function(ngEvent, room_id, unreadCount) {
+        $scope.unreadMessages = recentsService.getUnreadMessages();
+    });
     
     $scope.selectRoom = function(room) {
-        if ($rootScope.unreadMessages[room.room_id]) {
-            $rootScope.unreadMessages[room.room_id] = 0;
-        }
+        recentsService.markAsRead(room.room_id);
         $rootScope.goToPage('room/' + (room.room_alias ? room.room_alias : room.room_id) );
     };
-    
-    $scope.$on(eventHandlerService.MSG_EVENT, function(ngEvent, event, isLive) {
-        if (isLive && event.room_id !== $scope.recentsSelectedRoomID) {
-            if (!$rootScope.unreadMessages[event.room_id]) {
-                $rootScope.unreadMessages[event.room_id] = 0;
-            }
-            $rootScope.unreadMessages[event.room_id] += 1;
-            console.log("sel="+$scope.recentsSelectedRoomID+" unread:"+JSON.stringify($rootScope.unreadMessages, undefined, 2));
-        }
-    });
 
 }]);