From 8aa4b7bf7fdc31b3a146fe8fdc07922a4bfb1f78 Mon Sep 17 00:00:00 2001 From: Emmanuel ROHEE Date: Mon, 15 Sep 2014 17:31:07 +0200 Subject: [PATCH] Recents must not show temporary fake messages --- .../matrix/event-handler-service.js | 24 +++++++++++++++++++ webclient/recents/recents-controller.js | 7 ++++-- webclient/recents/recents-filter.js | 9 ++----- webclient/recents/recents.html | 2 +- 4 files changed, 32 insertions(+), 10 deletions(-) diff --git a/webclient/components/matrix/event-handler-service.js b/webclient/components/matrix/event-handler-service.js index a2c807b3f..6fd77c4f2 100644 --- a/webclient/components/matrix/event-handler-service.js +++ b/webclient/components/matrix/event-handler-service.js @@ -352,6 +352,30 @@ angular.module('eventHandlerService', []) resetRoomMessages(room_id); }, + /** + * Return the last message event of a room + * @param {String} room_id the room id + * @param {Boolean} filterFake true to not take into account fake messages + * @returns {undefined | Event} the last message event if available + */ + 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]; + + if (!filterEcho || undefined === message.echo_msg_state) { + lastMessage = message; + break; + } + } + } + + return lastMessage; + }, + /** * Compute the room users number, ie the number of members who has joined the room. * @param {String} room_id the room id diff --git a/webclient/recents/recents-controller.js b/webclient/recents/recents-controller.js index 2006f13a5..ee8a41c36 100644 --- a/webclient/recents/recents-controller.js +++ b/webclient/recents/recents-controller.js @@ -17,8 +17,11 @@ 'use strict'; angular.module('RecentsController', ['matrixService', 'matrixFilter']) -.controller('RecentsController', ['$rootScope', - function($rootScope) { +.controller('RecentsController', ['$rootScope', '$scope', 'eventHandlerService', + function($rootScope, $scope, eventHandlerService) { + + // Expose the service to the view + $scope.eventHandlerService = eventHandlerService; // $rootScope of the parent where the recents component is included can override this value // in order to highlight a specific room in the list diff --git a/webclient/recents/recents-filter.js b/webclient/recents/recents-filter.js index e8a706a5d..67fe49d4b 100644 --- a/webclient/recents/recents-filter.js +++ b/webclient/recents/recents-filter.js @@ -35,14 +35,9 @@ angular.module('RecentsController') // And time sort them // The room with the lastest message at first filtered.sort(function (roomA, roomB) { - var lastMsgRoomA, lastMsgRoomB; - if (roomA.messages && 0 < roomA.messages.length) { - lastMsgRoomA = roomA.messages[roomA.messages.length - 1]; - } - if (roomB.messages && 0 < roomB.messages.length) { - lastMsgRoomB = roomB.messages[roomB.messages.length - 1]; - } + var lastMsgRoomA = eventHandlerService.getLastMessage(roomA.room_id, true); + var lastMsgRoomB = eventHandlerService.getLastMessage(roomB.room_id, true); // Invite message does not have a body message nor ts // Puth them at the top of the list diff --git a/webclient/recents/recents.html b/webclient/recents/recents.html index eb9e269a4..789ffc9d2 100644 --- a/webclient/recents/recents.html +++ b/webclient/recents/recents.html @@ -16,7 +16,7 @@ - {{lastMsg = room.messages[room.messages.length - 1];""}} + {{ lastMsg = eventHandlerService.getLastMessage(room.room_id, true);"" }} {{ (lastMsg.ts) | date:'MMM d HH:mm' }}