2014-08-27 09:09:16 -04:00
|
|
|
/*
|
2014-09-03 12:29:13 -04:00
|
|
|
Copyright 2014 OpenMarket Ltd
|
2014-08-27 09:09:16 -04: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';
|
|
|
|
|
2014-09-04 05:19:28 -04:00
|
|
|
angular.module('RecentsController', ['matrixService', 'matrixFilter', 'eventHandlerService'])
|
2014-09-08 12:13:22 -04:00
|
|
|
.controller('RecentsController', ['$rootScope', '$scope', 'matrixService', 'eventHandlerService',
|
|
|
|
function($rootScope, $scope, matrixService, eventHandlerService) {
|
|
|
|
|
|
|
|
// FIXME: Angularjs reloads the controller (and resets its $scope) each time
|
|
|
|
// the page URL changes, use $rootScope to avoid to have to reload data
|
|
|
|
$rootScope.rooms;
|
2014-08-27 11:20:41 -04:00
|
|
|
|
2014-09-08 12:13:22 -04:00
|
|
|
// $rootScope of the parent where the recents component is included can override this value
|
2014-08-27 11:20:41 -04:00
|
|
|
// in order to highlight a specific room in the list
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.recentsSelectedRoomID;
|
2014-09-08 16:54:09 -04:00
|
|
|
|
2014-08-28 10:38:00 -04:00
|
|
|
var listenToEventStream = function() {
|
|
|
|
// Refresh the list on matrix invitation and message event
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.$on(eventHandlerService.MEMBER_EVENT, function(ngEvent, event, isLive) {
|
2014-09-04 05:03:49 -04:00
|
|
|
if (isLive) {
|
2014-09-09 10:10:07 -04:00
|
|
|
if (!$rootScope.rooms[event.room_id]) {
|
|
|
|
// The user has joined a new room, which we do not have data yet. The reason is that
|
|
|
|
// the room has appeared in the scope of the user rooms after the global initialSync
|
|
|
|
// FIXME: an initialSync on this specific room should be done
|
|
|
|
$rootScope.rooms[event.room_id] = {
|
|
|
|
room_id:event.room_id
|
|
|
|
};
|
|
|
|
}
|
2014-09-09 10:31:50 -04:00
|
|
|
else if (event.state_key === matrixService.config().user_id && "invite" !== event.membership && "join" !== event.membership) {
|
|
|
|
// The user has been kicked or banned from the room, remove this room from the recents
|
|
|
|
delete $rootScope.rooms[event.room_id];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($rootScope.rooms[event.room_id]) {
|
|
|
|
$rootScope.rooms[event.room_id].lastMsg = event;
|
|
|
|
}
|
2014-09-10 10:46:06 -04:00
|
|
|
|
|
|
|
// Update room users count
|
|
|
|
$rootScope.rooms[event.room_id].numUsersInRoom = getUsersCountInRoom(event.room_id);
|
2014-08-28 10:38:00 -04:00
|
|
|
}
|
|
|
|
});
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.$on(eventHandlerService.MSG_EVENT, function(ngEvent, event, isLive) {
|
2014-08-29 03:55:47 -04:00
|
|
|
if (isLive) {
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.rooms[event.room_id].lastMsg = event;
|
2014-08-29 03:55:47 -04:00
|
|
|
}
|
2014-08-28 10:38:00 -04:00
|
|
|
});
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.$on(eventHandlerService.CALL_EVENT, function(ngEvent, event, isLive) {
|
2014-09-01 10:53:07 -04:00
|
|
|
if (isLive) {
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.rooms[event.room_id].lastMsg = event;
|
2014-09-01 10:53:07 -04:00
|
|
|
}
|
|
|
|
});
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.$on(eventHandlerService.ROOM_CREATE_EVENT, function(ngEvent, event, isLive) {
|
2014-09-02 07:55:14 -04:00
|
|
|
if (isLive) {
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.rooms[event.room_id] = event;
|
2014-09-02 07:55:14 -04:00
|
|
|
}
|
|
|
|
});
|
2014-09-11 05:31:24 -04:00
|
|
|
$rootScope.$on(eventHandlerService.TOPIC_EVENT, function(ngEvent, event, isLive) {
|
|
|
|
if (isLive) {
|
|
|
|
$rootScope.rooms[event.room_id].lastMsg = event;
|
|
|
|
}
|
|
|
|
});
|
2014-08-28 10:38:00 -04:00
|
|
|
};
|
2014-08-27 09:09:16 -04:00
|
|
|
|
2014-09-10 10:46:06 -04:00
|
|
|
/**
|
|
|
|
* Compute the room users number, ie the number of members who has joined the room.
|
|
|
|
* @param {String} room_id the room id
|
|
|
|
* @returns {undefined | Number} the room users number if available
|
|
|
|
*/
|
|
|
|
var getUsersCountInRoom = function(room_id) {
|
|
|
|
var memberCount;
|
|
|
|
|
|
|
|
var room = $rootScope.events.rooms[room_id];
|
|
|
|
if (room) {
|
|
|
|
memberCount = 0;
|
|
|
|
|
|
|
|
for (var i in room.members) {
|
|
|
|
var member = room.members[i];
|
|
|
|
|
|
|
|
if ("join" === member.membership) {
|
|
|
|
memberCount = memberCount + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return memberCount;
|
|
|
|
}
|
2014-08-27 12:52:02 -04:00
|
|
|
|
2014-09-08 12:13:22 -04:00
|
|
|
$scope.onInit = function() {
|
|
|
|
// Init recents list only once
|
|
|
|
if ($rootScope.rooms) {
|
|
|
|
return;
|
|
|
|
}
|
2014-09-08 16:54:09 -04:00
|
|
|
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.rooms = {};
|
|
|
|
|
|
|
|
// Use initialSync data to init the recents list
|
|
|
|
eventHandlerService.waitForInitialSyncCompletion().then(
|
|
|
|
function(initialSyncData) {
|
|
|
|
|
|
|
|
var rooms = initialSyncData.data.rooms;
|
2014-08-28 11:48:55 -04:00
|
|
|
for (var i=0; i<rooms.length; i++) {
|
|
|
|
var room = rooms[i];
|
|
|
|
|
|
|
|
// Add room_alias & room_display_name members
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.rooms[room.room_id] = angular.extend(room, matrixService.getRoomAliasAndDisplayName(room));
|
2014-08-27 09:09:16 -04:00
|
|
|
|
|
|
|
// Create a shortcut for the last message of this room
|
2014-08-28 11:48:55 -04:00
|
|
|
if (room.messages && room.messages.chunk && room.messages.chunk[0]) {
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.rooms[room.room_id].lastMsg = room.messages.chunk[0];
|
2014-08-27 09:09:16 -04:00
|
|
|
}
|
2014-09-08 14:55:19 -04:00
|
|
|
|
2014-09-10 10:46:06 -04:00
|
|
|
$rootScope.rooms[room.room_id].numUsersInRoom = getUsersCountInRoom(room.room_id);
|
2014-08-27 09:09:16 -04:00
|
|
|
}
|
|
|
|
|
2014-08-28 10:38:00 -04:00
|
|
|
// From now, update recents from the stream
|
|
|
|
listenToEventStream();
|
2014-08-27 09:09:16 -04:00
|
|
|
},
|
|
|
|
function(error) {
|
2014-09-08 12:13:22 -04:00
|
|
|
$rootScope.feedback = "Failure: " + error.data;
|
2014-08-27 09:09:16 -04:00
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
}]);
|
|
|
|
|