diff --git a/TelegramCore.xcodeproj/project.pbxproj b/TelegramCore.xcodeproj/project.pbxproj index 05ba56ee98..44b7d5d1ee 100644 --- a/TelegramCore.xcodeproj/project.pbxproj +++ b/TelegramCore.xcodeproj/project.pbxproj @@ -360,6 +360,8 @@ D09D8C0B1D4FAB1D0081DBEC /* TelegramCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D09D8C011D4FAB1D0081DBEC /* TelegramCore.framework */; }; D09D8C101D4FAB1D0081DBEC /* TelegramCoreTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D09D8C0F1D4FAB1D0081DBEC /* TelegramCoreTests.m */; }; D09D8C121D4FAB1D0081DBEC /* TelegramCore.h in Headers */ = {isa = PBXBuildFile; fileRef = D09D8C041D4FAB1D0081DBEC /* TelegramCore.h */; settings = {ATTRIBUTES = (Public, ); }; }; + D0A472B61F4CBE8B00E0EEDA /* LoadedPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A472B51F4CBE8B00E0EEDA /* LoadedPeer.swift */; }; + D0A472B71F4CBE8B00E0EEDA /* LoadedPeer.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0A472B51F4CBE8B00E0EEDA /* LoadedPeer.swift */; }; D0AAD1A81E32602500D5B9DE /* AutoremoveTimeoutMessageAttribute.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0AAD1A71E32602500D5B9DE /* AutoremoveTimeoutMessageAttribute.swift */; }; D0AAD1AA1E32638500D5B9DE /* ApplyMaxReadIndexInteractively.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0AAD1A91E32638500D5B9DE /* ApplyMaxReadIndexInteractively.swift */; }; D0AAD1B81E326FE200D5B9DE /* ManagedAutoremoveMessageOperations.swift in Sources */ = {isa = PBXBuildFile; fileRef = D0AAD1B71E326FE200D5B9DE /* ManagedAutoremoveMessageOperations.swift */; }; @@ -784,6 +786,7 @@ D09D8C0A1D4FAB1D0081DBEC /* TelegramCoreTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = TelegramCoreTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; D09D8C0F1D4FAB1D0081DBEC /* TelegramCoreTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TelegramCoreTests.m; sourceTree = ""; }; D09D8C111D4FAB1D0081DBEC /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + D0A472B51F4CBE8B00E0EEDA /* LoadedPeer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LoadedPeer.swift; sourceTree = ""; }; D0AAD1A71E32602500D5B9DE /* AutoremoveTimeoutMessageAttribute.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AutoremoveTimeoutMessageAttribute.swift; sourceTree = ""; }; D0AAD1A91E32638500D5B9DE /* ApplyMaxReadIndexInteractively.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ApplyMaxReadIndexInteractively.swift; sourceTree = ""; }; D0AAD1B71E326FE200D5B9DE /* ManagedAutoremoveMessageOperations.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ManagedAutoremoveMessageOperations.swift; sourceTree = ""; }; @@ -1480,6 +1483,7 @@ C23BC3861E9BE3CA00D79F92 /* ImportContact.swift */, C205FEA71EB3B75900455808 /* ExportMessageLink.swift */, C230BEB51EE9A3760029586C /* ChannelAdminEventLogs.swift */, + D0A472B51F4CBE8B00E0EEDA /* LoadedPeer.swift */, ); name = Peers; sourceTree = ""; @@ -1748,6 +1752,7 @@ D0B843B71DA7FF30005F29E1 /* NBMetadataCoreMapper.m in Sources */, D0AB0B921D65E9FA002C78E7 /* ManagedServiceViews.swift in Sources */, D0DB7F031F43030C00591D48 /* InstallInteractiveReadMessagesAction.swift in Sources */, + D0A472B61F4CBE8B00E0EEDA /* LoadedPeer.swift in Sources */, D0AAD1B81E326FE200D5B9DE /* ManagedAutoremoveMessageOperations.swift in Sources */, D01A21A91F38CDDC00DDA104 /* ManagedSynchronizeSavedStickersOperations.swift in Sources */, D099EA1C1DE72867001AF5A8 /* PeerCommands.swift in Sources */, @@ -2133,6 +2138,7 @@ D001F3EF1E128A1C007A8C60 /* AccountIntermediateState.swift in Sources */, D0223A991EA564BD00211D94 /* MediaResourceNetworkStatsTag.swift in Sources */, D00C7CCD1E3620C30080C3D5 /* CachedChannelParticipants.swift in Sources */, + D0A472B71F4CBE8B00E0EEDA /* LoadedPeer.swift in Sources */, D03C536E1DAD5CA9004C17B3 /* PhoneNumber.swift in Sources */, D0BC387C1E40D2880044D6FE /* TogglePeerChatPinned.swift in Sources */, D0528E6B1E65DD2100E2FEF5 /* WebpagePreview.swift in Sources */, diff --git a/TelegramCore/Account.swift b/TelegramCore/Account.swift index e4fc8f1ce0..10dc540d74 100644 --- a/TelegramCore/Account.swift +++ b/TelegramCore/Account.swift @@ -10,7 +10,7 @@ import Foundation #endif import TelegramCorePrivateModule -public protocol AccountState: Coding { +public protocol AccountState: PostboxCoding { func equalsTo(_ other: AccountState) -> Bool } @@ -19,7 +19,7 @@ public func ==(lhs: AccountState, rhs: AccountState) -> Bool { } public class AuthorizedAccountState: AccountState { - public final class State: Coding, Equatable, CustomStringConvertible { + public final class State: PostboxCoding, Equatable, CustomStringConvertible { let pts: Int32 let qts: Int32 let date: Int32 @@ -32,14 +32,14 @@ public class AuthorizedAccountState: AccountState { self.seq = seq } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.pts = decoder.decodeInt32ForKey("pts", orElse: 0) self.qts = decoder.decodeInt32ForKey("qts", orElse: 0) self.date = decoder.decodeInt32ForKey("date", orElse: 0) self.seq = decoder.decodeInt32ForKey("seq", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.pts, forKey: "pts") encoder.encodeInt32(self.qts, forKey: "qts") encoder.encodeInt32(self.date, forKey: "date") @@ -56,13 +56,13 @@ public class AuthorizedAccountState: AccountState { let state: State? - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.masterDatacenterId = decoder.decodeInt32ForKey("masterDatacenterId", orElse: 0) self.peerId = PeerId(decoder.decodeInt64ForKey("peerId", orElse: 0)) self.state = decoder.decodeObjectForKey("state", decoder: { return State(decoder: $0) }) as? State } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.masterDatacenterId, forKey: "masterDatacenterId") encoder.encodeInt64(self.peerId.toInt64(), forKey: "peerId") if let state = self.state { diff --git a/TelegramCore/AccountIntermediateState.swift b/TelegramCore/AccountIntermediateState.swift index fc68f40de7..40935f8827 100644 --- a/TelegramCore/AccountIntermediateState.swift +++ b/TelegramCore/AccountIntermediateState.swift @@ -53,6 +53,7 @@ enum AccountStateMutationOperation { case ReadInbox(MessageId) case ReadOutbox(MessageId) case ResetReadState(PeerId, MessageId.Namespace, MessageId.Id, MessageId.Id, MessageId.Id, Int32) + case ResetMessageTagSummary(PeerId, MessageId.Namespace, Int32, MessageHistoryTagNamespaceCountValidityRange) case UpdateState(AuthorizedAccountState.State) case UpdateChannelState(PeerId, ChannelState) case UpdatePeerNotificationSettings(PeerId, PeerNotificationSettings) @@ -169,6 +170,10 @@ struct AccountMutableState { self.addOperation(.ResetReadState(peerId, namespace, maxIncomingReadId, maxOutgoingReadId, maxKnownId, count)) } + mutating func resetMessageTagSummary(_ peerId: PeerId, namespace: MessageId.Namespace, count: Int32, range: MessageHistoryTagNamespaceCountValidityRange) { + self.addOperation(.ResetMessageTagSummary(peerId, namespace, count, range)) + } + mutating func updateState(_ state: AuthorizedAccountState.State) { self.addOperation(.UpdateState(state)) } @@ -313,6 +318,8 @@ struct AccountMutableState { self.readInboxMaxIds[peerId] = MessageId(peerId: peerId, namespace: namespace, id: maxIncomingReadId) } } + case let .ResetMessageTagSummary(peerId, namespace, count, range): + break } self.operations.append(operation) diff --git a/TelegramCore/AccountState.swift b/TelegramCore/AccountState.swift index 25d06d1edd..f9310a992e 100644 --- a/TelegramCore/AccountState.swift +++ b/TelegramCore/AccountState.swift @@ -13,13 +13,13 @@ private enum SentAuthorizationCodeTypeValue: Int32 { case flashCall = 3 } -public enum SentAuthorizationCodeType: Coding, Equatable { +public enum SentAuthorizationCodeType: PostboxCoding, Equatable { case otherSession(length: Int32) case sms(length: Int32) case call(length: Int32) case flashCall(pattern: String) - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("v", orElse: 0) { case SentAuthorizationCodeTypeValue.otherSession.rawValue: self = .otherSession(length: decoder.decodeInt32ForKey("l", orElse: 0)) @@ -34,7 +34,7 @@ public enum SentAuthorizationCodeType: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case let .otherSession(length): encoder.encodeInt32(SentAuthorizationCodeTypeValue.otherSession.rawValue, forKey: "v") @@ -95,14 +95,14 @@ private enum UnauthorizedAccountStateContentsValue: Int32 { case signUp = 5 } -public enum UnauthorizedAccountStateContents: Coding, Equatable { +public enum UnauthorizedAccountStateContents: PostboxCoding, Equatable { case empty case phoneEntry(countryCode: Int32, number: String) case confirmationCodeEntry(number: String, type: SentAuthorizationCodeType, hash: String, timeout: Int32?, nextType: AuthorizationCodeNextType?) case passwordEntry(hint: String, number: String?, code: String?) case signUp(number: String, codeHash: String, code: String, firstName: String, lastName: String) - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("v", orElse: 0) { case UnauthorizedAccountStateContentsValue.empty.rawValue: self = .empty @@ -124,7 +124,7 @@ public enum UnauthorizedAccountStateContents: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case .empty: encoder.encodeInt32(UnauthorizedAccountStateContentsValue.empty.rawValue, forKey: "v") @@ -230,12 +230,12 @@ public final class UnauthorizedAccountState: AccountState { self.contents = contents } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.masterDatacenterId = decoder.decodeInt32ForKey("dc", orElse: 0) self.contents = decoder.decodeObjectForKey("c", decoder: { UnauthorizedAccountStateContents(decoder: $0) }) as! UnauthorizedAccountStateContents } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.masterDatacenterId, forKey: "dc") encoder.encodeObject(self.contents, forKey: "c") } diff --git a/TelegramCore/AccountStateManagementUtils.swift b/TelegramCore/AccountStateManagementUtils.swift index 08e435bb6a..53bedcdc4a 100644 --- a/TelegramCore/AccountStateManagementUtils.swift +++ b/TelegramCore/AccountStateManagementUtils.swift @@ -1154,7 +1154,7 @@ private func resolveMissingPeerCloudReadStates(account: Account, state: AccountM var missingPeers: [PeerId: Api.InputPeer] = [:] for peerId in state.initialState.peerIdsWithNewMessages { - if state.initialState.cloudReadStates[peerId] == nil && (peerId.namespace == Namespaces.Peer.CloudUser || peerId.namespace == Namespaces.Peer.CloudGroup) { + if state.initialState.cloudReadStates[peerId] == nil && (peerId.namespace == Namespaces.Peer.CloudUser || peerId.namespace == Namespaces.Peer.CloudGroup) { if let peer = state.peers[peerId], let inputPeer = apiInputPeer(peer) { missingPeers[peerId] = inputPeer } else { @@ -1319,6 +1319,8 @@ private func pollChannel(_ account: Account, peer: Peer, state: AccountMutableSt } updatedState.resetReadState(peer.id, namespace: Namespaces.Message.Cloud, maxIncomingReadId: readInboxMaxId, maxOutgoingReadId: readOutboxMaxId, maxKnownId: topMessage, count: unreadCount) + + updatedState.resetMessageTagSummary(peer.id, namespace: Namespaces.Message.Cloud, count: unreadMentionsCount, range: MessageHistoryTagNamespaceCountValidityRange(maxId: topMessage)) } } return (updatedState, difference != nil, apiTimeout) @@ -1423,7 +1425,7 @@ private func optimizedOperations(_ operations: [AccountStateMutationOperation]) var currentAddMessages: OptimizeAddMessagesState? for operation in operations { switch operation { - case .AddHole, .DeleteMessages, .DeleteMessagesWithGlobalIds, .EditMessage, .UpdateMedia, .MergeApiChats, .MergeApiUsers, .MergePeerPresences, .UpdatePeer, .ReadInbox, .ReadOutbox, .ResetReadState, .UpdatePeerNotificationSettings, .UpdateSecretChat, .AddSecretMessages, .ReadSecretOutbox, .AddPeerInputActivity, .UpdateCachedPeerData, .UpdatePinnedPeerIds, .ReadMessageContents, .UpdateMessageImpressionCount, .UpdateInstalledStickerPacks, .UpdateChatInputState, .UpdateCall, .UpdateLangPack: + case .AddHole, .DeleteMessages, .DeleteMessagesWithGlobalIds, .EditMessage, .UpdateMedia, .MergeApiChats, .MergeApiUsers, .MergePeerPresences, .UpdatePeer, .ReadInbox, .ReadOutbox, .ResetReadState, .ResetMessageTagSummary, .UpdatePeerNotificationSettings, .UpdateSecretChat, .AddSecretMessages, .ReadSecretOutbox, .AddPeerInputActivity, .UpdateCachedPeerData, .UpdatePinnedPeerIds, .ReadMessageContents, .UpdateMessageImpressionCount, .UpdateInstalledStickerPacks, .UpdateChatInputState, .UpdateCall, .UpdateLangPack: if let currentAddMessages = currentAddMessages, !currentAddMessages.messages.isEmpty { result.append(.AddMessages(currentAddMessages.messages, currentAddMessages.location)) } @@ -1509,6 +1511,8 @@ func replayFinalState(accountPeerId: PeerId, mediaBox: MediaBox, modifier: Modif modifier.applyOutgoingReadMaxId(messageId) case let .ResetReadState(peerId, namespace, maxIncomingReadId, maxOutgoingReadId, maxKnownId, count): modifier.resetIncomingReadStates([peerId: [namespace: .idBased(maxIncomingReadId: maxIncomingReadId, maxOutgoingReadId: maxOutgoingReadId, maxKnownId: maxKnownId, count: count)]]) + case let .ResetMessageTagSummary(peerId, namespace, count, range): + modifier.replaceMessageTagSummary(peerId: peerId, tagMask: .unseenPersonalMessage, namespace: namespace, count: count, maxId: range.maxId) case let .UpdateState(state): let currentState = modifier.getState() as! AuthorizedAccountState modifier.setState(currentState.changedState(state)) diff --git a/TelegramCore/ArchivedStickerPacksInfo.swift b/TelegramCore/ArchivedStickerPacksInfo.swift index e8ca064897..207161400d 100644 --- a/TelegramCore/ArchivedStickerPacksInfo.swift +++ b/TelegramCore/ArchivedStickerPacksInfo.swift @@ -32,11 +32,11 @@ public final class ArchivedStickerPacksInfo: OrderedItemListEntryContents { self.count = count } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.count = decoder.decodeInt32ForKey("c", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.count, forKey: "c") } } diff --git a/TelegramCore/AuthorSignatureMessageAttribute.swift b/TelegramCore/AuthorSignatureMessageAttribute.swift index 81ee82f2a2..66d0a2e67f 100644 --- a/TelegramCore/AuthorSignatureMessageAttribute.swift +++ b/TelegramCore/AuthorSignatureMessageAttribute.swift @@ -14,11 +14,11 @@ public class AuthorSignatureMessageAttribute: MessageAttribute { self.signature = signature } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.signature = decoder.decodeStringForKey("s", orElse: "") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.signature, forKey: "s") } } diff --git a/TelegramCore/AutoremoveTimeoutMessageAttribute.swift b/TelegramCore/AutoremoveTimeoutMessageAttribute.swift index d6ee2c388a..f65129bd86 100644 --- a/TelegramCore/AutoremoveTimeoutMessageAttribute.swift +++ b/TelegramCore/AutoremoveTimeoutMessageAttribute.swift @@ -20,12 +20,12 @@ public class AutoremoveTimeoutMessageAttribute: MessageAttribute { self.countdownBeginTime = countdownBeginTime } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.timeout = decoder.decodeInt32ForKey("t", orElse: 0) self.countdownBeginTime = decoder.decodeOptionalInt32ForKey("c") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.timeout, forKey: "t") if let countdownBeginTime = self.countdownBeginTime { encoder.encodeInt32(countdownBeginTime, forKey: "c") diff --git a/TelegramCore/BotInfo.swift b/TelegramCore/BotInfo.swift index e08b22046e..549de05bdd 100644 --- a/TelegramCore/BotInfo.swift +++ b/TelegramCore/BotInfo.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public struct BotCommand: Coding, Equatable { +public struct BotCommand: PostboxCoding, Equatable { public let text: String public let description: String @@ -14,12 +14,12 @@ public struct BotCommand: Coding, Equatable { self.description = description } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.text = decoder.decodeStringForKey("t", orElse: "") self.description = decoder.decodeStringForKey("d", orElse: "") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.text, forKey: "t") encoder.encodeString(self.description, forKey: "d") } @@ -29,7 +29,7 @@ public struct BotCommand: Coding, Equatable { } } -public final class BotInfo: Coding, Equatable { +public final class BotInfo: PostboxCoding, Equatable { public let description: String public let commands: [BotCommand] @@ -38,12 +38,12 @@ public final class BotInfo: Coding, Equatable { self.commands = commands } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.description = decoder.decodeStringForKey("d", orElse: "") self.commands = decoder.decodeObjectArrayWithDecoderForKey("c") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.description, forKey: "d") encoder.encodeObjectArray(self.commands, forKey: "c") } diff --git a/TelegramCore/CacheStorageSettings.swift b/TelegramCore/CacheStorageSettings.swift index c50ed98cc2..3878deaac9 100644 --- a/TelegramCore/CacheStorageSettings.swift +++ b/TelegramCore/CacheStorageSettings.swift @@ -18,11 +18,11 @@ public struct CacheStorageSettings: PreferencesEntry, Equatable { self.defaultCacheStorageTimeout = defaultCacheStorageTimeout } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.defaultCacheStorageTimeout = decoder.decodeInt32ForKey("dt", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.defaultCacheStorageTimeout, forKey: "dt") } diff --git a/TelegramCore/CachedChannelData.swift b/TelegramCore/CachedChannelData.swift index bacc55dc68..16ac511b5d 100644 --- a/TelegramCore/CachedChannelData.swift +++ b/TelegramCore/CachedChannelData.swift @@ -21,7 +21,7 @@ public struct CachedChannelFlags: OptionSet { public static let canSetStickerSet = CachedChannelFlags(rawValue: 1 << 2) } -public struct CachedChannelParticipantsSummary: Coding, Equatable { +public struct CachedChannelParticipantsSummary: PostboxCoding, Equatable { public let memberCount: Int32? public let adminCount: Int32? public let bannedCount: Int32? @@ -34,7 +34,7 @@ public struct CachedChannelParticipantsSummary: Coding, Equatable { self.kickedCount = kickedCount } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { if let memberCount = decoder.decodeOptionalInt32ForKey("p.m") { self.memberCount = memberCount } else { @@ -57,7 +57,7 @@ public struct CachedChannelParticipantsSummary: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { if let memberCount = self.memberCount { encoder.encodeInt32(memberCount, forKey: "p.m") } else { @@ -186,7 +186,7 @@ public final class CachedChannelData: CachedPeerData { return CachedChannelData(flags: self.flags, about: self.about, participantsSummary: self.participantsSummary, exportedInvitation: self.exportedInvitation, botInfos: self.botInfos, topParticipants: self.topParticipants, reportStatus: self.reportStatus, pinnedMessageId: self.pinnedMessageId, stickerPack: stickerPack) } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.flags = CachedChannelFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) self.about = decoder.decodeOptionalStringForKey("a") self.participantsSummary = CachedChannelParticipantsSummary(decoder: decoder) @@ -219,7 +219,7 @@ public final class CachedChannelData: CachedPeerData { self.peerIds = peerIds } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.flags.rawValue, forKey: "f") if let about = self.about { encoder.encodeString(about, forKey: "a") diff --git a/TelegramCore/CachedChannelParticipants.swift b/TelegramCore/CachedChannelParticipants.swift index 39f2afbdf5..c0b46eeb8d 100644 --- a/TelegramCore/CachedChannelParticipants.swift +++ b/TelegramCore/CachedChannelParticipants.swift @@ -12,7 +12,7 @@ private enum ChannelParticipantValue: Int32 { case moderator = 3 } -public struct ChannelParticipantAdminInfo: Coding, Equatable { +public struct ChannelParticipantAdminInfo: PostboxCoding, Equatable { public let rights: TelegramChannelAdminRights public let promotedBy: PeerId public let canBeEditedByAccountPeer: Bool @@ -23,13 +23,13 @@ public struct ChannelParticipantAdminInfo: Coding, Equatable { self.canBeEditedByAccountPeer = canBeEditedByAccountPeer } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.rights = decoder.decodeObjectForKey("r", decoder: { TelegramChannelAdminRights(decoder: $0) }) as! TelegramChannelAdminRights self.promotedBy = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) self.canBeEditedByAccountPeer = decoder.decodeInt32ForKey("e", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.rights, forKey: "r") encoder.encodeInt64(self.promotedBy.toInt64(), forKey: "p") encoder.encodeInt32(self.canBeEditedByAccountPeer ? 1 : 0, forKey: "e") @@ -40,7 +40,7 @@ public struct ChannelParticipantAdminInfo: Coding, Equatable { } } -public struct ChannelParticipantBannedInfo: Coding, Equatable { +public struct ChannelParticipantBannedInfo: PostboxCoding, Equatable { public let rights: TelegramChannelBannedRights public let restrictedBy: PeerId public let isMember: Bool @@ -51,13 +51,13 @@ public struct ChannelParticipantBannedInfo: Coding, Equatable { self.isMember = isMember } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.rights = decoder.decodeObjectForKey("r", decoder: { TelegramChannelBannedRights(decoder: $0) }) as! TelegramChannelBannedRights self.restrictedBy = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) self.isMember = decoder.decodeInt32ForKey("m", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.rights, forKey: "r") encoder.encodeInt64(self.restrictedBy.toInt64(), forKey: "p") encoder.encodeInt32(self.isMember ? 1 : 0, forKey: "m") @@ -68,7 +68,7 @@ public struct ChannelParticipantBannedInfo: Coding, Equatable { } } -public enum ChannelParticipant: Coding, Equatable { +public enum ChannelParticipant: PostboxCoding, Equatable { case creator(id: PeerId) case member(id: PeerId, invitedAt: Int32, adminInfo: ChannelParticipantAdminInfo?, banInfo: ChannelParticipantBannedInfo?) @@ -110,7 +110,7 @@ public enum ChannelParticipant: Coding, Equatable { } } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case ChannelParticipantValue.member.rawValue: self = .member(id: PeerId(decoder.decodeInt64ForKey("i", orElse: 0)), invitedAt: decoder.decodeInt32ForKey("t", orElse: 0), adminInfo: decoder.decodeObjectForKey("ai", decoder: { ChannelParticipantAdminInfo(decoder: $0) }) as? ChannelParticipantAdminInfo, banInfo: decoder.decodeObjectForKey("bi", decoder: { ChannelParticipantBannedInfo(decoder: $0) }) as? ChannelParticipantBannedInfo) @@ -121,7 +121,7 @@ public enum ChannelParticipant: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case let .member(id, invitedAt, adminInfo, banInfo): encoder.encodeInt32(ChannelParticipantValue.member.rawValue, forKey: "r") @@ -144,18 +144,18 @@ public enum ChannelParticipant: Coding, Equatable { } } -public final class CachedChannelParticipants: Coding, Equatable { +public final class CachedChannelParticipants: PostboxCoding, Equatable { public let participants: [ChannelParticipant] init(participants: [ChannelParticipant]) { self.participants = participants } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.participants = decoder.decodeObjectArrayWithDecoderForKey("p") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.participants, forKey: "p") } diff --git a/TelegramCore/CachedGroupData.swift b/TelegramCore/CachedGroupData.swift index 32046ac294..e909c31c1a 100644 --- a/TelegramCore/CachedGroupData.swift +++ b/TelegramCore/CachedGroupData.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public final class CachedPeerBotInfo: Coding, Equatable { +public final class CachedPeerBotInfo: PostboxCoding, Equatable { public let peerId: PeerId public let botInfo: BotInfo @@ -14,12 +14,12 @@ public final class CachedPeerBotInfo: Coding, Equatable { self.botInfo = botInfo } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.peerId = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) self.botInfo = decoder.decodeObjectForKey("i", decoder: { return BotInfo(decoder: $0) }) as! BotInfo } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.peerId.toInt64(), forKey: "p") encoder.encodeObject(self.botInfo, forKey: "i") } @@ -63,7 +63,7 @@ public final class CachedGroupData: CachedPeerData { self.peerIds = peerIds } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { let participants = decoder.decodeObjectForKey("p", decoder: { CachedGroupParticipants(decoder: $0) }) as? CachedGroupParticipants self.participants = participants self.exportedInvitation = decoder.decodeObjectForKey("i", decoder: { ExportedInvitation(decoder: $0) }) as? ExportedInvitation @@ -83,7 +83,7 @@ public final class CachedGroupData: CachedPeerData { self.peerIds = peerIds } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { if let participants = self.participants { encoder.encodeObject(participants, forKey: "p") } else { diff --git a/TelegramCore/CachedGroupParticipants.swift b/TelegramCore/CachedGroupParticipants.swift index 4938ce43b7..c8ed1cdb73 100644 --- a/TelegramCore/CachedGroupParticipants.swift +++ b/TelegramCore/CachedGroupParticipants.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public enum GroupParticipant: Coding, Equatable { +public enum GroupParticipant: PostboxCoding, Equatable { case member(id: PeerId, invitedBy: PeerId, invitedAt: Int32) case creator(id: PeerId) case admin(id: PeerId, invitedBy: PeerId, invitedAt: Int32) @@ -21,7 +21,7 @@ public enum GroupParticipant: Coding, Equatable { } } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("v", orElse: 0) { case 0: self = .member(id: PeerId(decoder.decodeInt64ForKey("i", orElse: 0)), invitedBy: PeerId(decoder.decodeInt64ForKey("b", orElse: 0)), invitedAt: decoder.decodeInt32ForKey("t", orElse: 0)) @@ -34,7 +34,7 @@ public enum GroupParticipant: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case let .member(id, invitedBy, invitedAt): encoder.encodeInt32(0, forKey: "v") @@ -87,7 +87,7 @@ public enum GroupParticipant: Coding, Equatable { } } -public final class CachedGroupParticipants: Coding, Equatable { +public final class CachedGroupParticipants: PostboxCoding, Equatable { public let participants: [GroupParticipant] let version: Int32 @@ -96,12 +96,12 @@ public final class CachedGroupParticipants: Coding, Equatable { self.version = version } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.participants = decoder.decodeObjectArrayWithDecoderForKey("p") self.version = decoder.decodeInt32ForKey("v", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.participants, forKey: "p") encoder.encodeInt32(self.version, forKey: "v") } diff --git a/TelegramCore/CachedStickerPack.swift b/TelegramCore/CachedStickerPack.swift index b2109470eb..d06a0ce4c5 100644 --- a/TelegramCore/CachedStickerPack.swift +++ b/TelegramCore/CachedStickerPack.swift @@ -7,7 +7,7 @@ import Foundation import SwiftSignalKit #endif -private final class CachedStickerPack: Coding { +private final class CachedStickerPack: PostboxCoding { let items: [StickerPackItem] let hash: Int32 @@ -16,12 +16,12 @@ private final class CachedStickerPack: Coding { self.hash = hash } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.items = decoder.decodeObjectArrayForKey("it").map { $0 as! StickerPackItem } self.hash = decoder.decodeInt32ForKey("h", orElse: 0) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.items, forKey: "it") encoder.encodeInt32(self.hash, forKey: "h") } diff --git a/TelegramCore/CachedUserData.swift b/TelegramCore/CachedUserData.swift index 29e10041f2..92015663d8 100644 --- a/TelegramCore/CachedUserData.swift +++ b/TelegramCore/CachedUserData.swift @@ -30,7 +30,7 @@ public final class CachedUserData: CachedPeerData { self.commonGroupCount = commonGroupCount } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.about = decoder.decodeOptionalStringForKey("a") self.botInfo = decoder.decodeObjectForKey("bi") as? BotInfo self.reportStatus = PeerReportStatus(rawValue: decoder.decodeInt32ForKey("r", orElse: 0))! @@ -38,7 +38,7 @@ public final class CachedUserData: CachedPeerData { self.commonGroupCount = decoder.decodeInt32ForKey("cg", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { if let about = self.about { encoder.encodeString(about, forKey: "a") } else { diff --git a/TelegramCore/ChannelAdminEventLogs.swift b/TelegramCore/ChannelAdminEventLogs.swift index 4bb2912543..24775f6690 100644 --- a/TelegramCore/ChannelAdminEventLogs.swift +++ b/TelegramCore/ChannelAdminEventLogs.swift @@ -1,4 +1,3 @@ - #if os(macOS) import PostboxMac import SwiftSignalKitMac @@ -7,7 +6,6 @@ import SwiftSignalKit #endif - public typealias AdminLogEventId = Int64 public struct AdminLogEvent { diff --git a/TelegramCore/ChannelMessageStateVersionAttribute.swift b/TelegramCore/ChannelMessageStateVersionAttribute.swift index b65e88e084..4e352ed0a6 100644 --- a/TelegramCore/ChannelMessageStateVersionAttribute.swift +++ b/TelegramCore/ChannelMessageStateVersionAttribute.swift @@ -12,11 +12,11 @@ public class ChannelMessageStateVersionAttribute: MessageAttribute { self.pts = pts } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.pts = decoder.decodeInt32ForKey("p", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.pts, forKey: "p") } } diff --git a/TelegramCore/ChannelState.swift b/TelegramCore/ChannelState.swift index ef44a621ac..b0dd9e7c3d 100644 --- a/TelegramCore/ChannelState.swift +++ b/TelegramCore/ChannelState.swift @@ -14,12 +14,12 @@ final class ChannelState: PeerChatState, Equatable, CustomStringConvertible { self.invalidatedPts = invalidatedPts } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.pts = decoder.decodeInt32ForKey("pts", orElse: 0) self.invalidatedPts = decoder.decodeOptionalInt32ForKey("ipts") } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.pts, forKey: "pts") if let invalidatedPts = self.invalidatedPts { encoder.encodeInt32(invalidatedPts, forKey: "ipts") diff --git a/TelegramCore/ChatContextResult.swift b/TelegramCore/ChatContextResult.swift index 9d099da185..9e6b0eae8c 100644 --- a/TelegramCore/ChatContextResult.swift +++ b/TelegramCore/ChatContextResult.swift @@ -9,13 +9,13 @@ import Foundation import MtProtoKitDynamic #endif -public enum ChatContextResultMessage: Coding, Equatable { +public enum ChatContextResultMessage: PostboxCoding, Equatable { case auto(caption: String, replyMarkup: ReplyMarkupMessageAttribute?) case text(text: String, entities: TextEntitiesMessageAttribute?, disableUrlPreview: Bool, replyMarkup: ReplyMarkupMessageAttribute?) case mapLocation(media: TelegramMediaMap, replyMarkup: ReplyMarkupMessageAttribute?) case contact(media: TelegramMediaContact, replyMarkup: ReplyMarkupMessageAttribute?) - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("_v", orElse: 0) { case 0: self = .auto(caption: decoder.decodeStringForKey("c", orElse: ""), replyMarkup: decoder.decodeObjectForKey("m") as? ReplyMarkupMessageAttribute) @@ -30,7 +30,7 @@ public enum ChatContextResultMessage: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case let .auto(caption, replyMarkup): encoder.encodeInt32(0, forKey: "_v") diff --git a/TelegramCore/CloudChatRemoveMessagesOperation.swift b/TelegramCore/CloudChatRemoveMessagesOperation.swift index bbc2a25b17..e9955ba37c 100644 --- a/TelegramCore/CloudChatRemoveMessagesOperation.swift +++ b/TelegramCore/CloudChatRemoveMessagesOperation.swift @@ -21,7 +21,7 @@ extension CloudChatRemoveMessagesType { } } -final class CloudChatRemoveMessagesOperation: Coding { +final class CloudChatRemoveMessagesOperation: PostboxCoding { let messageIds: [MessageId] let type: CloudChatRemoveMessagesType @@ -30,12 +30,12 @@ final class CloudChatRemoveMessagesOperation: Coding { self.type = type } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.messageIds = MessageId.decodeArrayFromBuffer(decoder.decodeBytesForKeyNoCopy("i")!) self.type = CloudChatRemoveMessagesType(rawValue: decoder.decodeInt32ForKey("t", orElse: 0))! } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() MessageId.encodeArrayToBuffer(self.messageIds, buffer: buffer) encoder.encodeBytes(buffer, forKey: "i") @@ -43,7 +43,7 @@ final class CloudChatRemoveMessagesOperation: Coding { } } -final class CloudChatRemoveChatOperation: Coding { +final class CloudChatRemoveChatOperation: PostboxCoding { let peerId: PeerId let reportChatSpam: Bool let topMessageId: MessageId? @@ -54,7 +54,7 @@ final class CloudChatRemoveChatOperation: Coding { self.topMessageId = topMessageId } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.peerId = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) self.reportChatSpam = decoder.decodeInt32ForKey("r", orElse: 0) != 0 if let messageIdPeerId = decoder.decodeOptionalInt64ForKey("m.p"), let messageIdNamespace = decoder.decodeOptionalInt32ForKey("m.n"), let messageIdId = decoder.decodeOptionalInt32ForKey("m.i") { @@ -64,7 +64,7 @@ final class CloudChatRemoveChatOperation: Coding { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.peerId.toInt64(), forKey: "p") encoder.encodeInt32(self.reportChatSpam ? 1 : 0, forKey: "r") if let topMessageId = self.topMessageId { @@ -79,7 +79,7 @@ final class CloudChatRemoveChatOperation: Coding { } } -final class CloudChatClearHistoryOperation: Coding { +final class CloudChatClearHistoryOperation: PostboxCoding { let peerId: PeerId let topMessageId: MessageId @@ -88,12 +88,12 @@ final class CloudChatClearHistoryOperation: Coding { self.topMessageId = topMessageId } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.peerId = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) self.topMessageId = MessageId(peerId: PeerId(decoder.decodeInt64ForKey("m.p", orElse: 0)), namespace: decoder.decodeInt32ForKey("m.n", orElse: 0), id: decoder.decodeInt32ForKey("m.i", orElse: 0)) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.peerId.toInt64(), forKey: "p") encoder.encodeInt64(self.topMessageId.peerId.toInt64(), forKey: "m.p") encoder.encodeInt32(self.topMessageId.namespace, forKey: "m.n") diff --git a/TelegramCore/CloudFileMediaResource.swift b/TelegramCore/CloudFileMediaResource.swift index 17a3deaa8b..5d59110b94 100644 --- a/TelegramCore/CloudFileMediaResource.swift +++ b/TelegramCore/CloudFileMediaResource.swift @@ -66,7 +66,7 @@ public class CloudFileMediaResource: TelegramCloudMediaResource, TelegramMultipa self.size = size } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.datacenterId = Int(decoder.decodeInt32ForKey("d", orElse: 0)) self.volumeId = decoder.decodeInt64ForKey("v", orElse: 0) self.localId = decoder.decodeInt32ForKey("l", orElse: 0) @@ -78,7 +78,7 @@ public class CloudFileMediaResource: TelegramCloudMediaResource, TelegramMultipa } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(Int32(self.datacenterId), forKey: "d") encoder.encodeInt64(self.volumeId, forKey: "v") encoder.encodeInt32(self.localId, forKey: "l") @@ -148,7 +148,7 @@ public class CloudDocumentMediaResource: TelegramCloudMediaResource, TelegramMul self.size = size } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.datacenterId = Int(decoder.decodeInt32ForKey("d", orElse: 0)) self.fileId = decoder.decodeInt64ForKey("f", orElse: 0) self.accessHash = decoder.decodeInt64ForKey("a", orElse: 0) @@ -159,7 +159,7 @@ public class CloudDocumentMediaResource: TelegramCloudMediaResource, TelegramMul } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(Int32(self.datacenterId), forKey: "d") encoder.encodeInt64(self.fileId, forKey: "f") encoder.encodeInt64(self.accessHash, forKey: "a") @@ -206,11 +206,11 @@ public class LocalFileMediaResource: TelegramMediaResource { self.fileId = fileId } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.fileId = decoder.decodeInt64ForKey("f", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.fileId, forKey: "f") } @@ -260,14 +260,14 @@ public class LocalFileReferenceMediaResource: TelegramMediaResource { self.size = size } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.localFilePath = decoder.decodeStringForKey("p", orElse: "") self.randomId = decoder.decodeInt64ForKey("r", orElse: 0) self.isUniquelyReferencedTemporaryFile = decoder.decodeInt32ForKey("t", orElse: 0) != 0 self.size = decoder.decodeOptionalInt32ForKey("s") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.localFilePath, forKey: "p") encoder.encodeInt64(self.randomId, forKey: "r") encoder.encodeInt32(self.isUniquelyReferencedTemporaryFile ? 1 : 0, forKey: "t") @@ -320,7 +320,7 @@ public final class HttpReferenceMediaResource: TelegramMediaResource { self.size = size } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.url = decoder.decodeStringForKey("u", orElse: "") if let size = decoder.decodeOptionalInt32ForKey("s") { self.size = Int(size) @@ -329,7 +329,7 @@ public final class HttpReferenceMediaResource: TelegramMediaResource { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.url, forKey: "u") if let size = self.size { encoder.encodeInt32(Int32(size), forKey: "s") @@ -393,14 +393,14 @@ public final class WebFileReferenceMediaResource: TelegramMediaResource, Telegra return Api.InputWebFileLocation.inputWebFileLocation(url: url, accessHash: accessHash) } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.url = decoder.decodeStringForKey("u", orElse: "") self.size = decoder.decodeInt32ForKey("s", orElse: 0) self._datacenterId = decoder.decodeInt32ForKey("d", orElse: 0) self.accessHash = decoder.decodeInt64ForKey("h", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.url, forKey: "u") encoder.encodeInt32(self.size, forKey: "s") encoder.encodeInt32(self._datacenterId, forKey: "d") @@ -468,7 +468,7 @@ public struct SecretFileMediaResource: TelegramCloudMediaResource, TelegramMulti self.key = key } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.fileId = decoder.decodeInt64ForKey("i", orElse: 0) self.accessHash = decoder.decodeInt64ForKey("a", orElse: 0) if let size = decoder.decodeOptionalInt32ForKey("s") { @@ -481,7 +481,7 @@ public struct SecretFileMediaResource: TelegramCloudMediaResource, TelegramMulti self.key = decoder.decodeObjectForKey("k", decoder: { SecretFileEncryptionKey(decoder: $0) }) as! SecretFileEncryptionKey } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.fileId, forKey: "i") encoder.encodeInt64(self.accessHash, forKey: "a") if let size = self.size { @@ -543,10 +543,10 @@ public final class EmptyMediaResource: TelegramMediaResource { public init() { } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { } public var id: MediaResourceId { diff --git a/TelegramCore/ConsumableContentMessageAttribute.swift b/TelegramCore/ConsumableContentMessageAttribute.swift index 02d7b13d9c..029615a641 100644 --- a/TelegramCore/ConsumableContentMessageAttribute.swift +++ b/TelegramCore/ConsumableContentMessageAttribute.swift @@ -12,11 +12,11 @@ public class ConsumableContentMessageAttribute: MessageAttribute { self.consumed = consumed } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.consumed = decoder.decodeInt32ForKey("c", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.consumed ? 1 : 0, forKey: "c") } } diff --git a/TelegramCore/ConsumablePersonalMentionMessageAttribute.swift b/TelegramCore/ConsumablePersonalMentionMessageAttribute.swift index fdf1402194..4cb1268dc5 100644 --- a/TelegramCore/ConsumablePersonalMentionMessageAttribute.swift +++ b/TelegramCore/ConsumablePersonalMentionMessageAttribute.swift @@ -14,12 +14,12 @@ public class ConsumablePersonalMentionMessageAttribute: MessageAttribute { self.pending = pending } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.consumed = decoder.decodeInt32ForKey("c", orElse: 0) != 0 self.pending = decoder.decodeInt32ForKey("p", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.consumed ? 1 : 0, forKey: "c") encoder.encodeInt32(self.pending ? 1 : 0, forKey: "p") } diff --git a/TelegramCore/ConsumePersonalMessageAction.swift b/TelegramCore/ConsumePersonalMessageAction.swift index 5cf5feb3ef..2cac19e09e 100644 --- a/TelegramCore/ConsumePersonalMessageAction.swift +++ b/TelegramCore/ConsumePersonalMessageAction.swift @@ -11,10 +11,10 @@ final class ConsumePersonalMessageAction: PendingMessageActionData { init() { } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { } func isEqual(to: PendingMessageActionData) -> Bool { diff --git a/TelegramCore/EarliestUnseenPersonalMentionMessage.swift b/TelegramCore/EarliestUnseenPersonalMentionMessage.swift index f7394725d4..7efd41752d 100644 --- a/TelegramCore/EarliestUnseenPersonalMentionMessage.swift +++ b/TelegramCore/EarliestUnseenPersonalMentionMessage.swift @@ -9,29 +9,81 @@ import Foundation import MtProtoKitDynamic #endif -public func earliestUnseenPersonalMentionMessage(postbox: Postbox, peerId: PeerId) -> Signal { - return postbox.modify { modifier -> Signal in - var resultMessageId: MessageId? +public enum EarliestUnseenPersonalMentionMessageResult { + case loading + case result(MessageId?) +} + +public func earliestUnseenPersonalMentionMessage(postbox: Postbox, network: Network, peerId: PeerId) -> Signal { + return earliestUnseenPersonalMentionMessage(postbox: postbox, network: network, peerId: peerId, locally: false) +} + +private func earliestUnseenPersonalMentionMessage(postbox: Postbox, network: Network, peerId: PeerId, locally: Bool) -> Signal { + return postbox.modify { modifier -> Signal in + var resultMessage: Message? + var resultHole: MessageHistoryHole? modifier.scanMessages(peerId: peerId, tagMask: .unseenPersonalMessage, { entry in switch entry { case let .message(message): for attribute in message.attributes { if let attribute = attribute as? ConsumablePersonalMentionMessageAttribute, !attribute.pending { - resultMessageId = message.id + resultMessage = message return false } } - break case let .hole(hole): - break + resultHole = hole + return false } return true }) - if let resultMessageId = resultMessageId { - return .single(resultMessageId) + if let resultMessage = resultMessage { + var invalidateHistoryPts: Int32? + + if peerId.namespace == Namespaces.Peer.CloudChannel { + if let channelState = modifier.getPeerChatState(peerId) as? ChannelState { + if let invalidatedPts = channelState.invalidatedPts { + var messagePts: Int32? + for attribute in resultMessage.attributes { + if let attribute = attribute as? ChannelMessageStateVersionAttribute { + messagePts = attribute.pts + break + } + } + + if let messagePts = messagePts { + if messagePts < invalidatedPts { + invalidateHistoryPts = invalidatedPts + } + } else { + invalidateHistoryPts = invalidatedPts + } + } + } + } + + if !locally, let _ = invalidateHistoryPts { + let validateSignal = fetchMessageHistoryHole(network: network, postbox: postbox, hole: MessageHistoryHole(stableId: UInt32.max, maxIndex: MessageIndex.upperBound(peerId: peerId), min: resultMessage.id.id - 1, tags: 0), direction: .LowerToUpper, tagMask: .unseenPersonalMessage) + |> mapToSignal { _ -> Signal in + return .complete() + } + |> then(earliestUnseenPersonalMentionMessage(postbox: postbox, network: network, peerId: peerId, locally: true)) + return .single(.loading) |> then(validateSignal) + } else { + return .single(.result(resultMessage.id)) + } + } else if let resultHole = resultHole, !locally { + let validateSignal = fetchMessageHistoryHole(network: network, postbox: postbox, hole: resultHole, direction: .LowerToUpper, tagMask: .unseenPersonalMessage) + |> mapToSignal { _ -> Signal in + return .complete() + } + |> then(earliestUnseenPersonalMentionMessage(postbox: postbox, network: network, peerId: peerId, locally: true)) + return .single(.loading) |> then(validateSignal) + } else if let summary = modifier.getMessageTagSummary(peerId: peerId, tagMask: .unseenPersonalMessage, namespace: Namespaces.Message.Cloud), summary.count > 0 { + modifier.replaceMessageTagSummary(peerId: peerId, tagMask: .unseenPersonalMessage, namespace: Namespaces.Message.Cloud, count: 0, maxId: summary.range.maxId) } - return .single(nil) + return .single(.result(nil)) } |> switchToLatest } diff --git a/TelegramCore/EditedMessageAttribute.swift b/TelegramCore/EditedMessageAttribute.swift index bcbc65e7c2..a1b9e4f541 100644 --- a/TelegramCore/EditedMessageAttribute.swift +++ b/TelegramCore/EditedMessageAttribute.swift @@ -12,11 +12,11 @@ public class EditedMessageAttribute: MessageAttribute { self.date = date } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.date = decoder.decodeInt32ForKey("d", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.date, forKey: "d") } } diff --git a/TelegramCore/ExportedInvitation.swift b/TelegramCore/ExportedInvitation.swift index 6234dff1d2..1e60aa6243 100644 --- a/TelegramCore/ExportedInvitation.swift +++ b/TelegramCore/ExportedInvitation.swift @@ -5,18 +5,18 @@ import Foundation import Postbox #endif -public struct ExportedInvitation: Coding, Equatable { +public struct ExportedInvitation: PostboxCoding, Equatable { public let link: String init(link: String) { self.link = link } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.link = decoder.decodeStringForKey("l", orElse: "") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.link, forKey: "l") } diff --git a/TelegramCore/FeaturedStickerPack.swift b/TelegramCore/FeaturedStickerPack.swift index 77953cb167..d92c863c5a 100644 --- a/TelegramCore/FeaturedStickerPack.swift +++ b/TelegramCore/FeaturedStickerPack.swift @@ -36,13 +36,13 @@ public final class FeaturedStickerPackItem: OrderedItemListEntryContents { self.unread = unread } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.info = decoder.decodeObjectForKey("i") as! StickerPackCollectionInfo self.topItems = decoder.decodeObjectArrayForKey("t") self.unread = decoder.decodeInt32ForKey("u", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.info, forKey: "i") encoder.encodeObjectArray(self.topItems, forKey: "t") encoder.encodeInt32(self.unread ? 1 : 0, forKey: "u") diff --git a/TelegramCore/ForwardSourceInfoAttribute.swift b/TelegramCore/ForwardSourceInfoAttribute.swift index 7bedb90c45..aee4c48459 100644 --- a/TelegramCore/ForwardSourceInfoAttribute.swift +++ b/TelegramCore/ForwardSourceInfoAttribute.swift @@ -12,11 +12,11 @@ public class ForwardSourceInfoAttribute: MessageAttribute { self.messageId = messageId } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.messageId = MessageId(peerId: PeerId(decoder.decodeInt64ForKey("p", orElse: 0)), namespace: decoder.decodeInt32ForKey("n", orElse: 0), id: decoder.decodeInt32ForKey("i", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.messageId.peerId.toInt64(), forKey: "p") encoder.encodeInt32(self.messageId.namespace, forKey: "n") encoder.encodeInt32(self.messageId.id, forKey: "i") diff --git a/TelegramCore/GlobalNotificationSettings.swift b/TelegramCore/GlobalNotificationSettings.swift index adcd200b40..4f5f8cd89c 100644 --- a/TelegramCore/GlobalNotificationSettings.swift +++ b/TelegramCore/GlobalNotificationSettings.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public struct MessageNotificationSettings: Coding, Equatable { +public struct MessageNotificationSettings: PostboxCoding, Equatable { public let enabled: Bool public let displayPreviews: Bool public let sound: PeerMessageSound @@ -20,13 +20,13 @@ public struct MessageNotificationSettings: Coding, Equatable { self.sound = sound } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.enabled = decoder.decodeInt32ForKey("e", orElse: 0) != 0 self.displayPreviews = decoder.decodeInt32ForKey("p", orElse: 0) != 0 self.sound = PeerMessageSound.decodeInline(decoder) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.enabled ? 1 : 0, forKey: "e") encoder.encodeInt32(self.displayPreviews ? 1 : 0, forKey: "p") self.sound.encodeInline(encoder) @@ -58,7 +58,7 @@ public struct MessageNotificationSettings: Coding, Equatable { } } -public struct GlobalNotificationSettingsSet: Coding, Equatable { +public struct GlobalNotificationSettingsSet: PostboxCoding, Equatable { public let privateChats: MessageNotificationSettings public let groupChats: MessageNotificationSettings @@ -71,12 +71,12 @@ public struct GlobalNotificationSettingsSet: Coding, Equatable { self.groupChats = groupChats } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.privateChats = decoder.decodeObjectForKey("p", decoder: { MessageNotificationSettings(decoder: $0) }) as! MessageNotificationSettings self.groupChats = decoder.decodeObjectForKey("g", decoder: { MessageNotificationSettings(decoder: $0) }) as! MessageNotificationSettings } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.privateChats, forKey: "p") encoder.encodeObject(self.groupChats, forKey: "g") } @@ -117,12 +117,12 @@ public struct GlobalNotificationSettings: PreferencesEntry, Equatable { self.remote = remote } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.toBeSynchronized = decoder.decodeObjectForKey("s", decoder: { GlobalNotificationSettingsSet(decoder: $0) }) as? GlobalNotificationSettingsSet self.remote = decoder.decodeObjectForKey("r", decoder: { GlobalNotificationSettingsSet(decoder: $0) }) as! GlobalNotificationSettingsSet } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { if let toBeSynchronized = self.toBeSynchronized { encoder.encodeObject(toBeSynchronized, forKey: "s") } else { diff --git a/TelegramCore/Holes.swift b/TelegramCore/Holes.swift index 35f633d0f2..cfde2b2faf 100644 --- a/TelegramCore/Holes.swift +++ b/TelegramCore/Holes.swift @@ -31,10 +31,10 @@ func fetchMessageHistoryHole(network: Network, postbox: Postbox, hole: MessageHi //|> delay(4.0, queue: Queue.concurrentDefaultQueue()) |> mapToSignal { peer in if let inputPeer = apiInputPeer(peer) { - let limit = 100 + let limit = 20 let request: Signal - if let tagMask = tagMask, let filter = messageFilterForTagMask(tagMask) { + if let tagMask = tagMask { if tagMask == MessageTags.unseenPersonalMessage { let offsetId: Int32 let addOffset: Int32 @@ -60,7 +60,7 @@ func fetchMessageHistoryHole(network: Network, postbox: Postbox, hole: MessageHi minId = 1 } request = network.request(Api.functions.messages.getUnreadMentions(peer: inputPeer, offsetId: offsetId, addOffset: addOffset, limit: Int32(selectedLimit), maxId: maxId, minId: minId)) - } else { + } else if let filter = messageFilterForTagMask(tagMask) { let offsetId: Int32 let addOffset: Int32 let selectedLimit = limit @@ -86,6 +86,9 @@ func fetchMessageHistoryHole(network: Network, postbox: Postbox, hole: MessageHi } request = network.request(Api.functions.messages.search(flags: 0, peer: inputPeer, q: "", fromId: nil, filter: filter, minDate: 0, maxDate: hole.maxIndex.timestamp, offsetId: offsetId, addOffset: addOffset, limit: Int32(selectedLimit), maxId: maxId, minId: minId)) + } else { + assertionFailure() + request = .never() } } else { let offsetId: Int32 @@ -269,7 +272,7 @@ func fetchChatListHole(network: Network, postbox: Postbox, hole: ChatListHole) - } readStates[peerId]![Namespaces.Message.Cloud] = .idBased(maxIncomingReadId: apiReadInboxMaxId, maxOutgoingReadId: apiReadOutboxMaxId, maxKnownId: apiTopMessage, count: apiUnreadCount) - if apiUnreadMentionsCount != 0 && apiTopMessage != 0 { + if apiTopMessage != 0 { mentionTagSummaries[peerId] = MessageHistoryTagNamespaceSummary(version: 1, count: apiUnreadMentionsCount, range: MessageHistoryTagNamespaceCountValidityRange(maxId: apiTopMessage)) } @@ -331,8 +334,8 @@ func fetchChatListHole(network: Network, postbox: Postbox, hole: ChatListHole) - } readStates[peerId]![Namespaces.Message.Cloud] = .idBased(maxIncomingReadId: apiReadInboxMaxId, maxOutgoingReadId: apiReadOutboxMaxId, maxKnownId: apiTopMessage, count: apiUnreadCount) - if apiUnreadMentionsCount != 0 && apiTopMessage != 0 { - mentionTagSummaries[peerId] = MessageHistoryTagNamespaceSummary(version: 1, count: apiUnreadMentionsCount, range: MessageHistoryTagNamespaceCountValidityRange(maxId: apiTopMessage)) + if apiTopMessage != 0 { + mentionTagSummaries[peerId] = MessageHistoryTagNamespaceSummary(version: 1, count: 0, range: MessageHistoryTagNamespaceCountValidityRange(maxId: apiTopMessage)) } notificationSettings[peerId] = TelegramPeerNotificationSettings(apiSettings: apiNotificationSettings) @@ -403,7 +406,7 @@ func fetchChatListHole(network: Network, postbox: Postbox, hole: ChatListHole) - } readStates[peerId]![Namespaces.Message.Cloud] = .idBased(maxIncomingReadId: apiReadInboxMaxId, maxOutgoingReadId: apiReadOutboxMaxId, maxKnownId: apiTopMessage, count: apiUnreadCount) - if apiUnreadMentionsCount != 0 && apiTopMessage != 0 { + if apiTopMessage != 0 { mentionTagSummaries[peerId] = MessageHistoryTagNamespaceSummary(version: 1, count: apiUnreadMentionsCount, range: MessageHistoryTagNamespaceCountValidityRange(maxId: apiTopMessage)) } diff --git a/TelegramCore/InlineBotMessageAttribute.swift b/TelegramCore/InlineBotMessageAttribute.swift index 45c5067906..a334ab64c7 100644 --- a/TelegramCore/InlineBotMessageAttribute.swift +++ b/TelegramCore/InlineBotMessageAttribute.swift @@ -16,11 +16,11 @@ public class InlineBotMessageAttribute: MessageAttribute { self.peerId = peerId } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.peerId = PeerId(decoder.decodeInt64ForKey("i", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.peerId.toInt64(), forKey: "i") } } diff --git a/TelegramCore/InstantPage.swift b/TelegramCore/InstantPage.swift index 1fcd3cd62c..3007fc56c6 100644 --- a/TelegramCore/InstantPage.swift +++ b/TelegramCore/InstantPage.swift @@ -31,7 +31,7 @@ private enum InstantPageBlockType: Int32 { case audio = 22 } -public indirect enum InstantPageBlock: Coding, Equatable { +public indirect enum InstantPageBlock: PostboxCoding, Equatable { case unsupported case title(RichText) case subtitle(RichText) @@ -56,7 +56,7 @@ public indirect enum InstantPageBlock: Coding, Equatable { case slideshow(items: [InstantPageBlock], caption: RichText) case channelBanner(TelegramChannel?) - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case InstantPageBlockType.unsupported.rawValue: self = .unsupported @@ -119,7 +119,7 @@ public indirect enum InstantPageBlock: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case .unsupported: encoder.encodeInt32(InstantPageBlockType.unsupported.rawValue, forKey: "r") @@ -399,14 +399,14 @@ public indirect enum InstantPageBlock: Coding, Equatable { } } -private final class MediaDictionary: Coding { +private final class MediaDictionary: PostboxCoding { let dict: [MediaId: Media] init(dict: [MediaId: Media]) { self.dict = dict } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { let idsBufer = decoder.decodeBytesForKey("i")! let mediaIds = MediaId.decodeArrayFromBuffer(idsBufer) let medias = decoder.decodeObjectArrayForKey("m") @@ -420,9 +420,9 @@ private final class MediaDictionary: Coding { self.dict = dict } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { var mediaIds: [MediaId] = [] - var medias: [Coding] = [] + var medias: [PostboxCoding] = [] for mediaId in self.dict.keys { mediaIds.append(mediaId) medias.append(self.dict[mediaId]!) @@ -434,7 +434,7 @@ private final class MediaDictionary: Coding { } } -public final class InstantPage: Coding, Equatable { +public final class InstantPage: PostboxCoding, Equatable { public let blocks: [InstantPageBlock] public let media: [MediaId: Media] public let isComplete: Bool @@ -445,13 +445,13 @@ public final class InstantPage: Coding, Equatable { self.isComplete = isComplete } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.blocks = decoder.decodeObjectArrayWithDecoderForKey("b") self.media = MediaDictionary(decoder: decoder).dict self.isComplete = decoder.decodeInt32ForKey("c", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.blocks, forKey: "b") MediaDictionary(dict: self.media).encode(encoder) encoder.encodeInt32(self.isComplete ? 1 : 0, forKey: "c") diff --git a/TelegramCore/LimitsConfiguration.swift b/TelegramCore/LimitsConfiguration.swift index 5791f3430d..9094612157 100644 --- a/TelegramCore/LimitsConfiguration.swift +++ b/TelegramCore/LimitsConfiguration.swift @@ -26,7 +26,7 @@ public final class LimitsConfiguration: PreferencesEntry { self.maxMessageEditingInterval = maxMessageEditingInterval } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.maxGroupMemberCount = decoder.decodeInt32ForKey("maxGroupMemberCount", orElse: 200) self.maxSupergroupMemberCount = decoder.decodeInt32ForKey("maxSupergroupMemberCount", orElse: 5000) self.maxMessageForwardBatchSize = decoder.decodeInt32ForKey("maxMessageForwardBatchSize", orElse: 50) @@ -35,7 +35,7 @@ public final class LimitsConfiguration: PreferencesEntry { self.maxMessageEditingInterval = decoder.decodeInt32ForKey("maxMessageEditingInterval", orElse: 2 * 24 * 60 * 60) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.maxGroupMemberCount, forKey: "maxGroupMemberCount") encoder.encodeInt32(self.maxSupergroupMemberCount, forKey: "maxSupergroupMemberCount") encoder.encodeInt32(self.maxMessageForwardBatchSize, forKey: "maxMessageForwardBatchSize") diff --git a/TelegramCore/LoadedPeer.swift b/TelegramCore/LoadedPeer.swift new file mode 100644 index 0000000000..d820cdbbfc --- /dev/null +++ b/TelegramCore/LoadedPeer.swift @@ -0,0 +1,75 @@ +#if os(macOS) + import PostboxMac + import SwiftSignalKitMac +#else + import Postbox + import SwiftSignalKit +#endif + +public func actualizedPeer(postbox: Postbox, network: Network, peer: Peer) -> Signal { + return postbox.modify { modifier -> Signal in + var signal: Signal + var actualizeChannel: Api.InputChannel? + if let currentPeer = modifier.getPeer(peer.id) { + signal = .single(currentPeer) + if let currentPeer = currentPeer as? TelegramChannel { + switch currentPeer.participationStatus { + case .left, .kicked: + actualizeChannel = apiInputChannel(currentPeer) + default: + break + } + } + } else { + signal = .single(peer) + if let peer = peer as? TelegramChannel { + actualizeChannel = apiInputChannel(peer) + } + } + if let actualizeChannel = actualizeChannel { + let remote = network.request(Api.functions.channels.getChannels(id: [actualizeChannel])) + |> map(Optional.init) + |> `catch` { _ -> Signal in + return .single(nil) + } + |> mapToSignal { result -> Signal in + var remotePeer: Peer? + if let result = result { + let chats: [Api.Chat] + switch result { + case let .chats(apiChats): + chats = apiChats + case let .chatsSlice(_, apiChats): + chats = apiChats + } + for chat in chats { + if let parsedPeer = parseTelegramGroupOrChannel(chat: chat), parsedPeer.id == peer.id { + remotePeer = parsedPeer + } + } + } + if let remotePeer = remotePeer { + return postbox.modify { modifier -> Peer in + updatePeers(modifier: modifier, peers: [remotePeer], update: { _, updated in + return updated + }) + return remotePeer + } + } else { + return .complete() + } + } + signal = signal |> then(remote) + } + + let updatedView: Signal = postbox.combinedView(keys: [.peer(peerId: peer.id)]) + |> mapToSignal { view -> Signal in + if let peerView = view.views[.peer(peerId: peer.id)] as? PeerView, let peer = peerView.peers[peerView.peerId] { + return .single(peer) + } + return .complete() + } + + return (signal |> then(updatedView)) |> distinctUntilChanged(isEqual: { $0.isEqual($1) }) + } |> switchToLatest +} diff --git a/TelegramCore/Localization.swift b/TelegramCore/Localization.swift index 6a729f3192..ff9c05984f 100644 --- a/TelegramCore/Localization.swift +++ b/TelegramCore/Localization.swift @@ -87,7 +87,7 @@ private func writeString(_ buffer: WriteBuffer, _ string: String) { } } -public final class Localization: Coding, Equatable { +public final class Localization: PostboxCoding, Equatable { public let version: Int32 public let entries: [LocalizationEntry] @@ -96,7 +96,7 @@ public final class Localization: Coding, Equatable { self.entries = entries } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.version = decoder.decodeInt32ForKey("v", orElse: 0) let count = decoder.decodeInt32ForKey("c", orElse: 0) var entries: [LocalizationEntry] = [] @@ -192,7 +192,7 @@ public final class Localization: Coding, Equatable { self.entries = entries } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(Int32(self.entries.count), forKey: "c") let buffer = WriteBuffer() diff --git a/TelegramCore/LocalizationSettings.swift b/TelegramCore/LocalizationSettings.swift index c1581a8696..1ef70a9123 100644 --- a/TelegramCore/LocalizationSettings.swift +++ b/TelegramCore/LocalizationSettings.swift @@ -14,12 +14,12 @@ public final class LocalizationSettings: PreferencesEntry, Equatable { self.localization = localization } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.languageCode = decoder.decodeStringForKey("lc", orElse: "en") self.localization = decoder.decodeObjectForKey("loc", decoder: { Localization(decoder: $0) }) as! Localization } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.languageCode, forKey: "lc") encoder.encodeObject(self.localization, forKey: "loc") } diff --git a/TelegramCore/LoggedOutAccountAttribute.swift b/TelegramCore/LoggedOutAccountAttribute.swift index c5a56a956d..c7e0283d51 100644 --- a/TelegramCore/LoggedOutAccountAttribute.swift +++ b/TelegramCore/LoggedOutAccountAttribute.swift @@ -9,10 +9,10 @@ public final class LoggedOutAccountAttribute: AccountRecordAttribute { public init() { } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { } public func isEqual(to: AccountRecordAttribute) -> Bool { diff --git a/TelegramCore/ManagedConsumePersonalMessagesActions.swift b/TelegramCore/ManagedConsumePersonalMessagesActions.swift index d07c2a3b05..5f5660dd40 100644 --- a/TelegramCore/ManagedConsumePersonalMessagesActions.swift +++ b/TelegramCore/ManagedConsumePersonalMessagesActions.swift @@ -11,10 +11,12 @@ import Foundation private final class ManagedConsumePersonalMessagesActionsHelper { var operationDisposables: [MessageId: Disposable] = [:] + var validateDisposables: [InvalidatedMessageHistoryTagsSummaryEntry: Disposable] = [:] - func update(_ entries: [PendingMessageActionsEntry]) -> (disposeOperations: [Disposable], beginOperations: [(PendingMessageActionsEntry, MetaDisposable)]) { + func update(entries: [PendingMessageActionsEntry], invalidateEntries: Set) -> (disposeOperations: [Disposable], beginOperations: [(PendingMessageActionsEntry, MetaDisposable)], beginValidateOperations: [(InvalidatedMessageHistoryTagsSummaryEntry, MetaDisposable)]) { var disposeOperations: [Disposable] = [] var beginOperations: [(PendingMessageActionsEntry, MetaDisposable)] = [] + var beginValidateOperations: [(InvalidatedMessageHistoryTagsSummaryEntry, MetaDisposable)] = [] var hasRunningOperationForPeerId = Set() var validIds = Set() @@ -43,7 +45,32 @@ private final class ManagedConsumePersonalMessagesActionsHelper { self.operationDisposables.removeValue(forKey: id) } - return (disposeOperations, beginOperations) + var validInvalidateEntries = Set() + + for entry in invalidateEntries { + if !hasRunningOperationForPeerId.contains(entry.key.peerId) { + validInvalidateEntries.insert(entry) + if self.validateDisposables[entry] == nil { + let disposable = MetaDisposable() + beginValidateOperations.append((entry, disposable)) + self.validateDisposables[entry] = disposable + } + } + } + + var removeValidateEntries: [InvalidatedMessageHistoryTagsSummaryEntry] = [] + for (entry, disposable) in self.validateDisposables { + if !validInvalidateEntries.contains(entry) { + removeValidateEntries.append(entry) + disposeOperations.append(disposable) + } + } + + for entry in removeValidateEntries { + self.validateDisposables.removeValue(forKey: entry) + } + + return (disposeOperations, beginOperations, beginValidateOperations) } func reset() -> [Disposable] { @@ -65,20 +92,24 @@ private func withTakenAction(postbox: Postbox, type: PendingMessageActionType, i } |> switchToLatest } - func managedConsumePersonalMessagesActions(postbox: Postbox, network: Network, stateManager: AccountStateManager) -> Signal { return Signal { _ in let helper = Atomic(value: ManagedConsumePersonalMessagesActionsHelper()) - let key = PostboxViewKey.pendingMessageActions(type: .consumeUnseenPersonalMessage) - let disposable = postbox.combinedView(keys: [key]).start(next: { view in + let actionsKey = PostboxViewKey.pendingMessageActions(type: .consumeUnseenPersonalMessage) + let invalidateKey = PostboxViewKey.invalidatedMessageHistoryTagSummaries(tagMask: .unseenPersonalMessage, namespace: Namespaces.Message.Cloud) + let disposable = postbox.combinedView(keys: [actionsKey, invalidateKey]).start(next: { view in var entries: [PendingMessageActionsEntry] = [] - if let v = view.views[key] as? PendingMessageActionsView { + var invalidateEntries = Set() + if let v = view.views[actionsKey] as? PendingMessageActionsView { entries = v.entries } + if let v = view.views[invalidateKey] as? InvalidatedMessageHistoryTagSummariesView { + invalidateEntries = v.entries + } - let (disposeOperations, beginOperations) = helper.with { helper -> (disposeOperations: [Disposable], beginOperations: [(PendingMessageActionsEntry, MetaDisposable)]) in - return helper.update(entries) + let (disposeOperations, beginOperations, beginValidateOperations) = helper.with { helper -> (disposeOperations: [Disposable], beginOperations: [(PendingMessageActionsEntry, MetaDisposable)], beginValidateOperations: [(InvalidatedMessageHistoryTagsSummaryEntry, MetaDisposable)]) in + return helper.update(entries: entries, invalidateEntries: invalidateEntries) } for disposable in disposeOperations { @@ -102,6 +133,14 @@ func managedConsumePersonalMessagesActions(postbox: Postbox, network: Network, s disposable.set(signal.start()) } + + for (entry, disposable) in beginValidateOperations { + let signal = synchronizeUnseenPersonalMentionsTag(postbox: postbox, network: network, entry: entry) + |> then(postbox.modify { modifier -> Void in + modifier.removeInvalidatedMessageHistoryTagsSummaryEntry(entry) + }) + disposable.set(signal.start()) + } }) return ActionDisposable { @@ -183,3 +222,41 @@ private func synchronizeConsumeMessageContents(modifier: Modifier, postbox: Post return .complete() } } + +private func synchronizeUnseenPersonalMentionsTag(postbox: Postbox, network: Network, entry: InvalidatedMessageHistoryTagsSummaryEntry) -> Signal { + return postbox.modify { modifier -> Signal in + if let peer = modifier.getPeer(entry.key.peerId), let inputPeer = apiInputPeer(peer) { + return network.request(Api.functions.messages.getPeerDialogs(peers: [inputPeer])) + |> map(Optional.init) + |> `catch` { _ -> Signal in + return .single(nil) + } + |> mapToSignal { result -> Signal in + if let result = result { + switch result { + case let .peerDialogs(dialogs, _, _, _, _): + if let dialog = dialogs.filter({ $0.peerId == entry.key.peerId }).first { + let apiTopMessage: Int32 + let apiUnreadMentionsCount: Int32 + switch dialog { + case let .dialog(_, _, topMessage, _, _, _, unreadMentionsCount, _, _, _): + apiTopMessage = topMessage + apiUnreadMentionsCount = unreadMentionsCount + } + + return postbox.modify { modifier -> Void in + modifier.replaceMessageTagSummary(peerId: entry.key.peerId, tagMask: entry.key.tagMask, namespace: entry.key.namespace, count: apiUnreadMentionsCount, maxId: apiTopMessage) + } + } else { + return .complete() + } + } + } else { + return .complete() + } + } + } else { + return .complete() + } + } |> switchToLatest +} diff --git a/TelegramCore/ManagedDeviceContacts.swift b/TelegramCore/ManagedDeviceContacts.swift index 70e1d1cfbd..6e8a3e4cf7 100644 --- a/TelegramCore/ManagedDeviceContacts.swift +++ b/TelegramCore/ManagedDeviceContacts.swift @@ -15,11 +15,11 @@ final class ManagedDeviceContactsMetaInfo: UnorderedItemListTagMetaInfo { self.version = version } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.version = decoder.decodeInt32ForKey("v", orElse: 0) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.version, forKey: "v") } @@ -32,7 +32,7 @@ final class ManagedDeviceContactsMetaInfo: UnorderedItemListTagMetaInfo { } } -final class ManagedDeviceContactEntryContents: Coding { +final class ManagedDeviceContactEntryContents: PostboxCoding { let firstName: String let lastName: String let phoneNumber: String @@ -47,7 +47,7 @@ final class ManagedDeviceContactEntryContents: Coding { self.importDelayedUntil = importDelayedUntil } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.firstName = decoder.decodeStringForKey("f", orElse: "") self.lastName = decoder.decodeStringForKey("l", orElse: "") self.phoneNumber = decoder.decodeStringForKey("n", orElse: "") @@ -59,7 +59,7 @@ final class ManagedDeviceContactEntryContents: Coding { self.importDelayedUntil = decoder.decodeOptionalInt32ForKey("dt") } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.firstName, forKey: "f") encoder.encodeString(self.lastName, forKey: "l") encoder.encodeString(self.phoneNumber, forKey: "n") diff --git a/TelegramCore/MarkMessageContentAsConsumedInteractively.swift b/TelegramCore/MarkMessageContentAsConsumedInteractively.swift index 84075ae996..3ae4613470 100644 --- a/TelegramCore/MarkMessageContentAsConsumedInteractively.swift +++ b/TelegramCore/MarkMessageContentAsConsumedInteractively.swift @@ -94,6 +94,7 @@ func markMessageContentAsConsumedRemotely(modifier: Modifier, messageId: Message } updatedAttributes[i] = ConsumablePersonalMentionMessageAttribute(consumed: true, pending: false) updatedTags.remove(.unseenPersonalMessage) + updateMessage = true } } diff --git a/TelegramCore/MultipartUpload.swift b/TelegramCore/MultipartUpload.swift index a86dee1f18..2e8e0bef79 100644 --- a/TelegramCore/MultipartUpload.swift +++ b/TelegramCore/MultipartUpload.swift @@ -16,7 +16,7 @@ import TelegramCorePrivateModule private typealias SignalKitTimer = SwiftSignalKit.Timer #endif -public final class SecretFileEncryptionKey: Coding, Equatable { +public final class SecretFileEncryptionKey: PostboxCoding, Equatable { public let aesKey: Data public let aesIv: Data @@ -25,12 +25,12 @@ public final class SecretFileEncryptionKey: Coding, Equatable { self.aesIv = aesIv } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.aesKey = decoder.decodeBytesForKey("k")!.makeData() self.aesIv = decoder.decodeBytesForKey("i")!.makeData() } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeBytes(MemoryBuffer(data: self.aesKey), forKey: "k") encoder.encodeBytes(MemoryBuffer(data: self.aesIv), forKey: "i") } diff --git a/TelegramCore/NotificationInfoMessageAttribute.swift b/TelegramCore/NotificationInfoMessageAttribute.swift index 7561d6ba2b..2d5b0e678d 100644 --- a/TelegramCore/NotificationInfoMessageAttribute.swift +++ b/TelegramCore/NotificationInfoMessageAttribute.swift @@ -30,11 +30,11 @@ public class NotificationInfoMessageAttribute: MessageAttribute { self.flags = flags } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.flags = NotificationInfoMessageAttributeFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.flags.rawValue, forKey: "f") } } diff --git a/TelegramCore/OutgoingChatContextResultMessageAttribute.swift b/TelegramCore/OutgoingChatContextResultMessageAttribute.swift index 3e6b3b3ddc..f135a1878a 100644 --- a/TelegramCore/OutgoingChatContextResultMessageAttribute.swift +++ b/TelegramCore/OutgoingChatContextResultMessageAttribute.swift @@ -14,12 +14,12 @@ public class OutgoingChatContextResultMessageAttribute: MessageAttribute { self.id = id } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.queryId = decoder.decodeInt64ForKey("q", orElse: 0) self.id = decoder.decodeStringForKey("i", orElse: "") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.queryId, forKey: "q") encoder.encodeString(self.id, forKey: "i") } diff --git a/TelegramCore/OutgoingContentInfoMessageAttribute.swift b/TelegramCore/OutgoingContentInfoMessageAttribute.swift index 8c19ec7ba5..75be97b25a 100644 --- a/TelegramCore/OutgoingContentInfoMessageAttribute.swift +++ b/TelegramCore/OutgoingContentInfoMessageAttribute.swift @@ -26,11 +26,11 @@ public class OutgoingContentInfoMessageAttribute: MessageAttribute { self.flags = flags } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.flags = OutgoingContentInfoFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.flags.rawValue, forKey: "f") } diff --git a/TelegramCore/OutgoingMessageInfoAttribute.swift b/TelegramCore/OutgoingMessageInfoAttribute.swift index d1f9aca02e..27216a9fe6 100644 --- a/TelegramCore/OutgoingMessageInfoAttribute.swift +++ b/TelegramCore/OutgoingMessageInfoAttribute.swift @@ -28,12 +28,12 @@ public class OutgoingMessageInfoAttribute: MessageAttribute { self.flags = flags } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.uniqueId = decoder.decodeInt64ForKey("u", orElse: 0) self.flags = OutgoingMessageInfoFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.uniqueId, forKey: "u") encoder.encodeInt32(self.flags.rawValue, forKey: "f") } diff --git a/TelegramCore/PeerAccessRestrictionInfo.swift b/TelegramCore/PeerAccessRestrictionInfo.swift index 65956c45ad..446fa9850b 100644 --- a/TelegramCore/PeerAccessRestrictionInfo.swift +++ b/TelegramCore/PeerAccessRestrictionInfo.swift @@ -5,18 +5,18 @@ import Foundation import Postbox #endif -public final class PeerAccessRestrictionInfo: Coding, Equatable { +public final class PeerAccessRestrictionInfo: PostboxCoding, Equatable { public let reason: String init(reason: String) { self.reason = reason } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.reason = decoder.decodeStringForKey("rsn", orElse: "") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.reason, forKey: "rsn") } diff --git a/TelegramCore/ProxySettings.swift b/TelegramCore/ProxySettings.swift index 8fec8c8203..ef7c5c9980 100644 --- a/TelegramCore/ProxySettings.swift +++ b/TelegramCore/ProxySettings.swift @@ -22,14 +22,14 @@ public final class ProxySettings: PreferencesEntry { self.password = password } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.host = decoder.decodeStringForKey("host", orElse: "") self.port = decoder.decodeInt32ForKey("port", orElse: 0) self.username = decoder.decodeOptionalStringForKey("username") self.password = decoder.decodeOptionalStringForKey("password") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.host, forKey: "host") encoder.encodeInt32(self.port, forKey: "port") if let username = self.username { diff --git a/TelegramCore/RecentMediaItem.swift b/TelegramCore/RecentMediaItem.swift index 61f2206215..92264eed2c 100644 --- a/TelegramCore/RecentMediaItem.swift +++ b/TelegramCore/RecentMediaItem.swift @@ -36,11 +36,11 @@ public final class RecentMediaItem: OrderedItemListEntryContents, Equatable { self.media = media } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.media = decoder.decodeObjectForKey("m") as! Media } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.media, forKey: "m") } diff --git a/TelegramCore/RecentPeerItem.swift b/TelegramCore/RecentPeerItem.swift index e7c0e88274..176bf92a70 100644 --- a/TelegramCore/RecentPeerItem.swift +++ b/TelegramCore/RecentPeerItem.swift @@ -32,11 +32,11 @@ public final class RecentPeerItem: OrderedItemListEntryContents { self.rating = rating } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.rating = decoder.decodeDoubleForKey("r", orElse: 0.0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeDouble(self.rating, forKey: "r") } } diff --git a/TelegramCore/ReplyMarkupMessageAttribute.swift b/TelegramCore/ReplyMarkupMessageAttribute.swift index a599bd5136..950ecea97c 100644 --- a/TelegramCore/ReplyMarkupMessageAttribute.swift +++ b/TelegramCore/ReplyMarkupMessageAttribute.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public enum ReplyMarkupButtonAction: Coding, Equatable { +public enum ReplyMarkupButtonAction: PostboxCoding, Equatable { case text case url(String) case callback(MemoryBuffer) @@ -15,7 +15,7 @@ public enum ReplyMarkupButtonAction: Coding, Equatable { case openWebApp case payment - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("v", orElse: 0) { case 0: self = .text @@ -38,7 +38,7 @@ public enum ReplyMarkupButtonAction: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case .text: encoder.encodeInt32(0, forKey: "v") @@ -117,7 +117,7 @@ public enum ReplyMarkupButtonAction: Coding, Equatable { } } -public struct ReplyMarkupButton: Coding, Equatable { +public struct ReplyMarkupButton: PostboxCoding, Equatable { public let title: String public let action: ReplyMarkupButtonAction @@ -126,12 +126,12 @@ public struct ReplyMarkupButton: Coding, Equatable { self.action = action } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.title = decoder.decodeStringForKey(".t", orElse: "") self.action = ReplyMarkupButtonAction(decoder: decoder) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.title, forKey: ".t") self.action.encode(encoder) } @@ -141,18 +141,18 @@ public struct ReplyMarkupButton: Coding, Equatable { } } -public struct ReplyMarkupRow: Coding, Equatable { +public struct ReplyMarkupRow: PostboxCoding, Equatable { public let buttons: [ReplyMarkupButton] init(buttons: [ReplyMarkupButton]) { self.buttons = buttons } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.buttons = decoder.decodeObjectArrayWithDecoderForKey("b") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.buttons, forKey: "b") } @@ -188,12 +188,12 @@ public class ReplyMarkupMessageAttribute: MessageAttribute, Equatable { self.flags = flags } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.rows = decoder.decodeObjectArrayWithDecoderForKey("r") self.flags = ReplyMarkupMessageFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.rows, forKey: "r") encoder.encodeInt32(self.flags.rawValue, forKey: "f") } diff --git a/TelegramCore/ReplyMessageAttribute.swift b/TelegramCore/ReplyMessageAttribute.swift index 12c1e579f1..b8edf34112 100644 --- a/TelegramCore/ReplyMessageAttribute.swift +++ b/TelegramCore/ReplyMessageAttribute.swift @@ -16,12 +16,12 @@ public class ReplyMessageAttribute: MessageAttribute { self.messageId = messageId } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { let namespaceAndId: Int64 = decoder.decodeInt64ForKey("i", orElse: 0) self.messageId = MessageId(peerId: PeerId(decoder.decodeInt64ForKey("p", orElse: 0)), namespace: Int32(namespaceAndId & 0xffffffff), id: Int32((namespaceAndId >> 32) & 0xffffffff)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { let namespaceAndId = Int64(self.messageId.namespace) | (Int64(self.messageId.id) << 32) encoder.encodeInt64(namespaceAndId, forKey: "i") encoder.encodeInt64(self.messageId.peerId.toInt64(), forKey: "p") diff --git a/TelegramCore/ResolvePeerByName.swift b/TelegramCore/ResolvePeerByName.swift index 37acc88c49..8ef15584d5 100644 --- a/TelegramCore/ResolvePeerByName.swift +++ b/TelegramCore/ResolvePeerByName.swift @@ -7,7 +7,7 @@ import Foundation import SwiftSignalKit #endif -final class CachedResolvedByNamePeer: Coding { +final class CachedResolvedByNamePeer: PostboxCoding { let peerId: PeerId? let timestamp: Int32 @@ -29,7 +29,7 @@ final class CachedResolvedByNamePeer: Coding { self.timestamp = timestamp } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { if let peerId = decoder.decodeOptionalInt64ForKey("p") { self.peerId = PeerId(peerId) } else { @@ -38,7 +38,7 @@ final class CachedResolvedByNamePeer: Coding { self.timestamp = decoder.decodeInt32ForKey("t", orElse: 0) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { if let peerId = self.peerId { encoder.encodeInt64(peerId.toInt64(), forKey: "p") } else { diff --git a/TelegramCore/RichText.swift b/TelegramCore/RichText.swift index 179f9c89a8..65d35af655 100644 --- a/TelegramCore/RichText.swift +++ b/TelegramCore/RichText.swift @@ -18,7 +18,7 @@ private enum RichTextTypes: Int32 { case concat = 9 } -public indirect enum RichText: Coding, Equatable { +public indirect enum RichText: PostboxCoding, Equatable { case empty case plain(String) case bold(RichText) @@ -30,7 +30,7 @@ public indirect enum RichText: Coding, Equatable { case email(text: RichText, email: String) case concat([RichText]) - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case RichTextTypes.empty.rawValue: self = .empty @@ -63,7 +63,7 @@ public indirect enum RichText: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case .empty: encoder.encodeInt32(RichTextTypes.empty.rawValue, forKey: "r") diff --git a/TelegramCore/SavedStickerItem.swift b/TelegramCore/SavedStickerItem.swift index c86626c560..a51aacb25b 100644 --- a/TelegramCore/SavedStickerItem.swift +++ b/TelegramCore/SavedStickerItem.swift @@ -14,12 +14,12 @@ public final class SavedStickerItem: OrderedItemListEntryContents, Equatable { self.stringRepresentations = stringRepresentations } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.file = decoder.decodeObjectForKey("f") as! TelegramMediaFile self.stringRepresentations = decoder.decodeStringArrayForKey("sr") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.file, forKey: "f") encoder.encodeStringArray(self.stringRepresentations, forKey: "sr") } diff --git a/TelegramCore/SecretChatEncryptionConfig.swift b/TelegramCore/SecretChatEncryptionConfig.swift index baa24f8f43..160a69e8b4 100644 --- a/TelegramCore/SecretChatEncryptionConfig.swift +++ b/TelegramCore/SecretChatEncryptionConfig.swift @@ -9,7 +9,7 @@ import Foundation import MtProtoKitDynamic #endif -final class SecretChatEncryptionConfig: Coding { +final class SecretChatEncryptionConfig: PostboxCoding { let g: Int32 let p: MemoryBuffer let version: Int32 @@ -20,13 +20,13 @@ final class SecretChatEncryptionConfig: Coding { self.version = version } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.g = decoder.decodeInt32ForKey("g", orElse: 0) self.p = decoder.decodeBytesForKey("p")! self.version = decoder.decodeInt32ForKey("v", orElse: 0) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.g, forKey: "g") encoder.encodeBytes(self.p, forKey: "p") encoder.encodeInt32(self.version, forKey: "v") diff --git a/TelegramCore/SecretChatFileReference.swift b/TelegramCore/SecretChatFileReference.swift index 2305f9331c..2dd9b618da 100644 --- a/TelegramCore/SecretChatFileReference.swift +++ b/TelegramCore/SecretChatFileReference.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -final class SecretChatFileReference: Coding { +final class SecretChatFileReference: PostboxCoding { let id: Int64 let accessHash: Int64 let size: Int32 @@ -20,7 +20,7 @@ final class SecretChatFileReference: Coding { self.keyFingerprint = keyFingerprint } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.id = decoder.decodeInt64ForKey("i", orElse: 0) self.accessHash = decoder.decodeInt64ForKey("a", orElse: 0) self.size = decoder.decodeInt32ForKey("s", orElse: 0) @@ -28,7 +28,7 @@ final class SecretChatFileReference: Coding { self.keyFingerprint = decoder.decodeInt32ForKey("f", orElse: 0) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.id, forKey: "i") encoder.encodeInt64(self.accessHash, forKey: "a") encoder.encodeInt32(self.size, forKey: "s") diff --git a/TelegramCore/SecretChatIncomingDecryptedOperation.swift b/TelegramCore/SecretChatIncomingDecryptedOperation.swift index 41b9f0de8b..7087cb03af 100644 --- a/TelegramCore/SecretChatIncomingDecryptedOperation.swift +++ b/TelegramCore/SecretChatIncomingDecryptedOperation.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -struct SecretChatOperationSequenceInfo: Coding { +struct SecretChatOperationSequenceInfo: PostboxCoding { let topReceivedOperationIndex: Int32 let operationIndex: Int32 @@ -14,18 +14,18 @@ struct SecretChatOperationSequenceInfo: Coding { self.operationIndex = operationIndex } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.topReceivedOperationIndex = decoder.decodeInt32ForKey("r", orElse: 0) self.operationIndex = decoder.decodeInt32ForKey("o", orElse: 0) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.topReceivedOperationIndex, forKey: "r") encoder.encodeInt32(self.operationIndex, forKey: "o") } } -final class SecretChatIncomingDecryptedOperation: Coding { +final class SecretChatIncomingDecryptedOperation: PostboxCoding { let timestamp: Int32 let layer: Int32 let sequenceInfo: SecretChatOperationSequenceInfo? @@ -40,7 +40,7 @@ final class SecretChatIncomingDecryptedOperation: Coding { self.file = file } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.timestamp = decoder.decodeInt32ForKey("t", orElse: 0) self.layer = decoder.decodeInt32ForKey("l", orElse: 0) self.sequenceInfo = decoder.decodeObjectForKey("s", decoder: { SecretChatOperationSequenceInfo(decoder: $0) }) as? SecretChatOperationSequenceInfo @@ -48,7 +48,7 @@ final class SecretChatIncomingDecryptedOperation: Coding { self.file = decoder.decodeObjectForKey("f", decoder: { SecretChatFileReference(decoder: $0) }) as? SecretChatFileReference } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.timestamp, forKey: "t") encoder.encodeInt32(self.layer, forKey: "l") if let sequenceInfo = self.sequenceInfo { diff --git a/TelegramCore/SecretChatIncomingEncryptedOperation.swift b/TelegramCore/SecretChatIncomingEncryptedOperation.swift index 7e4e6aa5aa..d6176a3aed 100644 --- a/TelegramCore/SecretChatIncomingEncryptedOperation.swift +++ b/TelegramCore/SecretChatIncomingEncryptedOperation.swift @@ -27,7 +27,7 @@ enum SecretChatIncomingEncryptedOperationType: Int32 { } } -final class SecretChatIncomingEncryptedOperation: Coding { +final class SecretChatIncomingEncryptedOperation: PostboxCoding { let peerId: PeerId let globallyUniqueId: Int64 let timestamp: Int32 @@ -46,7 +46,7 @@ final class SecretChatIncomingEncryptedOperation: Coding { self.mediaFileReference = mediaFileReference } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.peerId = PeerId(decoder.decodeInt64ForKey("p", orElse: 0)) self.globallyUniqueId = decoder.decodeInt64ForKey("u", orElse: 0) self.timestamp = decoder.decodeInt32ForKey("t", orElse: 0) @@ -56,7 +56,7 @@ final class SecretChatIncomingEncryptedOperation: Coding { self.mediaFileReference = decoder.decodeObjectForKey("m", decoder: { SecretChatFileReference(decoder: $0) }) as? SecretChatFileReference } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.peerId.toInt64(), forKey: "p") encoder.encodeInt64(self.globallyUniqueId, forKey: "u") encoder.encodeInt32(self.timestamp, forKey: "t") diff --git a/TelegramCore/SecretChatKeychain.swift b/TelegramCore/SecretChatKeychain.swift index d866ac1aac..563624e714 100644 --- a/TelegramCore/SecretChatKeychain.swift +++ b/TelegramCore/SecretChatKeychain.swift @@ -5,11 +5,11 @@ import Foundation import Postbox #endif -enum SecretChatKeyValidity: Coding, Equatable { +enum SecretChatKeyValidity: PostboxCoding, Equatable { case indefinite case sequenceBasedIndexRange(fromCanonicalIndex: Int32) - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case 0: self = .indefinite @@ -21,7 +21,7 @@ enum SecretChatKeyValidity: Coding, Equatable { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case .indefinite: encoder.encodeInt32(0, forKey: "r") @@ -49,7 +49,7 @@ enum SecretChatKeyValidity: Coding, Equatable { } } -final class SecretChatKey: Coding, Equatable { +final class SecretChatKey: PostboxCoding, Equatable { let fingerprint: Int64 let key: MemoryBuffer let validity: SecretChatKeyValidity @@ -62,14 +62,14 @@ final class SecretChatKey: Coding, Equatable { self.useCount = useCount } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.fingerprint = decoder.decodeInt64ForKey("f", orElse: 0) self.key = decoder.decodeBytesForKey("k")! self.validity = decoder.decodeObjectForKey("v", decoder: { SecretChatKeyValidity(decoder: $0) }) as! SecretChatKeyValidity self.useCount = decoder.decodeInt32ForKey("u", orElse: 0) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.fingerprint, forKey: "f") encoder.encodeBytes(self.key, forKey: "k") encoder.encodeObject(self.validity, forKey: "v") @@ -94,18 +94,18 @@ final class SecretChatKey: Coding, Equatable { } } -final class SecretChatKeychain: Coding, Equatable { +final class SecretChatKeychain: PostboxCoding, Equatable { let keys: [SecretChatKey] init(keys: [SecretChatKey]) { self.keys = keys } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.keys = decoder.decodeObjectArrayWithDecoderForKey("k") } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.keys, forKey: "k") } diff --git a/TelegramCore/SecretChatOutgoingOperation.swift b/TelegramCore/SecretChatOutgoingOperation.swift index 4cfcf0d7c2..be306cd25d 100644 --- a/TelegramCore/SecretChatOutgoingOperation.swift +++ b/TelegramCore/SecretChatOutgoingOperation.swift @@ -11,12 +11,12 @@ private enum SecretChatOutgoingFileValue: Int32 { case uploadedLarge = 2 } -enum SecretChatOutgoingFileReference: Coding { +enum SecretChatOutgoingFileReference: PostboxCoding { case remote(id: Int64, accessHash: Int64) case uploadedRegular(id: Int64, partCount: Int32, md5Digest: String, keyFingerprint: Int32) case uploadedLarge(id: Int64, partCount: Int32, keyFingerprint: Int32) - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("v", orElse: 0) { case SecretChatOutgoingFileValue.remote.rawValue: self = .remote(id: decoder.decodeInt64ForKey("i", orElse: 0), accessHash: decoder.decodeInt64ForKey("a", orElse: 0)) @@ -30,7 +30,7 @@ enum SecretChatOutgoingFileReference: Coding { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case let .remote(id, accessHash): encoder.encodeInt32(SecretChatOutgoingFileValue.remote.rawValue, forKey: "v") @@ -51,7 +51,7 @@ enum SecretChatOutgoingFileReference: Coding { } } -struct SecretChatOutgoingFile: Coding { +struct SecretChatOutgoingFile: PostboxCoding { let reference: SecretChatOutgoingFileReference let size: Int32 let key: SecretFileEncryptionKey @@ -62,13 +62,13 @@ struct SecretChatOutgoingFile: Coding { self.key = key } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.reference = decoder.decodeObjectForKey("r", decoder: { SecretChatOutgoingFileReference(decoder: $0) }) as! SecretChatOutgoingFileReference self.size = decoder.decodeInt32ForKey("s", orElse: 0) self.key = SecretFileEncryptionKey(aesKey: decoder.decodeBytesForKey("k")!.makeData(), aesIv: decoder.decodeBytesForKey("i")!.makeData()) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.reference, forKey: "r") encoder.encodeInt32(self.size, forKey: "s") encoder.encodeBytes(MemoryBuffer(data: self.key.aesKey), forKey: "k") @@ -98,7 +98,7 @@ private enum SecretChatOutgoingOperationValue: Int32 { case terminate = 14 } -enum SecretChatOutgoingOperationContents: Coding { +enum SecretChatOutgoingOperationContents: PostboxCoding { case initialHandshakeAccept(gA: MemoryBuffer, accessHash: Int64, b: MemoryBuffer) case sendMessage(layer: SecretChatLayer, id: MessageId, file: SecretChatOutgoingFile?) case readMessagesContent(layer: SecretChatLayer, actionGloballyUniqueId: Int64, globallyUniqueIds: [Int64]) @@ -115,7 +115,7 @@ enum SecretChatOutgoingOperationContents: Coding { case setMessageAutoremoveTimeout(layer: SecretChatLayer, actionGloballyUniqueId: Int64, timeout: Int32, messageId: MessageId) case terminate(reportSpam: Bool) - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case SecretChatOutgoingOperationValue.initialHandshakeAccept.rawValue: self = .initialHandshakeAccept(gA: decoder.decodeBytesForKey("g")!, accessHash: decoder.decodeInt64ForKey("h", orElse: 0), b: decoder.decodeBytesForKey("b")!) @@ -153,7 +153,7 @@ enum SecretChatOutgoingOperationContents: Coding { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case let .initialHandshakeAccept(gA, accessHash, b): encoder.encodeInt32(SecretChatOutgoingOperationValue.initialHandshakeAccept.rawValue, forKey: "r") @@ -244,7 +244,7 @@ enum SecretChatOutgoingOperationContents: Coding { } } -final class SecretChatOutgoingOperation: Coding { +final class SecretChatOutgoingOperation: PostboxCoding { let contents: SecretChatOutgoingOperationContents let mutable: Bool let delivered: Bool @@ -255,13 +255,13 @@ final class SecretChatOutgoingOperation: Coding { self.delivered = delivered } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.contents = decoder.decodeObjectForKey("c", decoder: { SecretChatOutgoingOperationContents(decoder: $0) }) as! SecretChatOutgoingOperationContents self.mutable = decoder.decodeInt32ForKey("m", orElse: 0) != 0 self.delivered = decoder.decodeInt32ForKey("d", orElse: 0) != 0 } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.contents, forKey: "c") encoder.encodeInt32(self.mutable ? 1 : 0, forKey: "m") encoder.encodeInt32(self.delivered ? 1 : 0, forKey: "d") diff --git a/TelegramCore/SecretChatState.swift b/TelegramCore/SecretChatState.swift index a81478d4fe..4f765e70d6 100644 --- a/TelegramCore/SecretChatState.swift +++ b/TelegramCore/SecretChatState.swift @@ -15,7 +15,7 @@ enum SecretChatLayer: Int32 { case layer46 = 46 } -public struct SecretChatKeySha1Fingerprint: Coding, Equatable { +public struct SecretChatKeySha1Fingerprint: PostboxCoding, Equatable { public let k0: Int64 public let k1: Int64 public let k2: Int32 @@ -43,13 +43,13 @@ public struct SecretChatKeySha1Fingerprint: Coding, Equatable { self.k2 = k2 } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.k0 = decoder.decodeInt64ForKey("k0", orElse: 0) self.k1 = decoder.decodeInt64ForKey("k1", orElse: 0) self.k2 = decoder.decodeInt32ForKey("k2", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.k0, forKey: "k0") encoder.encodeInt64(self.k1, forKey: "k1") encoder.encodeInt32(self.k2, forKey: "k2") @@ -82,7 +82,7 @@ public struct SecretChatKeySha1Fingerprint: Coding, Equatable { } } -public struct SecretChatKeySha256Fingerprint: Coding, Equatable { +public struct SecretChatKeySha256Fingerprint: PostboxCoding, Equatable { public let k0: Int64 public let k1: Int64 public let k2: Int64 @@ -113,14 +113,14 @@ public struct SecretChatKeySha256Fingerprint: Coding, Equatable { self.k3 = k3 } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.k0 = decoder.decodeInt64ForKey("k0", orElse: 0) self.k1 = decoder.decodeInt64ForKey("k1", orElse: 0) self.k2 = decoder.decodeInt64ForKey("k2", orElse: 0) self.k3 = decoder.decodeInt64ForKey("k3", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.k0, forKey: "k0") encoder.encodeInt64(self.k1, forKey: "k1") encoder.encodeInt64(self.k2, forKey: "k2") @@ -156,7 +156,7 @@ public struct SecretChatKeySha256Fingerprint: Coding, Equatable { } } -public struct SecretChatKeyFingerprint: Coding, Equatable { +public struct SecretChatKeyFingerprint: PostboxCoding, Equatable { public let sha1: SecretChatKeySha1Fingerprint public let sha256: SecretChatKeySha256Fingerprint @@ -165,12 +165,12 @@ public struct SecretChatKeyFingerprint: Coding, Equatable { self.sha256 = sha256 } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.sha1 = decoder.decodeObjectForKey("1", decoder: { SecretChatKeySha1Fingerprint(decoder: $0) }) as! SecretChatKeySha1Fingerprint self.sha256 = decoder.decodeObjectForKey("2", decoder: { SecretChatKeySha256Fingerprint(decoder: $0) }) as! SecretChatKeySha256Fingerprint } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.sha1, forKey: "1") encoder.encodeObject(self.sha256, forKey: "2") } @@ -193,11 +193,11 @@ private enum SecretChatEmbeddedStateValue: Int32 { case sequenceBasedLayer = 3 } -enum SecretChatHandshakeState: Coding, Equatable { +enum SecretChatHandshakeState: PostboxCoding, Equatable { case accepting case requested(g: Int32, p: MemoryBuffer, a: MemoryBuffer) - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case 0: self = .accepting @@ -209,7 +209,7 @@ enum SecretChatHandshakeState: Coding, Equatable { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case .accepting: encoder.encodeInt32(0, forKey: "r") @@ -239,7 +239,7 @@ enum SecretChatHandshakeState: Coding, Equatable { } } -struct SecretChatLayerNegotiationState: Coding, Equatable { +struct SecretChatLayerNegotiationState: PostboxCoding, Equatable { let activeLayer: Int32 let locallyRequestedLayer: Int32? let remotelyRequestedLayer: Int32? @@ -250,13 +250,13 @@ struct SecretChatLayerNegotiationState: Coding, Equatable { self.remotelyRequestedLayer = remotelyRequestedLayer } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.activeLayer = decoder.decodeInt32ForKey("a", orElse: 0) self.locallyRequestedLayer = decoder.decodeOptionalInt32ForKey("lr") self.remotelyRequestedLayer = decoder.decodeOptionalInt32ForKey("rr") } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.activeLayer, forKey: "a") if let locallyRequestedLayer = self.locallyRequestedLayer { encoder.encodeInt32(locallyRequestedLayer, forKey: "lr") @@ -295,13 +295,13 @@ private enum SecretChatRekeySessionDataValue: Int32 { case accepted = 3 } -enum SecretChatRekeySessionData: Coding, Equatable { +enum SecretChatRekeySessionData: PostboxCoding, Equatable { case requesting case requested(a: MemoryBuffer, config: SecretChatEncryptionConfig) case accepting case accepted(key: MemoryBuffer, keyFingerprint: Int64) - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case SecretChatRekeySessionDataValue.requesting.rawValue: self = .requesting @@ -316,7 +316,7 @@ enum SecretChatRekeySessionData: Coding, Equatable { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case .requesting: encoder.encodeInt32(SecretChatRekeySessionDataValue.requesting.rawValue, forKey: "r") @@ -363,7 +363,7 @@ enum SecretChatRekeySessionData: Coding, Equatable { } } -struct SecretChatRekeySessionState: Coding, Equatable { +struct SecretChatRekeySessionState: PostboxCoding, Equatable { let id: Int64 let data: SecretChatRekeySessionData @@ -372,12 +372,12 @@ struct SecretChatRekeySessionState: Coding, Equatable { self.data = data } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.id = decoder.decodeInt64ForKey("i", orElse: 0) self.data = decoder.decodeObjectForKey("d", decoder: { SecretChatRekeySessionData(decoder: $0) }) as! SecretChatRekeySessionData } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.id, forKey: "i") encoder.encodeObject(self.data, forKey: "d") } @@ -393,7 +393,7 @@ struct SecretChatRekeySessionState: Coding, Equatable { } } -struct SecretChatSequenceBasedLayerState: Coding, Equatable { +struct SecretChatSequenceBasedLayerState: PostboxCoding, Equatable { let layerNegotiationState: SecretChatLayerNegotiationState let rekeyState: SecretChatRekeySessionState? let baseIncomingOperationIndex: Int32 @@ -408,7 +408,7 @@ struct SecretChatSequenceBasedLayerState: Coding, Equatable { self.topProcessedCanonicalIncomingOperationIndex = topProcessedCanonicalIncomingOperationIndex } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.layerNegotiationState = decoder.decodeObjectForKey("ln", decoder: { SecretChatLayerNegotiationState(decoder: $0) }) as! SecretChatLayerNegotiationState self.rekeyState = decoder.decodeObjectForKey("rs", decoder: { SecretChatRekeySessionState(decoder: $0) }) as? SecretChatRekeySessionState self.baseIncomingOperationIndex = decoder.decodeInt32ForKey("bi", orElse: 0) @@ -420,7 +420,7 @@ struct SecretChatSequenceBasedLayerState: Coding, Equatable { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.layerNegotiationState, forKey: "ln") if let rekeyState = self.rekeyState { encoder.encodeObject(rekeyState, forKey: "rs") @@ -477,7 +477,7 @@ struct SecretChatSequenceBasedLayerState: Coding, Equatable { } } -enum SecretChatEmbeddedState: Coding, Equatable { +enum SecretChatEmbeddedState: PostboxCoding, Equatable { case terminated case handshake(SecretChatHandshakeState) case basicLayer @@ -494,7 +494,7 @@ enum SecretChatEmbeddedState: Coding, Equatable { } } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case SecretChatEmbeddedStateValue.terminated.rawValue: self = .terminated @@ -510,7 +510,7 @@ enum SecretChatEmbeddedState: Coding, Equatable { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case .terminated: encoder.encodeInt32(SecretChatEmbeddedStateValue.terminated.rawValue, forKey: "r") @@ -574,7 +574,7 @@ final class SecretChatState: PeerChatState, SecretChatKeyState, Equatable { self.messageAutoremoveTimeout = messageAutoremoveTimeout } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.role = SecretChatRole(rawValue: decoder.decodeInt32ForKey("r", orElse: 0))! self.embeddedState = decoder.decodeObjectForKey("s", decoder: { return SecretChatEmbeddedState(decoder: $0) }) as! SecretChatEmbeddedState self.keychain = decoder.decodeObjectForKey("k", decoder: { return SecretChatKeychain(decoder: $0) }) as! SecretChatKeychain @@ -582,7 +582,7 @@ final class SecretChatState: PeerChatState, SecretChatKeyState, Equatable { self.messageAutoremoveTimeout = decoder.decodeOptionalInt32ForKey("a") } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.role.rawValue, forKey: "r") encoder.encodeObject(self.embeddedState, forKey: "s") encoder.encodeObject(self.keychain, forKey: "k") diff --git a/TelegramCore/StickerPack.swift b/TelegramCore/StickerPack.swift index 90571bf8a3..73e2c9daf8 100644 --- a/TelegramCore/StickerPack.swift +++ b/TelegramCore/StickerPack.swift @@ -54,7 +54,7 @@ public final class StickerPackCollectionInfo: ItemCollectionInfo, Equatable { self.count = count } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.id = ItemCollectionId(namespace: decoder.decodeInt32ForKey("i.n", orElse: 0), id: decoder.decodeInt64ForKey("i.i", orElse: 0)) self.accessHash = decoder.decodeInt64ForKey("a", orElse: 0) self.title = decoder.decodeStringForKey("t", orElse: "") @@ -64,7 +64,7 @@ public final class StickerPackCollectionInfo: ItemCollectionInfo, Equatable { self.count = decoder.decodeInt32ForKey("n", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.id.namespace, forKey: "i.n") encoder.encodeInt64(self.id.id, forKey: "i.i") encoder.encodeInt64(self.accessHash, forKey: "a") @@ -115,13 +115,13 @@ public final class StickerPackItem: ItemCollectionItem, Equatable { self.indexKeys = indexKeys } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.index = ItemCollectionItemIndex(index: decoder.decodeInt32ForKey("i.n", orElse: 0), id: decoder.decodeInt64ForKey("i.i", orElse: 0)) self.file = decoder.decodeObjectForKey("f") as! TelegramMediaFile self.indexKeys = decoder.decodeBytesArrayForKey("s") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.index.index, forKey: "i.n") encoder.encodeInt64(self.index.id, forKey: "i.i") encoder.encodeObject(self.file, forKey: "f") diff --git a/TelegramCore/SuggestedLocalizationEntry.swift b/TelegramCore/SuggestedLocalizationEntry.swift index 8e7d8e4d3c..979a0369d4 100644 --- a/TelegramCore/SuggestedLocalizationEntry.swift +++ b/TelegramCore/SuggestedLocalizationEntry.swift @@ -16,12 +16,12 @@ public final class SuggestedLocalizationEntry: PreferencesEntry { self.isSeen = isSeen } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.languageCode = decoder.decodeStringForKey("lc", orElse: "en") self.isSeen = decoder.decodeInt32ForKey("s", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.languageCode, forKey: "lc") encoder.encodeInt32(self.isSeen ? 1 : 0, forKey: "s") } diff --git a/TelegramCore/SynchronizeChatInputStateOperation.swift b/TelegramCore/SynchronizeChatInputStateOperation.swift index 70e08e1376..1b4185e262 100644 --- a/TelegramCore/SynchronizeChatInputStateOperation.swift +++ b/TelegramCore/SynchronizeChatInputStateOperation.swift @@ -5,18 +5,18 @@ import Foundation import Postbox #endif -final class SynchronizeChatInputStateOperation: Coding { +final class SynchronizeChatInputStateOperation: PostboxCoding { let previousState: SynchronizeableChatInputState? init(previousState: SynchronizeableChatInputState?) { self.previousState = previousState } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.previousState = decoder.decodeObjectForKey("p", decoder: { SynchronizeableChatInputState(decoder: $0) }) as? SynchronizeableChatInputState } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { if let previousState = self.previousState { encoder.encodeObject(previousState, forKey: "p") } else { diff --git a/TelegramCore/SynchronizeConsumeMessageContentsOperation.swift b/TelegramCore/SynchronizeConsumeMessageContentsOperation.swift index 29fe2a5a76..5598d245e2 100644 --- a/TelegramCore/SynchronizeConsumeMessageContentsOperation.swift +++ b/TelegramCore/SynchronizeConsumeMessageContentsOperation.swift @@ -5,18 +5,18 @@ import Foundation import Postbox #endif -final class SynchronizeConsumeMessageContentsOperation: Coding { +final class SynchronizeConsumeMessageContentsOperation: PostboxCoding { let messageIds: [MessageId] init(messageIds: [MessageId]) { self.messageIds = messageIds } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.messageIds = MessageId.decodeArrayFromBuffer(decoder.decodeBytesForKeyNoCopy("i")!) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() MessageId.encodeArrayToBuffer(self.messageIds, buffer: buffer) encoder.encodeBytes(buffer, forKey: "i") diff --git a/TelegramCore/SynchronizeInstalledStickerPacksOperations.swift b/TelegramCore/SynchronizeInstalledStickerPacksOperations.swift index a376560543..1b4ee2ffd6 100644 --- a/TelegramCore/SynchronizeInstalledStickerPacksOperations.swift +++ b/TelegramCore/SynchronizeInstalledStickerPacksOperations.swift @@ -10,36 +10,36 @@ enum SynchronizeInstalledStickerPacksOperationNamespace: Int32 { case masks = 1 } -final class SynchronizeInstalledStickerPacksOperation: Coding { +final class SynchronizeInstalledStickerPacksOperation: PostboxCoding { let previousPacks: [ItemCollectionId] init(previousPacks: [ItemCollectionId]) { self.previousPacks = previousPacks } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.previousPacks = ItemCollectionId.decodeArrayFromBuffer(decoder.decodeBytesForKey("p")!) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() ItemCollectionId.encodeArrayToBuffer(self.previousPacks, buffer: buffer) encoder.encodeBytes(buffer, forKey: "p") } } -final class SynchronizeMarkFeaturedStickerPacksAsSeenOperation: Coding { +final class SynchronizeMarkFeaturedStickerPacksAsSeenOperation: PostboxCoding { let ids: [ItemCollectionId] init(ids: [ItemCollectionId]) { self.ids = ids } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.ids = ItemCollectionId.decodeArrayFromBuffer(decoder.decodeBytesForKey("p")!) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() ItemCollectionId.encodeArrayToBuffer(self.ids, buffer: buffer) encoder.encodeBytes(buffer, forKey: "p") diff --git a/TelegramCore/SynchronizeLocalizationUpdatesOperation.swift b/TelegramCore/SynchronizeLocalizationUpdatesOperation.swift index 9573b9e888..f48ecd8934 100644 --- a/TelegramCore/SynchronizeLocalizationUpdatesOperation.swift +++ b/TelegramCore/SynchronizeLocalizationUpdatesOperation.swift @@ -7,14 +7,14 @@ import Foundation import SwiftSignalKit #endif -final class SynchronizeLocalizationUpdatesOperation: Coding { +final class SynchronizeLocalizationUpdatesOperation: PostboxCoding { init() { } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { } } diff --git a/TelegramCore/SynchronizePinnedChatsOperation.swift b/TelegramCore/SynchronizePinnedChatsOperation.swift index baca954cfc..058edc0e65 100644 --- a/TelegramCore/SynchronizePinnedChatsOperation.swift +++ b/TelegramCore/SynchronizePinnedChatsOperation.swift @@ -5,18 +5,18 @@ import Foundation import Postbox #endif -final class SynchronizePinnedChatsOperation: Coding { +final class SynchronizePinnedChatsOperation: PostboxCoding { let previousPeerIds: [PeerId] init(previousPeerIds: [PeerId]) { self.previousPeerIds = previousPeerIds } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.previousPeerIds = PeerId.decodeArrayFromBuffer(decoder.decodeBytesForKey("p")!) } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() PeerId.encodeArrayToBuffer(self.previousPeerIds, buffer: buffer) encoder.encodeBytes(buffer, forKey: "p") diff --git a/TelegramCore/SynchronizeSavedGifsOperation.swift b/TelegramCore/SynchronizeSavedGifsOperation.swift index e0702b43f0..aa5417f7b4 100644 --- a/TelegramCore/SynchronizeSavedGifsOperation.swift +++ b/TelegramCore/SynchronizeSavedGifsOperation.swift @@ -13,12 +13,12 @@ private enum SynchronizeSavedGifsOperationContentType: Int32 { case sync } -enum SynchronizeSavedGifsOperationContent: Coding { +enum SynchronizeSavedGifsOperationContent: PostboxCoding { case add(id: Int64, accessHash: Int64) case remove(id: Int64, accessHash: Int64) case sync - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case SynchronizeSavedGifsOperationContentType.add.rawValue: self = .add(id: decoder.decodeInt64ForKey("i", orElse: 0), accessHash: decoder.decodeInt64ForKey("h", orElse: 0)) @@ -32,7 +32,7 @@ enum SynchronizeSavedGifsOperationContent: Coding { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case let .add(id, accessHash): encoder.encodeInt32(SynchronizeSavedGifsOperationContentType.add.rawValue, forKey: "r") @@ -48,18 +48,18 @@ enum SynchronizeSavedGifsOperationContent: Coding { } } -final class SynchronizeSavedGifsOperation: Coding { +final class SynchronizeSavedGifsOperation: PostboxCoding { let content: SynchronizeSavedGifsOperationContent init(content: SynchronizeSavedGifsOperationContent) { self.content = content } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.content = decoder.decodeObjectForKey("c", decoder: { SynchronizeSavedGifsOperationContent(decoder: $0) }) as! SynchronizeSavedGifsOperationContent } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.content, forKey: "c") } } diff --git a/TelegramCore/SynchronizeSavedStickersOperation.swift b/TelegramCore/SynchronizeSavedStickersOperation.swift index dcbc1a6d11..1593f32dde 100644 --- a/TelegramCore/SynchronizeSavedStickersOperation.swift +++ b/TelegramCore/SynchronizeSavedStickersOperation.swift @@ -13,12 +13,12 @@ private enum SynchronizeSavedStickersOperationContentType: Int32 { case sync } -enum SynchronizeSavedStickersOperationContent: Coding { +enum SynchronizeSavedStickersOperationContent: PostboxCoding { case add(id: Int64, accessHash: Int64) case remove(id: Int64, accessHash: Int64) case sync - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case SynchronizeSavedStickersOperationContentType.add.rawValue: self = .add(id: decoder.decodeInt64ForKey("i", orElse: 0), accessHash: decoder.decodeInt64ForKey("h", orElse: 0)) @@ -32,7 +32,7 @@ enum SynchronizeSavedStickersOperationContent: Coding { } } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { switch self { case let .add(id, accessHash): encoder.encodeInt32(SynchronizeSavedStickersOperationContentType.add.rawValue, forKey: "r") @@ -48,18 +48,18 @@ enum SynchronizeSavedStickersOperationContent: Coding { } } -final class SynchronizeSavedStickersOperation: Coding { +final class SynchronizeSavedStickersOperation: PostboxCoding { let content: SynchronizeSavedStickersOperationContent init(content: SynchronizeSavedStickersOperationContent) { self.content = content } - init(decoder: Decoder) { + init(decoder: PostboxDecoder) { self.content = decoder.decodeObjectForKey("c", decoder: { SynchronizeSavedStickersOperationContent(decoder: $0) }) as! SynchronizeSavedStickersOperationContent } - func encode(_ encoder: Encoder) { + func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.content, forKey: "c") } } diff --git a/TelegramCore/SynchronizeableChatInputState.swift b/TelegramCore/SynchronizeableChatInputState.swift index d4158133c9..8086b146f4 100644 --- a/TelegramCore/SynchronizeableChatInputState.swift +++ b/TelegramCore/SynchronizeableChatInputState.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public struct SynchronizeableChatInputState: Coding, Equatable { +public struct SynchronizeableChatInputState: PostboxCoding, Equatable { public let replyToMessageId: MessageId? public let text: String public let timestamp: Int32 @@ -16,7 +16,7 @@ public struct SynchronizeableChatInputState: Coding, Equatable { self.timestamp = timestamp } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.text = decoder.decodeStringForKey("t", orElse: "") self.timestamp = decoder.decodeInt32ForKey("s", orElse: 0) if let messageIdPeerId = decoder.decodeOptionalInt64ForKey("m.p"), let messageIdNamespace = decoder.decodeOptionalInt32ForKey("m.n"), let messageIdId = decoder.decodeOptionalInt32ForKey("m.i") { @@ -26,7 +26,7 @@ public struct SynchronizeableChatInputState: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.text, forKey: "t") encoder.encodeInt32(self.timestamp, forKey: "s") if let replyToMessageId = self.replyToMessageId { diff --git a/TelegramCore/TelegramChannel.swift b/TelegramCore/TelegramChannel.swift index a0a2820612..89749bb5df 100644 --- a/TelegramCore/TelegramChannel.swift +++ b/TelegramCore/TelegramChannel.swift @@ -102,7 +102,7 @@ public enum TelegramChannelInfo: Equatable { } } - fileprivate func encode(encoder: Encoder) { + fileprivate func encode(encoder: PostboxEncoder) { switch self { case let .broadcast(info): encoder.encodeInt32(0, forKey: "i.t") @@ -113,7 +113,7 @@ public enum TelegramChannelInfo: Equatable { } } - fileprivate static func decode(decoder: Decoder) -> TelegramChannelInfo { + fileprivate static func decode(decoder: PostboxDecoder) -> TelegramChannelInfo { let type: Int32 = decoder.decodeInt32ForKey("i.t", orElse: 0) if type == 0 { return .broadcast(TelegramChannelBroadcastInfo(flags: TelegramChannelBroadcastFlags(rawValue: decoder.decodeInt32ForKey("i.f", orElse: 0)))) @@ -176,7 +176,7 @@ public final class TelegramChannel: Peer { self.bannedRights = bannedRights } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.id = PeerId(decoder.decodeInt64ForKey("i", orElse: 0)) self.accessHash = decoder.decodeOptionalInt64ForKey("ah") self.title = decoder.decodeStringForKey("t", orElse: "") @@ -192,7 +192,7 @@ public final class TelegramChannel: Peer { self.bannedRights = decoder.decodeObjectForKey("br", decoder: { TelegramChannelBannedRights(decoder: $0) }) as? TelegramChannelBannedRights } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.id.toInt64(), forKey: "i") if let accessHash = self.accessHash { encoder.encodeInt64(accessHash, forKey: "ah") diff --git a/TelegramCore/TelegramChannelAdminRights.swift b/TelegramCore/TelegramChannelAdminRights.swift index cd5d8045e4..344b71a72e 100644 --- a/TelegramCore/TelegramChannelAdminRights.swift +++ b/TelegramCore/TelegramChannelAdminRights.swift @@ -63,18 +63,18 @@ public struct TelegramChannelAdminRightsFlags: OptionSet { } } -public struct TelegramChannelAdminRights: Coding, Equatable { +public struct TelegramChannelAdminRights: PostboxCoding, Equatable { public let flags: TelegramChannelAdminRightsFlags public init(flags: TelegramChannelAdminRightsFlags) { self.flags = flags } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.flags = TelegramChannelAdminRightsFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.flags.rawValue, forKey: "f") } diff --git a/TelegramCore/TelegramChannelBannedRights.swift b/TelegramCore/TelegramChannelBannedRights.swift index c8579f027b..6e2cfe6bd8 100644 --- a/TelegramCore/TelegramChannelBannedRights.swift +++ b/TelegramCore/TelegramChannelBannedRights.swift @@ -26,7 +26,7 @@ public struct TelegramChannelBannedRightsFlags: OptionSet { public static let banEmbedLinks = TelegramChannelBannedRightsFlags(rawValue: 1 << 7) } -public struct TelegramChannelBannedRights: Coding, Equatable { +public struct TelegramChannelBannedRights: PostboxCoding, Equatable { public let flags: TelegramChannelBannedRightsFlags public let untilDate: Int32 @@ -35,12 +35,12 @@ public struct TelegramChannelBannedRights: Coding, Equatable { self.untilDate = untilDate } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.flags = TelegramChannelBannedRightsFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) self.untilDate = decoder.decodeInt32ForKey("d", orElse: 0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.flags.rawValue, forKey: "f") encoder.encodeInt32(self.untilDate, forKey: "d") } diff --git a/TelegramCore/TelegramGroup.swift b/TelegramCore/TelegramGroup.swift index 6db83f8d56..137caa47a7 100644 --- a/TelegramCore/TelegramGroup.swift +++ b/TelegramCore/TelegramGroup.swift @@ -73,7 +73,7 @@ public final class TelegramGroup: Peer { self.version = version } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.id = PeerId(decoder.decodeInt64ForKey("i", orElse: 0)) self.title = decoder.decodeStringForKey("t", orElse: "") self.photo = decoder.decodeObjectArrayForKey("ph") @@ -92,7 +92,7 @@ public final class TelegramGroup: Peer { self.version = Int(decoder.decodeInt32ForKey("v", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.id.toInt64(), forKey: "i") encoder.encodeString(self.title, forKey: "t") encoder.encodeObjectArray(self.photo, forKey: "ph") diff --git a/TelegramCore/TelegramMediaAction.swift b/TelegramCore/TelegramMediaAction.swift index 09a83ffcf2..a5a31174b6 100644 --- a/TelegramCore/TelegramMediaAction.swift +++ b/TelegramCore/TelegramMediaAction.swift @@ -12,7 +12,7 @@ public enum PhoneCallDiscardReason: Int32 { case busy = 3 } -public enum TelegramMediaActionType: Coding, Equatable { +public enum TelegramMediaActionType: PostboxCoding, Equatable { case unknown case groupCreated(title: String) case addedMembers(peerIds: [PeerId]) @@ -29,7 +29,7 @@ public enum TelegramMediaActionType: Coding, Equatable { case gameScore(gameId: Int64, score: Int32) case phoneCall(callId: Int64, discardReason: PhoneCallDiscardReason?, duration: Int32?) case paymentSent(currency: String, totalAmount: Int64) - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { let rawValue: Int32 = decoder.decodeInt32ForKey("_rawValue", orElse: 0) switch rawValue { case 1: @@ -72,7 +72,7 @@ public enum TelegramMediaActionType: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case .unknown: break @@ -272,11 +272,11 @@ public final class TelegramMediaAction: Media { self.action = action } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.action = TelegramMediaActionType(decoder: decoder) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { self.action.encode(encoder) } diff --git a/TelegramCore/TelegramMediaContact.swift b/TelegramCore/TelegramMediaContact.swift index 8e67220e0c..2d14d5138d 100644 --- a/TelegramCore/TelegramMediaContact.swift +++ b/TelegramCore/TelegramMediaContact.swift @@ -25,7 +25,7 @@ public final class TelegramMediaContact: Media { } } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.firstName = decoder.decodeStringForKey("n.f", orElse: "") self.lastName = decoder.decodeStringForKey("n.l", orElse: "") self.phoneNumber = decoder.decodeStringForKey("pn", orElse: "") @@ -38,7 +38,7 @@ public final class TelegramMediaContact: Media { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.firstName, forKey: "n.f") encoder.encodeString(self.lastName, forKey: "n.l") encoder.encodeString(self.phoneNumber, forKey: "pn") diff --git a/TelegramCore/TelegramMediaExpiredContent.swift b/TelegramCore/TelegramMediaExpiredContent.swift index 0854dad348..fda5e6ecc0 100644 --- a/TelegramCore/TelegramMediaExpiredContent.swift +++ b/TelegramCore/TelegramMediaExpiredContent.swift @@ -20,11 +20,11 @@ public final class TelegramMediaExpiredContent: Media { self.data = data } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.data = TelegramMediaExpiredContentData(rawValue: decoder.decodeInt32ForKey("d", orElse: 0))! } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.data.rawValue, forKey: "d") } diff --git a/TelegramCore/TelegramMediaFile.swift b/TelegramCore/TelegramMediaFile.swift index 007edd8c95..cb94c218e7 100644 --- a/TelegramCore/TelegramMediaFile.swift +++ b/TelegramCore/TelegramMediaFile.swift @@ -13,11 +13,11 @@ private let typeVideo: Int32 = 4 private let typeAudio: Int32 = 5 private let typeHasLinkedStickers: Int32 = 6 -public enum StickerPackReference: Coding, Equatable { +public enum StickerPackReference: PostboxCoding, Equatable { case id(id: Int64, accessHash: Int64) case name(String) - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("r", orElse: 0) { case 0: self = .id(id: decoder.decodeInt64ForKey("i", orElse: 0), accessHash: decoder.decodeInt64ForKey("h", orElse: 0)) @@ -29,7 +29,7 @@ public enum StickerPackReference: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case let .id(id, accessHash): encoder.encodeInt32(0, forKey: "r") @@ -73,7 +73,7 @@ public struct TelegramMediaVideoFlags: OptionSet { public static let instantRoundVideo = TelegramMediaVideoFlags(rawValue: 1 << 0) } -public struct StickerMaskCoords: Coding { +public struct StickerMaskCoords: PostboxCoding { public let n: Int32 public let x: Double public let y: Double @@ -86,14 +86,14 @@ public struct StickerMaskCoords: Coding { self.zoom = zoom } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.n = decoder.decodeInt32ForKey("n", orElse: 0) self.x = decoder.decodeDoubleForKey("x", orElse: 0.0) self.y = decoder.decodeDoubleForKey("y", orElse: 0.0) self.zoom = decoder.decodeDoubleForKey("z", orElse: 0.0) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.n, forKey: "n") encoder.encodeDouble(self.x, forKey: "x") encoder.encodeDouble(self.y, forKey: "y") @@ -101,7 +101,7 @@ public struct StickerMaskCoords: Coding { } } -public enum TelegramMediaFileAttribute: Coding { +public enum TelegramMediaFileAttribute: PostboxCoding { case FileName(fileName: String) case Sticker(displayText: String, packReference: StickerPackReference?, maskData: StickerMaskCoords?) case ImageSize(size: CGSize) @@ -110,7 +110,7 @@ public enum TelegramMediaFileAttribute: Coding { case Audio(isVoice: Bool, duration: Int, title: String?, performer: String?, waveform: MemoryBuffer?) case HasLinkedStickers - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { let type: Int32 = decoder.decodeInt32ForKey("t", orElse: 0) switch type { case typeFileName: @@ -137,7 +137,7 @@ public enum TelegramMediaFileAttribute: Coding { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case let .FileName(fileName): encoder.encodeInt32(typeFileName, forKey: "t") @@ -222,7 +222,7 @@ public final class TelegramMediaFile: Media, Equatable { self.attributes = attributes } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.fileId = MediaId(decoder.decodeBytesForKeyNoCopy("i")!) self.resource = decoder.decodeObjectForKey("r") as! TelegramMediaResource self.previewRepresentations = decoder.decodeObjectArrayForKey("pr") @@ -235,7 +235,7 @@ public final class TelegramMediaFile: Media, Equatable { self.attributes = decoder.decodeObjectArrayForKey("at") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() self.fileId.encodeToBuffer(buffer) encoder.encodeBytes(buffer, forKey: "i") diff --git a/TelegramCore/TelegramMediaGame.swift b/TelegramCore/TelegramMediaGame.swift index d8f17ce5da..03beb97a09 100644 --- a/TelegramCore/TelegramMediaGame.swift +++ b/TelegramCore/TelegramMediaGame.swift @@ -29,7 +29,7 @@ public final class TelegramMediaGame: Media { self.file = file } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.gameId = decoder.decodeInt64ForKey("i", orElse: 0) self.accessHash = decoder.decodeInt64ForKey("h", orElse: 0) self.name = decoder.decodeStringForKey("n", orElse: "") @@ -39,7 +39,7 @@ public final class TelegramMediaGame: Media { self.file = decoder.decodeObjectForKey("f") as? TelegramMediaFile } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.gameId, forKey: "i") encoder.encodeInt64(self.accessHash, forKey: "h") encoder.encodeString(self.name, forKey: "n") diff --git a/TelegramCore/TelegramMediaImage.swift b/TelegramCore/TelegramMediaImage.swift index b90572e281..02ce510f0e 100644 --- a/TelegramCore/TelegramMediaImage.swift +++ b/TelegramCore/TelegramMediaImage.swift @@ -23,12 +23,12 @@ public final class TelegramMediaImage: Media, Equatable { self.representations = representations } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.imageId = MediaId(decoder.decodeBytesForKeyNoCopy("i")!) self.representations = decoder.decodeObjectArrayForKey("r") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() self.imageId.encodeToBuffer(buffer) encoder.encodeBytes(buffer, forKey: "i") @@ -80,7 +80,7 @@ public func ==(lhs: TelegramMediaImage, rhs: TelegramMediaImage) -> Bool { return lhs.isEqual(rhs) } -public final class TelegramMediaImageRepresentation: Coding, Equatable, CustomStringConvertible { +public final class TelegramMediaImageRepresentation: PostboxCoding, Equatable, CustomStringConvertible { public let dimensions: CGSize public let resource: TelegramMediaResource @@ -89,12 +89,12 @@ public final class TelegramMediaImageRepresentation: Coding, Equatable, CustomSt self.resource = resource } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.dimensions = CGSize(width: CGFloat(decoder.decodeInt32ForKey("dx", orElse: 0)), height: CGFloat(decoder.decodeInt32ForKey("dy", orElse: 0))) self.resource = decoder.decodeObjectForKey("r") as! TelegramMediaResource } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(Int32(self.dimensions.width), forKey: "dx") encoder.encodeInt32(Int32(self.dimensions.height), forKey: "dy") encoder.encodeObject(self.resource, forKey: "r") diff --git a/TelegramCore/TelegramMediaInvoice.swift b/TelegramCore/TelegramMediaInvoice.swift index 1be1dc63a9..ec4c078265 100644 --- a/TelegramCore/TelegramMediaInvoice.swift +++ b/TelegramCore/TelegramMediaInvoice.swift @@ -48,7 +48,7 @@ public final class TelegramMediaInvoice: Media { self.flags = flags } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.title = decoder.decodeStringForKey("t", orElse: "") self.description = decoder.decodeStringForKey("d", orElse: "") self.currency = decoder.decodeStringForKey("c", orElse: "") @@ -64,7 +64,7 @@ public final class TelegramMediaInvoice: Media { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.title, forKey: "t") encoder.encodeString(self.description, forKey: "d") encoder.encodeString(self.currency, forKey: "nc") diff --git a/TelegramCore/TelegramMediaMap.swift b/TelegramCore/TelegramMediaMap.swift index 80e0c80b3b..d791f7b37f 100644 --- a/TelegramCore/TelegramMediaMap.swift +++ b/TelegramCore/TelegramMediaMap.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public final class NamedGeoPlace: Coding { +public final class NamedGeoPlace: PostboxCoding { public let country: String? public let state: String? public let city: String? @@ -20,7 +20,7 @@ public final class NamedGeoPlace: Coding { self.street = street } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.country = decoder.decodeOptionalStringForKey("gp_co") self.state = decoder.decodeOptionalStringForKey("gp_sta") self.city = decoder.decodeOptionalStringForKey("gp_ci") @@ -28,7 +28,7 @@ public final class NamedGeoPlace: Coding { self.street = decoder.decodeOptionalStringForKey("gp_str") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { if let country = self.country { encoder.encodeString(country, forKey: "gp_co") } @@ -51,7 +51,7 @@ public final class NamedGeoPlace: Coding { } } -public final class MapVenue: Coding { +public final class MapVenue: PostboxCoding { public let title: String public let address: String? public let provider: String? @@ -64,14 +64,14 @@ public final class MapVenue: Coding { self.id = id } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.title = decoder.decodeStringForKey("ti", orElse: "") self.address = decoder.decodeOptionalStringForKey("ad") self.provider = decoder.decodeOptionalStringForKey("pr") self.id = decoder.decodeOptionalStringForKey("id") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.title, forKey: "ti") if let address = self.address { @@ -102,14 +102,14 @@ public final class TelegramMediaMap: Media { self.venue = venue } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.latitude = decoder.decodeDoubleForKey("la", orElse: 0.0) self.longitude = decoder.decodeDoubleForKey("lo", orElse: 0.0) self.geoPlace = decoder.decodeObjectForKey("gp", decoder: { NamedGeoPlace(decoder: $0) }) as? NamedGeoPlace self.venue = decoder.decodeObjectForKey("ve", decoder: { MapVenue(decoder: $0) }) as? MapVenue } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeDouble(self.latitude, forKey: "la") encoder.encodeDouble(self.longitude, forKey: "lo") if let geoPlace = self.geoPlace { diff --git a/TelegramCore/TelegramMediaResource.swift b/TelegramCore/TelegramMediaResource.swift index b47645fd40..afd1ba173b 100644 --- a/TelegramCore/TelegramMediaResource.swift +++ b/TelegramCore/TelegramMediaResource.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public protocol TelegramMediaResource: MediaResource, Coding { +public protocol TelegramMediaResource: MediaResource, PostboxCoding { func isEqual(to: TelegramMediaResource) -> Bool } diff --git a/TelegramCore/TelegramMediaWebDocument.swift b/TelegramCore/TelegramMediaWebDocument.swift index a9292080d5..b4c313aff1 100644 --- a/TelegramCore/TelegramMediaWebDocument.swift +++ b/TelegramCore/TelegramMediaWebDocument.swift @@ -23,14 +23,14 @@ public class TelegramMediaWebFile: Media { self.attributes = attributes } - public required init(decoder: Decoder) { + public required init(decoder: PostboxDecoder) { self.resource = decoder.decodeObjectForKey("r") as! TelegramMediaResource self.mimeType = decoder.decodeStringForKey("mt", orElse: "") self.size = decoder.decodeInt32ForKey("s", orElse: 0) self.attributes = decoder.decodeObjectArrayForKey("at") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObject(self.resource, forKey: "r") encoder.encodeString(self.mimeType, forKey: "mt") encoder.encodeInt32(self.size, forKey: "s") diff --git a/TelegramCore/TelegramMediaWebpage.swift b/TelegramCore/TelegramMediaWebpage.swift index 2111abd566..b7d7781904 100644 --- a/TelegramCore/TelegramMediaWebpage.swift +++ b/TelegramCore/TelegramMediaWebpage.swift @@ -5,9 +5,10 @@ import Foundation import Postbox #endif -public final class TelegramMediaWebpageLoadedContent: Coding, Equatable { +public final class TelegramMediaWebpageLoadedContent: PostboxCoding, Equatable { public let url: String public let displayUrl: String + public let hash: Int32 public let type: String? public let websiteName: String? public let title: String? @@ -22,9 +23,10 @@ public final class TelegramMediaWebpageLoadedContent: Coding, Equatable { public let file: TelegramMediaFile? public let instantPage: InstantPage? - public init(url: String, displayUrl: String, type: String?, websiteName: String?, title: String?, text: String?, embedUrl: String?, embedType: String?, embedSize: CGSize?, duration: Int?, author: String?, image: TelegramMediaImage?, file: TelegramMediaFile?, instantPage: InstantPage?) { + public init(url: String, displayUrl: String, hash: Int32, type: String?, websiteName: String?, title: String?, text: String?, embedUrl: String?, embedType: String?, embedSize: CGSize?, duration: Int?, author: String?, image: TelegramMediaImage?, file: TelegramMediaFile?, instantPage: InstantPage?) { self.url = url self.displayUrl = displayUrl + self.hash = hash self.type = type self.websiteName = websiteName self.title = title @@ -39,9 +41,10 @@ public final class TelegramMediaWebpageLoadedContent: Coding, Equatable { self.instantPage = instantPage } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.url = decoder.decodeStringForKey("u", orElse: "") self.displayUrl = decoder.decodeStringForKey("d", orElse: "") + self.hash = decoder.decodeInt32ForKey("ha", orElse: 0) self.type = decoder.decodeOptionalStringForKey("ty") self.websiteName = decoder.decodeOptionalStringForKey("ws") self.title = decoder.decodeOptionalStringForKey("ti") @@ -79,9 +82,10 @@ public final class TelegramMediaWebpageLoadedContent: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeString(self.url, forKey: "u") encoder.encodeString(self.displayUrl, forKey: "d") + encoder.encodeInt32(self.hash, forKey: "ha") if let type = self.type { encoder.encodeString(type, forKey: "ty") } else { @@ -204,7 +208,7 @@ public final class TelegramMediaWebpage: Media, Equatable { self.content = content } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.webpageId = MediaId(decoder.decodeBytesForKeyNoCopy("i")!) if decoder.decodeInt32ForKey("ct", orElse: 0) == 0 { @@ -214,7 +218,7 @@ public final class TelegramMediaWebpage: Media, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { let buffer = WriteBuffer() self.webpageId.encodeToBuffer(buffer) encoder.encodeBytes(buffer, forKey: "i") @@ -287,7 +291,7 @@ func telegramMediaWebpageFromApiWebpage(_ webpage: Api.WebPage) -> TelegramMedia if let cachedPage = cachedPage { instantPage = InstantPage(apiPage: cachedPage) } - return TelegramMediaWebpage(webpageId: MediaId(namespace: Namespaces.Media.CloudWebpage, id: id), content: .Loaded(TelegramMediaWebpageLoadedContent(url: url, displayUrl: displayUrl, type: type, websiteName: siteName, title: title, text: description, embedUrl: embedUrl, embedType: embedType, embedSize: embedSize, duration: webpageDuration, author: author, image: image, file: file, instantPage: instantPage))) + return TelegramMediaWebpage(webpageId: MediaId(namespace: Namespaces.Media.CloudWebpage, id: id), content: .Loaded(TelegramMediaWebpageLoadedContent(url: url, displayUrl: displayUrl, hash: hash, type: type, websiteName: siteName, title: title, text: description, embedUrl: embedUrl, embedType: embedType, embedSize: embedSize, duration: webpageDuration, author: author, image: image, file: file, instantPage: instantPage))) case .webPageEmpty: return nil } diff --git a/TelegramCore/TelegramPeerNotificationSettings.swift b/TelegramCore/TelegramPeerNotificationSettings.swift index fbce403022..266f71d6b4 100644 --- a/TelegramCore/TelegramPeerNotificationSettings.swift +++ b/TelegramCore/TelegramPeerNotificationSettings.swift @@ -9,7 +9,7 @@ public enum PeerMuteState: Equatable { case unmuted case muted(until: Int32) - fileprivate static func decodeInline(_ decoder: Decoder) -> PeerMuteState { + fileprivate static func decodeInline(_ decoder: PostboxDecoder) -> PeerMuteState { switch decoder.decodeInt32ForKey("m.v", orElse: 0) { case 0: return .unmuted @@ -20,7 +20,7 @@ public enum PeerMuteState: Equatable { } } - fileprivate func encodeInline(_ encoder: Encoder) { + fileprivate func encodeInline(_ encoder: PostboxEncoder) { switch self { case .unmuted: encoder.encodeInt32(0, forKey: "m.v") @@ -61,7 +61,7 @@ public enum PeerMessageSound: Equatable { case bundledModern(id: Int32) case bundledClassic(id: Int32) - static func decodeInline(_ decoder: Decoder) -> PeerMessageSound { + static func decodeInline(_ decoder: PostboxDecoder) -> PeerMessageSound { switch decoder.decodeInt32ForKey("s.v", orElse: 0) { case PeerMessageSoundValue.none.rawValue: return .none @@ -75,7 +75,7 @@ public enum PeerMessageSound: Equatable { } } - func encodeInline(_ encoder: Encoder) { + func encodeInline(_ encoder: PostboxEncoder) { switch self { case .none: encoder.encodeInt32(PeerMessageSoundValue.none.rawValue, forKey: "s.v") @@ -134,12 +134,12 @@ public final class TelegramPeerNotificationSettings: PeerNotificationSettings, E self.messageSound = messageSound } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.muteState = PeerMuteState.decodeInline(decoder) self.messageSound = PeerMessageSound.decodeInline(decoder) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { self.muteState.encodeInline(encoder) self.messageSound.encodeInline(encoder) } diff --git a/TelegramCore/TelegramSecretChat.swift b/TelegramCore/TelegramSecretChat.swift index ac2a7c83ac..3a9cc8da6d 100644 --- a/TelegramCore/TelegramSecretChat.swift +++ b/TelegramCore/TelegramSecretChat.swift @@ -33,7 +33,7 @@ public final class TelegramSecretChat: Peer { self.messageAutoremoveTimeout = messageAutoremoveTimeout } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.id = PeerId(decoder.decodeInt64ForKey("i", orElse: 0)) self.regularPeerId = PeerId(decoder.decodeInt64ForKey("r", orElse: 0)) self.notificationSettingsPeerId = self.regularPeerId @@ -45,7 +45,7 @@ public final class TelegramSecretChat: Peer { self.messageAutoremoveTimeout = decoder.decodeOptionalInt32ForKey("at") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.id.toInt64(), forKey: "i") encoder.encodeInt64(self.regularPeerId.toInt64(), forKey: "r") encoder.encodeInt64(self.accessHash, forKey: "h") @@ -85,11 +85,11 @@ public final class CachedSecretChatData: CachedPeerData { self.reportStatus = reportStatus } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.reportStatus = PeerReportStatus(rawValue: decoder.decodeInt32ForKey("rs", orElse: 0))! } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.reportStatus.rawValue, forKey: "rs") } diff --git a/TelegramCore/TelegramUser.swift b/TelegramCore/TelegramUser.swift index 09bef0153b..7ecc562ed9 100644 --- a/TelegramCore/TelegramUser.swift +++ b/TelegramCore/TelegramUser.swift @@ -35,7 +35,7 @@ public struct BotUserInfoFlags: OptionSet { public static let requiresGeolocationForInlineRequests = BotUserInfoFlags(rawValue: (1 << 2)) } -public struct BotUserInfo: Coding, Equatable { +public struct BotUserInfo: PostboxCoding, Equatable { public let flags: BotUserInfoFlags public let inlinePlaceholder: String? @@ -44,12 +44,12 @@ public struct BotUserInfo: Coding, Equatable { self.inlinePlaceholder = inlinePlaceholder } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.flags = BotUserInfoFlags(rawValue: decoder.decodeInt32ForKey("f", orElse: 0)) self.inlinePlaceholder = decoder.decodeOptionalStringForKey("ip") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(self.flags.rawValue, forKey: "f") if let inlinePlaceholder = self.inlinePlaceholder { encoder.encodeString(inlinePlaceholder, forKey: "ip") @@ -107,7 +107,7 @@ public final class TelegramUser: Peer { self.flags = flags } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.id = PeerId(decoder.decodeInt64ForKey("i", orElse: 0)) let accessHash: Int64 = decoder.decodeInt64ForKey("ah", orElse: 0) @@ -134,7 +134,7 @@ public final class TelegramUser: Peer { self.flags = UserInfoFlags(rawValue: decoder.decodeInt32ForKey("fl", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt64(self.id.toInt64(), forKey: "i") if let accessHash = self.accessHash { diff --git a/TelegramCore/TelegramUserPresence.swift b/TelegramCore/TelegramUserPresence.swift index de5d943df2..96e55ec545 100644 --- a/TelegramCore/TelegramUserPresence.swift +++ b/TelegramCore/TelegramUserPresence.swift @@ -5,7 +5,7 @@ import Foundation import Postbox #endif -public enum UserPresenceStatus: Comparable, Coding { +public enum UserPresenceStatus: Comparable, PostboxCoding { case none case present(until: Int32) case recently @@ -89,7 +89,7 @@ public enum UserPresenceStatus: Comparable, Coding { } } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { switch decoder.decodeInt32ForKey("v", orElse: 0) { case 0: self = .none @@ -106,7 +106,7 @@ public enum UserPresenceStatus: Comparable, Coding { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { switch self { case .none: encoder.encodeInt32(0, forKey: "v") @@ -130,11 +130,11 @@ public final class TelegramUserPresence: PeerPresence, Equatable { self.status = status } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.status = UserPresenceStatus(decoder: decoder) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { self.status.encode(encoder) } diff --git a/TelegramCore/TeleramMediaUnsupported.swift b/TelegramCore/TeleramMediaUnsupported.swift index 9779379905..81f7900092 100644 --- a/TelegramCore/TeleramMediaUnsupported.swift +++ b/TelegramCore/TeleramMediaUnsupported.swift @@ -12,10 +12,10 @@ public final class TelegramMediaUnsupported: Media { init() { } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { } public func isEqual(_ other: Media) -> Bool { diff --git a/TelegramCore/TextEntitiesMessageAttribute.swift b/TelegramCore/TextEntitiesMessageAttribute.swift index 4525f3437c..24b202c324 100644 --- a/TelegramCore/TextEntitiesMessageAttribute.swift +++ b/TelegramCore/TextEntitiesMessageAttribute.swift @@ -97,7 +97,7 @@ public enum MessageTextEntityType: Equatable { } } -public struct MessageTextEntity: Coding, Equatable { +public struct MessageTextEntity: PostboxCoding, Equatable { public let range: Range public let type: MessageTextEntityType @@ -106,7 +106,7 @@ public struct MessageTextEntity: Coding, Equatable { self.type = type } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.range = Int(decoder.decodeInt32ForKey("start", orElse: 0)) ..< Int(decoder.decodeInt32ForKey("end", orElse: 0)) let type: Int32 = decoder.decodeInt32ForKey("_rawValue", orElse: 0) switch type { @@ -137,7 +137,7 @@ public struct MessageTextEntity: Coding, Equatable { } } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(Int32(self.range.lowerBound), forKey: "start") encoder.encodeInt32(Int32(self.range.upperBound), forKey: "end") switch self.type { @@ -195,11 +195,11 @@ public class TextEntitiesMessageAttribute: MessageAttribute, Equatable { self.entities = entities } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.entities = decoder.decodeObjectArrayWithDecoderForKey("entities") } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeObjectArray(self.entities, forKey: "entities") } diff --git a/TelegramCore/TwoStepVerification.swift b/TelegramCore/TwoStepVerification.swift index d9f7c7c1a9..0f0d6765ba 100644 --- a/TelegramCore/TwoStepVerification.swift +++ b/TelegramCore/TwoStepVerification.swift @@ -236,7 +236,7 @@ public func recoverTwoStepVerificationPassword(account: Account, code: String) - } } -public struct TemporaryTwoStepPasswordToken: Coding, Equatable { +public struct TemporaryTwoStepPasswordToken: PostboxCoding, Equatable { public let token: Data public let validUntilDate: Int32 public let requiresBiometrics: Bool @@ -247,13 +247,13 @@ public struct TemporaryTwoStepPasswordToken: Coding, Equatable { self.requiresBiometrics = requiresBiometrics } - public init(decoder: Decoder) { + public init(decoder: PostboxDecoder) { self.token = decoder.decodeBytesForKey("t")!.makeData() self.validUntilDate = decoder.decodeInt32ForKey("d", orElse: 0) self.requiresBiometrics = decoder.decodeInt32ForKey("b", orElse: 0) != 0 } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeBytes(MemoryBuffer(data: self.token), forKey: "t") encoder.encodeInt32(self.validUntilDate, forKey: "d") encoder.encodeInt32(self.requiresBiometrics ? 1 : 0, forKey: "b") diff --git a/TelegramCore/ViewCountMessageAttribute.swift b/TelegramCore/ViewCountMessageAttribute.swift index 7f4f56e4a1..fe75c429f2 100644 --- a/TelegramCore/ViewCountMessageAttribute.swift +++ b/TelegramCore/ViewCountMessageAttribute.swift @@ -14,11 +14,11 @@ public class ViewCountMessageAttribute: MessageAttribute { self.count = count } - required public init(decoder: Decoder) { + required public init(decoder: PostboxDecoder) { self.count = Int(decoder.decodeInt32ForKey("c", orElse: 0)) } - public func encode(_ encoder: Encoder) { + public func encode(_ encoder: PostboxEncoder) { encoder.encodeInt32(Int32(self.count), forKey: "c") } } diff --git a/TelegramCore/WebpagePreview.swift b/TelegramCore/WebpagePreview.swift index f0571f21d4..03d36ac732 100644 --- a/TelegramCore/WebpagePreview.swift +++ b/TelegramCore/WebpagePreview.swift @@ -31,3 +31,24 @@ public func webpagePreview(account: Account, url: String) -> Signal Signal { + if case let .Loaded(content) = webpage.content { + return network.request(Api.functions.messages.getWebPage(url: content.url, hash: content.hash)) + |> `catch` { _ -> Signal in + return .single(.webPageNotModified) + } + |> mapToSignal { result -> Signal in + if let updatedWebpage = telegramMediaWebpageFromApiWebpage(result), case .Loaded = updatedWebpage.content { + return postbox.modify { modifier -> TelegramMediaWebpage in + modifier.updateMedia(updatedWebpage.webpageId, update: updatedWebpage) + return updatedWebpage + } + } else { + return .complete() + } + } + } else { + return .complete() + } +}