diff --git a/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m b/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m index 882de632f3..05d22b5f91 100644 --- a/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m +++ b/Telegram/NotificationService/NotificationServiceObjC/Sources/Serialization.m @@ -3,7 +3,7 @@ @implementation Serialization - (NSUInteger)currentLayer { - return 129; + return 131; } - (id _Nullable)parseMessage:(NSData * _Nullable)data { diff --git a/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTRequestErrorContext.h b/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTRequestErrorContext.h index fc190383be..b1e9361e08 100644 --- a/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTRequestErrorContext.h +++ b/submodules/MtProtoKit/PublicHeaders/MtProtoKit/MTRequestErrorContext.h @@ -10,5 +10,6 @@ @property (nonatomic) NSUInteger floodWaitSeconds; @property (nonatomic) bool waitingForTokenExport; +@property (nonatomic, strong) id waitingForRequestToComplete; @end diff --git a/submodules/MtProtoKit/Sources/MTRequestMessageService.m b/submodules/MtProtoKit/Sources/MTRequestMessageService.m index 2a3d4963d6..2420074e9f 100644 --- a/submodules/MtProtoKit/Sources/MTRequestMessageService.m +++ b/submodules/MtProtoKit/Sources/MTRequestMessageService.m @@ -196,6 +196,20 @@ { if (request.errorContext != nil) { + if (request.errorContext.waitingForRequestToComplete != nil) { + bool foundDependency = false; + for (MTRequest *anotherRequest in _requests) { + if (request.errorContext.waitingForRequestToComplete == anotherRequest.internalId) { + foundDependency = true; + break; + } + } + + if (!foundDependency) { + needTransaction = true; + } + } + if (request.requestContext == nil) { if (request.errorContext.minimalExecuteTime > currentTime + DBL_EPSILON) @@ -407,10 +421,23 @@ if (request.errorContext != nil) { - if (request.errorContext.minimalExecuteTime > currentTime) + if (request.errorContext.minimalExecuteTime > currentTime) { continue; - if (request.errorContext.waitingForTokenExport) + } + if (request.errorContext.waitingForTokenExport) { continue; + } + + bool foundDependency = false; + for (MTRequest *anotherRequest in _requests) { + if (request.errorContext.waitingForRequestToComplete == anotherRequest.internalId) { + foundDependency = true; + break; + } + } + if (foundDependency) { + continue; + } } if (request.requestContext == nil || (!request.requestContext.waitingForMessageId && !request.requestContext.delivered && request.requestContext.transactionId == nil)) @@ -690,6 +717,20 @@ request.errorContext.minimalExecuteTime = MAX(request.errorContext.minimalExecuteTime, MTAbsoluteSystemTime() + 2.0); } } + else if (rpcError.errorCode == 400 || [rpcError.errorDescription isEqualToString:@"MSG_WAIT_TIMEOUT"]) + { + if (request.errorContext == nil) { + request.errorContext = [[MTRequestErrorContext alloc] init]; + } + + for (MTRequest *anotherRequest in _requests) { + if (request.shouldDependOnRequest != nil && request.shouldDependOnRequest(anotherRequest)) { + request.errorContext.waitingForRequestToComplete = anotherRequest.internalId; + break; + } + } + restartRequest = true; + } else if (rpcError.errorCode == 420 || [rpcError.errorDescription rangeOfString:@"FLOOD_WAIT_"].location != NSNotFound) { if (request.errorContext == nil) diff --git a/submodules/TelegramCore/Sources/State/AccountViewTracker.swift b/submodules/TelegramCore/Sources/State/AccountViewTracker.swift index 5374bffdcd..8d32c69bee 100644 --- a/submodules/TelegramCore/Sources/State/AccountViewTracker.swift +++ b/submodules/TelegramCore/Sources/State/AccountViewTracker.swift @@ -1410,22 +1410,37 @@ public final class AccountViewTracker { let polled = self.polledChannel(peerId: peerId).start() var addHole = false - let historyIsValid: Signal + let pollingCompleted: Signal if let context = self.channelPollingContexts[peerId] { if !context.isUpdatedValue { addHole = true } - historyIsValid = context.isUpdated.get() + pollingCompleted = context.isUpdated.get() } else { addHole = true - historyIsValid = .single(true) + pollingCompleted = .single(true) } + let isAutomaticallyTracked = Promise(false) if addHole { - let _ = self.account?.postbox.transaction({ transaction -> Void in + let _ = (self.account!.postbox.transaction { transaction -> Bool in if transaction.getPeerChatListIndex(peerId) == nil { transaction.addHole(peerId: peerId, namespace: Namespaces.Message.Cloud, space: .everywhere, range: 1 ... (Int32.max - 1)) + return false + } else { + return true } - }).start() + } + |> deliverOn(self.queue)).start(next: { isTracked in + isAutomaticallyTracked.set(.single(isTracked)) + }) + } + + let historyIsValid = combineLatest(queue: self.queue, + pollingCompleted, + isAutomaticallyTracked.get() + ) + |> map { lhs, rhs -> Bool in + return lhs || rhs } let validHistory = historyIsValid diff --git a/submodules/TelegramCore/Sources/State/Serialization.swift b/submodules/TelegramCore/Sources/State/Serialization.swift index 2d992c7f59..96684c4e81 100644 --- a/submodules/TelegramCore/Sources/State/Serialization.swift +++ b/submodules/TelegramCore/Sources/State/Serialization.swift @@ -210,7 +210,7 @@ public class BoxedMessage: NSObject { public class Serialization: NSObject, MTSerialization { public func currentLayer() -> UInt { - return 130 + return 131 } public func parseMessage(_ data: Data!) -> Any! { diff --git a/submodules/TelegramUI/Sources/ChatControllerNode.swift b/submodules/TelegramUI/Sources/ChatControllerNode.swift index 4c22d96741..b49f29d649 100644 --- a/submodules/TelegramUI/Sources/ChatControllerNode.swift +++ b/submodules/TelegramUI/Sources/ChatControllerNode.swift @@ -2331,10 +2331,16 @@ class ChatControllerNode: ASDisplayNode, UIScrollViewDelegate { } else { webpage = self.chatPresentationInterfaceState.urlPreview?.1 } - #if DEBUG - //webpage = nil - #endif + messages.append(.message(text: text.string, attributes: attributes, mediaReference: webpage.flatMap(AnyMediaReference.standalone), replyToMessageId: self.chatPresentationInterfaceState.interfaceState.replyMessageId, localGroupingKey: nil, correlationId: nil)) + + #if DEBUG + if text.string == "sleep" { + messages.append(messages[0]) + messages.append(messages[0]) + messages.append(messages[0]) + } + #endif } } diff --git a/submodules/TelegramUI/Sources/ChatHistoryViewForLocation.swift b/submodules/TelegramUI/Sources/ChatHistoryViewForLocation.swift index 6a60a30845..7f42d9f06b 100644 --- a/submodules/TelegramUI/Sources/ChatHistoryViewForLocation.swift +++ b/submodules/TelegramUI/Sources/ChatHistoryViewForLocation.swift @@ -93,15 +93,13 @@ func chatHistoryViewForLocation(_ location: ChatHistoryLocationInput, context: A let (cachedData, cachedDataMessages, readStateData) = extractAdditionalData(view: view, chatLocation: chatLocation) let combinedInitialData = ChatHistoryCombinedInitialData(initialData: initialData, buttonKeyboardMessage: view.topTaggedMessages.first, cachedData: cachedData, cachedDataMessages: cachedDataMessages, readStateData: readStateData) - - if view.isLoading { - preloaded = false - return .Loading(initialData: combinedInitialData, type: .Generic(type: updateType)) - } if preloaded { return .HistoryView(view: view, type: .Generic(type: updateType), scrollPosition: nil, flashIndicators: false, originalScrollPosition: nil, initialData: combinedInitialData, id: location.id) } else { + if view.isLoading { + return .Loading(initialData: combinedInitialData, type: .Generic(type: updateType)) + } var scrollPosition: ChatHistoryViewScrollPosition? let canScrollToRead: Bool @@ -155,6 +153,12 @@ func chatHistoryViewForLocation(_ location: ChatHistoryLocationInput, context: A } else if view.isAddedToChatList, let historyScrollState = (initialData?.chatInterfaceState as? ChatInterfaceState)?.historyScrollState, tagMask == nil { scrollPosition = .positionRestoration(index: historyScrollState.messageIndex, relativeOffset: CGFloat(historyScrollState.relativeOffset)) } else { + if case .peer = chatLocation, !view.isAddedToChatList { + if view.holeEarlier && view.entries.count <= 2 { + fadeIn = true + return .Loading(initialData: combinedInitialData, type: .Generic(type: updateType)) + } + } if view.entries.isEmpty && (view.holeEarlier || view.holeLater) { fadeIn = true return .Loading(initialData: combinedInitialData, type: .Generic(type: updateType)) @@ -181,11 +185,6 @@ func chatHistoryViewForLocation(_ location: ChatHistoryLocationInput, context: A let (cachedData, cachedDataMessages, readStateData) = extractAdditionalData(view: view, chatLocation: chatLocation) let combinedInitialData = ChatHistoryCombinedInitialData(initialData: initialData, buttonKeyboardMessage: view.topTaggedMessages.first, cachedData: cachedData, cachedDataMessages: cachedDataMessages, readStateData: readStateData) - - if view.isLoading { - preloaded = false - return .Loading(initialData: combinedInitialData, type: .Generic(type: updateType)) - } if preloaded { return .HistoryView(view: view, type: .Generic(type: updateType), scrollPosition: nil, flashIndicators: false, originalScrollPosition: nil, initialData: combinedInitialData, id: location.id)