@ -78,21 +78,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -78,21 +78,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
} ) ;
} ;
function userNameClean ( username ) {
return username && username . toLowerCase ( ) || '' ;
}
function resolveUsername ( username ) {
var searchUserName = userNameClean ( username ) ;
var foundUserID = usernames [ searchUserName ] ;
if ( foundUserID &&
userNameClean ( users [ foundUserID ] . username ) == searchUserName ) {
return qSync . when ( foundUserID ) ;
}
return MtpApiManager . invokeApi ( 'contacts.resolveUsername' , { username : username } ) . then ( function ( resolveResult ) {
saveApiUser ( resolveResult ) ;
return resolveResult . id ;
} ) ;
return usernames [ username ] || 0 ;
}
function saveApiUsers ( apiUsers ) {
@ -122,7 +109,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -122,7 +109,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
}
if ( apiUser . username ) {
usernames [ userNameClean ( apiUser . username ) ] = userID ;
var searchUsername = SearchIndexManager . cleanUsername ( apiUser . username ) ;
usernames [ searchUsername ] = userID ;
}
apiUser . sortName = SearchIndexManager . cleanSearchText ( apiUser . first _name + ' ' + ( apiUser . last _name || '' ) ) ;
@ -562,6 +550,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -562,6 +550,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
. service ( 'AppChatsManager' , function ( $q , $rootScope , $modal , _ , MtpApiFileManager , MtpApiManager , AppUsersManager , AppPhotosManager , RichTextProcessor ) {
var chats = { } ,
usernames = { } ,
chatsFull = { } ,
chatFullPromises = { } ,
cachedPhotoLocations = { } ;
@ -583,6 +572,11 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -583,6 +572,11 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
apiChat . num = ( Math . abs ( apiChat . id >> 1 ) % ( Config . Mobile ? 4 : 8 ) ) + 1 ;
if ( apiChat . username ) {
var searchUsername = SearchIndexManager . cleanUsername ( apiChat . username ) ;
usernames [ searchUsername ] = apiChat . id ;
}
if ( chats [ apiChat . id ] === undefined ) {
chats [ apiChat . id ] = apiChat ;
} else {
@ -599,23 +593,28 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -599,23 +593,28 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
return chats [ id ] || { id : id , deleted : true } ;
}
function resolveUsername ( username ) {
return usernames [ username ] || 0 ;
}
function isChannel ( id ) {
return ( chats [ id ] || { } ) . _ == 'channel' ;
}
function getChatInput ( id ) {
return id || 0 ;
}
function getChannelInput ( id ) {
if ( ! id ) {
return { _ : 'inputChatEmpty' } ;
return { _ : 'inputChannel Empty' } ;
}
if ( isChannel ( id ) ) {
return {
_ : 'inputChannel' ,
channel _id : id ,
access _hash : getChat ( id ) . access _hash || 0
}
}
return { _ : 'inputChat' , chat _id : id } ;
}
function getChatFull ( id ) {
if ( chatsFull [ id ] !== undefined ) {
@ -781,19 +780,23 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -781,19 +780,23 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
getChat : getChat ,
isChannel : isChannel ,
getChatInput : getChatInput ,
getChannelInput : getChannelInput ,
getChatFull : getChatFull ,
getChatPhoto : getChatPhoto ,
getChatString : getChatString ,
getChatInviteLink : getChatInviteLink ,
resolveUsername : resolveUsername ,
hasChat : hasChat ,
wrapForFull : wrapForFull ,
openChat : openChat
}
} )
. service ( 'AppPeersManager' , function ( AppUsersManager , AppChatsManager , MtpApiManager ) {
return {
getInputPeer : function ( peerString ) {
. service ( 'AppPeersManager' , function ( qSync , AppUsersManager , AppChatsManager , MtpApiManager ) {
var usernames = { } ;
function getInputPeer ( peerString ) {
var firstChar = peerString . charAt ( 0 ) ,
peerParams = peerString . substr ( 1 ) . split ( '_' ) ;
@ -817,30 +820,32 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -817,30 +820,32 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
chat _id : peerParams [ 0 ]
}
}
} ,
getInputPeerByID : function ( peerID ) {
if ( peerID > 0 ) {
return {
_ : 'inputPeerUser' ,
user _id : peerID ,
access _hash : AppUsersManager . getUser ( peerID ) . access _hash || 0
} ;
} else if ( peerID < 0 ) {
}
function getInputPeerByID ( peerID ) {
if ( peerID < 0 ) {
var chatID = - peerID ;
if ( ! AppChatsManager . isChannel ( chatID ) ) {
return {
_ : 'inputPeerChat' ,
chat _id : chatID
} ;
}
} else {
return {
_ : 'inputPeerChannel' ,
channel _id : chatID ,
access _hash : AppChatsManager . getChat ( chatID ) . access _hash || 0
}
}
} ,
getPeerSearchText : function ( peerID ) {
}
return {
_ : 'inputPeerUser' ,
user _id : peerID ,
access _hash : AppUsersManager . getUser ( peerID ) . access _hash || 0
} ;
}
function getPeerSearchText ( peerID ) {
var text ;
if ( peerID > 0 ) {
text = '%pu ' + AppUsersManager . getUserSearchText ( peerID ) ;
@ -849,14 +854,16 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -849,14 +854,16 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
text = '%pg ' + ( chat . title || '' ) ;
}
return text ;
} ,
getPeerString : function ( peerID ) {
}
function getPeerString ( peerID ) {
if ( peerID > 0 ) {
return AppUsersManager . getUserString ( peerID ) ;
}
return AppChatsManager . getChatString ( - peerID ) ;
} ,
getOutputPeer : function ( peerID ) {
}
function getOutputPeer ( peerID ) {
if ( peerID > 0 ) {
return { _ : 'peerUser' , user _id : peerID } ;
}
@ -865,28 +872,66 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -865,28 +872,66 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
return { _ : 'peerChannel' , channel _id : chatID }
}
return { _ : 'peerChat' , chat _id : chatID }
} ,
getPeerID : function ( peerString ) {
}
function resolveUsername ( username ) {
var searchUserName = SearchIndexManager . cleanUsername ( username ) ;
var foundUserID , foundChatID , foundPeerID , foundUsername ;
if ( foundUserID == AppUsersManager . resolveUsername ( searchUserName ) ) {
foundUsername = AppUsersManager . getUser ( foundUserID ) . username ;
if ( SearchIndexManager . cleanUsername ( foundUsername ) == searchUserName ) {
return qSync . when ( foundUserID ) ;
}
}
if ( foundChatID == AppChatsManager . resolveUsername ( searchUserName ) ) {
foundUsername = AppChatsManager . getChat ( foundChatID ) . username ;
if ( SearchIndexManager . cleanUsername ( foundUsername ) == searchUserName ) {
return qSync . when ( - foundChatID ) ;
}
}
return MtpApiManager . invokeApi ( 'contacts.resolveUsername' , { username : username } ) . then ( function ( resolveResult ) {
AppUsersManager . saveApiUsers ( resolveResult . users ) ;
AppChatsManager . saveApiChats ( resolveResult . chats ) ;
return getPeerID ( resolveResult . peer ) ;
} ) ;
}
function getPeerID ( peerString ) {
if ( angular . isObject ( peerString ) ) {
return peerString . user _id
? peerString . user _id
: - peerString . chat _id ;
: - ( peerString . channel _id || peerString . chat _id ) ;
}
var isUser = peerString . charAt ( 0 ) == 'u' ,
peerParams = peerString . substr ( 1 ) . split ( '_' ) ;
return isUser ? peerParams [ 0 ] : - peerParams [ 0 ] || 0 ;
} ,
getPeer : function ( peerID ) {
}
function getPeer ( peerID ) {
return peerID > 0
? AppUsersManager . getUser ( peerID )
: AppChatsManager . getChat ( - peerID ) ;
} ,
getPeerPhoto : function ( peerID , userPlaceholder , chatPlaceholder ) {
}
function getPeerPhoto ( peerID , userPlaceholder , chatPlaceholder ) {
return peerID > 0
? AppUsersManager . getUserPhoto ( peerID , userPlaceholder )
: AppChatsManager . getChatPhoto ( - peerID , chatPlaceholder )
}
return {
getInputPeer : getInputPeer ,
getInputPeerByID : getInputPeerByID ,
getPeerSearchText : getPeerSearchText ,
getPeerString : getPeerString ,
getOutputPeer : getOutputPeer ,
getPeerID : getPeerID ,
getPeer : getPeer ,
getPeerPhoto : getPeerPhoto ,
resolveUsername : resolveUsername ,
usernames : usernames
}
} )
@ -1147,9 +1192,9 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -1147,9 +1192,9 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
function requestHistory ( inputPeer , maxID , limit , offset ) {
return MtpApiManager . invokeApi ( 'messages.getHistory' , {
peer : inputPeer ,
offset : offset || 0 ,
add _ offset: offset || 0 ,
limit : limit || 0 ,
max _id : maxID || 0
offset _id : maxID || 0
} , { noErrorBox : true } ) . then ( function ( historyResult ) {
AppUsersManager . saveApiUsers ( historyResult . users ) ;
AppChatsManager . saveApiChats ( historyResult . chats ) ;
@ -1503,6 +1548,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -1503,6 +1548,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
}
return MtpApiManager . invokeApi ( 'messages.search' , {
flags : 0 ,
peer : inputPeer ,
q : query || '' ,
filter : inputFilter || { _ : 'inputMessagesFilterEmpty' } ,
@ -1699,6 +1745,14 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -1699,6 +1745,14 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
apiMessage . date -= serverTimeOffset ;
var toPeerID = AppPeersManager . getPeerID ( apiMessage . to _id ) ;
var isChannel = apiMessage . to _id . _ == 'peerChannel' ;
apiMessage . toID = toPeerID ;
apiMessage . fromID = apiMessage . from _id || toPeerID ;
if ( apiMessage . fwd _from _id ) {
apiMessage . fwdFromID = AppPeersManager . getPeerID ( apiMessage . fwd _from _id ) ;
}
var mediaContext = {
user _id : apiMessage . from _id ,
date : apiMessage . date
@ -1726,8 +1780,16 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -1726,8 +1780,16 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
break ;
}
}
if ( apiMessage . action && apiMessage . action . _ == 'messageActionChatEditPhoto' ) {
if ( apiMessage . action ) {
if ( apiMessage . action . _ == 'messageActionChatEditPhoto' ) {
AppPhotosManager . savePhoto ( apiMessage . action . photo , mediaContext ) ;
if ( isChannel ) {
apiMessage . action . _ = 'messageActionChannelEditPhoto' ;
}
}
if ( apiMessage . action . _ == 'messageActionChatEditTitle' && isChannel ) {
apiMessage . action . _ = 'messageActionChannelEditTitle' ;
}
}
if ( apiMessage . reply _markup ) {
apiMessage . reply _markup . pFlags = {
@ -1819,7 +1881,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -1819,7 +1881,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
if ( entities . length ) {
flags |= 8 ;
}
console . log ( flags , entities ) ;
// console.log(flags, entities);
MtpApiManager . invokeApi ( 'messages.sendMessage' , {
flags : flags ,
peer : inputPeer ,
@ -2379,8 +2441,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -2379,8 +2441,8 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
message . media . progress = messagesStorage [ msgID ] . media . progress ;
}
var fromUser = AppUsersManager . getUser ( message . from _id ) ;
var fromBot = fromUser . pFlags . bot && fromUser . username || false ;
var fromUser = message . from _id && AppUsersManager . getUser ( message . from _id ) ;
var fromBot = fromUser && fromUser . pFlags . bot && fromUser . username || false ;
var withBot = ( fromBot ||
message . to _id && (
message . to _id . chat _id ||
@ -2451,11 +2513,14 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -2451,11 +2513,14 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
else if ( message . action ) {
switch ( message . action . _ ) {
case 'messageActionChatEditPhoto' :
case 'messageActionChannelEditPhoto' :
message . action . photo = AppPhotosManager . wrapForHistory ( message . action . photo . id ) ;
break ;
case 'messageActionChatCreate' :
case 'messageActionChatEditTitle' :
case 'messageActionChannelCreate' :
case 'messageActionChannelEditTitle' :
message . action . rTitle = RichTextProcessor . wrapRichText ( message . action . title , { noLinks : true , noLinebreaks : true } ) || _ ( 'chat_title_deleted' ) ;
break ;
@ -2568,29 +2633,29 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -2568,29 +2633,29 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
}
}
if ( curMessage . fwd _from _id &&
if ( curMessage . fwdFromID &&
curMessage . media &&
curMessage . media . document &&
curMessage . media . document . sticker &&
( curMessage . from _id != ( prevMessage || { } ) . from _id || ! ( prevMessage || { } ) . fwd _from _id ) ) {
delete curMessage . fwd _from _id ;
( curMessage . from _id != ( prevMessage || { } ) . from _id || ! ( prevMessage || { } ) . fwdFromID ) ) {
delete curMessage . fwdFromID ;
curMessage . _ = 'message' ;
}
if ( prevMessage &&
curMessage . from _id == prevMessage . from _id &&
! prevMessage . fwd _from _id == ! curMessage . fwd _from _id &&
! prevMessage . fwdFromID == ! curMessage . fwdFromID &&
! prevMessage . action &&
! curMessage . action &&
curMessage . date < prevMessage . date + 900 ) {
var singleLine = curMessage . message && curMessage . message . length < 70 && curMessage . message . indexOf ( "\n" ) == - 1 && ! curMessage . reply _to _msg _id ;
if ( groupFwd && curMessage . fwd _from _id && curMessage . fwd _from _id == prevMessage . fwd _from _id ) {
if ( groupFwd && curMessage . fwdFromID && curMessage . fwdFromID == prevMessage . fwdFromID ) {
curMessage . grouped = singleLine ? 'im_grouped_fwd_short' : 'im_grouped_fwd' ;
} else {
curMessage . grouped = ! curMessage . fwd _from _id && singleLine ? 'im_grouped_short' : 'im_grouped' ;
curMessage . grouped = ! curMessage . fwdFromID && singleLine ? 'im_grouped_short' : 'im_grouped' ;
}
if ( groupFwd && curMessage . fwd _from _id ) {
if ( groupFwd && curMessage . fwdFromID ) {
if ( ! prevMessage . grouped ) {
prevMessage . grouped = 'im_grouped_fwd_start' ;
}
@ -2601,7 +2666,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -2601,7 +2666,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
} else if ( prevMessage || ! i ) {
delete curMessage . grouped ;
if ( groupFwd && prevMessage && prevMessage . grouped && prevMessage . fwd _from _id ) {
if ( groupFwd && prevMessage && prevMessage . grouped && prevMessage . fwdFromID ) {
prevMessage . grouped += ' im_grouped_fwd_end' ;
}
}
@ -2649,7 +2714,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -2649,7 +2714,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
var notifySettings = NotificationsManager . getNotifySettings ( ) ;
if ( message . fwd _from _id && options . fwd _count ) {
if ( message . fwdFromID && options . fwd _count ) {
notificationMessage = fwdMessagesPluralize ( options . fwd _count ) ;
} else if ( message . message ) {
if ( notifySettings . nopreview ) {
@ -2924,7 +2989,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -2924,7 +2989,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
notifyPeerToHandle . from _id = message . from _id ;
notifyPeerToHandle . fwd _count = 0 ;
}
if ( message . fwd _from _id ) {
if ( message . fwdFromID ) {
notifyPeerToHandle . fwd _count ++ ;
}
@ -3132,6 +3197,22 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -3132,6 +3197,22 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
}
} )
. service ( 'AppChannelsManager' , function ( ) {
var AppMessagesManager = { } ;
function getDialogs ( ) {
}
function setMessagesManager ( messagesManager ) {
AppMessagesManager = messagesManager ;
}
return {
setMessagesManager : setMessagesManager
}
} )
. service ( 'AppPhotosManager' , function ( $modal , $window , $rootScope , MtpApiManager , MtpApiFileManager , AppUsersManager , FileManager ) {
var photos = { } ,
windowW = $ ( window ) . width ( ) ,
@ -4473,10 +4554,13 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -4473,10 +4554,13 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
}
if ( update . _ == 'updateNewMessage' ) {
var message = update . message ;
var fwdPeerID = message . fwd _from _id ? AppPeersManager . getPeerID ( message . fwd _from _id ) : 0 ;
var toPeerID = AppPeersManager . getPeerID ( message . to _id ) ;
if ( message . from _id && ! AppUsersManager . hasUser ( message . from _id ) ||
message . fwd _from _id && ! AppUsersManager . hasUser ( message . fwd _from _id ) ||
message . to _id . user _id && ! AppUsersManager . hasUser ( message . to _id . user _id ) ||
message . to _id . chat _id && ! AppChatsManager . hasChat ( message . to _id . chat _id ) ) {
fwdPeerID > 0 && ! AppUsersManager . hasUser ( fwdPeerID ) ||
fwdPeerID < 0 && ! AppChatsManager . hasChat ( - fwdPeerID ) ||
toPeerID > 0 && ! AppUsersManager . hasUser ( toPeerID ) ||
toPeerID < 0 && ! AppChatsManager . hasChat ( - toPeerID ) ) {
console . warn ( dT ( ) , 'Short update not enough data' , message ) ;
forceGetDifference ( ) ;
return false ;
@ -4820,9 +4904,9 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -4820,9 +4904,9 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
rawOffset += match . index + match [ 0 ] . length ;
}
if ( entities . length ) {
console . log ( 'parse entities' , text , entities . slice ( ) ) ;
}
// if (entities.length) {
// console.log('parse entities', text, entities.slice());
// }
return entities ;
}
@ -6091,24 +6175,24 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
@@ -6091,24 +6175,24 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils'])
var matches ;
if ( matches = url . match ( /^resolve\?domain=(.+?)(?:&(start|startgroup)=(.+))?$/ ) ) {
AppUs ersManager . resolveUsername ( matches [ 1 ] ) . then ( function ( us erID) {
AppPe ersManager . resolveUsername ( matches [ 1 ] ) . then ( function ( pe erID) {
if ( matches [ 2 ] == 'startgroup' ) {
if ( peerID > 0 && AppUsersManager . isBot ( peerID ) && matches [ 2 ] == 'startgroup' ) {
PeersSelectService . selectPeer ( {
confirm _type : 'INVITE_TO_GROUP' ,
noUsers : true
} ) . then ( function ( p eerString) {
var p eerID = AppPeersManager . getPeerID ( p eerString) ;
var chatID = p eerID < 0 ? - p eerID : 0 ;
AppMessagesManager . startBot ( userID , c hatID, matches [ 3 ] ) . then ( function ( ) {
$rootScope . $broadcast ( 'history_focus' , { peerString : p eerString} ) ;
} ) . then ( function ( toP eerString) {
var toP eerID = AppPeersManager . getPeerID ( toP eerString) ;
var toChatID = toP eerID < 0 ? - toP eerID : 0 ;
AppMessagesManager . startBot ( peerID , toC hatID, matches [ 3 ] ) . then ( function ( ) {
$rootScope . $broadcast ( 'history_focus' , { toPeerString : toP eerString} ) ;
} ) ;
} ) ;
return true ;
}
$rootScope . $broadcast ( 'history_focus' , {
peerString : AppUsersManager . getUserString ( us erID ) ,
peerString : AppPeersManager . getPeerString ( pe erID ) ,
startParam : matches [ 3 ]
} ) ;
} ) ;