From fe13574c44bf231c7f28b0539229fea2783cc595 Mon Sep 17 00:00:00 2001 From: Peter <> Date: Sat, 25 May 2019 00:07:04 +0200 Subject: [PATCH] Discussion group updates --- TelegramCore/AccountViewTracker.swift | 14 +++-- TelegramCore/CachedChannelData.swift | 56 +++++++++---------- TelegramCore/ChannelAdminEventLogs.swift | 3 +- TelegramCore/ChatHistoryPreloadManager.swift | 3 + .../ManageChannelDiscussionGroup.swift | 25 ++++++--- TelegramCore/UpdateCachedPeerData.swift | 8 +-- 6 files changed, 64 insertions(+), 45 deletions(-) diff --git a/TelegramCore/AccountViewTracker.swift b/TelegramCore/AccountViewTracker.swift index 79b3694a4c..a6fca3056c 100644 --- a/TelegramCore/AccountViewTracker.swift +++ b/TelegramCore/AccountViewTracker.swift @@ -742,7 +742,6 @@ public final class AccountViewTracker { func forceUpdateCachedPeerData(peerId: PeerId) { self.queue.async { let context: PeerCachedDataContext - var dataUpdated = false if let existingContext = self.cachedDataContexts[peerId] { context = existingContext } else { @@ -928,7 +927,14 @@ public final class AccountViewTracker { } } - func wrappedPeerViewSignal(peerId: PeerId, signal: Signal) -> Signal { + func wrappedPeerViewSignal(peerId: PeerId, signal: Signal, updateData: Bool) -> Signal { + if updateData { + self.queue.async { + if let existingContext = self.cachedDataContexts[peerId] { + existingContext.timestamp = nil + } + } + } return withState(signal, { [weak self] () -> Int32 in if let strongSelf = self { return OSAtomicIncrement32(&strongSelf.nextViewId) @@ -946,9 +952,9 @@ public final class AccountViewTracker { }) } - public func peerView(_ peerId: PeerId) -> Signal { + public func peerView(_ peerId: PeerId, updateData: Bool = false) -> Signal { if let account = self.account { - return wrappedPeerViewSignal(peerId: peerId, signal: account.postbox.peerView(id: peerId)) + return wrappedPeerViewSignal(peerId: peerId, signal: account.postbox.peerView(id: peerId), updateData: updateData) } else { return .never() } diff --git a/TelegramCore/CachedChannelData.swift b/TelegramCore/CachedChannelData.swift index e6ab58318c..a1185ff12f 100644 --- a/TelegramCore/CachedChannelData.swift +++ b/TelegramCore/CachedChannelData.swift @@ -138,7 +138,7 @@ public final class CachedChannelData: CachedPeerData { public let minAvailableMessageId: MessageId? public let migrationReference: ChannelMigrationReference? - public let associatedPeerId:PeerId? + public let linkedDiscussionPeerId: PeerId? public let peerIds: Set public let messageIds: Set @@ -160,10 +160,10 @@ public final class CachedChannelData: CachedPeerData { self.stickerPack = nil self.minAvailableMessageId = nil self.migrationReference = nil - self.associatedPeerId = nil + self.linkedDiscussionPeerId = nil } - init(isNotAccessible: Bool, flags: CachedChannelFlags, about: String?, participantsSummary: CachedChannelParticipantsSummary, exportedInvitation: ExportedInvitation?, botInfos: [CachedPeerBotInfo], reportStatus: PeerReportStatus, pinnedMessageId: MessageId?, stickerPack: StickerPackCollectionInfo?, minAvailableMessageId: MessageId?, migrationReference: ChannelMigrationReference?, associatedPeerId: PeerId?) { + init(isNotAccessible: Bool, flags: CachedChannelFlags, about: String?, participantsSummary: CachedChannelParticipantsSummary, exportedInvitation: ExportedInvitation?, botInfos: [CachedPeerBotInfo], reportStatus: PeerReportStatus, pinnedMessageId: MessageId?, stickerPack: StickerPackCollectionInfo?, minAvailableMessageId: MessageId?, migrationReference: ChannelMigrationReference?, linkedDiscussionPeerId: PeerId?) { self.isNotAccessible = isNotAccessible self.flags = flags self.about = about @@ -175,15 +175,15 @@ public final class CachedChannelData: CachedPeerData { self.stickerPack = stickerPack self.minAvailableMessageId = minAvailableMessageId self.migrationReference = migrationReference - self.associatedPeerId = associatedPeerId + self.linkedDiscussionPeerId = linkedDiscussionPeerId var peerIds = Set() for botInfo in botInfos { peerIds.insert(botInfo.peerId) } - if let associatedPeerId = associatedPeerId { - peerIds.insert(associatedPeerId) + if let linkedDiscussionPeerId = linkedDiscussionPeerId { + peerIds.insert(linkedDiscussionPeerId) } self.peerIds = peerIds @@ -196,51 +196,51 @@ public final class CachedChannelData: CachedPeerData { } func withUpdatedIsNotAccessible(_ isNotAccessible: Bool) -> CachedChannelData { - return CachedChannelData(isNotAccessible: isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedFlags(_ flags: CachedChannelFlags) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedAbout(_ about: String?) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedParticipantsSummary(_ participantsSummary: CachedChannelParticipantsSummary) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedExportedInvitation(_ exportedInvitation: ExportedInvitation?) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedBotInfos(_ botInfos: [CachedPeerBotInfo]) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedReportStatus(_ reportStatus: PeerReportStatus) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedPinnedMessageId(_ pinnedMessageId: MessageId?) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedStickerPack(_ stickerPack: StickerPackCollectionInfo?) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedMinAvailableMessageId(_ minAvailableMessageId: MessageId?) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } func withUpdatedMigrationReference(_ migrationReference: ChannelMigrationReference?) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: migrationReference, associatedPeerId: self.associatedPeerId) + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: migrationReference, linkedDiscussionPeerId: self.linkedDiscussionPeerId) } - func withUpdatedAssociatedPeerId(_ groupId: PeerId?) -> CachedChannelData { - return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, associatedPeerId: groupId) + func withUpdatedLinkedDiscussionPeerId(_ linkedDiscussionPeerId: PeerId?) -> CachedChannelData { + return CachedChannelData(isNotAccessible: self.isNotAccessible, flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: self.stickerPack, minAvailableMessageId: self.minAvailableMessageId, migrationReference: self.migrationReference, linkedDiscussionPeerId: linkedDiscussionPeerId) } public init(decoder: PostboxDecoder) { @@ -276,14 +276,14 @@ public final class CachedChannelData: CachedPeerData { peerIds.insert(botInfo.peerId) } - if let associatedPeerId = decoder.decodeOptionalInt64ForKey("dgi") { - self.associatedPeerId = PeerId(associatedPeerId) + if let linkedDiscussionPeerId = decoder.decodeOptionalInt64ForKey("dgi") { + self.linkedDiscussionPeerId = PeerId(linkedDiscussionPeerId) } else { - self.associatedPeerId = nil + self.linkedDiscussionPeerId = nil } - if let associatedPeerId = associatedPeerId { - peerIds.insert(associatedPeerId) + if let linkedDiscussionPeerId = self.linkedDiscussionPeerId { + peerIds.insert(linkedDiscussionPeerId) } self.peerIds = peerIds @@ -293,8 +293,6 @@ public final class CachedChannelData: CachedPeerData { messageIds.insert(pinnedMessageId) } self.messageIds = messageIds - - } public func encode(_ encoder: PostboxEncoder) { @@ -341,8 +339,8 @@ public final class CachedChannelData: CachedPeerData { } else { encoder.encodeNil(forKey: "mr") } - if let associatedPeerId = self.associatedPeerId { - encoder.encodeInt64(associatedPeerId.toInt64(), forKey: "dgi") + if let linkedDiscussionPeerId = self.linkedDiscussionPeerId { + encoder.encodeInt64(linkedDiscussionPeerId.toInt64(), forKey: "dgi") } else { encoder.encodeNil(forKey: "dgi") } @@ -361,7 +359,7 @@ public final class CachedChannelData: CachedPeerData { return false } - if other.associatedPeerId != self.associatedPeerId { + if other.linkedDiscussionPeerId != self.linkedDiscussionPeerId { return false } diff --git a/TelegramCore/ChannelAdminEventLogs.swift b/TelegramCore/ChannelAdminEventLogs.swift index 965440978d..80e1110ce9 100644 --- a/TelegramCore/ChannelAdminEventLogs.swift +++ b/TelegramCore/ChannelAdminEventLogs.swift @@ -55,6 +55,7 @@ public enum AdminLogEventAction { case updateDefaultBannedRights(prev: TelegramChatBannedRights, new: TelegramChatBannedRights ) case pollStopped(Message) + case linkedPeerUpdated(previous: Peer?, updated: Peer?) } public enum ChannelAdminLogEventError { @@ -208,7 +209,7 @@ public func channelAdminLogEvents(postbox: Postbox, network: Network, peerId: Pe action = .pollStopped(rendered) } case let .channelAdminLogEventActionChangeLinkedChat(prevValue, newValue): - break + action = .linkedPeerUpdated(previous: prevValue == 0 ? nil : peers[PeerId(namespace: Namespaces.Peer.CloudChannel, id: prevValue)], updated: newValue == 0 ? nil : peers[PeerId(namespace: Namespaces.Peer.CloudChannel, id: newValue)]) } let peerId = PeerId(namespace: Namespaces.Peer.CloudUser, id: userId) if let action = action { diff --git a/TelegramCore/ChatHistoryPreloadManager.swift b/TelegramCore/ChatHistoryPreloadManager.swift index 1db39a70d1..7c19c1c238 100644 --- a/TelegramCore/ChatHistoryPreloadManager.swift +++ b/TelegramCore/ChatHistoryPreloadManager.swift @@ -223,6 +223,9 @@ final class ChatHistoryPreloadManager { guard let strongSelf = self else { return } + #if DEBUG + return; + #endif var indices: [(ChatHistoryPreloadIndex, Bool, Bool)] = [] for entry in view.0.entries { if case let .MessageEntry(index, _, readState, notificationSettings, _, _, _, _) = entry { diff --git a/TelegramCore/ManageChannelDiscussionGroup.swift b/TelegramCore/ManageChannelDiscussionGroup.swift index 9bbd21acc1..fad5800477 100644 --- a/TelegramCore/ManageChannelDiscussionGroup.swift +++ b/TelegramCore/ManageChannelDiscussionGroup.swift @@ -56,8 +56,6 @@ public enum ChannelDiscussionGroupError { case generic } - -//channels.setDiscussionGroup broadcast:InputChannel group:InputChannel = Bool; public func updateGroupDiscussionForChannel(network: Network, postbox: Postbox, channelId: PeerId, groupId: PeerId?) -> Signal { return postbox.transaction { transaction -> (channel: Peer?, group: Peer?) in @@ -81,18 +79,31 @@ public func updateGroupDiscussionForChannel(network: Network, postbox: Postbox, } |> map { result in switch result { - case .boolTrue: - return true - case .boolFalse: - return false + case .boolTrue: + return true + case .boolFalse: + return false } } } |> mapToSignal { result in if result { return postbox.transaction { transaction in + var previousGroupId: PeerId? transaction.updatePeerCachedData(peerIds: Set([channelId]), update: { (_, current) -> CachedPeerData? in - return (current as? CachedChannelData ?? CachedChannelData()).withUpdatedAssociatedPeerId(groupId) + let current: CachedChannelData = current as? CachedChannelData ?? CachedChannelData() + previousGroupId = current.linkedDiscussionPeerId + return current.withUpdatedLinkedDiscussionPeerId(groupId) }) + if let previousGroupId = previousGroupId { + transaction.updatePeerCachedData(peerIds: Set([previousGroupId]), update: { (_, current) -> CachedPeerData? in + return (current as? CachedChannelData ?? CachedChannelData()).withUpdatedLinkedDiscussionPeerId(nil) + }) + } + if let groupId = groupId { + transaction.updatePeerCachedData(peerIds: Set([groupId]), update: { (_, current) -> CachedPeerData? in + return (current as? CachedChannelData ?? CachedChannelData()).withUpdatedLinkedDiscussionPeerId(channelId) + }) + } } |> introduceError(ChannelDiscussionGroupError.self) |> map { _ in diff --git a/TelegramCore/UpdateCachedPeerData.swift b/TelegramCore/UpdateCachedPeerData.swift index ddf9d0df0c..9acef644d4 100644 --- a/TelegramCore/UpdateCachedPeerData.swift +++ b/TelegramCore/UpdateCachedPeerData.swift @@ -271,12 +271,12 @@ func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId: PeerId, network channelFlags.insert(.canSetStickerSet) } - let associatedPeerId: PeerId? + let linkedDiscussionPeerId: PeerId? if let linkedChatId = linkedChatId { - associatedPeerId = PeerId(namespace: Namespaces.Peer.CloudChannel, id: linkedChatId) + linkedDiscussionPeerId = PeerId(namespace: Namespaces.Peer.CloudChannel, id: linkedChatId) } else { - associatedPeerId = nil + linkedDiscussionPeerId = nil } var botInfos: [CachedPeerBotInfo] = [] @@ -365,7 +365,7 @@ func fetchAndUpdateCachedPeerData(accountPeerId: PeerId, peerId: PeerId, network .withUpdatedStickerPack(stickerPack) .withUpdatedMinAvailableMessageId(minAvailableMessageId) .withUpdatedMigrationReference(migrationReference) - .withUpdatedAssociatedPeerId(associatedPeerId) + .withUpdatedLinkedDiscussionPeerId(linkedDiscussionPeerId) }) if let minAvailableMessageId = minAvailableMessageId, minAvailableMessageIdUpdated {