diff --git a/TelegramCore/AccountStateManagementUtils.swift b/TelegramCore/AccountStateManagementUtils.swift index c7210de701..a8ba6451b7 100644 --- a/TelegramCore/AccountStateManagementUtils.swift +++ b/TelegramCore/AccountStateManagementUtils.swift @@ -1081,7 +1081,7 @@ private func finalStateWithUpdatesAndServerTime(account: Account, state: Account return peer } }) - case let .updateContactLink(userId, myLink, _): + case let .updateContactLink(userId, myLink, foreignLink): let isContact: Bool switch myLink { case .contactLinkContact: @@ -1089,7 +1089,26 @@ private func finalStateWithUpdatesAndServerTime(account: Account, state: Account default: isContact = false } - updatedState.updatePeerIsContact(PeerId(namespace: Namespaces.Peer.CloudUser, id: userId), isContact: isContact) + let userPeerId = PeerId(namespace: Namespaces.Peer.CloudUser, id: userId) + updatedState.updatePeerIsContact(userPeerId, isContact: isContact) + updatedState.updateCachedPeerData(userPeerId, { current in + let previous: CachedUserData + if let current = current as? CachedUserData { + previous = current + } else { + previous = CachedUserData() + } + let hasPhone: Bool? + switch foreignLink { + case .contactLinkContact, .contactLinkHasPhone: + hasPhone = true + case .contactLinkNone: + hasPhone = false + case .contactLinkUnknown: + hasPhone = nil + } + return previous.withUpdatedHasAccountPeerPhone(hasPhone) + }) case let .updateEncryption(chat, date): updatedState.updateSecretChat(chat: chat, timestamp: date) case let .updateNewEncryptedMessage(message, _): diff --git a/TelegramCore/CachedUserData.swift b/TelegramCore/CachedUserData.swift index b1b9cb1b68..3b757579f0 100644 --- a/TelegramCore/CachedUserData.swift +++ b/TelegramCore/CachedUserData.swift @@ -13,6 +13,7 @@ public final class CachedUserData: CachedPeerData { public let commonGroupCount: Int32 public let callsAvailable: Bool public let callsPrivate: Bool + public let hasAccountPeerPhone: Bool? public let peerIds = Set() public let messageIds = Set() @@ -26,9 +27,10 @@ public final class CachedUserData: CachedPeerData { self.commonGroupCount = 0 self.callsAvailable = false self.callsPrivate = false + self.hasAccountPeerPhone = nil } - init(about: String?, botInfo: BotInfo?, reportStatus: PeerReportStatus, isBlocked: Bool, commonGroupCount: Int32, callsAvailable: Bool, callsPrivate: Bool) { + init(about: String?, botInfo: BotInfo?, reportStatus: PeerReportStatus, isBlocked: Bool, commonGroupCount: Int32, callsAvailable: Bool, callsPrivate: Bool, hasAccountPeerPhone: Bool?) { self.about = about self.botInfo = botInfo self.reportStatus = reportStatus @@ -36,6 +38,7 @@ public final class CachedUserData: CachedPeerData { self.commonGroupCount = commonGroupCount self.callsAvailable = callsAvailable self.callsPrivate = callsPrivate + self.hasAccountPeerPhone = hasAccountPeerPhone } public init(decoder: PostboxDecoder) { @@ -46,6 +49,7 @@ public final class CachedUserData: CachedPeerData { self.commonGroupCount = decoder.decodeInt32ForKey("cg", orElse: 0) self.callsAvailable = decoder.decodeInt32ForKey("ca", orElse: 0) != 0 self.callsPrivate = decoder.decodeInt32ForKey("cp", orElse: 0) != 0 + self.hasAccountPeerPhone = decoder.decodeOptionalInt32ForKey("hp").flatMap({ $0 != 0 }) } public func encode(_ encoder: PostboxEncoder) { @@ -64,6 +68,11 @@ public final class CachedUserData: CachedPeerData { encoder.encodeInt32(self.commonGroupCount, forKey: "cg") encoder.encodeInt32(self.callsAvailable ? 1 : 0, forKey: "ca") encoder.encodeInt32(self.callsPrivate ? 1 : 0, forKey: "cp") + if let hasAccountPeerPhone = self.hasAccountPeerPhone { + encoder.encodeInt32(hasAccountPeerPhone ? 1 : 0, forKey: "hp") + } else { + encoder.encodeNil(forKey: "hp") + } } public func isEqual(to: CachedPeerData) -> Bool { @@ -71,34 +80,38 @@ public final class CachedUserData: CachedPeerData { return false } - return other.about == self.about && other.botInfo == self.botInfo && self.reportStatus == other.reportStatus && self.isBlocked == other.isBlocked && self.commonGroupCount == other.commonGroupCount && self.callsAvailable == other.callsAvailable && self.callsPrivate == other.callsPrivate + return other.about == self.about && other.botInfo == self.botInfo && self.reportStatus == other.reportStatus && self.isBlocked == other.isBlocked && self.commonGroupCount == other.commonGroupCount && self.callsAvailable == other.callsAvailable && self.callsPrivate == other.callsPrivate && self.hasAccountPeerPhone == other.hasAccountPeerPhone } func withUpdatedAbout(_ about: String?) -> CachedUserData { - return CachedUserData(about: about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate) + return CachedUserData(about: about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate, hasAccountPeerPhone: self.hasAccountPeerPhone) } func withUpdatedBotInfo(_ botInfo: BotInfo?) -> CachedUserData { - return CachedUserData(about: self.about, botInfo: botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate) + return CachedUserData(about: self.about, botInfo: botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate, hasAccountPeerPhone: self.hasAccountPeerPhone) } func withUpdatedReportStatus(_ reportStatus: PeerReportStatus) -> CachedUserData { - return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate) + return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate, hasAccountPeerPhone: self.hasAccountPeerPhone) } func withUpdatedIsBlocked(_ isBlocked: Bool) -> CachedUserData { - return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate) + return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate, hasAccountPeerPhone: self.hasAccountPeerPhone) } func withUpdatedCommonGroupCount(_ commonGroupCount: Int32) -> CachedUserData { - return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate) + return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate, hasAccountPeerPhone: self.hasAccountPeerPhone) } func withUpdatedCallsAvailable(_ callsAvailable: Bool) -> CachedUserData { - return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: callsAvailable, callsPrivate: self.callsPrivate) + return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: callsAvailable, callsPrivate: self.callsPrivate, hasAccountPeerPhone: self.hasAccountPeerPhone) } func withUpdatedCallsPrivate(_ callsPrivate: Bool) -> CachedUserData { - return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: callsPrivate) + return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: callsPrivate, hasAccountPeerPhone: self.hasAccountPeerPhone) + } + + func withUpdatedHasAccountPeerPhone(_ hasAccountPeerPhone: Bool?) -> CachedUserData { + return CachedUserData(about: self.about, botInfo: self.botInfo, reportStatus: self.reportStatus, isBlocked: self.isBlocked, commonGroupCount: self.commonGroupCount, callsAvailable: self.callsAvailable, callsPrivate: self.callsPrivate, hasAccountPeerPhone: hasAccountPeerPhone) } } diff --git a/TelegramCore/UpdateCachedPeerData.swift b/TelegramCore/UpdateCachedPeerData.swift index 63ecba7cb1..79b382fe25 100644 --- a/TelegramCore/UpdateCachedPeerData.swift +++ b/TelegramCore/UpdateCachedPeerData.swift @@ -131,7 +131,7 @@ func fetchAndUpdateCachedPeerData(peerId: PeerId, network: Network, postbox: Pos previous = CachedUserData() } switch result { - case let .userFull(flags, _, about, _, _, _, apiBotInfo, commonChatsCount): + case let .userFull(flags, _, about, link, _, _, apiBotInfo, commonChatsCount): let botInfo: BotInfo? if let apiBotInfo = apiBotInfo { botInfo = BotInfo(apiBotInfo: apiBotInfo) @@ -141,7 +141,20 @@ func fetchAndUpdateCachedPeerData(peerId: PeerId, network: Network, postbox: Pos let isBlocked = (flags & (1 << 0)) != 0 let callsAvailable = (flags & (1 << 4)) != 0 let callsPrivate = (flags & (1 << 5)) != 0 - return previous.withUpdatedAbout(about).withUpdatedBotInfo(botInfo).withUpdatedCommonGroupCount(commonChatsCount).withUpdatedIsBlocked(isBlocked).withUpdatedCallsAvailable(callsAvailable).withUpdatedCallsPrivate(callsPrivate) + let hasPhone: Bool? + switch link { + case let .link(_, foreignLink, _): + switch foreignLink { + case .contactLinkContact, .contactLinkHasPhone: + hasPhone = true + case .contactLinkNone: + hasPhone = false + case .contactLinkUnknown: + hasPhone = nil + } + break + } + return previous.withUpdatedAbout(about).withUpdatedBotInfo(botInfo).withUpdatedCommonGroupCount(commonChatsCount).withUpdatedIsBlocked(isBlocked).withUpdatedCallsAvailable(callsAvailable).withUpdatedCallsPrivate(callsPrivate).withUpdatedHasAccountPeerPhone(hasPhone) } }) }