From 7a185a8f5ac91af8689af5258d6085c486b94e6a Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Wed, 28 Aug 2019 19:48:33 +0300 Subject: [PATCH] API update [skip ci] --- submodules/TelegramApi/Sources/Api0.swift | 7 +- submodules/TelegramApi/Sources/Api1.swift | 92 +++++----------- submodules/TelegramApi/Sources/Api3.swift | 104 +++++++++--------- .../TelegramCore/EnqueueMessage.swift | 25 +++-- .../TelegramCore/MessageUtils.swift | 2 +- .../TelegramCore/PendingMessageManager.swift | 3 + .../TelegramCore/StoreMessage_Telegram.swift | 4 +- .../TelegramCore/TelegramCore/Theme.swift | 20 ++-- .../TelegramCore/TelegramCore/Themes.swift | 94 +++++++++++++--- .../TelegramCore/TelegramCore/Wallpaper.swift | 2 +- 10 files changed, 190 insertions(+), 163 deletions(-) diff --git a/submodules/TelegramApi/Sources/Api0.swift b/submodules/TelegramApi/Sources/Api0.swift index c6f4eed860..3132ce6cd9 100644 --- a/submodules/TelegramApi/Sources/Api0.swift +++ b/submodules/TelegramApi/Sources/Api0.swift @@ -240,8 +240,8 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-1263546448] = { return Api.Update.parse_updatePeerLocated($0) } dict[967122427] = { return Api.Update.parse_updateNewScheduledMessage($0) } dict[-1870238482] = { return Api.Update.parse_updateDeleteScheduledMessages($0) } - dict[-2112423005] = { return Api.Update.parse_updateTheme($0) } dict[357013699] = { return Api.Update.parse_updateMessageReactions($0) } + dict[-2112423005] = { return Api.Update.parse_updateTheme($0) } dict[1558266229] = { return Api.PopularContact.parse_popularContact($0) } dict[-373643672] = { return Api.FolderPeer.parse_folderPeer($0) } dict[367766557] = { return Api.ChannelParticipant.parse_channelParticipant($0) } @@ -428,7 +428,6 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-2128640689] = { return Api.account.SentEmailCode.parse_sentEmailCode($0) } dict[-1038136962] = { return Api.EncryptedFile.parse_encryptedFileEmpty($0) } dict[1248893260] = { return Api.EncryptedFile.parse_encryptedFile($0) } - dict[-557924733] = { return Api.CodeSettings.parse_codeSettings($0) } dict[-391902247] = { return Api.SecureValueError.parse_secureValueErrorData($0) } dict[12467706] = { return Api.SecureValueError.parse_secureValueErrorFrontSide($0) } dict[-2037765467] = { return Api.SecureValueError.parse_secureValueErrorReverseSide($0) } @@ -726,7 +725,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-104284986] = { return Api.WebDocument.parse_webDocumentNoProxy($0) } dict[475467473] = { return Api.WebDocument.parse_webDocument($0) } dict[1211967244] = { return Api.Theme.parse_themeDocumentNotModified($0) } - dict[1464749545] = { return Api.Theme.parse_theme($0) } + dict[975846885] = { return Api.Theme.parse_theme($0) } dict[-1290580579] = { return Api.contacts.Found.parse_found($0) } dict[-368018716] = { return Api.ChannelAdminLogEventsFilter.parse_channelAdminLogEventsFilter($0) } dict[1889961234] = { return Api.PeerNotifySettings.parse_peerNotifySettingsEmpty($0) } @@ -1088,8 +1087,6 @@ public struct Api { _1.serialize(buffer, boxed) case let _1 as Api.EncryptedFile: _1.serialize(buffer, boxed) - case let _1 as Api.CodeSettings: - _1.serialize(buffer, boxed) case let _1 as Api.SecureValueError: _1.serialize(buffer, boxed) case let _1 as Api.NotifyPeer: diff --git a/submodules/TelegramApi/Sources/Api1.swift b/submodules/TelegramApi/Sources/Api1.swift index 687ae5d6b6..e8851bbe79 100644 --- a/submodules/TelegramApi/Sources/Api1.swift +++ b/submodules/TelegramApi/Sources/Api1.swift @@ -4143,8 +4143,8 @@ public extension Api { case updatePeerLocated(peers: [Api.PeerLocated]) case updateNewScheduledMessage(message: Api.Message) case updateDeleteScheduledMessages(peer: Api.Peer, messages: [Int32]) - case updateTheme(theme: Api.Theme) case updateMessageReactions(peer: Api.Peer, msgId: Int32, reactions: Api.MessageReactions) + case updateTheme(theme: Api.Theme) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { @@ -4763,12 +4763,6 @@ public extension Api { serializeInt32(item, buffer: buffer, boxed: false) } break - case .updateTheme(let theme): - if boxed { - buffer.appendInt32(-2112423005) - } - theme.serialize(buffer, true) - break case .updateMessageReactions(let peer, let msgId, let reactions): if boxed { buffer.appendInt32(357013699) @@ -4777,6 +4771,12 @@ public extension Api { serializeInt32(msgId, buffer: buffer, boxed: false) reactions.serialize(buffer, true) break + case .updateTheme(let theme): + if boxed { + buffer.appendInt32(-2112423005) + } + theme.serialize(buffer, true) + break } } @@ -4928,10 +4928,10 @@ public extension Api { return ("updateNewScheduledMessage", [("message", message)]) case .updateDeleteScheduledMessages(let peer, let messages): return ("updateDeleteScheduledMessages", [("peer", peer), ("messages", messages)]) - case .updateTheme(let theme): - return ("updateTheme", [("theme", theme)]) case .updateMessageReactions(let peer, let msgId, let reactions): return ("updateMessageReactions", [("peer", peer), ("msgId", msgId), ("reactions", reactions)]) + case .updateTheme(let theme): + return ("updateTheme", [("theme", theme)]) } } @@ -6177,19 +6177,6 @@ public extension Api { return nil } } - public static func parse_updateTheme(_ reader: BufferReader) -> Update? { - var _1: Api.Theme? - if let signature = reader.readInt32() { - _1 = Api.parse(reader, signature: signature) as? Api.Theme - } - let _c1 = _1 != nil - if _c1 { - return Api.Update.updateTheme(theme: _1!) - } - else { - return nil - } - } public static func parse_updateMessageReactions(_ reader: BufferReader) -> Update? { var _1: Api.Peer? if let signature = reader.readInt32() { @@ -6211,6 +6198,19 @@ public extension Api { return nil } } + public static func parse_updateTheme(_ reader: BufferReader) -> Update? { + var _1: Api.Theme? + if let signature = reader.readInt32() { + _1 = Api.parse(reader, signature: signature) as? Api.Theme + } + let _c1 = _1 != nil + if _c1 { + return Api.Update.updateTheme(theme: _1!) + } + else { + return nil + } + } } public enum PopularContact: TypeConstructorDescription { @@ -10642,40 +10642,6 @@ public extension Api { } } - } - public enum CodeSettings: TypeConstructorDescription { - case codeSettings(flags: Int32) - - public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { - switch self { - case .codeSettings(let flags): - if boxed { - buffer.appendInt32(-557924733) - } - serializeInt32(flags, buffer: buffer, boxed: false) - break - } - } - - public func descriptionFields() -> (String, [(String, Any)]) { - switch self { - case .codeSettings(let flags): - return ("codeSettings", [("flags", flags)]) - } - } - - public static func parse_codeSettings(_ reader: BufferReader) -> CodeSettings? { - var _1: Int32? - _1 = reader.readInt32() - let _c1 = _1 != nil - if _c1 { - return Api.CodeSettings.codeSettings(flags: _1!) - } - else { - return nil - } - } - } public enum SecureValueError: TypeConstructorDescription { case secureValueErrorData(type: Api.SecureValueType, dataHash: Buffer, field: String, text: String) @@ -18455,7 +18421,7 @@ public extension Api { } public enum Theme: TypeConstructorDescription { case themeDocumentNotModified - case theme(flags: Int32, id: Int64, accessHash: Int64, slug: String, title: String, document: Api.Document) + case theme(flags: Int32, id: Int64, accessHash: Int64, slug: String, title: String, document: Api.Document?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { @@ -18467,14 +18433,14 @@ public extension Api { break case .theme(let flags, let id, let accessHash, let slug, let title, let document): if boxed { - buffer.appendInt32(1464749545) + buffer.appendInt32(975846885) } serializeInt32(flags, buffer: buffer, boxed: false) serializeInt64(id, buffer: buffer, boxed: false) serializeInt64(accessHash, buffer: buffer, boxed: false) serializeString(slug, buffer: buffer, boxed: false) serializeString(title, buffer: buffer, boxed: false) - document.serialize(buffer, true) + if Int(flags) & Int(1 << 2) != 0 {document!.serialize(buffer, true)} break } } @@ -18503,17 +18469,17 @@ public extension Api { var _5: String? _5 = parseString(reader) var _6: Api.Document? - if let signature = reader.readInt32() { + if Int(_1!) & Int(1 << 2) != 0 {if let signature = reader.readInt32() { _6 = Api.parse(reader, signature: signature) as? Api.Document - } + } } let _c1 = _1 != nil let _c2 = _2 != nil let _c3 = _3 != nil let _c4 = _4 != nil let _c5 = _5 != nil - let _c6 = _6 != nil + let _c6 = (Int(_1!) & Int(1 << 2) == 0) || _6 != nil if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 { - return Api.Theme.theme(flags: _1!, id: _2!, accessHash: _3!, slug: _4!, title: _5!, document: _6!) + return Api.Theme.theme(flags: _1!, id: _2!, accessHash: _3!, slug: _4!, title: _5!, document: _6) } else { return nil diff --git a/submodules/TelegramApi/Sources/Api3.swift b/submodules/TelegramApi/Sources/Api3.swift index 47f4660cbc..5beab5564d 100644 --- a/submodules/TelegramApi/Sources/Api3.swift +++ b/submodules/TelegramApi/Sources/Api3.swift @@ -2924,6 +2924,33 @@ public extension Api { }) } + public static func forwardMessages(flags: Int32, fromPeer: Api.InputPeer, id: [Int32], randomId: [Int64], toPeer: Api.InputPeer, scheduleDate: Int32?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + let buffer = Buffer() + buffer.appendInt32(-637606386) + serializeInt32(flags, buffer: buffer, boxed: false) + fromPeer.serialize(buffer, true) + buffer.appendInt32(481674261) + buffer.appendInt32(Int32(id.count)) + for item in id { + serializeInt32(item, buffer: buffer, boxed: false) + } + buffer.appendInt32(481674261) + buffer.appendInt32(Int32(randomId.count)) + for item in randomId { + serializeInt64(item, buffer: buffer, boxed: false) + } + toPeer.serialize(buffer, true) + if Int(flags) & Int(1 << 10) != 0 {serializeInt32(scheduleDate!, buffer: buffer, boxed: false)} + return (FunctionDescription(name: "messages.forwardMessages", parameters: [("flags", flags), ("fromPeer", fromPeer), ("id", id), ("randomId", randomId), ("toPeer", toPeer), ("scheduleDate", scheduleDate)]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Updates? in + let reader = BufferReader(buffer) + var result: Api.Updates? + if let signature = reader.readInt32() { + result = Api.parse(reader, signature: signature) as? Api.Updates + } + return result + }) + } + public static func sendInlineBotResult(flags: Int32, peer: Api.InputPeer, replyToMsgId: Int32?, randomId: Int64, queryId: Int64, id: String, scheduleDate: Int32?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() buffer.appendInt32(570955184) @@ -2944,6 +2971,31 @@ public extension Api { }) } + public static func editMessage(flags: Int32, peer: Api.InputPeer, id: Int32, message: String?, media: Api.InputMedia?, replyMarkup: Api.ReplyMarkup?, entities: [Api.MessageEntity]?, scheduleDate: Int32?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + let buffer = Buffer() + buffer.appendInt32(1224152952) + serializeInt32(flags, buffer: buffer, boxed: false) + peer.serialize(buffer, true) + serializeInt32(id, buffer: buffer, boxed: false) + if Int(flags) & Int(1 << 11) != 0 {serializeString(message!, buffer: buffer, boxed: false)} + if Int(flags) & Int(1 << 14) != 0 {media!.serialize(buffer, true)} + if Int(flags) & Int(1 << 2) != 0 {replyMarkup!.serialize(buffer, true)} + if Int(flags) & Int(1 << 3) != 0 {buffer.appendInt32(481674261) + buffer.appendInt32(Int32(entities!.count)) + for item in entities! { + item.serialize(buffer, true) + }} + if Int(flags) & Int(1 << 15) != 0 {serializeInt32(scheduleDate!, buffer: buffer, boxed: false)} + return (FunctionDescription(name: "messages.editMessage", parameters: [("flags", flags), ("peer", peer), ("id", id), ("message", message), ("media", media), ("replyMarkup", replyMarkup), ("entities", entities), ("scheduleDate", scheduleDate)]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Updates? in + let reader = BufferReader(buffer) + var result: Api.Updates? + if let signature = reader.readInt32() { + result = Api.parse(reader, signature: signature) as? Api.Updates + } + return result + }) + } + public static func sendMultiMedia(flags: Int32, peer: Api.InputPeer, replyToMsgId: Int32?, multiMedia: [Api.InputSingleMedia], scheduleDate: Int32?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() buffer.appendInt32(-872345397) @@ -2966,33 +3018,6 @@ public extension Api { }) } - public static func forwardMessages(flags: Int32, fromPeer: Api.InputPeer, id: [Int32], randomId: [Int64], toPeer: Api.InputPeer, scheduleDate: Int32?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { - let buffer = Buffer() - buffer.appendInt32(-637606386) - serializeInt32(flags, buffer: buffer, boxed: false) - fromPeer.serialize(buffer, true) - buffer.appendInt32(481674261) - buffer.appendInt32(Int32(id.count)) - for item in id { - serializeInt32(item, buffer: buffer, boxed: false) - } - buffer.appendInt32(481674261) - buffer.appendInt32(Int32(randomId.count)) - for item in randomId { - serializeInt64(item, buffer: buffer, boxed: false) - } - toPeer.serialize(buffer, true) - if Int(flags) & Int(1 << 10) != 0 {serializeInt32(scheduleDate!, buffer: buffer, boxed: false)} - return (FunctionDescription(name: "messages.forwardMessages", parameters: [("flags", flags), ("fromPeer", fromPeer), ("id", id), ("randomId", randomId), ("toPeer", toPeer), ("scheduleDate", scheduleDate)]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Updates? in - let reader = BufferReader(buffer) - var result: Api.Updates? - if let signature = reader.readInt32() { - result = Api.parse(reader, signature: signature) as? Api.Updates - } - return result - }) - } - public static func getScheduledHistory(peer: Api.InputPeer, hash: Int32) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() buffer.appendInt32(-490575781) @@ -3065,31 +3090,6 @@ public extension Api { }) } - public static func editMessage(flags: Int32, peer: Api.InputPeer, id: Int32, message: String?, media: Api.InputMedia?, replyMarkup: Api.ReplyMarkup?, entities: [Api.MessageEntity]?, scheduleDate: Int32?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { - let buffer = Buffer() - buffer.appendInt32(1224152952) - serializeInt32(flags, buffer: buffer, boxed: false) - peer.serialize(buffer, true) - serializeInt32(id, buffer: buffer, boxed: false) - if Int(flags) & Int(1 << 11) != 0 {serializeString(message!, buffer: buffer, boxed: false)} - if Int(flags) & Int(1 << 14) != 0 {media!.serialize(buffer, true)} - if Int(flags) & Int(1 << 2) != 0 {replyMarkup!.serialize(buffer, true)} - if Int(flags) & Int(1 << 3) != 0 {buffer.appendInt32(481674261) - buffer.appendInt32(Int32(entities!.count)) - for item in entities! { - item.serialize(buffer, true) - }} - if Int(flags) & Int(1 << 15) != 0 {serializeInt32(scheduleDate!, buffer: buffer, boxed: false)} - return (FunctionDescription(name: "messages.editMessage", parameters: [("flags", flags), ("peer", peer), ("id", id), ("message", message), ("media", media), ("replyMarkup", replyMarkup), ("entities", entities), ("scheduleDate", scheduleDate)]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.Updates? in - let reader = BufferReader(buffer) - var result: Api.Updates? - if let signature = reader.readInt32() { - result = Api.parse(reader, signature: signature) as? Api.Updates - } - return result - }) - } - public static func sendReaction(flags: Int32, peer: Api.InputPeer, msgId: Int32, reaction: String?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() buffer.appendInt32(627641572) diff --git a/submodules/TelegramCore/TelegramCore/EnqueueMessage.swift b/submodules/TelegramCore/TelegramCore/EnqueueMessage.swift index 5d6bb1e239..7224e62459 100644 --- a/submodules/TelegramCore/TelegramCore/EnqueueMessage.swift +++ b/submodules/TelegramCore/TelegramCore/EnqueueMessage.swift @@ -329,17 +329,6 @@ func enqueueMessages(transaction: Transaction, account: Account, peerId: PeerId, attributes.append(ConsumableContentMessageAttribute(consumed: false)) } } - if let peer = peer as? TelegramChannel { - switch peer.info { - case let .broadcast(info): - attributes.append(ViewCountMessageAttribute(count: 1)) - if info.flags.contains(.messagesShouldHaveSignatures) { - attributes.append(AuthorSignatureMessageAttribute(signature: accountPeer.debugDisplayTitle)) - } - case .group: - break - } - } var entitiesAttribute: TextEntitiesMessageAttribute? for attribute in attributes { @@ -398,6 +387,20 @@ func enqueueMessages(transaction: Transaction, account: Account, peerId: PeerId, } } + if let peer = peer as? TelegramChannel { + switch peer.info { + case let .broadcast(info): + if messageNamespace != Namespaces.Message.ScheduledLocal { + attributes.append(ViewCountMessageAttribute(count: 1)) + } + if info.flags.contains(.messagesShouldHaveSignatures) { + attributes.append(AuthorSignatureMessageAttribute(signature: accountPeer.debugDisplayTitle)) + } + case .group: + break + } + } + storeMessages.append(StoreMessage(peerId: peerId, namespace: messageNamespace, globallyUniqueId: randomId, groupingKey: localGroupingKey, timestamp: effectiveTimestamp, flags: flags, tags: tags, globalTags: globalTags, localTags: localTags, forwardInfo: nil, authorId: authorId, text: text, attributes: attributes, media: mediaList)) case let .forward(source, grouping, requestedAttributes): let sourceMessage = transaction.getMessage(source) diff --git a/submodules/TelegramCore/TelegramCore/MessageUtils.swift b/submodules/TelegramCore/TelegramCore/MessageUtils.swift index 153d35fcf6..1709b078fe 100644 --- a/submodules/TelegramCore/TelegramCore/MessageUtils.swift +++ b/submodules/TelegramCore/TelegramCore/MessageUtils.swift @@ -194,7 +194,7 @@ public extension Message { if self.flags.contains(.Failed) { return true } else if self.id.namespace == Namespaces.Message.ScheduledCloud { - return timestamp > self.timestamp + return timestamp > self.timestamp + 30 } else { return false } diff --git a/submodules/TelegramCore/TelegramCore/PendingMessageManager.swift b/submodules/TelegramCore/TelegramCore/PendingMessageManager.swift index f97e33be98..0059a66ffd 100644 --- a/submodules/TelegramCore/TelegramCore/PendingMessageManager.swift +++ b/submodules/TelegramCore/TelegramCore/PendingMessageManager.swift @@ -64,6 +64,7 @@ public enum PendingMessageFailureReason { case publicBan case mediaRestricted case slowmodeActive + case tooMuchScheduled } private func reasonForError(_ error: String) -> PendingMessageFailureReason? { @@ -75,6 +76,8 @@ private func reasonForError(_ error: String) -> PendingMessageFailureReason? { return .mediaRestricted } else if error.hasPrefix("SLOWMODE_WAIT") { return .slowmodeActive + } else if error.hasPrefix("SCHEDULE_TOO_MUCH") { + return .tooMuchScheduled } else { return nil } diff --git a/submodules/TelegramCore/TelegramCore/StoreMessage_Telegram.swift b/submodules/TelegramCore/TelegramCore/StoreMessage_Telegram.swift index 4f08eb87a9..3c33ae3317 100644 --- a/submodules/TelegramCore/TelegramCore/StoreMessage_Telegram.swift +++ b/submodules/TelegramCore/TelegramCore/StoreMessage_Telegram.swift @@ -501,7 +501,7 @@ extension StoreMessage { attributes.append(ReplyMessageAttribute(messageId: MessageId(peerId: peerId, namespace: Namespaces.Message.Cloud, id: replyToMsgId))) } - if let views = views { + if let views = views, namespace != Namespaces.Message.ScheduledCloud { attributes.append(ViewCountMessageAttribute(count: Int(views))) } @@ -537,8 +537,6 @@ extension StoreMessage { attributes.append(ContentRequiresValidationMessageAttribute()) } - - if let reactions = reactions { attributes.append(ReactionsMessageAttribute(apiReactions: reactions)) } diff --git a/submodules/TelegramCore/TelegramCore/Theme.swift b/submodules/TelegramCore/TelegramCore/Theme.swift index 96e936ede8..d785dafb65 100644 --- a/submodules/TelegramCore/TelegramCore/Theme.swift +++ b/submodules/TelegramCore/TelegramCore/Theme.swift @@ -14,11 +14,11 @@ public final class TelegramTheme: OrderedItemListEntryContents, Equatable { public let accessHash: Int64 public let slug: String public let title: String - public let file: TelegramMediaFile + public let file: TelegramMediaFile? public let isCreator: Bool public let isDefault: Bool - public init(id: Int64, accessHash: Int64, slug: String, title: String, file: TelegramMediaFile, isCreator: Bool, isDefault: Bool) { + public init(id: Int64, accessHash: Int64, slug: String, title: String, file: TelegramMediaFile?, isCreator: Bool, isDefault: Bool) { self.id = id self.accessHash = accessHash self.slug = slug @@ -33,7 +33,7 @@ public final class TelegramTheme: OrderedItemListEntryContents, Equatable { self.accessHash = decoder.decodeInt64ForKey("accessHash", orElse: 0) self.slug = decoder.decodeStringForKey("slug", orElse: "") self.title = decoder.decodeStringForKey("title", orElse: "") - self.file = decoder.decodeObjectForKey("file", decoder: { TelegramMediaFile(decoder: $0) }) as! TelegramMediaFile + self.file = decoder.decodeObjectForKey("file", decoder: { TelegramMediaFile(decoder: $0) }) as? TelegramMediaFile self.isCreator = decoder.decodeInt32ForKey("isCreator", orElse: 0) != 0 self.isDefault = decoder.decodeInt32ForKey("isDefault", orElse: 0) != 0 } @@ -43,7 +43,11 @@ public final class TelegramTheme: OrderedItemListEntryContents, Equatable { encoder.encodeInt64(self.accessHash, forKey: "accessHash") encoder.encodeString(self.slug, forKey: "slug") encoder.encodeString(self.title, forKey: "title") - encoder.encodeObject(self.file, forKey: "file") + if let file = self.file { + encoder.encodeObject(file, forKey: "file") + } else { + encoder.encodeNil(forKey: "file") + } encoder.encodeInt32(self.isCreator ? 1 : 0, forKey: "isCreator") encoder.encodeInt32(self.isDefault ? 1 : 0, forKey: "isDefault") } @@ -61,7 +65,7 @@ public final class TelegramTheme: OrderedItemListEntryContents, Equatable { if lhs.title != rhs.title { return false } - if lhs.file.id != rhs.file.id { + if lhs.file?.id != rhs.file?.id { return false } if lhs.isCreator != rhs.isCreator { @@ -78,11 +82,7 @@ extension TelegramTheme { convenience init?(apiTheme: Api.Theme) { switch apiTheme { case let .theme(flags, id, accessHash, slug, title, document): - if let file = telegramMediaFileFromApiDocument(document) { - self.init(id: id, accessHash: accessHash, slug: slug, title: title, file: file, isCreator: (flags & 1 << 0) != 0, isDefault: (flags & 1 << 1) != 0) - } else { - return nil - } + self.init(id: id, accessHash: accessHash, slug: slug, title: title, file: document.flatMap(telegramMediaFileFromApiDocument), isCreator: (flags & 1 << 0) != 0, isDefault: (flags & 1 << 1) != 0) default: return nil } diff --git a/submodules/TelegramCore/TelegramCore/Themes.swift b/submodules/TelegramCore/TelegramCore/Themes.swift index 227803c976..726bf75693 100644 --- a/submodules/TelegramCore/TelegramCore/Themes.swift +++ b/submodules/TelegramCore/TelegramCore/Themes.swift @@ -96,6 +96,26 @@ public func getTheme(account: Account, slug: String) -> Signal Signal { + guard let file = theme.file, let fileId = file.id?.id else { + return .fail(.generic) + } + return account.network.request(Api.functions.account.getTheme(format: themeFormat, theme: .inputTheme(id: theme.id, accessHash: theme.accessHash), documentId: fileId)) + |> mapError { _ -> GetThemeError in return .generic } + |> map { theme -> CheckThemeUpdatedResult in + if let theme = TelegramTheme(apiTheme: theme) { + return .updated(theme) + } else { + return .notModified + } + } +} + public func saveTheme(account: Account, theme: TelegramTheme) -> Signal { return saveUnsaveTheme(account: account, theme: theme, unsave: false) } @@ -124,7 +144,7 @@ public func installTheme(account: Account, theme: TelegramTheme) -> Signal Signal { +private func uploadTheme(account: Account, resource: MediaResource) -> Signal { let fileName = "theme.\(themeFileExtension)" let mimeType = "application/x-tgtheme-\(themeFormat)" return uploadedTheme(postbox: account.postbox, network: account.network, resource: resource) |> mapError { _ -> UploadThemeError in return .generic } - |> mapToSignal { result -> Signal<(UploadThemeStatus, MediaResource?), UploadThemeError> in + |> mapToSignal { result -> Signal<(UploadThemeResult, MediaResource?), UploadThemeError> in switch result.content { case .error: return .fail(.generic) @@ -170,7 +190,7 @@ private func uploadTheme(account: Account, resource: MediaResource) -> Signal mapError { _ in return UploadThemeError.generic } - |> mapToSignal { document -> Signal<(UploadThemeStatus, MediaResource?), UploadThemeError> in + |> mapToSignal { document -> Signal<(UploadThemeResult, MediaResource?), UploadThemeError> in if let file = telegramMediaFileFromApiDocument(document) { return .single((.complete(file), result.resource)) } else { @@ -182,7 +202,7 @@ private func uploadTheme(account: Account, resource: MediaResource) -> Signal map { result, _ -> UploadThemeStatus in + |> map { result, _ -> UploadThemeResult in return result } } @@ -191,18 +211,23 @@ public enum CreateThemeError { case generic } -public func createTheme(account: Account, resource: MediaResource, title: String, slug: String) -> Signal { +public enum CreateThemeResult { + case result(TelegramTheme) + case progress(Float) +} + +public func createTheme(account: Account, resource: MediaResource, title: String) -> Signal { return uploadTheme(account: account, resource: resource) |> mapError { _ in return CreateThemeError.generic } - |> mapToSignal { status -> Signal in - switch status { + |> mapToSignal { result -> Signal in + switch result { case let .complete(file): if let resource = file.resource as? CloudDocumentMediaResource { - return account.network.request(Api.functions.account.createTheme(slug: slug, title: title, document: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference)))) + return account.network.request(Api.functions.account.createTheme(slug: "", title: title, document: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference)))) |> mapError { _ in return CreateThemeError.generic } - |> mapToSignal { apiTheme -> Signal in + |> mapToSignal { apiTheme -> Signal in if let theme = TelegramTheme(apiTheme: apiTheme) { - return .single(theme) + return .single(.result(theme)) } else { return .fail(.generic) } @@ -211,17 +236,16 @@ public func createTheme(account: Account, resource: MediaResource, title: String else { return .fail(.generic) } - default: - return .complete() + case let .progress(progress): + return .single(.progress(progress)) } } } -public func updateTheme(account: Account, theme: TelegramTheme, title: String?, slug: String?, resource: MediaResource?) -> Signal { +public func updateTheme(account: Account, theme: TelegramTheme, title: String?, slug: String?, resource: MediaResource?) -> Signal { guard title != nil || slug != nil || resource != nil else { return .complete() } - var flags: Int32 = 0 if let _ = title { flags |= 1 << 1 @@ -230,6 +254,42 @@ public func updateTheme(account: Account, theme: TelegramTheme, title: String?, flags |= 1 << 0 } - return .never() - //return account.network.request(Api.functions.account.updateTheme(flags: flags, theme: .inputTheme(id: theme.id, accessHash: theme.accessHash), slug: slug, title: title, document: <#T##Api.InputDocument?#>)) + let uploadSignal: Signal + if let resource = resource { + uploadSignal = uploadTheme(account: account, resource: resource) + |> map(Optional.init) + } else { + uploadSignal = .single(nil) + } + return uploadSignal + |> mapError { _ -> CreateThemeError in + return .generic + } + |> mapToSignal { result -> Signal in + let inputDocument: Api.InputDocument? + if let status = result { + switch status { + case let .complete(file): + if let resource = file.resource as? CloudDocumentMediaResource { + inputDocument = .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference)) + } else { + return .fail(.generic) + } + case let .progress(progress): + return .single(.progress(progress)) + } + } else { + inputDocument = nil + } + + return account.network.request(Api.functions.account.updateTheme(flags: flags, theme: .inputTheme(id: theme.id, accessHash: theme.accessHash), slug: slug, title: title, document: inputDocument)) + |> mapError { _ in return CreateThemeError.generic } + |> mapToSignal { apiTheme -> Signal in + if let theme = TelegramTheme(apiTheme: apiTheme) { + return .single(.result(theme)) + } else { + return .fail(.generic) + } + } + } } diff --git a/submodules/TelegramCore/TelegramCore/Wallpaper.swift b/submodules/TelegramCore/TelegramCore/Wallpaper.swift index 7a56e2bcf8..0d5563cee6 100644 --- a/submodules/TelegramCore/TelegramCore/Wallpaper.swift +++ b/submodules/TelegramCore/TelegramCore/Wallpaper.swift @@ -64,7 +64,7 @@ public enum TelegramWallpaper: OrderedItemListEntryContents, Equatable { case 3: let settings = decoder.decodeObjectForKey("settings", decoder: { WallpaperSettings(decoder: $0) }) as? WallpaperSettings ?? WallpaperSettings() if let file = decoder.decodeObjectForKey("file", decoder: { TelegramMediaFile(decoder: $0) }) as? TelegramMediaFile { - self = .file(id: decoder.decodeInt64ForKey("id", orElse: 0), accessHash: decoder.decodeInt64ForKey("accessHash", orElse: 0), isCreator: decoder.decodeInt32ForKey("isCreator", orElse: 0) != 0, isDefault: decoder.decodeInt32ForKey("isDefault", orElse: 0) != 0, isPattern: decoder.decodeInt32ForKey("isPattern", orElse: 0) != 0, isDark: decoder.decodeInt32ForKey("isDark", orElse: 0) != 0, slug: decoder.decodeStringForKey("slug", orElse: ""), file: decoder.decodeObjectForKey("file", decoder: { TelegramMediaFile(decoder: $0) }) as! TelegramMediaFile, settings: settings) + self = .file(id: decoder.decodeInt64ForKey("id", orElse: 0), accessHash: decoder.decodeInt64ForKey("accessHash", orElse: 0), isCreator: decoder.decodeInt32ForKey("isCreator", orElse: 0) != 0, isDefault: decoder.decodeInt32ForKey("isDefault", orElse: 0) != 0, isPattern: decoder.decodeInt32ForKey("isPattern", orElse: 0) != 0, isDark: decoder.decodeInt32ForKey("isDark", orElse: 0) != 0, slug: decoder.decodeStringForKey("slug", orElse: ""), file: file, settings: settings) } else { self = .color(0xffffff) }