@ -175,7 +175,8 @@ angular.module('myApp.services')
message . pFlags . unread = true ;
message . pFlags . unread = true ;
}
}
if ( historiesStorage [ peerID ] === undefined ) {
if ( historiesStorage [ peerID ] === undefined &&
! message . deleted ) {
var historyStorage = { count : null , history : [ mid ] , pending : [ ] } ;
var historyStorage = { count : null , history : [ mid ] , pending : [ ] } ;
historiesStorage [ peerID ] = historyStorage ;
historiesStorage [ peerID ] = historyStorage ;
if ( mergeReplyKeyboard ( historyStorage , message ) ) {
if ( mergeReplyKeyboard ( historyStorage , message ) ) {
@ -308,10 +309,18 @@ angular.module('myApp.services')
ApiUpdatesManager . addChannelState ( - peerID , historyResult . pts ) ;
ApiUpdatesManager . addChannelState ( - peerID , historyResult . pts ) ;
}
}
var length = historyResult . messages . length ;
if ( length &&
historyResult . messages [ length - 1 ] . deleted ) {
historyResult . messages . splice ( length - 1 , 1 ) ;
length -- ;
historyResult . count -- ;
}
if (
if (
peerID < 0 ||
peerID < 0 ||
! AppUsersManager . isBot ( peerID ) ||
! AppUsersManager . isBot ( peerID ) ||
( historyResult . messages . length == limit && limit < historyResult . count )
( length == limit && limit < historyResult . count )
) {
) {
return historyResult ;
return historyResult ;
}
}
@ -364,7 +373,7 @@ angular.module('myApp.services')
function fillHistoryStorage ( peerID , maxID , fullLimit , historyStorage ) {
function fillHistoryStorage ( peerID , maxID , fullLimit , historyStorage ) {
// console.log('fill history storage', peerID, maxID, fullLimit, angular.copy(historyStorage));
// console.log('fill history storage', peerID, maxID, fullLimit, angular.copy(historyStorage));
var offset = ( migratedFromTo [ peerID ] && ! maxID ) ? 1 : 0 ;
var offset = ( migratedFromTo [ peerID ] && ! maxID ) ? 1 : 0 ;
return requestHistory ( peerID , maxID , fullLimit , offset ) . then ( function ( historyResult ) {
return requestHistory ( peerID , maxID , fullLimit , offset ) . then ( function ( historyResult ) {
historyStorage . count = historyResult . count || historyResult . messages . length ;
historyStorage . count = historyResult . count || historyResult . messages . length ;
var offset = 0 ;
var offset = 0 ;
@ -1039,8 +1048,13 @@ angular.module('myApp.services')
} ) ;
} ) ;
}
}
function doFlushHistory ( inputPeer ) {
function doFlushHistory ( inputPeer , justClear ) {
var flags = 0 ;
if ( justClear ) {
flags |= 1 ;
}
return MtpApiManager . invokeApi ( 'messages.deleteHistory' , {
return MtpApiManager . invokeApi ( 'messages.deleteHistory' , {
flags : flags ,
peer : inputPeer ,
peer : inputPeer ,
max _id : 0
max _id : 0
} ) . then ( function ( affectedHistory ) {
} ) . then ( function ( affectedHistory ) {
@ -1055,18 +1069,22 @@ angular.module('myApp.services')
if ( ! affectedHistory . offset ) {
if ( ! affectedHistory . offset ) {
return true ;
return true ;
}
}
return doFlushHistory ( inputPeer ) ;
return doFlushHistory ( inputPeer , justClear ) ;
} ) ;
} ) ;
}
}
function flushHistory ( peerID ) {
function flushHistory ( peerID , justClear ) {
return doFlushHistory ( AppPeersManager . getInputPeerByID ( peerID ) ) . then ( function ( ) {
return doFlushHistory ( AppPeersManager . getInputPeerByID ( peerID ) , justClear ) . then ( function ( ) {
var foundDialog = getDialogByPeerID ( peerID ) ;
if ( justClear ) {
if ( foundDialog [ 0 ] ) {
$rootScope . $broadcast ( 'dialog_flush' , { peerID : peerID } ) ;
dialogsStorage . dialogs . splice ( foundDialog [ 1 ] , 1 ) ;
} else {
var foundDialog = getDialogByPeerID ( peerID ) ;
if ( foundDialog [ 0 ] ) {
dialogsStorage . dialogs . splice ( foundDialog [ 1 ] , 1 ) ;
}
delete historiesStorage [ peerID ] ;
$rootScope . $broadcast ( 'dialog_drop' , { peerID : peerID } ) ;
}
}
delete historiesStorage [ peerID ] ;
$rootScope . $broadcast ( 'dialog_flush' , { peerID : peerID } ) ;
} ) ;
} ) ;
}
}
@ -1120,7 +1138,8 @@ angular.module('myApp.services')
apiMessage . toID = toPeerID ;
apiMessage . toID = toPeerID ;
apiMessage . fromID = apiMessage . pFlags . post ? toPeerID : apiMessage . from _id ;
apiMessage . fromID = apiMessage . pFlags . post ? toPeerID : apiMessage . from _id ;
apiMessage . signID = apiMessage . pFlags . post && apiMessage . from _id || fwdHeader && fwdHeader . from _id ;
apiMessage . signID = apiMessage . pFlags . post && apiMessage . from _id ||
fwdHeader && fwdHeader . channel _id && fwdHeader . from _id ;
if ( apiMessage . via _bot _id > 0 ) {
if ( apiMessage . via _bot _id > 0 ) {
apiMessage . viaBotID = apiMessage . via _bot _id ;
apiMessage . viaBotID = apiMessage . via _bot _id ;
@ -1200,6 +1219,13 @@ angular.module('myApp.services')
migrateFrom = - channelID ;
migrateFrom = - channelID ;
migrateTo = - apiMessage . action . channel _id ;
migrateTo = - apiMessage . action . channel _id ;
break ;
break ;
case 'messageActionHistoryClear' :
apiMessage . deleted = true ;
apiMessage . clear _history = true ;
apiMessage . pFlags . out = false ;
apiMessage . pFlags . unread = false ;
break ;
}
}
if ( migrateFrom &&
if ( migrateFrom &&
migrateTo &&
migrateTo &&
@ -2232,6 +2258,11 @@ angular.module('myApp.services')
}
}
for ( i = start ; i < end ; i ++ ) {
for ( i = start ; i < end ; i ++ ) {
if ( history [ i ] . deleted ) {
history . splice ( i , 1 ) ;
end -- ;
continue ;
}
curMessage = history [ i ] ;
curMessage = history [ i ] ;
curDay = Math . floor ( ( curMessage . date + ServerTimeManager . midnightOffset ) / 86400 ) ;
curDay = Math . floor ( ( curMessage . date + ServerTimeManager . midnightOffset ) / 86400 ) ;
@ -2731,11 +2762,22 @@ angular.module('myApp.services')
safeReplaceObject ( wasForHistory , newForHistory ) ;
safeReplaceObject ( wasForHistory , newForHistory ) ;
messagesForHistory [ mid ] = wasForHistory ;
messagesForHistory [ mid ] = wasForHistory ;
}
}
$rootScope . $broadcast ( 'message_edit' , {
peerID : peerID ,
if ( message . clear _history ) {
id : message . id ,
var foundDialog = getDialogByPeerID ( peerID ) ;
mid : mid
if ( foundDialog [ 0 ] &&
} ) ;
foundDialog [ 0 ] . top _message == mid ) {
$rootScope . $broadcast ( 'dialog_flush' , { peerID : peerID } ) ;
} else {
$rootScope . $broadcast ( 'history_delete' , { peerID : peerID , msgs : [ mid ] } ) ;
}
} else {
$rootScope . $broadcast ( 'message_edit' , {
peerID : peerID ,
id : message . id ,
mid : mid
} ) ;
}
break ;
break ;
case 'updateReadHistoryInbox' :
case 'updateReadHistoryInbox' :
@ -2875,15 +2917,6 @@ angular.module('myApp.services')
}
}
angular . forEach ( historiesUpdated , function ( updatedData , peerID ) {
angular . forEach ( historiesUpdated , function ( updatedData , peerID ) {
var foundDialog = getDialogByPeerID ( peerID ) ;
if ( foundDialog [ 0 ] ) {
if ( updatedData . unread ) {
foundDialog [ 0 ] . unread _count -= updatedData . unread ;
$rootScope . $broadcast ( 'dialog_unread' , { peerID : peerID , count : foundDialog [ 0 ] . unread _count } ) ;
}
}
var historyStorage = historiesStorage [ peerID ] ;
var historyStorage = historiesStorage [ peerID ] ;
if ( historyStorage !== undefined ) {
if ( historyStorage !== undefined ) {
var newHistory = [ ] ,
var newHistory = [ ] ,
@ -2912,6 +2945,22 @@ angular.module('myApp.services')
$rootScope . $broadcast ( 'history_delete' , { peerID : peerID , msgs : updatedData . msgs } ) ;
$rootScope . $broadcast ( 'history_delete' , { peerID : peerID , msgs : updatedData . msgs } ) ;
}
}
var foundDialog = getDialogByPeerID ( peerID ) [ 0 ] ;
if ( foundDialog ) {
if ( updatedData . unread ) {
foundDialog . unread _count -= updatedData . unread ;
$rootScope . $broadcast ( 'dialog_unread' , {
peerID : peerID ,
count : foundDialog . unread _count
} ) ;
}
if ( updatedData . msgs [ foundDialog . top _message ] ) {
reloadConversation ( peerID ) ;
}
}
} ) ;
} ) ;
break ;
break ;
@ -2999,11 +3048,28 @@ angular.module('myApp.services')
saveMessages ( dialogsResult . messages ) ;
saveMessages ( dialogsResult . messages ) ;
var updatedDialogs = { } ;
var updatedDialogs = { } ;
var hasUpdated = false ;
angular . forEach ( dialogsResult . dialogs , function ( dialog ) {
angular . forEach ( dialogsResult . dialogs , function ( dialog ) {
saveConversation ( dialog ) ;
if ( dialog . top _message ) {
updatedDialogs [ dialog . peerID ] = dialog ;
saveConversation ( dialog ) ;
if ( getDialogByPeerID ( dialog . peerID ) . length ) {
$rootScope . $broadcast ( 'dialog_top' , dialog ) ;
} else {
updatedDialogs [ dialog . peerID ] = dialog ;
hasUpdated = true ;
}
} else {
var peerID = AppPeersManager . getPeerID ( dialog . peer ) ;
var foundDialog = getDialogByPeerID ( peerID ) ;
if ( foundDialog . length ) {
dialogsStorage . dialogs . splice ( foundDialog [ 1 ] , 1 ) ;
$rootScope . $broadcast ( 'dialog_drop' , { peerID : peerID } ) ;
}
}
} ) ;
} ) ;
$rootScope . $broadcast ( 'dialogs_multiupdate' , updatedDialogs ) ;
if ( hasUpdated ) {
$rootScope . $broadcast ( 'dialogs_multiupdate' , updatedDialogs ) ;
}
} ) ;
} ) ;
}
}