@ -405,12 +405,15 @@ angular.module('myApp.services', [])
}
}
} )
} )
. service ( 'AppMessagesManager' , function ( $q , $rootScope , $filter , $sanitize , ApiUpdatesManager , AppUsersManager , AppChatsManager , AppPeersManager , AppPhotosManager , AppVideoManager , AppDocsManager , MtpApiManager , RichTextProcessor ) {
. service ( 'AppMessagesManager' , function ( $q , $rootScope , $filter , $sanitize , ApiUpdatesManager , AppUsersManager , AppChatsManager , AppPeersManager , AppPhotosManager , AppVideoManager , AppDocsManager , MtpApiManager , MtpApiFileManager , RichTextProcessor ) {
var messagesStorage = { } ;
var messagesStorage = { } ;
var messagesForHistory = { } ;
var messagesForHistory = { } ;
var historiesStorage = { } ;
var historiesStorage = { } ;
var dialogsStorage = { count : null , dialogs : [ ] } ;
var dialogsStorage = { count : null , dialogs : [ ] } ;
var pendingByRandomID = { } ;
var pendingByMessageID = { } ;
var tempID = - 1 ;
function getDialogs ( offset , limit ) {
function getDialogs ( offset , limit ) {
if ( dialogsStorage . count !== null && dialogsStorage . dialogs . length >= offset + limit ) {
if ( dialogsStorage . count !== null && dialogsStorage . dialogs . length >= offset + limit ) {
@ -458,10 +461,14 @@ angular.module('myApp.services', [])
var peerID = AppPeersManager . getPeerID ( inputPeer ) ,
var peerID = AppPeersManager . getPeerID ( inputPeer ) ,
historyStorage = historiesStorage [ peerID ] ,
historyStorage = historiesStorage [ peerID ] ,
offset = 0 ;
offset = 0 ,
resultPending = [ ] ;
if ( historyStorage === undefined ) {
if ( historyStorage === undefined ) {
historyStorage = historiesStorage [ peerID ] = { count : null , history : [ ] } ;
historyStorage = historiesStorage [ peerID ] = { count : null , history : [ ] , pending : [ ] } ;
}
else if ( ! maxID && historyStorage . pending . length ) {
resultPending = historyStorage . pending . slice ( ) ;
}
}
@ -477,7 +484,7 @@ angular.module('myApp.services', [])
if ( historyStorage . count !== null && historyStorage . history . length >= offset + limit ) {
if ( historyStorage . count !== null && historyStorage . history . length >= offset + limit ) {
return $q . when ( {
return $q . when ( {
count : historyStorage . count ,
count : historyStorage . count ,
history : historyStorage . history . slice ( offset , offset + limit )
history : resultPending . concat ( historyStorage . history . slice ( offset , offset + limit ) )
} ) ;
} ) ;
}
}
@ -515,7 +522,7 @@ angular.module('myApp.services', [])
deferred . resolve ( {
deferred . resolve ( {
count : historyStorage . count ,
count : historyStorage . count ,
history : historyStorage . history . slice ( offset , offset + limit )
history : resultPending . concat ( historyStorage . history . slice ( offset , offset + limit ) )
} ) ;
} ) ;
} , function ( error ) {
} , function ( error ) {
deferred . reject ( error ) ;
deferred . reject ( error ) ;
@ -619,6 +626,205 @@ angular.module('myApp.services', [])
} ) ;
} ) ;
}
}
function sendText ( peerID , text ) {
var messageID = tempID -- ,
randomID = [ nextRandomInt ( 0xFFFFFFFF ) , nextRandomInt ( 0xFFFFFFFF ) ] ,
randomIDS = bigint ( randomID [ 0 ] ) . shiftLeft ( 32 ) . add ( bigint ( randomID [ 1 ] ) ) . toString ( ) ,
historyStorage = historiesStorage [ peerID ] ,
inputPeer = AppPeersManager . getInputPeerByID ( peerID ) ,
message ;
if ( historyStorage === undefined ) {
historyStorage = historiesStorage [ peerID ] = { count : null , history : [ ] , pending : [ ] } ;
}
MtpApiManager . getUserID ( ) . then ( function ( fromID ) {
message = {
_ : 'message' ,
id : messageID ,
from _id : fromID ,
to _id : AppPeersManager . getOutputPeer ( peerID ) ,
out : true ,
unread : true ,
date : ( + new Date ( ) ) / 1000 ,
message : text ,
media : { _ : 'messageMediaEmpty' } ,
random _id : randomIDS ,
pending : true
} ;
message . send = function ( ) {
MtpApiManager . invokeApi ( 'messages.sendMessage' , {
peer : inputPeer ,
message : text ,
random _id : randomID
} ) . then ( function ( result ) {
if ( ApiUpdatesManager . saveSeq ( result . seq ) ) {
ApiUpdatesManager . saveUpdate ( {
_ : 'updateMessageID' ,
random _id : randomIDS ,
id : result . id
} ) ;
message . date = result . date ;
message . id = result . id ;
ApiUpdatesManager . saveUpdate ( {
_ : 'updateNewMessage' ,
message : message ,
pts : result . pts
} ) ;
}
} ) ;
} ;
saveMessages ( [ message ] ) ;
historyStorage . pending . unshift ( messageID ) ;
$rootScope . $broadcast ( 'history_append' , { peerID : peerID , messageID : messageID } ) ;
// setTimeout(function () {
message . send ( ) ;
// }, 5000);
} ) ;
pendingByRandomID [ randomIDS ] = [ peerID , messageID ] ;
} ;
function sendFile ( peerID , file , options ) {
var messageID = tempID -- ,
randomID = [ nextRandomInt ( 0xFFFFFFFF ) , nextRandomInt ( 0xFFFFFFFF ) ] ,
randomIDS = bigint ( randomID [ 0 ] ) . shiftLeft ( 32 ) . add ( bigint ( randomID [ 1 ] ) ) . toString ( ) ,
historyStorage = historiesStorage [ peerID ] ,
inputPeer = AppPeersManager . getInputPeerByID ( peerID ) ,
isPhoto = file . type == 'image/jpeg' && file . size <= 5242880 ; // 5Mb
if ( historyStorage === undefined ) {
historyStorage = historiesStorage [ peerID ] = { count : null , history : [ ] , pending : [ ] } ;
}
MtpApiManager . getUserID ( ) . then ( function ( fromID ) {
var media = {
_ : 'messageMediaPending' ,
type : isPhoto ? 'photo' : 'doc' ,
file _name : file . name ,
size : file . size ,
progress : { percent : 1 }
} ;
var message = {
_ : 'message' ,
id : messageID ,
from _id : fromID ,
to _id : AppPeersManager . getOutputPeer ( peerID ) ,
out : true ,
unread : true ,
date : ( + new Date ( ) ) / 1000 ,
message : '' ,
media : media ,
random _id : randomIDS ,
pending : true
} ;
message . send = function ( ) {
MtpApiFileManager . uploadFile ( file ) . then ( function ( inputFile ) {
var inputMedia ;
if ( isPhoto ) {
inputMedia = { _ : 'inputMediaUploadedPhoto' , file : inputFile } ;
} else {
inputMedia = { _ : 'inputMediaUploadedDocument' , file : inputFile , file _name : file . name , mime _type : file . type } ;
}
MtpApiManager . invokeApi ( 'messages.sendMedia' , {
peer : inputPeer ,
media : inputMedia ,
random _id : randomID
} ) . then ( function ( result ) {
if ( ApiUpdatesManager . saveSeq ( result . seq ) ) {
ApiUpdatesManager . saveUpdate ( {
_ : 'updateMessageID' ,
random _id : randomIDS ,
id : result . message . id
} ) ;
message . date = result . message . date ;
message . id = result . message . id ;
message . media = result . message . media ;
ApiUpdatesManager . saveUpdate ( {
_ : 'updateNewMessage' ,
message : message ,
pts : result . pts
} ) ;
}
} ) ;
} , null , function ( progress ) {
// dLog('upload progress', progress);
var historyMessage = messagesForHistory [ messageID ] ,
percent = Math . max ( 1 , Math . floor ( 100 * progress . done / progress . total ) ) ;
media . progress . percent = percent ;
if ( historyMessage ) {
historyMessage . media . progress . percent = percent ;
$rootScope . $broadcast ( 'history_update' , { peerID : peerID } ) ;
}
} ) ;
} ;
saveMessages ( [ message ] ) ;
historyStorage . pending . unshift ( messageID ) ;
$rootScope . $broadcast ( 'history_append' , { peerID : peerID , messageID : messageID } ) ;
// setTimeout(function () {
message . send ( ) ;
// }, 5000);
} ) ;
pendingByRandomID [ randomIDS ] = [ peerID , messageID ] ;
}
function finalizePendingMessage ( randomID , finalMessage ) {
var pendingData = pendingByRandomID [ randomID ] ;
// dLog('pdata', randomID, pendingData);
if ( pendingData ) {
var peerID = pendingData [ 0 ] ,
tempID = pendingData [ 1 ] ,
historyStorage = historiesStorage [ peerID ] ,
index = false ,
message = false ,
historyMessage = false ,
i ;
// dLog('pending', randomID, historyStorage.pending);
for ( i = 0 ; i < historyStorage . pending . length ; i ++ ) {
if ( historyStorage . pending [ i ] == tempID ) {
historyStorage . pending . splice ( i , 1 ) ;
break ;
}
}
if ( message = messagesStorage [ tempID ] ) {
delete message . pending ;
delete message . random _id ;
delete message . send ;
}
if ( historyMessage = messagesForHistory [ tempID ] ) {
messagesForHistory [ finalMessage . id ] = angular . extend ( historyMessage , wrapForHistory ( finalMessage . id ) ) ;
delete historyMessage . pending ;
delete historyMessage . random _id ;
delete historyMessage . send ;
}
delete messagesForHistory [ tempID ] ;
delete messagesStorage [ tempID ] ;
return message ;
}
return false ;
}
function getMessagePeer ( message ) {
function getMessagePeer ( message ) {
var toID = message . to _id && AppPeersManager . getPeerID ( message . to _id ) || 0 ;
var toID = message . to _id && AppPeersManager . getPeerID ( message . to _id ) || 0 ;
@ -660,8 +866,8 @@ angular.module('myApp.services', [])
return message ;
return message ;
}
}
function wrapForHistory ( msgID ) {
function wrapForHistory ( msgID , force ) {
if ( messagesForHistory [ msgID ] !== undefined ) {
if ( ! force && messagesForHistory [ msgID ] !== undefined ) {
return messagesForHistory [ msgID ] ;
return messagesForHistory [ msgID ] ;
}
}
@ -679,6 +885,10 @@ angular.module('myApp.services', [])
case 'messageMediaVideo' :
case 'messageMediaVideo' :
message . media . video = AppVideoManager . wrapForHistory ( message . media . video . id ) ;
message . media . video = AppVideoManager . wrapForHistory ( message . media . video . id ) ;
break ;
break ;
case 'messageMediaDocument' :
message . media . document = AppDocsManager . wrapForHistory ( message . media . document . id ) ;
break ;
}
}
if ( message . media . user _id ) {
if ( message . media . user _id ) {
@ -714,10 +924,13 @@ angular.module('myApp.services', [])
return [ ] ;
return [ ] ;
}
}
$rootScope . $on ( 'apiUpdate' , function ( e , update ) {
$rootScope . $on ( 'apiUpdate' , function ( e , update ) {
dLog ( 'on apiUpdate' , update ) ;
dLog ( 'on apiUpdate' , update ) ;
switch ( update . _ ) {
switch ( update . _ ) {
case 'updateMessageID' :
pendingByMessageID [ update . id ] = update . random _id ;
break ;
case 'updateNewMessage' :
case 'updateNewMessage' :
var message = update . message ,
var message = update . message ,
peerID = getMessagePeer ( message ) ,
peerID = getMessagePeer ( message ) ,
@ -729,7 +942,7 @@ angular.module('myApp.services', [])
return false ;
return false ;
}
}
} else {
} else {
historyStorage = historiesStorage [ peerID ] = { count : null , history : [ ] } ;
historyStorage = historiesStorage [ peerID ] = { count : null , history : [ ] , pending : [ ] } ;
}
}
saveMessages ( [ message ] ) ;
saveMessages ( [ message ] ) ;
@ -737,8 +950,23 @@ angular.module('myApp.services', [])
if ( historyStorage . count !== null ) {
if ( historyStorage . count !== null ) {
historyStorage . count ++ ;
historyStorage . count ++ ;
}
}
historyStorage . history . unshift ( message . id ) ;
historyStorage . history . unshift ( message . id ) ;
$rootScope . $broadcast ( 'history_append' , { peerID : peerID , messageID : message . id } ) ;
var randomID = pendingByMessageID [ message . id ] ,
pendingMessage ;
if ( randomID ) {
if ( pendingMessage = finalizePendingMessage ( randomID , message ) ) {
$rootScope . $broadcast ( 'history_update' , { peerID : peerID } ) ;
}
delete pendingByMessageID [ message . id ] ;
}
// dLog(11, randomID, pendingMessage);
if ( ! pendingMessage ) {
$rootScope . $broadcast ( 'history_append' , { peerID : peerID , messageID : message . id } ) ;
}
var foundDialog = getDialogByPeerID ( peerID ) ,
var foundDialog = getDialogByPeerID ( peerID ) ,
dialog ;
dialog ;
@ -767,6 +995,7 @@ angular.module('myApp.services', [])
if ( message ) {
if ( message ) {
message . unread = false ;
message . unread = false ;
if ( messagesForHistory [ messageID ] ) {
if ( messagesForHistory [ messageID ] ) {
// dLog(222, messagesForHistory[messageID]);
messagesForHistory [ messageID ] . unread = false ;
messagesForHistory [ messageID ] . unread = false ;
}
}
peerID = getMessagePeer ( message ) ;
peerID = getMessagePeer ( message ) ;
@ -782,6 +1011,7 @@ angular.module('myApp.services', [])
angular . forEach ( dialogsUpdated , function ( count , peerID ) {
angular . forEach ( dialogsUpdated , function ( count , peerID ) {
$rootScope . $broadcast ( 'dialog_unread' , { peerID : peerID , count : count } ) ;
$rootScope . $broadcast ( 'dialog_unread' , { peerID : peerID , count : count } ) ;
} ) ;
} ) ;
// $rootScope.$broadcast('history_update');
break ;
break ;
}
}
} ) ;
} ) ;
@ -791,6 +1021,8 @@ angular.module('myApp.services', [])
getHistory : getHistory ,
getHistory : getHistory ,
readHistory : readHistory ,
readHistory : readHistory ,
saveMessages : saveMessages ,
saveMessages : saveMessages ,
sendText : sendText ,
sendFile : sendFile ,
getMessagePeer : getMessagePeer ,
getMessagePeer : getMessagePeer ,
wrapForDialog : wrapForDialog ,
wrapForDialog : wrapForDialog ,
wrapForHistory : wrapForHistory
wrapForHistory : wrapForHistory
@ -1011,6 +1243,7 @@ angular.module('myApp.services', [])
. service ( 'AppDocsManager' , function ( $rootScope , $modal , $window , $timeout , MtpApiFileManager , AppUsersManager ) {
. service ( 'AppDocsManager' , function ( $rootScope , $modal , $window , $timeout , MtpApiFileManager , AppUsersManager ) {
var docs = { } ;
var docs = { } ;
var docsForHistory = { } ;
function saveDoc ( apiDoc ) {
function saveDoc ( apiDoc ) {
docs [ apiDoc . id ] = apiDoc ;
docs [ apiDoc . id ] = apiDoc ;
@ -1026,6 +1259,10 @@ angular.module('myApp.services', [])
} ;
} ;
function wrapForHistory ( docID ) {
function wrapForHistory ( docID ) {
if ( docsForHistory [ docID ] !== undefined ) {
return docsForHistory [ docID ] ;
}
var doc = angular . copy ( docs [ docID ] ) ,
var doc = angular . copy ( docs [ docID ] ) ,
width = 100 ,
width = 100 ,
height = 100 ,
height = 100 ,
@ -1045,23 +1282,31 @@ angular.module('myApp.services', [])
thumb . location = thumbPhotoSize . location ;
thumb . location = thumbPhotoSize . location ;
thumb . size = thumbPhotoSize . size ;
thumb . size = thumbPhotoSize . size ;
} else {
thumb = false ;
}
}
doc . thumb = thumb ;
doc . thumb = thumb ;
return doc ;
return docsForHistory [ docID ] = doc ;
}
}
function openDoc ( docID , accessHash ) {
function openDoc ( docID , accessHash ) {
var doc = docs [ docID ] ,
var doc = docs [ docID ] ,
historyDoc = docsForHistory [ docID ] || doc || { } ,
inputFileLocation = {
inputFileLocation = {
_ : 'inputDocumentFileLocation' ,
_ : 'inputDocumentFileLocation' ,
id : docID ,
id : docID ,
access _hash : accessHash || doc . access _hash
access _hash : accessHash || doc . access _hash
} ,
} ;
scope = { } ;
historyDoc . progress = { enabled : true , percent : 1 } ;
scope . progress = { enabled : true , percent : 1 } ;
function updateDownloadProgress ( progress ) {
dLog ( 'dl progress' , progress ) ;
historyDoc . progress . percent = Math . max ( 1 , Math . floor ( 100 * progress . done / progress . total ) ) ;
$rootScope . $broadcast ( 'history_update' ) ;
}
if ( window . chrome && chrome . fileSystem && chrome . fileSystem . chooseEntry ) {
if ( window . chrome && chrome . fileSystem && chrome . fileSystem . chooseEntry ) {
@ -1076,13 +1321,16 @@ angular.module('myApp.services', [])
} ]
} ]
} , function ( writableFileEntry ) {
} , function ( writableFileEntry ) {
MtpApiFileManager . downloadFile ( doc . dc _id , inputFileLocation , doc . size , writableFileEntry ) . then ( function ( url ) {
MtpApiFileManager . downloadFile ( doc . dc _id , inputFileLocation , doc . size , writableFileEntry ) . then ( function ( url ) {
delete historyDoc . progress ;
dLog ( 'file save done' ) ;
dLog ( 'file save done' ) ;
} ) ;
} , function ( e ) {
dLog ( 'document download failed' , e ) ;
historyDoc . progress . enabled = false ;
} , updateDownloadProgress ) ;
} ) ;
} ) ;
} else {
} else {
MtpApiFileManager . downloadFile ( doc . dc _id , inputFileLocation , doc . size ) . then ( function ( url ) {
MtpApiFileManager . downloadFile ( doc . dc _id , inputFileLocation , doc . size ) . then ( function ( url ) {
scope . progress . enabled = false ;
delete historyDoc . progress ;
var a = $ ( '<a>Download</a>' ) . attr ( 'href' , url ) . attr ( 'target' , '_blank' ) . attr ( 'download' , doc . file _name ) . appendTo ( 'body' ) ;
var a = $ ( '<a>Download</a>' ) . attr ( 'href' , url ) . attr ( 'target' , '_blank' ) . attr ( 'download' , doc . file _name ) . appendTo ( 'body' ) ;
a [ 0 ] . dataset . downloadurl = [ 'png' , doc . file _name , url ] . join ( ':' ) ;
a [ 0 ] . dataset . downloadurl = [ 'png' , doc . file _name , url ] . join ( ':' ) ;
@ -1092,9 +1340,8 @@ angular.module('myApp.services', [])
} , 100 ) ;
} , 100 ) ;
} , function ( e ) {
} , function ( e ) {
dLog ( 'document download failed' , e ) ;
dLog ( 'document download failed' , e ) ;
} , function ( progress ) {
historyDoc . progress . enabled = false ;
scope . progress . percent = Math . max ( 1 , Math . floor ( 100 * progress . done / progress . total ) ) ;
} , updateDownloadProgress ) ;
} ) ;
}
}
}
}
@ -1240,6 +1487,11 @@ angular.module('myApp.services', [])
AppUsersManager . saveApiUsers ( differenceResult . users ) ;
AppUsersManager . saveApiUsers ( differenceResult . users ) ;
AppChatsManager . saveApiChats ( differenceResult . chats ) ;
AppChatsManager . saveApiChats ( differenceResult . chats ) ;
// Should be first because of updateMessageID
angular . forEach ( differenceResult . other _updates , function ( update ) {
saveUpdate ( update , true ) ;
} ) ;
angular . forEach ( differenceResult . new _messages , function ( apiMessage ) {
angular . forEach ( differenceResult . new _messages , function ( apiMessage ) {
saveUpdate ( {
saveUpdate ( {
_ : 'updateNewMessage' ,
_ : 'updateNewMessage' ,
@ -1248,10 +1500,6 @@ angular.module('myApp.services', [])
} , true ) ;
} , true ) ;
} ) ;
} ) ;
angular . forEach ( differenceResult . other _updates , function ( update ) {
saveUpdate ( update , true ) ;
} ) ;
var nextState = differenceResult . intermediate _state || differenceResult . state ;
var nextState = differenceResult . intermediate _state || differenceResult . state ;
curState . seq = nextState . seq ;
curState . seq = nextState . seq ;
curState . pts = nextState . pts ;
curState . pts = nextState . pts ;