2014-08-27 15:50:07 +02:00
|
|
|
/*
|
2014-09-03 18:29:13 +02:00
|
|
|
Copyright 2014 OpenMarket Ltd
|
2014-08-27 15:50:07 +02:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
angular.module('RecentsController')
|
2014-09-15 18:22:38 +02:00
|
|
|
.filter('orderRecents', ["matrixService", "eventHandlerService", function(matrixService, eventHandlerService) {
|
2014-08-27 15:50:07 +02:00
|
|
|
return function(rooms) {
|
2014-09-15 18:22:38 +02:00
|
|
|
var user_id = matrixService.config().user_id;
|
|
|
|
|
2014-08-27 15:50:07 +02:00
|
|
|
// Transform the dict into an array
|
|
|
|
// The key, room_id, is already in value objects
|
|
|
|
var filtered = [];
|
2014-09-15 16:31:59 +02:00
|
|
|
angular.forEach(rooms, function(room, room_id) {
|
2014-11-03 12:44:39 +01:00
|
|
|
room.recent = {};
|
|
|
|
var meEvent = room.current_room_state.state("m.room.member", user_id);
|
2014-09-15 18:22:38 +02:00
|
|
|
// Show the room only if the user has joined it or has been invited
|
|
|
|
// (ie, do not show it if he has been banned)
|
|
|
|
var member = eventHandlerService.getMember(room_id, user_id);
|
2014-11-03 12:44:39 +01:00
|
|
|
room.recent.me = member;
|
2014-09-15 18:22:38 +02:00
|
|
|
if (member && ("invite" === member.membership || "join" === member.membership)) {
|
2014-11-03 12:44:39 +01:00
|
|
|
if ("invite" === member.membership) {
|
|
|
|
room.recent.inviter = member.user_id;
|
|
|
|
}
|
2014-09-15 18:22:38 +02:00
|
|
|
// Count users here
|
|
|
|
// TODO: Compute it directly in eventHandlerService
|
2014-11-03 12:44:39 +01:00
|
|
|
room.recent.numUsersInRoom = eventHandlerService.getUsersCountInRoom(room_id);
|
2014-09-18 15:19:35 +02:00
|
|
|
|
|
|
|
filtered.push(room);
|
2014-09-15 18:22:38 +02:00
|
|
|
}
|
2014-11-03 12:44:39 +01:00
|
|
|
else if (meEvent && "invite" === meEvent.content.membership) {
|
2014-09-18 17:34:13 +02:00
|
|
|
// The only information we have about the room is that the user has been invited
|
|
|
|
filtered.push(room);
|
|
|
|
}
|
2014-11-03 12:44:39 +01:00
|
|
|
else {
|
|
|
|
console.error("Dropping "+room.room_id);
|
|
|
|
}
|
|
|
|
|
2014-08-27 15:50:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// And time sort them
|
|
|
|
// The room with the lastest message at first
|
2014-09-15 16:31:59 +02:00
|
|
|
filtered.sort(function (roomA, roomB) {
|
|
|
|
|
2014-09-15 17:31:07 +02:00
|
|
|
var lastMsgRoomA = eventHandlerService.getLastMessage(roomA.room_id, true);
|
|
|
|
var lastMsgRoomB = eventHandlerService.getLastMessage(roomB.room_id, true);
|
2014-09-15 16:31:59 +02:00
|
|
|
|
2014-08-27 15:50:07 +02:00
|
|
|
// Invite message does not have a body message nor ts
|
|
|
|
// Puth them at the top of the list
|
2014-09-15 16:31:59 +02:00
|
|
|
if (undefined === lastMsgRoomA) {
|
2014-08-27 15:50:07 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2014-09-15 16:31:59 +02:00
|
|
|
else if (undefined === lastMsgRoomB) {
|
2014-08-27 15:50:07 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else {
|
2014-10-18 00:53:24 +02:00
|
|
|
return lastMsgRoomB.origin_server_ts - lastMsgRoomA.origin_server_ts;
|
2014-08-27 15:50:07 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
return filtered;
|
|
|
|
};
|
2014-10-18 00:53:24 +02:00
|
|
|
}]);
|