diff --git a/app/js/controllers.js b/app/js/controllers.js index 7f256479..c0406e47 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -1111,9 +1111,10 @@ angular.module('myApp.controllers', ['myApp.i18n']) $rootScope.selectedPeerID = peerID; $scope.curDialog.peerID = peerID; - $scope.curDialog.inputPeer = AppPeersManager.getInputPeer(newPeer); $scope.historyFilter.mediaType = false; + AppPeersManager.getInputPeer(newPeer); + updateBotActions(); selectedCancel(true); @@ -1309,7 +1310,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) curLessJump = ++lessJump, limit = 0, backLimit = 20; - AppMessagesManager.getHistory($scope.curDialog.inputPeer, minID, limit, backLimit).then(function (historyResult) { + AppMessagesManager.getHistory($scope.curDialog.peerID, minID, limit, backLimit).then(function (historyResult) { lessActive = false; if (curJump != jump || curLessJump != lessJump) return; @@ -1358,8 +1359,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) inputMediaFilter = $scope.historyFilter.mediaType && {_: inputMediaFilters[$scope.historyFilter.mediaType]}, limit = Config.Mobile ? 20 : 0, getMessagesPromise = inputMediaFilter - ? AppMessagesManager.getSearch($scope.curDialog.inputPeer, '', inputMediaFilter, maxID, limit) - : AppMessagesManager.getHistory($scope.curDialog.inputPeer, maxID, limit); + ? AppMessagesManager.getSearch($scope.curDialog.peerID, '', inputMediaFilter, maxID, limit) + : AppMessagesManager.getHistory($scope.curDialog.peerID, maxID, limit); getMessagesPromise.then(function (historyResult) { moreActive = false; @@ -1425,8 +1426,8 @@ angular.module('myApp.controllers', ['myApp.i18n']) var curJump = ++jump, inputMediaFilter = $scope.historyFilter.mediaType && {_: inputMediaFilters[$scope.historyFilter.mediaType]}, getMessagesPromise = inputMediaFilter - ? AppMessagesManager.getSearch($scope.curDialog.inputPeer, '', inputMediaFilter, maxID) - : AppMessagesManager.getHistory($scope.curDialog.inputPeer, maxID, limit, backLimit, prerenderedLen); + ? AppMessagesManager.getSearch($scope.curDialog.peerID, '', inputMediaFilter, maxID) + : AppMessagesManager.getHistory($scope.curDialog.peerID, maxID, limit, backLimit, prerenderedLen); $scope.state.mayBeHasMore = true; @@ -1491,7 +1492,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) }, 2800); } - AppMessagesManager.readHistory($scope.curDialog.inputPeer); + AppMessagesManager.readHistory($scope.curDialog.peerID); updateBotActions(); updateChannelActions(); @@ -1666,7 +1667,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) function selectedFlush () { ErrorService.confirm({type: 'HISTORY_FLUSH'}).then(function () { - AppMessagesManager.flushHistory($scope.curDialog.inputPeer).then(function () { + AppMessagesManager.flushHistory($scope.curDialog.peerID).then(function () { selectedCancel(); }); }) @@ -1860,7 +1861,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) } } else { $timeout(function () { - AppMessagesManager.readHistory($scope.curDialog.inputPeer); + AppMessagesManager.readHistory($scope.curDialog.peerID); }); } @@ -1957,7 +1958,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) } } else { $timeout(function () { - AppMessagesManager.readHistory($scope.curDialog.inputPeer); + AppMessagesManager.readHistory($scope.curDialog.peerID); }); } @@ -2045,7 +2046,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $rootScope.$watch('idle.isIDLE', function (newVal) { if (!newVal && $scope.curDialog && $scope.curDialog.peerID && !$scope.historyFilter.mediaType && !$scope.historyState.skipped) { - AppMessagesManager.readHistory($scope.curDialog.inputPeer); + AppMessagesManager.readHistory($scope.curDialog.peerID); } if (!newVal) { unreadAfterIdle = false; @@ -2370,7 +2371,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) if (newVal && newVal.length) { if (!$scope.historyFilter.mediaType && !$scope.historyState.skipped) { - AppMessagesManager.readHistory($scope.curDialog.inputPeer); + AppMessagesManager.readHistory($scope.curDialog.peerID); } var backupDraftObj = {}; @@ -2384,11 +2385,12 @@ angular.module('myApp.controllers', ['myApp.i18n']) } function onTyping () { - if ($scope.curDialog.inputPeer._ == 'inputPeerChannel') { + if (AppPeersManager.isChannel($scope.curDialog.peerID) && + !AppPeersManager.isMegagroup($scope.curDialog.peerID)) { return false; } MtpApiManager.invokeApi('messages.setTyping', { - peer: $scope.curDialog.inputPeer, + peer: AppPeersManager.getInputPeerByID($scope.curDialog.peerID), action: {_: 'sendMessageTypingAction'} }); } @@ -3078,7 +3080,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.flushHistory = function () { ErrorService.confirm({type: 'HISTORY_FLUSH'}).then(function () { - AppMessagesManager.flushHistory(AppPeersManager.getInputPeerByID($scope.userID)).then(function () { + AppMessagesManager.flushHistory($scope.userID).then(function () { $scope.goToHistory(); }); }); @@ -3238,7 +3240,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.flushHistory = function () { ErrorService.confirm({type: 'HISTORY_FLUSH'}).then(function () { - AppMessagesManager.flushHistory(AppPeersManager.getInputPeerByID(-$scope.chatID)).then(function () { + AppMessagesManager.flushHistory(-$scope.chatID).then(function () { $rootScope.$broadcast('history_focus', {peerString: $scope.chatFull.peerString}); }); }); diff --git a/app/js/lib/tl_utils.js b/app/js/lib/tl_utils.js index 1c356ab1..12be9f69 100644 --- a/app/js/lib/tl_utils.js +++ b/app/js/lib/tl_utils.js @@ -518,8 +518,18 @@ TLDeserialization.prototype.fetchObject = function (type, field) { if (type.substr(0, 6) == 'Vector' || type.substr(0, 6) == 'vector') { if (type.charAt(0) == 'V') { - var constructor = this.readInt(field + '[id]'); - if (constructor != 0x1cb5c415) { + var constructor = this.readInt(field + '[id]'), + constructorCmp = uintToInt(constructor); + + if (constructorCmp == 0x3072cfa1) { // Gzip packed + var compressed = this.fetchBytes(field + '[packed_string]'), + uncompressed = gzipUncompress(compressed), + buffer = bytesToArrayBuffer(uncompressed), + newDeserializer = (new TLDeserialization(buffer)); + + return newDeserializer.fetchObject(type, field); + } + if (constructorCmp != 0x1cb5c415) { throw new Error('Invalid vector constructor ' + constructor); } } diff --git a/app/js/messages_manager.js b/app/js/messages_manager.js index 759d98f8..1250838c 100644 --- a/app/js/messages_manager.js +++ b/app/js/messages_manager.js @@ -70,8 +70,8 @@ angular.module('myApp.services') var dialogsNum = 0; var minDialogsIndex = Math.pow(2, 50); - var migratedFrom = {}; - var migratedTo = {}; + var migratedFromTo = {}; + var migratedToFrom = {}; function getConversations (query, offsetIndex, limit) { var curDialogStorage = dialogsStorage; @@ -234,8 +234,8 @@ angular.module('myApp.services') var chat = AppChatsManager.getChat(-peerID); if (chat && chat.migrated_to && chat.pFlags.deactivated) { var migratedToPeer = AppPeersManager.getPeerID(chat.migrated_to); - migratedFrom[peerID] = migratedToPeer; - migratedTo[migratedToPeer] = peerID; + migratedFromTo[peerID] = migratedToPeer; + migratedToFrom[migratedToPeer] = peerID; return; } } @@ -323,8 +323,7 @@ angular.module('myApp.services') } } - function requestHistory (inputPeer, maxID, limit, offset) { - var peerID = AppPeersManager.getPeerID(inputPeer); + function requestHistory (peerID, maxID, limit, offset) { var isChannel = AppPeersManager.isChannel(peerID); var isMegagroup = isChannel && AppPeersManager.isMegagroup(peerID); @@ -341,7 +340,7 @@ angular.module('myApp.services') }); } else { promise = MtpApiManager.invokeApi('messages.getHistory', { - peer: inputPeer, + peer: AppPeersManager.getInputPeerByID(peerID), offset_id: maxID ? getMessageLocalID(maxID) : 0, add_offset: offset || 0, limit: limit || 0 @@ -472,15 +471,14 @@ angular.module('myApp.services') }; } - function fillHistoryStorage (inputPeer, maxID, fullLimit, historyStorage) { - // console.log('fill history storage', inputPeer, maxID, fullLimit, angular.copy(historyStorage)); - return requestHistory (inputPeer, maxID, fullLimit).then(function (historyResult) { + function fillHistoryStorage (peerID, maxID, fullLimit, historyStorage) { + // console.log('fill history storage', peerID, maxID, fullLimit, angular.copy(historyStorage)); + var migratedNextPeer = migratedFromTo[peerID]; + var migratedPrevPeer = migratedToFrom[peerID]; + var isMigrated = migratedNextPeer !== undefined || migratedPrevPeer !== undefined; + + return requestHistory (peerID, maxID, fullLimit).then(function (historyResult) { historyStorage.count = historyResult.count || historyResult.messages.length; - var peerID = AppPeersManager.getPeerID(inputPeer); - var migratedFromPeer = migratedTo[peerID]; - // if () { - // historyStorage.count++; - // } var offset = 0; if (!maxID && historyResult.messages.length) { @@ -494,22 +492,41 @@ angular.module('myApp.services') } } + var wasTotalCount = historyStorage.history.length; + historyStorage.history.splice(offset, historyStorage.history.length - offset); angular.forEach(historyResult.messages, function (message) { if (mergeReplyKeyboard(historyStorage, message)) { - $rootScope.$broadcast('history_reply_markup', {peerID: AppPeersManager.getPeerID(inputPeer)}); + $rootScope.$broadcast('history_reply_markup', {peerID: peerID}); } historyStorage.history.push(message.mid); }); - fullLimit -= historyResult.messages.length; + var totalCount = historyStorage.history.length; + fullLimit -= (totalCount - wasTotalCount); - if (fullLimit > 0 && - (historyStorage.history.length < historyStorage.count || migratedFromPeer)) { - maxID = historyStorage.history[historyStorage.history.length - 1]; - return fillHistoryStorage(inputPeer, maxID, fullLimit, historyStorage); + if (isMigrated) { + historyStorage.count = Math.max(historyStorage.count, totalCount) + 1; } + if (fullLimit > 0) { + maxID = historyStorage.history[totalCount - 1]; + if (isMigrated) { + if (!historyResult.messages.length) { + if (migratedPrevPeer) { + maxID = 0; + peerID = migratedPrevPeer; + } else { + historyStorage.count = totalCount; + return true; + } + } + return fillHistoryStorage(peerID, maxID, fullLimit, historyStorage); + } + else if (totalCount < historyStorage.count) { + return fillHistoryStorage(peerID, maxID, fullLimit, historyStorage); + } + } return true; }); }; @@ -529,9 +546,11 @@ angular.module('myApp.services') return $q.when(result); } - function getHistory (inputPeer, maxID, limit, backLimit, prerendered) { - var peerID = AppPeersManager.getPeerID(inputPeer), - historyStorage = historiesStorage[peerID], + function getHistory (peerID, maxID, limit, backLimit, prerendered) { + if (migratedFromTo[peerID]) { + peerID = migratedFromTo[peerID]; + } + var historyStorage = historiesStorage[peerID], offset = 0, offsetNotFound = false, unreadOffset = false, @@ -543,6 +562,15 @@ angular.module('myApp.services') historyStorage = historiesStorage[peerID] = {count: null, history: [], pending: []}; } + var isMigrated = false; + var reqPeerID = peerID; + if (migratedToFrom[peerID]) { + isMigrated = true; + if (maxID && maxID < fullMsgIDModulus) { + reqPeerID = migratedToFrom[peerID]; + } + } + if (!limit && !maxID) { var foundDialog = getDialogByPeerID(peerID)[0]; if (foundDialog && foundDialog.unread_count > 1) { @@ -611,8 +639,11 @@ angular.module('myApp.services') offset = -backLimit; limit += backLimit; } - return requestHistory(inputPeer, maxID, limit, offset).then(function (historyResult) { + return requestHistory(reqPeerID, maxID, limit, offset).then(function (historyResult) { historyStorage.count = historyResult.count || historyResult.messages.length; + if (isMigrated) { + historyStorage.count++; + } var history = []; angular.forEach(historyResult.messages, function (message) { @@ -631,7 +662,7 @@ angular.module('myApp.services') }) } - return fillHistoryStorage(inputPeer, maxID, limit, historyStorage).then(function () { + return fillHistoryStorage(peerID, maxID, limit, historyStorage).then(function () { offset = 0; if (maxID > 0) { for (offset = 0; offset < historyStorage.history.length; offset++) { @@ -728,10 +759,9 @@ angular.module('myApp.services') return false; } - function getSearch (inputPeer, query, inputFilter, maxID, limit) { + function getSearch (peerID, query, inputFilter, maxID, limit) { var foundMsgs = [], useSearchCache = !query, - peerID = AppPeersManager.getPeerID(inputPeer), newSearchFilter = {peer: peerID, filter: inputFilter}, sameSearchCache = useSearchCache && angular.equals(lastSearchFilter, newSearchFilter); @@ -817,7 +847,7 @@ angular.module('myApp.services') } apiPromise = MtpApiManager.invokeApi('messages.search', { flags: flags, - peer: inputPeer, + peer: AppPeersManager.getInputPeerByID(peerID), q: query || '', filter: inputFilter || {_: 'inputMessagesFilterEmpty'}, min_date: 0, @@ -884,6 +914,7 @@ angular.module('myApp.services') function deleteMessages (messageIDs) { var splitted = splitMessageIDsByChannels(messageIDs); + debugger; var promises = []; angular.forEach(splitted.msgIDs, function (msgIDs, channelID) { var promise; @@ -940,10 +971,9 @@ angular.module('myApp.services') return $q.all(promises); } - function readHistory (inputPeer) { + function readHistory (peerID) { // console.trace('start read'); - var peerID = AppPeersManager.getPeerID(inputPeer), - isChannel = AppPeersManager.isChannel(peerID), + var isChannel = AppPeersManager.isChannel(peerID), historyStorage = historiesStorage[peerID], foundDialog = getDialogByPeerID(peerID)[0]; @@ -981,7 +1011,7 @@ angular.module('myApp.services') }); } else { apiPromise = MtpApiManager.invokeApi('messages.readHistory', { - peer: inputPeer, + peer: AppPeersManager.getInputPeerByID(peerID), max_id: 0 }); } @@ -1059,9 +1089,8 @@ angular.module('myApp.services') }); } - function flushHistory (inputPeer) { - var peerID = AppPeersManager.getPeerID(inputPeer); - return doFlushHistory(inputPeer).then(function () { + function flushHistory (peerID) { + return doFlushHistory(AppPeersManager.getInputPeerByID(peerID)).then(function () { var foundDialog = getDialogByPeerID(peerID); if (foundDialog[0]) { dialogsStorage.dialogs.splice(foundDialog[1], 1); @@ -1201,7 +1230,6 @@ angular.module('myApp.services') randomID = [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)], randomIDS = bigint(randomID[0]).shiftLeft(32).add(bigint(randomID[1])).toString(), historyStorage = historiesStorage[peerID], - inputPeer = AppPeersManager.getInputPeerByID(peerID), flags = 0, pFlags = {}, replyToMsgID = options.replyToMsgID, @@ -1285,7 +1313,7 @@ angular.module('myApp.services') // console.log(flags, entities); MtpApiManager.invokeApi('messages.sendMessage', { flags: flags, - peer: inputPeer, + peer: AppPeersManager.getInputPeerByID(peerID), message: text, random_id: randomID, reply_to_msg_id: getMessageLocalID(replyToMsgID), @@ -1343,7 +1371,6 @@ angular.module('myApp.services') randomID = [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)], randomIDS = bigint(randomID[0]).shiftLeft(32).add(bigint(randomID[1])).toString(), historyStorage = historiesStorage[peerID], - inputPeer = AppPeersManager.getInputPeerByID(peerID), flags = 0, pFlags = {}, replyToMsgID = options.replyToMsgID, @@ -1474,7 +1501,7 @@ angular.module('myApp.services') } MtpApiManager.invokeApi('messages.sendMedia', { flags: flags, - peer: inputPeer, + peer: AppPeersManager.getInputPeerByID(peerID), media: inputMedia, random_id: randomID, reply_to_msg_id: getMessageLocalID(replyToMsgID) @@ -1532,7 +1559,6 @@ angular.module('myApp.services') randomID = [nextRandomInt(0xFFFFFFFF), nextRandomInt(0xFFFFFFFF)], randomIDS = bigint(randomID[0]).shiftLeft(32).add(bigint(randomID[1])).toString(), historyStorage = historiesStorage[peerID], - inputPeer = AppPeersManager.getInputPeerByID(peerID), replyToMsgID = options.replyToMsgID, isChannel = AppPeersManager.isChannel(peerID), isMegagroup = isChannel && AppPeersManager.isMegagroup(peerID), @@ -1635,7 +1661,7 @@ angular.module('myApp.services') MtpApiManager.invokeApi('messages.sendMedia', { flags: flags, - peer: inputPeer, + peer: AppPeersManager.getInputPeerByID(peerID), media: inputMedia, random_id: randomID, reply_to_msg_id: getMessageLocalID(replyToMsgID) @@ -2827,10 +2853,9 @@ angular.module('myApp.services') function reloadChannelDialog (channelID) { var peerID = -channelID; - var inputPeer = AppPeersManager.getInputPeerByID(peerID); return $q.all([ AppProfileManager.getChannelFull(channelID, true), - getHistory(inputPeer, 0) + getHistory(peerID, 0) ]).then(function (results) { var channelResult = results[0]; var historyResult = results[1]; diff --git a/app/js/services.js b/app/js/services.js index 1a367287..4bbae001 100755 --- a/app/js/services.js +++ b/app/js/services.js @@ -599,7 +599,7 @@ angular.module('myApp.services', ['myApp.i18n', 'izhukov.utils']) }; function getChat (id) { - return chats[id] || {id: id, deleted: true}; + return chats[id] || {id: id, deleted: true, access_hash: channelAccess[id]}; } function hasRights (id, action) {