mirror of
https://github.com/Swiftgram/Telegram-iOS.git
synced 2025-10-09 03:20:48 +00:00
Delay cached data update until peer becomes available
This commit is contained in:
parent
7dc57e0bfa
commit
b80074c2cf
@ -772,9 +772,22 @@ public final class AccountViewTracker {
|
||||
self.cachedDataContexts[peerId] = context
|
||||
}
|
||||
context.timestamp = CFAbsoluteTimeGetCurrent()
|
||||
if let account = self.account {
|
||||
context.disposable.set(combineLatest(fetchAndUpdateSupplementalCachedPeerData(peerId: peerId, network: account.network, postbox: account.postbox), fetchAndUpdateCachedPeerData(accountPeerId: account.peerId, peerId: peerId, network: account.network, postbox: account.postbox)).start())
|
||||
guard let account = self.account else {
|
||||
return
|
||||
}
|
||||
let queue = self.queue
|
||||
context.disposable.set(combineLatest(fetchAndUpdateSupplementalCachedPeerData(peerId: peerId, network: account.network, postbox: account.postbox), fetchAndUpdateCachedPeerData(accountPeerId: account.peerId, peerId: peerId, network: account.network, postbox: account.postbox)).start(next: { [weak self] supplementalStatus, cachedStatus in
|
||||
queue.async {
|
||||
guard let strongSelf = self else {
|
||||
return
|
||||
}
|
||||
if !supplementalStatus || !cachedStatus {
|
||||
if let existingContext = strongSelf.cachedDataContexts[peerId] {
|
||||
existingContext.timestamp = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
@ -801,9 +814,22 @@ public final class AccountViewTracker {
|
||||
context.viewIds.insert(viewId)
|
||||
|
||||
if dataUpdated {
|
||||
if let account = self.account {
|
||||
context.disposable.set(combineLatest(fetchAndUpdateSupplementalCachedPeerData(peerId: peerId, network: account.network, postbox: account.postbox), fetchAndUpdateCachedPeerData(accountPeerId: account.peerId, peerId: peerId, network: account.network, postbox: account.postbox)).start())
|
||||
guard let account = self.account else {
|
||||
return
|
||||
}
|
||||
let queue = self.queue
|
||||
context.disposable.set(combineLatest(fetchAndUpdateSupplementalCachedPeerData(peerId: peerId, network: account.network, postbox: account.postbox), fetchAndUpdateCachedPeerData(accountPeerId: account.peerId, peerId: peerId, network: account.network, postbox: account.postbox)).start(next: { [weak self] supplementalStatus, cachedStatus in
|
||||
queue.async {
|
||||
guard let strongSelf = self else {
|
||||
return
|
||||
}
|
||||
if !supplementalStatus || !cachedStatus {
|
||||
if let existingContext = strongSelf.cachedDataContexts[peerId] {
|
||||
existingContext.timestamp = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,19 +2,30 @@ import Foundation
|
||||
import Postbox
|
||||
import TelegramApi
|
||||
import SwiftSignalKit
|
||||
|
||||
import SyncCore
|
||||
|
||||
func fetchAndUpdateSupplementalCachedPeerData(peerId rawPeerId: PeerId, network: Network, postbox: Postbox) -> Signal<Void, NoError> {
|
||||
return postbox.transaction { transaction -> Signal<Void, NoError> in
|
||||
guard let rawPeer = transaction.getPeer(rawPeerId) else {
|
||||
func fetchAndUpdateSupplementalCachedPeerData(peerId rawPeerId: PeerId, network: Network, postbox: Postbox) -> Signal<Bool, NoError> {
|
||||
return postbox.combinedView(keys: [.basicPeer(rawPeerId)])
|
||||
|> mapToSignal { views -> Signal<Peer, NoError> in
|
||||
guard let view = views.views[.basicPeer(rawPeerId)] as? BasicPeerView else {
|
||||
return .complete()
|
||||
}
|
||||
guard let peer = view.peer else {
|
||||
return .complete()
|
||||
}
|
||||
return .single(peer)
|
||||
}
|
||||
|> take(1)
|
||||
|> mapToSignal { _ -> Signal<Bool, NoError> in
|
||||
return postbox.transaction { transaction -> Signal<Bool, NoError> in
|
||||
guard let rawPeer = transaction.getPeer(rawPeerId) else {
|
||||
return .single(false)
|
||||
}
|
||||
|
||||
let peer: Peer
|
||||
if let secretChat = rawPeer as? TelegramSecretChat {
|
||||
guard let user = transaction.getPeer(secretChat.regularPeerId) else {
|
||||
return .complete()
|
||||
return .single(false)
|
||||
}
|
||||
peer = user
|
||||
} else {
|
||||
@ -25,24 +36,24 @@ func fetchAndUpdateSupplementalCachedPeerData(peerId rawPeerId: PeerId, network:
|
||||
|
||||
if let cachedData = cachedData as? CachedUserData {
|
||||
if cachedData.peerStatusSettings != nil {
|
||||
return .complete()
|
||||
return .single(true)
|
||||
}
|
||||
} else if let cachedData = cachedData as? CachedGroupData {
|
||||
if cachedData.peerStatusSettings != nil {
|
||||
return .complete()
|
||||
return .single(true)
|
||||
}
|
||||
} else if let cachedData = cachedData as? CachedChannelData {
|
||||
if cachedData.peerStatusSettings != nil {
|
||||
return .complete()
|
||||
return .single(true)
|
||||
}
|
||||
} else if let cachedData = cachedData as? CachedSecretChatData {
|
||||
if cachedData.peerStatusSettings != nil {
|
||||
return .complete()
|
||||
return .single(true)
|
||||
}
|
||||
}
|
||||
|
||||
if peer.id.namespace == Namespaces.Peer.SecretChat {
|
||||
return postbox.transaction { transaction -> Void in
|
||||
return postbox.transaction { transaction -> Bool in
|
||||
var peerStatusSettings: PeerStatusSettings
|
||||
if let peer = transaction.getPeer(peer.id), let associatedPeerId = peer.associatedPeerId, !transaction.isPeerContact(peerId: associatedPeerId) {
|
||||
if let peer = peer as? TelegramSecretChat, case .creator = peer.role {
|
||||
@ -64,14 +75,16 @@ func fetchAndUpdateSupplementalCachedPeerData(peerId rawPeerId: PeerId, network:
|
||||
return CachedSecretChatData(peerStatusSettings: peerStatusSettings)
|
||||
}
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
} else if let inputPeer = apiInputPeer(peer) {
|
||||
return network.request(Api.functions.messages.getPeerSettings(peer: inputPeer))
|
||||
|> retryRequest
|
||||
|> mapToSignal { peerSettings -> Signal<Void, NoError> in
|
||||
|> mapToSignal { peerSettings -> Signal<Bool, NoError> in
|
||||
let peerStatusSettings = PeerStatusSettings(apiSettings: peerSettings)
|
||||
|
||||
return postbox.transaction { transaction -> Void in
|
||||
return postbox.transaction { transaction -> Bool in
|
||||
transaction.updatePeerCachedData(peerIds: Set([peer.id]), update: { _, current in
|
||||
switch peer.id.namespace {
|
||||
case Namespaces.Peer.CloudUser:
|
||||
@ -103,16 +116,30 @@ func fetchAndUpdateSupplementalCachedPeerData(peerId rawPeerId: PeerId, network:
|
||||
}
|
||||
return current
|
||||
})
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return .complete()
|
||||
return .single(false)
|
||||
}
|
||||
}
|
||||
|> switchToLatest
|
||||
}
|
||||
}
|
||||
|
||||
func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId: PeerId, network: Network, postbox: Postbox) -> Signal<Void, NoError> {
|
||||
func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId: PeerId, network: Network, postbox: Postbox) -> Signal<Bool, NoError> {
|
||||
return postbox.combinedView(keys: [.basicPeer(rawPeerId)])
|
||||
|> mapToSignal { views -> Signal<Peer, NoError> in
|
||||
guard let view = views.views[.basicPeer(rawPeerId)] as? BasicPeerView else {
|
||||
return .complete()
|
||||
}
|
||||
guard let peer = view.peer else {
|
||||
return .complete()
|
||||
}
|
||||
return .single(peer)
|
||||
}
|
||||
|> take(1)
|
||||
|> mapToSignal { _ -> Signal<Bool, NoError> in
|
||||
return postbox.transaction { transaction -> (Api.InputUser?, Peer?, PeerId) in
|
||||
guard let rawPeer = transaction.getPeer(rawPeerId) else {
|
||||
if rawPeerId == accountPeerId {
|
||||
@ -138,12 +165,12 @@ func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId: PeerI
|
||||
return (apiInputUser(peer), peer, peer.id)
|
||||
}
|
||||
}
|
||||
|> mapToSignal { inputUser, maybePeer, peerId -> Signal<Void, NoError> in
|
||||
|> mapToSignal { inputUser, maybePeer, peerId -> Signal<Bool, NoError> in
|
||||
if let inputUser = inputUser {
|
||||
return network.request(Api.functions.users.getFullUser(id: inputUser))
|
||||
|> retryRequest
|
||||
|> mapToSignal { result -> Signal<Void, NoError> in
|
||||
return postbox.transaction { transaction -> Void in
|
||||
|> mapToSignal { result -> Signal<Bool, NoError> in
|
||||
return postbox.transaction { transaction -> Bool in
|
||||
switch result {
|
||||
case let .userFull(userFull):
|
||||
let telegramUser = TelegramUser(user: userFull.user)
|
||||
@ -181,13 +208,14 @@ func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId: PeerI
|
||||
return previous.withUpdatedAbout(userFull.about).withUpdatedBotInfo(botInfo).withUpdatedCommonGroupCount(userFull.commonChatsCount).withUpdatedIsBlocked(isBlocked).withUpdatedCallsAvailable(callsAvailable).withUpdatedCallsPrivate(callsPrivate).withUpdatedCanPinMessages(canPinMessages).withUpdatedPeerStatusSettings(peerStatusSettings).withUpdatedPinnedMessageId(pinnedMessageId).withUpdatedHasScheduledMessages(hasScheduledMessages)
|
||||
}
|
||||
})
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else if peerId.namespace == Namespaces.Peer.CloudGroup {
|
||||
return network.request(Api.functions.messages.getFullChat(chatId: peerId.id))
|
||||
|> retryRequest
|
||||
|> mapToSignal { result -> Signal<Void, NoError> in
|
||||
return postbox.transaction { transaction -> Void in
|
||||
|> mapToSignal { result -> Signal<Bool, NoError> in
|
||||
return postbox.transaction { transaction -> Bool in
|
||||
switch result {
|
||||
case let .chatFull(fullChat, chats, users):
|
||||
switch fullChat {
|
||||
@ -263,6 +291,7 @@ func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId: PeerI
|
||||
break
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else if let inputChannel = maybePeer.flatMap(apiInputChannel) {
|
||||
@ -272,10 +301,10 @@ func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId: PeerI
|
||||
if error.errorDescription == "CHANNEL_PRIVATE" {
|
||||
return .single(nil)
|
||||
}
|
||||
return .complete()
|
||||
return .single(nil)
|
||||
}
|
||||
|> mapToSignal { result -> Signal<Void, NoError> in
|
||||
return postbox.transaction { transaction -> Void in
|
||||
|> mapToSignal { result -> Signal<Bool, NoError> in
|
||||
return postbox.transaction { transaction -> Bool in
|
||||
if let result = result {
|
||||
switch result {
|
||||
case let .chatFull(fullChat, chats, users):
|
||||
@ -440,10 +469,12 @@ func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId rawPeerId: PeerI
|
||||
return updated
|
||||
})
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return .complete()
|
||||
return .single(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user