@ -118,8 +118,58 @@ angular.module('myApp.services', [])
} ;
} ;
} )
} )
. service ( 'AppUsersManager' , function ( $rootScope , $modal , MtpApiFileManager , MtpApiManager , RichTextProcessor ) {
. service ( 'AppUsersManager' , function ( $rootScope , $modal , $modalStack , MtpApiFileManager , MtpApiManager , RichTextProcessor , SearchIndexManager ) {
var users = { } ;
var users = { } ,
contactsFillPromise ,
contactsIndex = SearchIndexManager . createIndex ( ) ;
function fillContacts ( ) {
if ( contactsFillPromise ) {
return contactsFillPromise ;
}
return contactsFillPromise = MtpApiManager . invokeApi ( 'contacts.getContacts' , {
hash : ''
} ) . then ( function ( result ) {
var contactsList = [ ] ,
userID , searchText , i ;
saveApiUsers ( result . users ) ;
for ( var i = 0 ; i < result . contacts . length ; i ++ ) {
userID = result . contacts [ i ] . user _id ;
contactsList . push ( userID ) ;
SearchIndexManager . indexObject ( userID , getUserSearchText ( userID ) , contactsIndex ) ;
}
return contactsList ;
} ) ;
}
function getUserSearchText ( id ) {
var user = users [ id ] ;
if ( ! user ) {
return false ;
}
return ( user . first _name || '' ) + ' ' + ( user . last _name || '' ) + ' ' + ( user . phone || '' ) ;
}
function getContacts ( query ) {
return fillContacts ( ) . then ( function ( contactsList ) {
if ( angular . isString ( query ) && query . length ) {
var results = SearchIndexManager . search ( query , contactsIndex ) ,
filteredContactsList = [ ] ;
for ( var i = 0 ; i < contactsList . length ; i ++ ) {
if ( results [ contactsList [ i ] ] ) {
filteredContactsList . push ( contactsList [ i ] )
}
}
contactsList = filteredContactsList ;
}
return contactsList ;
} ) ;
} ;
function saveApiUsers ( apiUsers ) {
function saveApiUsers ( apiUsers ) {
angular . forEach ( apiUsers , saveApiUser ) ;
angular . forEach ( apiUsers , saveApiUser ) ;
@ -137,6 +187,9 @@ angular.module('myApp.services', [])
apiUser . rFirstName = RichTextProcessor . wrapRichText ( apiUser . last _name , { noLinks : true , noLinebreaks : true } ) || 'DELETED' ;
apiUser . rFirstName = RichTextProcessor . wrapRichText ( apiUser . last _name , { noLinks : true , noLinebreaks : true } ) || 'DELETED' ;
apiUser . rFullName = RichTextProcessor . wrapRichText ( apiUser . last _name , { noLinks : true , noLinebreaks : true } ) || 'DELETED' ;
apiUser . rFullName = RichTextProcessor . wrapRichText ( apiUser . last _name , { noLinks : true , noLinebreaks : true } ) || 'DELETED' ;
}
}
apiUser . sortName = $ . trim ( ( apiUser . last _name || '' ) + ' ' + apiUser . first _name ) ;
apiUser . sortStatus = apiUser . status && ( apiUser . status . expires || apiUser . status . was _online ) || 0 ;
if ( users [ apiUser . id ] === undefined ) {
if ( users [ apiUser . id ] === undefined ) {
users [ apiUser . id ] = apiUser ;
users [ apiUser . id ] = apiUser ;
@ -229,9 +282,11 @@ angular.module('myApp.services', [])
// console.log('on apiUpdate', update);
// console.log('on apiUpdate', update);
switch ( update . _ ) {
switch ( update . _ ) {
case 'updateUserStatus' :
case 'updateUserStatus' :
var userID = update . user _id ;
var userID = update . user _id ,
if ( users [ userID ] ) {
user = users [ userID ] ;
users [ userID ] . status = update . status ;
if ( user ) {
user . status = update . status ;
user . sortStatus = update . status && ( update . status . expires || update . status . was _online ) || 0 ;
$rootScope . $broadcast ( 'user_update' , userID ) ;
$rootScope . $broadcast ( 'user_update' , userID ) ;
}
}
break ;
break ;
@ -248,11 +303,13 @@ angular.module('myApp.services', [])
return {
return {
getContacts : getContacts ,
saveApiUsers : saveApiUsers ,
saveApiUsers : saveApiUsers ,
saveApiUser : saveApiUser ,
saveApiUser : saveApiUser ,
getUser : getUser ,
getUser : getUser ,
getUserPhoto : getUserPhoto ,
getUserPhoto : getUserPhoto ,
getUserString : getUserString ,
getUserString : getUserString ,
getUserSearchText : getUserSearchText ,
hasUser : hasUser ,
hasUser : hasUser ,
wrapForFull : wrapForFull ,
wrapForFull : wrapForFull ,
openUser : openUser
openUser : openUser
@ -379,8 +436,7 @@ angular.module('myApp.services', [])
getPeerSearchText : function ( peerID ) {
getPeerSearchText : function ( peerID ) {
var text ;
var text ;
if ( peerID > 0 ) {
if ( peerID > 0 ) {
var user = AppUsersManager . getUser ( peerID ) ;
text = AppUsersManager . getUserSearchText ( peerID ) ;
text = ( user . first _name || '' ) + ' ' + ( user . last _name || '' ) + ' ' + ( user . phone || '' ) ;
} else if ( peerID < 0 ) {
} else if ( peerID < 0 ) {
var chat = AppChatsManager . getChat ( - peerID ) ;
var chat = AppChatsManager . getChat ( - peerID ) ;
text = chat . title || '' ;
text = chat . title || '' ;
@ -463,6 +519,7 @@ angular.module('myApp.services', [])
}
}
function search ( query , searchIndex ) {
function search ( query , searchIndex ) {
console . time ( 'search' ) ;
var shortIndexes = searchIndex . shortIndexes ,
var shortIndexes = searchIndex . shortIndexes ,
fullTexts = searchIndex . fullTexts ;
fullTexts = searchIndex . fullTexts ;
@ -499,11 +556,12 @@ angular.module('myApp.services', [])
}
}
}
}
console . timeEnd ( 'search' ) ;
return newFoundObjs ;
return newFoundObjs ;
}
}
} )
} )
. service ( 'AppMessagesManager' , function ( $q , $rootScope , $filter , $sanitize , $ location , ApiUpdatesManager , AppUsersManager , AppChatsManager , AppPeersManager , AppPhotosManager , AppVideoManager , AppDocsManager , AppAudioManager , MtpApiManager , MtpApiFileManager , RichTextProcessor , NotificationsManager , SearchIndexManager ) {
. service ( 'AppMessagesManager' , function ( $q , $rootScope , $location , ApiUpdatesManager , AppUsersManager , AppChatsManager , AppPeersManager , AppPhotosManager , AppVideoManager , AppDocsManager , AppAudioManager , MtpApiManager , MtpApiFileManager , RichTextProcessor , NotificationsManager , SearchIndexManager ) {
var messagesStorage = { } ;
var messagesStorage = { } ;
var messagesForHistory = { } ;
var messagesForHistory = { } ;
@ -706,7 +764,7 @@ angular.module('myApp.services', [])
}
}
function readHistory ( inputPeer ) {
function readHistory ( inputPeer ) {
// console.log ('start read');
// console.trace ('start read');
var peerID = AppPeersManager . getPeerID ( inputPeer ) ,
var peerID = AppPeersManager . getPeerID ( inputPeer ) ,
historyStorage = historiesStorage [ peerID ] ,
historyStorage = historiesStorage [ peerID ] ,
foundDialog = getDialogByPeerID ( peerID ) ;
foundDialog = getDialogByPeerID ( peerID ) ;
@ -718,7 +776,8 @@ angular.module('myApp.services', [])
return false ;
return false ;
}
}
var wasUnread = false ;
var messageID ,
message ;
// console.log(historyStorage);
// console.log(historyStorage);
for ( i = 0 ; i < historyStorage . history . length ; i ++ ) {
for ( i = 0 ; i < historyStorage . history . length ; i ++ ) {
messageID = historyStorage . history [ i ] ;
messageID = historyStorage . history [ i ] ;
@ -727,11 +786,10 @@ angular.module('myApp.services', [])
if ( message && ! message . out ) {
if ( message && ! message . out ) {
if ( message . unread ) {
if ( message . unread ) {
// console.log('unread');
// console.log('unread');
wasUnread = true ;
break ;
} else if ( ! wasUnread ) {
// console.log('bad2');
return false ;
}
}
// console.log('bad2', message);
return false ;
}
}
}
}
@ -743,6 +801,7 @@ angular.module('myApp.services', [])
return processAffectedHistory ( inputPeer , affectedHistory , 'messages.readHistory' ) ;
return processAffectedHistory ( inputPeer , affectedHistory , 'messages.readHistory' ) ;
} ) . then ( function ( ) {
} ) . then ( function ( ) {
if ( foundDialog [ 0 ] ) {
if ( foundDialog [ 0 ] ) {
// console.log('done read history', peerID);
foundDialog [ 0 ] . unread _count = 0 ;
foundDialog [ 0 ] . unread _count = 0 ;
$rootScope . $broadcast ( 'dialog_unread' , { peerID : peerID , count : 0 } ) ;
$rootScope . $broadcast ( 'dialog_unread' , { peerID : peerID , count : 0 } ) ;
}
}
@ -768,7 +827,7 @@ angular.module('myApp.services', [])
}
}
function flushHistory ( inputPeer ) {
function flushHistory ( inputPeer ) {
// console.log('start read ');
// console.log('start flush ');
var peerID = AppPeersManager . getPeerID ( inputPeer ) ,
var peerID = AppPeersManager . getPeerID ( inputPeer ) ,
historyStorage = historiesStorage [ peerID ] ;
historyStorage = historiesStorage [ peerID ] ;
@ -1242,7 +1301,7 @@ angular.module('myApp.services', [])
}
}
$rootScope . $on ( 'apiUpdate' , function ( e , update ) {
$rootScope . $on ( 'apiUpdate' , function ( e , update ) {
console . log ( 'on apiUpdate' , update ) ;
// console.log('on apiUpdate', update);
switch ( update . _ ) {
switch ( update . _ ) {
case 'updateMessageID' :
case 'updateMessageID' :
pendingByMessageID [ update . id ] = update . random _id ;
pendingByMessageID [ update . id ] = update . random _id ;
@ -1295,10 +1354,13 @@ angular.module('myApp.services', [])
dialog = { peerID : peerID , unread _count : 0 , top _message : false }
dialog = { peerID : peerID , unread _count : 0 , top _message : false }
}
}
if ( ! message . out && message . unread ) {
if ( ! message . out && message . unread ) {
// console.log('inc unread count', dialog.unread_count);
dialog . unread _count ++ ;
dialog . unread _count ++ ;
}
}
dialog . top _message = message . id ;
dialog . top _message = message . id ;
// console.log('new message', message, peerID, historyStorage, foundDialog, dialog);
SearchIndexManager . indexObject ( peerID , AppPeersManager . getPeerSearchText ( peerID ) , dialogsIndex ) ;
SearchIndexManager . indexObject ( peerID , AppPeersManager . getPeerSearchText ( peerID ) , dialogsIndex ) ;
dialogsStorage . dialogs . unshift ( dialog ) ;
dialogsStorage . dialogs . unshift ( dialog ) ;
@ -1321,8 +1383,8 @@ angular.module('myApp.services', [])
for ( i = 0 ; i < update . messages . length ; i ++ ) {
for ( i = 0 ; i < update . messages . length ; i ++ ) {
messageID = update . messages [ i ] ;
messageID = update . messages [ i ] ;
message = messagesStorage [ messageID ] ;
message = messagesStorage [ messageID ] ;
// console.log('read', messageID, message);
// console.log('read', messageID, message.unread, message );
if ( message ) {
if ( message && message . unread ) {
message . unread = false ;
message . unread = false ;
if ( messagesForHistory [ messageID ] ) {
if ( messagesForHistory [ messageID ] ) {
messagesForHistory [ messageID ] . unread = false ;
messagesForHistory [ messageID ] . unread = false ;
@ -2015,8 +2077,8 @@ angular.module('myApp.services', [])
}
}
if ( seqStart != curState . seq + 1 ) {
if ( seqStart != curState . seq + 1 ) {
// console.log('seq hole', seqStart, curState.seq);
if ( seqStart > curState . seq ) {
if ( seqStart != curState . seq ) {
console . warn ( 'Seq hole' , seqStart , curState . seq ) ;
getDifference ( ) ;
getDifference ( ) ;
}
}
return false ;
return false ;