@ -17,62 +17,68 @@
'use strict' ;
angular . module ( 'RecentsController' , [ 'matrixService' , 'matrixFilter' , 'eventHandlerService' ] )
. controller ( 'RecentsController' , [ '$scope' , 'matrixService' , 'eventHandlerService' ,
function ( $scope , matrixService , eventHandlerService ) {
$scope . rooms = { } ;
. controller ( 'RecentsController' , [ '$rootScope' , '$scope' , 'matrixService' , 'eventHandlerService' ,
function ( $rootScope , $scope , matrixService , eventHandlerService ) {
// $scope of the parent where the recents component is included can override this value
// 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 ;
// $rootScope of the parent where the recents component is included can override this value
// in order to highlight a specific room in the list
$scope . recentsSelectedRoomID ;
$rootS cope . recentsSelectedRoomID ;
var listenToEventStream = function ( ) {
// Refresh the list on matrix invitation and message event
$s cope . $on ( eventHandlerService . MEMBER _EVENT , function ( ngEvent , event , isLive ) {
$rootS cope . $on ( eventHandlerService . MEMBER _EVENT , function ( ngEvent , event , isLive ) {
if ( isLive ) {
$s cope . rooms [ event . room _id ] . lastMsg = event ;
$rootS cope . rooms [ event . room _id ] . lastMsg = event ;
}
} ) ;
$s cope . $on ( eventHandlerService . MSG _EVENT , function ( ngEvent , event , isLive ) {
$rootS cope . $on ( eventHandlerService . MSG _EVENT , function ( ngEvent , event , isLive ) {
if ( isLive ) {
$s cope . rooms [ event . room _id ] . lastMsg = event ;
$rootS cope . rooms [ event . room _id ] . lastMsg = event ;
}
} ) ;
$s cope . $on ( eventHandlerService . CALL _EVENT , function ( ngEvent , event , isLive ) {
$rootS cope . $on ( eventHandlerService . CALL _EVENT , function ( ngEvent , event , isLive ) {
if ( isLive ) {
$s cope . rooms [ event . room _id ] . lastMsg = event ;
$rootS cope . rooms [ event . room _id ] . lastMsg = event ;
}
} ) ;
$s cope . $on ( eventHandlerService . ROOM _CREATE _EVENT , function ( ngEvent , event , isLive ) {
$rootS cope . $on ( eventHandlerService . ROOM _CREATE _EVENT , function ( ngEvent , event , isLive ) {
if ( isLive ) {
$s cope . rooms [ event . room _id ] = event ;
$rootS cope . rooms [ event . room _id ] = event ;
}
} ) ;
} ;
var refresh = function ( ) {
// List all rooms joined or been invited to
// TODO: This is a pity that event-stream-service.js makes the same call
// We should be able to reuse event-stream-service.js fetched data
matrixService . rooms ( 1 , false ) . then (
function ( response ) {
// Reset data
$scope . rooms = { } ;
$scope . onInit = function ( ) {
// Init recents list only once
if ( $rootScope . rooms ) {
return ;
}
$rootScope . rooms = { } ;
// Use initialSync data to init the recents list
eventHandlerService . waitForInitialSyncCompletion ( ) . then (
function ( initialSyncData ) {
var rooms = response . data . rooms ;
var rooms = initialSyncData . data . rooms ;
for ( var i = 0 ; i < rooms . length ; i ++ ) {
var room = rooms [ i ] ;
// Add room_alias & room_display_name members
$s cope . rooms [ room . room _id ] = angular . extend ( room , matrixService . getRoomAliasAndDisplayName ( room ) ) ;
$rootS cope . rooms [ room . room _id ] = angular . extend ( room , matrixService . getRoomAliasAndDisplayName ( room ) ) ;
// Create a shortcut for the last message of this room
if ( room . messages && room . messages . chunk && room . messages . chunk [ 0 ] ) {
$s cope . rooms [ room . room _id ] . lastMsg = room . messages . chunk [ 0 ] ;
$rootS cope . rooms [ room . room _id ] . lastMsg = room . messages . chunk [ 0 ] ;
}
}
var presence = response . data . presence ;
var presence = initialSyncData . data . presence ;
for ( var i = 0 ; i < presence . length ; ++ i ) {
eventHandlerService . handleEvent ( presence [ i ] , false ) ;
}
@ -81,16 +87,10 @@ angular.module('RecentsController', ['matrixService', 'matrixFilter', 'eventHand
listenToEventStream ( ) ;
} ,
function ( error ) {
$s cope . feedback = "Failure: " + error . data ;
$rootS cope . feedback = "Failure: " + error . data ;
}
) ;
} ;
$scope . onInit = function ( ) {
eventHandlerService . waitForInitialSyncCompletion ( ) . then ( function ( ) {
refresh ( ) ;
} ) ;
} ;
} ] ) ;