summary refs log tree commit diff
path: root/webclient/components
diff options
context:
space:
mode:
Diffstat (limited to 'webclient/components')
-rw-r--r--webclient/components/matrix/event-handler-service.js275
-rw-r--r--webclient/components/matrix/model-service.js11
2 files changed, 57 insertions, 229 deletions
diff --git a/webclient/components/matrix/event-handler-service.js b/webclient/components/matrix/event-handler-service.js
index 9d33b6715e..00e3edf184 100644
--- a/webclient/components/matrix/event-handler-service.js
+++ b/webclient/components/matrix/event-handler-service.js
@@ -50,57 +50,28 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
 
     var reset = function() {
         initialSyncDeferred = $q.defer();
-
-        $rootScope.events = {
-            rooms: {} // will contain roomId: { messages:[], members:{userid1: event} }
-        };
-
+        
         $rootScope.presence = {};
 
         eventMap = {};
     };
     reset();
 
-    var initRoom = function(room_id, room) {
-        if (!(room_id in $rootScope.events.rooms)) {
-            console.log("Creating new rooms entry for " + room_id);
-            $rootScope.events.rooms[room_id] = {
-                room_id: room_id,
-                messages: [],
-                members: {},
-                // Pagination information
-                pagination: {
-                    earliest_token: "END"   // how far back we've paginated
-                }
-            };
-        }
-
-        if (room) { // we got an existing room object from initialsync, seemingly.
-            // Report all other metadata of the room object (membership, inviter, visibility, ...)
-            for (var field in room) {
-                if (!room.hasOwnProperty(field)) continue;
-
-                if (-1 === ["room_id", "messages", "state"].indexOf(field)) { // why indexOf - why not ===? --Matthew
-                    $rootScope.events.rooms[room_id][field] = room[field];
-                }
-            }
-            $rootScope.events.rooms[room_id].membership = room.membership;
-        }
-    };
-
     var resetRoomMessages = function(room_id) {
-        if ($rootScope.events.rooms[room_id]) {
-            $rootScope.events.rooms[room_id].messages = [];
-        }
+        var room = modelService.getRoom(room_id);
+        room.events = [];
     };
     
     // Generic method to handle events data
-    var handleRoomDateEvent = function(event, isLiveEvent, addToRoomMessages) {
+    var handleRoomStateEvent = function(event, isLiveEvent, addToRoomMessages) {
         var __room = modelService.getRoom(event.room_id);
         if (addToRoomMessages) {
+            // some state events are displayed as messages, so add them.
             __room.addMessageEvent(event, !isLiveEvent);
         }
+        
         if (isLiveEvent) {
+            // update the current room state with the latest state
             __room.current_room_state.storeStateEvent(event);
         }
         else {
@@ -113,39 +84,10 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
                 }
             }
         }
-    
-        // =====================================
-    
-        // Add topic changes as if they were a room message
-        if (addToRoomMessages) {
-            if (isLiveEvent) {
-                $rootScope.events.rooms[event.room_id].messages.push(event);
-            }
-            else {
-                $rootScope.events.rooms[event.room_id].messages.unshift(event);
-            }
-        }
-
-        // live events always update, but non-live events only update if the
-        // ts is later.
-        var latestData = true;
-        if (!isLiveEvent) {
-            var eventTs = event.origin_server_ts;
-            var storedEvent = $rootScope.events.rooms[event.room_id][event.type];
-            if (storedEvent) {
-                if (storedEvent.origin_server_ts > eventTs) {
-                    // ignore it, we have a newer one already.
-                    latestData = false;
-                }
-            }
-        }
-        if (latestData) {
-            $rootScope.events.rooms[event.room_id][event.type] = event;         
-        }
+        // TODO: handle old_room_state
     };
     
     var handleRoomCreate = function(event, isLiveEvent) {
-        // For now, we do not use the event data. Simply signal it to the app controllers
         $rootScope.$broadcast(ROOM_CREATE_EVENT, event, isLiveEvent);
     };
 
@@ -183,7 +125,7 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
             
             if (shouldBing && isIdle) {
                 console.log("Displaying notification for "+JSON.stringify(event));
-                var member = getMember(event.room_id, event.user_id);
+                var member = modelService.getMember(event.room_id, event.user_id);
                 var displayname = getUserDisplayName(event.room_id, event.user_id);
 
                 var message = event.content.body;
@@ -195,9 +137,9 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
                 }
 
                 var roomTitle = matrixService.getRoomIdToAliasMapping(event.room_id);
-                var theRoom = $rootScope.events.rooms[event.room_id];
-                if (!roomTitle && theRoom && theRoom["m.room.name"] && theRoom["m.room.name"].content) {
-                    roomTitle = theRoom["m.room.name"].content.name;
+                var theRoom = modelService.getRoom(event.room_id);
+                if (!roomTitle && theRoom.current_room_state.state("m.room.name") && theRoom.current_room_state.state("m.room.name").content) {
+                    roomTitle = theRoom.current_room_state.state("m.room.name").content.name;
                 }
 
                 if (!roomTitle) {
@@ -235,6 +177,7 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         
         if (event.user_id !== matrixService.config().user_id) {
             __room.addMessageEvent(event, !isLiveEvent);
+            displayNotification(event);
         }
         else {
             // we may have locally echoed this, so we should replace the event
@@ -242,37 +185,8 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
             __room.addOrReplaceMessageEvent(event, !isLiveEvent);
         }
         
-        // =======================
-
-        if (isLiveEvent) {
-            if (event.user_id === matrixService.config().user_id &&
-                (event.content.msgtype === "m.text" || event.content.msgtype === "m.emote") ) {
-                // Assume we've already echoed it. So, there is a fake event in the messages list of the room
-                // Replace this fake event by the true one
-                var index = getRoomEventIndex(event.room_id, event.event_id);
-                if (index) {
-                    $rootScope.events.rooms[event.room_id].messages[index] = event;
-                }
-                else {
-                    $rootScope.events.rooms[event.room_id].messages.push(event);
-                }
-            }
-            else {
-                $rootScope.events.rooms[event.room_id].messages.push(event);
-                displayNotification(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.
         $rootScope.$broadcast(MSG_EVENT, event, isLiveEvent);
     };
     
@@ -284,6 +198,7 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         // Exception: Do not do this if the event is a room state event because such events already come
         // as room messages events. Moreover, when they come as room messages events, they are relatively ordered
         // with other other room messages
+        // FIXME this isn't true anymore, work out what this is doing.
         if (!isStateEvent) {
             // could be a membership change, display name change, etc.
             // Find out which one.
@@ -301,14 +216,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
             // If there was a change we want to display, dump it in the message
             // list.
             if (memberChanges) {
-                if (isLiveEvent) {
-                    $rootScope.events.rooms[event.room_id].messages.push(event);
-                }
-                else {
-                    $rootScope.events.rooms[event.room_id].messages.unshift(event);
-                }
-                // ============
-                
                 __room.addMessageEvent(event, !isLiveEvent);
             }
         }
@@ -316,7 +223,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         // Use data from state event or the latest data from the stream.
         // Do not care of events that come when paginating back
         if (isStateEvent || isLiveEvent) {
-            $rootScope.events.rooms[event.room_id].members[event.state_key] = event;
             __room.current_room_state.members[event.state_key] = event;
         }
         
@@ -329,30 +235,28 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
     };
     
     var handlePowerLevels = function(event, isLiveEvent) {
-        // Keep the latest data. Do not care of events that come when paginating back
-        if (!$rootScope.events.rooms[event.room_id][event.type] || isLiveEvent) {
-            $rootScope.events.rooms[event.room_id][event.type] = event;
-            $rootScope.$broadcast(POWERLEVEL_EVENT, event, isLiveEvent);   
-        }
+        handleRoomStateEvent(event, isLiveEvent);
+        $rootScope.$broadcast(POWERLEVEL_EVENT, event, isLiveEvent);   
     };
 
     var handleRoomName = function(event, isLiveEvent, isStateEvent) {
         console.log("handleRoomName room_id: " + event.room_id + " - isLiveEvent: " + isLiveEvent + " - name: " + event.content.name);
-        handleRoomDateEvent(event, isLiveEvent, !isStateEvent);
+        handleRoomStateEvent(event, isLiveEvent, !isStateEvent);
         $rootScope.$broadcast(NAME_EVENT, event, isLiveEvent);
     };
     
 
     var handleRoomTopic = function(event, isLiveEvent, isStateEvent) {
         console.log("handleRoomTopic room_id: " + event.room_id + " - isLiveEvent: " + isLiveEvent + " - topic: " + event.content.topic);
-        handleRoomDateEvent(event, isLiveEvent, !isStateEvent);
+        handleRoomStateEvent(event, isLiveEvent, !isStateEvent);
         $rootScope.$broadcast(TOPIC_EVENT, event, isLiveEvent);
     };
 
     var handleCallEvent = function(event, isLiveEvent) {
         $rootScope.$broadcast(CALL_EVENT, event, isLiveEvent);
         if (event.type === 'm.call.invite') {
-            $rootScope.events.rooms[event.room_id].messages.push(event);
+            var __room = modelService.getRoom(event.room_id);
+            __room.addMessageEvent(event, !isLiveEvent);
         }
     };
 
@@ -366,8 +270,9 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         // we need to remove something possibly: do we know the redacted
         // event ID?
         if (eventMap[event.redacts]) {
+            var __room = modelService.getRoom(event.room_id);
             // remove event from list of messages in this room.
-            var eventList = $rootScope.events.rooms[event.room_id].messages;
+            var eventList = __room.events;
             for (var i=0; i<eventList.length; i++) {
                 if (eventList[i].event_id === event.redacts) {
                     console.log("Removing event " + event.redacts);
@@ -376,50 +281,9 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
                 }
             }
 
-            // broadcast the redaction so controllers can nuke this
             console.log("Redacted an event.");
         }
     }
-    
-    /**
-     * Get the index of the event in $rootScope.events.rooms[room_id].messages
-     * @param {type} room_id the room id
-     * @param {type} event_id the event id to look for
-     * @returns {Number | undefined} the index. undefined if not found.
-     */
-    var getRoomEventIndex = function(room_id, event_id) {
-        var index;
-
-        var room = $rootScope.events.rooms[room_id];
-        if (room) {
-            // Start looking from the tail since the first goal of this function 
-            // is to find a messaged among the latest ones
-            for (var i = room.messages.length - 1; i > 0; i--) {
-                var message = room.messages[i];
-                if (event_id === message.event_id) {
-                    index = i;
-                    break;
-                }
-            }
-        }
-        return index;
-    };
-    
-    /**
-     * Get the member object of a room member
-     * @param {String} room_id the room id
-     * @param {String} user_id the id of the user
-     * @returns {undefined | Object} the member object of this user in this room if he is part of the room
-     */
-    var getMember = function(room_id, user_id) {
-        var member;
-
-        var room = $rootScope.events.rooms[room_id];
-        if (room) {
-            member = room.members[user_id];
-        }
-        return member;
-    };
 
     /**
      * Return the display name of an user acccording to data already downloaded
@@ -431,17 +295,17 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         var displayName;
 
         // Get the user display name from the member list of the room
-        var member = getMember(room_id, user_id);
+        var member = modelService.getMember(room_id, user_id);
         if (member && member.content.displayname) { // Do not consider null displayname
             displayName = member.content.displayname;
 
             // Disambiguate users who have the same displayname in the room
             if (user_id !== matrixService.config().user_id) {
-                var room = $rootScope.events.rooms[room_id];
+                var room = modelService.getRoom(room_id);
 
-                for (var member_id in room.members) {
-                    if (room.members.hasOwnProperty(member_id) && member_id !== user_id) {
-                        var member2 = room.members[member_id];
+                for (var member_id in room.current_room_state.members) {
+                    if (room.current_room_state.members.hasOwnProperty(member_id) && member_id !== user_id) {
+                        var member2 = room.current_room_state.members[member_id];
                         if (member2.content.displayname && member2.content.displayname === displayName) {
                             displayName = displayName + " (" + user_id + ")";
                             break;
@@ -479,19 +343,9 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
             reset();
             $rootScope.$broadcast(RESET_EVENT);
         },
-        
-        initRoom: function(room) {
-            initRoom(room.room_id, room);
-        },
     
         handleEvent: function(event, isLiveEvent, isStateEvent) {
 
-            // FIXME: /initialSync on a particular room is not yet available
-            // So initRoom on a new room is not called. Make sure the room data is initialised here
-            if (event.room_id) {
-                initRoom(event.room_id);
-            }
-
             // Avoid duplicated events
             // Needed for rooms where initialSync has not been done. 
             // In this case, we do not know where to start pagination. So, it starts from the END
@@ -550,7 +404,7 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
                         // displays on the Room Info screen.
                         if (typeof(event.state_key) === "string") { // incls. 0-len strings
                             if (event.room_id) {
-                                handleRoomDateEvent(event, isLiveEvent, false);
+                                handleRoomStateEvent(event, isLiveEvent, false);
                             }
                         }
                         console.log("Unable to handle event type " + event.type);
@@ -570,8 +424,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
 
         // Handle messages from /initialSync or /messages
         handleRoomMessages: function(room_id, messages, isLiveEvents, dir) {
-            initRoom(room_id);
-
             var events = messages.chunk;
 
             // Handles messages according to their time order
@@ -582,8 +434,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
                 }
                 
                 // Store how far back we've paginated
-                $rootScope.events.rooms[room_id].pagination.earliest_token = messages.end;
-                
                 var __room = modelService.getRoom(room_id);
                 __room.old_room_state.pagination_token = messages.end;
                 
@@ -594,8 +444,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
                     this.handleEvent(events[i], isLiveEvents, isLiveEvents);
                 }
                 // Store where to start pagination
-                $rootScope.events.rooms[room_id].pagination.earliest_token = messages.start;
-                
                 var __room = modelService.getRoom(room_id);
                 __room.old_room_state.pagination_token = messages.start;
             }
@@ -607,8 +455,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
             var rooms = response.data.rooms;
             for (var i = 0; i < rooms.length; ++i) {
                 var room = rooms[i];
-
-                this.initRoom(room);
                 
                 // FIXME: This is ming: the HS should be sending down the m.room.member
                 // event for the invite in .state but it isn't, so fudge it for now.
@@ -636,9 +482,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
                 var __room = modelService.getRoom(room.room_id);
                 __room.current_room_state.storeStateEvents(room.state);
                 __room.old_room_state.storeStateEvents(room.state);
-                
-                
-
 
                 if ("messages" in room) {
                     this.handleRoomMessages(room.room_id, room.messages, false);
@@ -674,15 +517,13 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         getLastMessage: function(room_id, filterEcho) {
             var lastMessage;
 
-            var room = $rootScope.events.rooms[room_id];
-            if (room) {
-                for (var i = room.messages.length - 1; i >= 0; i--) {
-                    var message = room.messages[i];
+            var events = modelService.getRoom(room_id).events;
+            for (var i = events.length - 1; i >= 0; i--) {
+                var message = events[i];
 
-                    if (!filterEcho || undefined === message.echo_msg_state) {
-                        lastMessage = message;
-                        break;
-                    }
+                if (!filterEcho || undefined === message.echo_msg_state) {
+                    lastMessage = message;
+                    break;
                 }
             }
 
@@ -697,18 +538,16 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         getUsersCountInRoom: function(room_id) {
             var memberCount;
 
-            var room = $rootScope.events.rooms[room_id];
-            if (room) {
-                memberCount = 0;
+            var room = modelService.getRoom(room_id);
+            memberCount = 0;
 
-                for (var i in room.members) {
-                    if (!room.members.hasOwnProperty(i)) continue;
+            for (var i in room.current_room_state.members) {
+                if (!room.current_room_state.members.hasOwnProperty(i)) continue;
 
-                    var member = room.members[i];
+                var member = room.current_room_state.members[i];
 
-                    if ("join" === member.membership) {
-                        memberCount = memberCount + 1;
-                    }
+                if ("join" === member.membership) {
+                    memberCount = memberCount + 1;
                 }
             }
 
@@ -716,16 +555,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
         },
         
         /**
-         * Get the member object of a room member
-         * @param {String} room_id the room id
-         * @param {String} user_id the id of the user
-         * @returns {undefined | Object} the member object of this user in this room if he is part of the room
-         */
-        getMember: function(room_id, user_id) {
-            return getMember(room_id, user_id);
-        },
-        
-        /**
          * Return the power level of an user in a particular room
          * @param {String} room_id the room id
          * @param {String} user_id the user id
@@ -733,14 +562,14 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
          */
         getUserPowerLevel: function(room_id, user_id) {
             var powerLevel = 0;
-            var room = $rootScope.events.rooms[room_id];
-            if (room && room["m.room.power_levels"]) {
-                if (user_id in room["m.room.power_levels"].content) {
-                    powerLevel = room["m.room.power_levels"].content[user_id];
+            var room = modelService.getRoom(room_id).current_room_state;
+            if (room.state("m.room.power_levels")) {
+                if (user_id in room.state("m.room.power_levels").content) {
+                    powerLevel = room.state("m.room.power_levels").content[user_id];
                 }
                 else {
                     // Use the room default user power
-                    powerLevel = room["m.room.power_levels"].content["default"];
+                    powerLevel = room.state("m.room.power_levels").content["default"];
                 }
             }
             return powerLevel;
@@ -754,18 +583,6 @@ function(matrixService, $rootScope, $q, $timeout, mPresence, notificationService
          */
         getUserDisplayName: function(room_id, user_id) {
             return getUserDisplayName(room_id, user_id);
-        },
-
-        setRoomVisibility: function(room_id, visible) {
-            if (!visible) {
-                return;
-            }
-            initRoom(room_id);
-            
-            var room = $rootScope.events.rooms[room_id];
-            if (room) {
-                room.visibility = visible;
-            }
         }
     };
 }]);
diff --git a/webclient/components/matrix/model-service.js b/webclient/components/matrix/model-service.js
index ff6abfe422..d5acd7eda9 100644
--- a/webclient/components/matrix/model-service.js
+++ b/webclient/components/matrix/model-service.js
@@ -148,6 +148,17 @@ angular.module('modelService', [])
         
         getRooms: function() {
             return rooms;
+        },
+        
+        /**
+         * Get the member object of a room member
+         * @param {String} room_id the room id
+         * @param {String} user_id the id of the user
+         * @returns {undefined | Object} the member object of this user in this room if he is part of the room
+         */
+        getMember: function(room_id, user_id) {
+            var room = this.getRoom(room_id);
+            return room.current_room_state.members[user_id];
         }
     
     };