diff --git a/Postbox/MutableBasicPeerView.swift b/Postbox/MutableBasicPeerView.swift new file mode 100644 index 0000000000..a126580e3e --- /dev/null +++ b/Postbox/MutableBasicPeerView.swift @@ -0,0 +1,33 @@ +import Foundation + +final class MutableBasicPeerView: MutablePostboxView { + private let peerId: PeerId + fileprivate var peer: Peer? + + init(postbox: Postbox, peerId: PeerId) { + self.peerId = peerId + self.peer = postbox.peerTable.get(peerId) + } + + func replay(postbox: Postbox, transaction: PostboxTransaction) -> Bool { + var updated = false + if let peer = transaction.currentUpdatedPeers[self.peerId] { + self.peer = peer + updated = true + } + + return updated + } + + func immutableView() -> PostboxView { + return BasicPeerView(self) + } +} + +public final class BasicPeerView: PostboxView { + public let peer: Peer? + + init(_ view: MutableBasicPeerView) { + self.peer = view.peer + } +} diff --git a/Postbox/PeerView.swift b/Postbox/PeerView.swift index 26ba69baaf..fd389a7fa5 100644 --- a/Postbox/PeerView.swift +++ b/Postbox/PeerView.swift @@ -17,6 +17,7 @@ public struct PeerViewComponents: OptionSet { final class MutablePeerView: MutablePostboxView { let peerId: PeerId + let contactPeerId: PeerId let components: PeerViewComponents var notificationSettings: PeerNotificationSettings? var cachedData: CachedPeerData? @@ -28,8 +29,6 @@ final class MutablePeerView: MutablePostboxView { init(postbox: Postbox, peerId: PeerId, components: PeerViewComponents) { self.components = components - let cachedData = postbox.cachedPeerDataTable.get(peerId) - let peerIsContact = postbox.contactsTable.isContact(peerId: peerId) let getPeer: (PeerId) -> Peer? = { peerId in return postbox.peerTable.get(peerId) @@ -40,16 +39,21 @@ final class MutablePeerView: MutablePostboxView { } self.peerId = peerId - self.cachedData = cachedData - self.peerIsContact = peerIsContact self.groupId = postbox.chatListIndexTable.get(peerId: peerId).inclusion.groupId var peerIds = Set() var messageIds = Set() peerIds.insert(peerId) + if let peer = getPeer(peerId), let associatedPeerId = peer.associatedPeerId { peerIds.insert(associatedPeerId) + self.contactPeerId = associatedPeerId + self.peerIsContact = postbox.contactsTable.isContact(peerId: associatedPeerId) + } else { + self.contactPeerId = peerId } - if let cachedData = cachedData { + self.cachedData = postbox.cachedPeerDataTable.get(contactPeerId) + self.peerIsContact = postbox.contactsTable.isContact(peerId: self.contactPeerId) + if let cachedData = self.cachedData { peerIds.formUnion(cachedData.peerIds) messageIds.formUnion(cachedData.messageIds) } @@ -102,7 +106,7 @@ final class MutablePeerView: MutablePostboxView { var updateMessages = false - if let cachedData = updatedCachedPeerData[self.peerId], self.cachedData == nil || !self.cachedData!.isEqual(to: cachedData) { + if let cachedData = updatedCachedPeerData[self.contactPeerId], self.cachedData == nil || !self.cachedData!.isEqual(to: cachedData) { if self.cachedData?.messageIds != cachedData.messageIds { updateMessages = true } @@ -227,12 +231,12 @@ final class MutablePeerView: MutablePostboxView { if let replaceContactPeerIds = replaceContactPeerIds { if self.peerIsContact { - if !replaceContactPeerIds.contains(self.peerId) { + if !replaceContactPeerIds.contains(self.contactPeerId) { self.peerIsContact = false updated = true } } else { - if replaceContactPeerIds.contains(self.peerId) { + if replaceContactPeerIds.contains(self.contactPeerId) { self.peerIsContact = true updated = true } diff --git a/Postbox/Views.swift b/Postbox/Views.swift index 956a523d04..86e870ac6b 100644 --- a/Postbox/Views.swift +++ b/Postbox/Views.swift @@ -26,6 +26,7 @@ public enum PostboxViewKey: Hashable { case synchronizeGroupMessageStats case peerNotificationSettingsBehaviorTimestampView case peerChatInclusion(PeerId) + case basicPeer(PeerId) public var hashValue: Int { switch self { @@ -79,6 +80,8 @@ public enum PostboxViewKey: Hashable { return 15 case let .peerChatInclusion(peerId): return peerId.hashValue + case let .basicPeer(peerId): + return peerId.hashValue } } @@ -234,6 +237,12 @@ public enum PostboxViewKey: Hashable { } else { return false } + case let .basicPeer(id): + if case .basicPeer(id) = rhs { + return true + } else { + return false + } } } } @@ -290,5 +299,7 @@ func postboxViewForKey(postbox: Postbox, key: PostboxViewKey) -> MutablePostboxV return MutablePeerNotificationSettingsBehaviorTimestampView(postbox: postbox) case let .peerChatInclusion(peerId): return MutablePeerChatInclusionView(postbox: postbox, peerId: peerId) + case let .basicPeer(peerId): + return MutableBasicPeerView(postbox: postbox, peerId: peerId) } } diff --git a/Postbox_Xcode.xcodeproj/project.pbxproj b/Postbox_Xcode.xcodeproj/project.pbxproj index 9101e93058..b661a6eb47 100644 --- a/Postbox_Xcode.xcodeproj/project.pbxproj +++ b/Postbox_Xcode.xcodeproj/project.pbxproj @@ -272,6 +272,8 @@ D0BEAF6E1E54B77900BD963D /* AccountManagerRecordTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0BEAF6C1E54B77900BD963D /* AccountManagerRecordTable.swift */; }; D0BEAF701E54BC1E00BD963D /* AccountRecordsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0BEAF6F1E54BC1E00BD963D /* AccountRecordsView.swift */; }; D0BEAF711E54BC1E00BD963D /* AccountRecordsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0BEAF6F1E54BC1E00BD963D /* AccountRecordsView.swift */; }; + D0BFE51D22AFD5AF00143D08 /* MutableBasicPeerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0BFE51C22AFD5AF00143D08 /* MutableBasicPeerView.swift */; }; + D0BFE51E22AFD5AF00143D08 /* MutableBasicPeerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0BFE51C22AFD5AF00143D08 /* MutableBasicPeerView.swift */; }; D0C07F6A1B67DB4800966E43 /* SwiftSignalKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D0C07F691B67DB4800966E43 /* SwiftSignalKit.framework */; }; D0C0B5AB1EE1AB08000F4D2C /* ReverseAssociatedPeerTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C0B5AA1EE1AB08000F4D2C /* ReverseAssociatedPeerTable.swift */; }; D0C0B5AC1EE1AB08000F4D2C /* ReverseAssociatedPeerTable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0C0B5AA1EE1AB08000F4D2C /* ReverseAssociatedPeerTable.swift */; }; @@ -547,6 +549,7 @@ D0BEAF691E54B5FB00BD963D /* AccountManagerMetadataTable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccountManagerMetadataTable.swift; sourceTree = ""; }; D0BEAF6C1E54B77900BD963D /* AccountManagerRecordTable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccountManagerRecordTable.swift; sourceTree = ""; }; D0BEAF6F1E54BC1E00BD963D /* AccountRecordsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AccountRecordsView.swift; sourceTree = ""; }; + D0BFE51C22AFD5AF00143D08 /* MutableBasicPeerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MutableBasicPeerView.swift; sourceTree = ""; }; D0C07F691B67DB4800966E43 /* SwiftSignalKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SwiftSignalKit.framework; sourceTree = BUILT_PRODUCTS_DIR; }; D0C0B5AA1EE1AB08000F4D2C /* ReverseAssociatedPeerTable.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ReverseAssociatedPeerTable.swift; sourceTree = ""; }; D0C26D7D1FE3FA4E004ABF18 /* PinnedItemId.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PinnedItemId.swift; sourceTree = ""; }; @@ -934,6 +937,7 @@ D0CA8E47227208FE008A74C3 /* SynchronizeGroupMessageStatsView.swift */, D06CA13122772DE40094E707 /* PeerNotificationSettingsBehaviorTimestampView.swift */, D0E1199F229834BC008CAE3A /* MutablePeerChatInclusionView.swift */, + D0BFE51C22AFD5AF00143D08 /* MutableBasicPeerView.swift */, ); name = Views; sourceTree = ""; @@ -1295,6 +1299,7 @@ D0F7B1DD1E045C6A007EB8A5 /* PeerChatInterfaceStateTable.swift in Sources */, D098C6F21D7E1201007784E4 /* Database.swift in Sources */, D07E7B49224E562C00BB053B /* PostboxUpgrade_21to22.swift in Sources */, + D0BFE51E22AFD5AF00143D08 /* MutableBasicPeerView.swift in Sources */, D0B844061DAB91B5005F29E1 /* MediaResourceStatus.swift in Sources */, D0F7B1DB1E045C6A007EB8A5 /* ItemCollectionInfoTable.swift in Sources */, D0F7B1D41E045C6A007EB8A5 /* PeerPresenceTable.swift in Sources */, @@ -1558,6 +1563,7 @@ D0D510F41D63BA8400A97B8A /* PostboxTransaction.swift in Sources */, D0977F9C1B822DB4009994B2 /* ValueBox.swift in Sources */, D07516441B2D9CEF00AE42E0 /* sqlite3.c in Sources */, + D0BFE51D22AFD5AF00143D08 /* MutableBasicPeerView.swift in Sources */, D001388620BD942B007C9721 /* PostboxUpgrade_16to17.swift in Sources */, D0D511001D64A58900A97B8A /* IpcPipe.swift in Sources */, D01C7EDE1EF73F71008305F1 /* MessageHistoryTextIndexTable.swift in Sources */,