From c60d85373b15e5506c69a06a55682eb64b66b528 Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Fri, 10 Feb 2023 14:12:55 +0400 Subject: [PATCH] Update API --- .../Sources/ImportStickerPack.swift | 89 +++++++++++++------ .../Sources/ImportStickerPackController.swift | 2 +- .../ImportStickerPackControllerNode.swift | 10 +-- submodules/TelegramApi/Sources/Api0.swift | 2 +- submodules/TelegramApi/Sources/Api10.swift | 18 ++-- submodules/TelegramApi/Sources/Api30.swift | 45 +++++++++- .../Sources/State/Serialization.swift | 2 +- .../Stickers/ImportStickers.swift | 38 ++++++-- 8 files changed, 154 insertions(+), 52 deletions(-) diff --git a/submodules/ImportStickerPackUI/Sources/ImportStickerPack.swift b/submodules/ImportStickerPackUI/Sources/ImportStickerPack.swift index 2a37ff824d..922ad9a4d8 100644 --- a/submodules/ImportStickerPackUI/Sources/ImportStickerPack.swift +++ b/submodules/ImportStickerPackUI/Sources/ImportStickerPack.swift @@ -11,18 +11,39 @@ enum StickerVerificationStatus { public class ImportStickerPack { public enum StickerPackType { - case image - case animation - case video - - var importType: CreateStickerSetType { - switch self { + public enum ContentType { + case image + case animation + case video + + var importType: CreateStickerSetType.ContentType { + switch self { case .image: return .image case .animation: return .animation case .video: return .video + } + } + } + + case stickers(content: ContentType) + case emoji(content: ContentType, textColored: Bool) + + var contentType: StickerPackType.ContentType { + switch self { + case let .stickers(content), let .emoji(content, _): + return content + } + } + + var importType: CreateStickerSetType { + switch self { + case let .stickers(content): + return .stickers(content: content.importType) + case let .emoji(content, textColored): + return .emoji(content: content.importType, textColored: textColored) } } } @@ -51,12 +72,14 @@ public class ImportStickerPack { let content: Content let emojis: [String] + let keywords: String let uuid: UUID var resource: MediaResource? - init(content: Content, emojis: [String], uuid: UUID = UUID()) { + init(content: Content, emojis: [String], keywords: String, uuid: UUID = UUID()) { self.content = content self.emojis = emojis + self.keywords = keywords self.uuid = uuid } @@ -88,13 +111,25 @@ public class ImportStickerPack { self.software = json["software"] as? String ?? "" let isAnimated = json["isAnimated"] as? Bool ?? false let isVideo = json["isVideo"] as? Bool ?? false + let isEmoji = json["isEmoji"] as? Bool ?? false + let isTextColored = json["isTextColored"] as? Bool ?? false let type: StickerPackType - if isAnimated { - type = .animation - } else if isVideo { - type = .video + if isEmoji { + if isAnimated { + type = .emoji(content: .animation, textColored: isTextColored) + } else if isVideo { + type = .emoji(content: .video, textColored: isTextColored) + } else { + type = .emoji(content: .image, textColored: isTextColored) + } } else { - type = .image + if isAnimated { + type = .stickers(content: .animation) + } else if isVideo { + type = .stickers(content: .video) + } else { + type = .stickers(content: .image) + } } self.type = type @@ -102,23 +137,23 @@ public class ImportStickerPack { if let dataString = sticker["data"] as? String, let mimeType = sticker["mimeType"] as? String, let data = Data(base64Encoded: dataString) { var content: Sticker.Content? switch mimeType.lowercased() { - case "image/png": - if case .image = type { - content = .image(data) - } - case "application/x-tgsticker": - if case .animation = type { - content = .animation(data) - } - case "video/webm", "image/webp", "image/gif": - if case .video = type { - content = .video(data, mimeType) - } - default: - break + case "image/png": + if case .image = type.contentType { + content = .image(data) + } + case "application/x-tgsticker": + if case .animation = type.contentType { + content = .animation(data) + } + case "video/webm", "image/webp", "image/gif": + if case .video = type.contentType { + content = .video(data, mimeType) + } + default: + break } if let content = content { - return Sticker(content: content, emojis: sticker["emojis"] as? [String] ?? []) + return Sticker(content: content, emojis: sticker["emojis"] as? [String] ?? [], keywords: sticker["keywords"] as? String ?? "") } } return nil diff --git a/submodules/ImportStickerPackUI/Sources/ImportStickerPackController.swift b/submodules/ImportStickerPackUI/Sources/ImportStickerPackController.swift index 1f49b051f9..3fdf142b98 100644 --- a/submodules/ImportStickerPackUI/Sources/ImportStickerPackController.swift +++ b/submodules/ImportStickerPackUI/Sources/ImportStickerPackController.swift @@ -79,7 +79,7 @@ public final class ImportStickerPackController: ViewController, StandalonePresen Queue.mainQueue().after(0.1) { self.controllerNode.updateStickerPack(self.stickerPack, verifiedStickers: Set(), declinedStickers: Set(), uploadedStickerResources: [:]) - if case .image = self.stickerPack.type { + if case .image = self.stickerPack.type.contentType { } else { let _ = (self.context.account.postbox.loadedPeerWithId(self.context.account.peerId) |> deliverOnMainQueue).start(next: { [weak self] peer in diff --git a/submodules/ImportStickerPackUI/Sources/ImportStickerPackControllerNode.swift b/submodules/ImportStickerPackUI/Sources/ImportStickerPackControllerNode.swift index 95d4c99ab8..e16fa6fa03 100644 --- a/submodules/ImportStickerPackUI/Sources/ImportStickerPackControllerNode.swift +++ b/submodules/ImportStickerPackUI/Sources/ImportStickerPackControllerNode.swift @@ -605,9 +605,9 @@ final class ImportStickerPackControllerNode: ViewControllerTracingNode, UIScroll if let localResource = item.stickerItem.resource { self.context.account.postbox.mediaBox.copyResourceData(from: localResource.id, to: resource.id) } - stickers.append(ImportSticker(resource: resource, emojis: item.stickerItem.emojis, dimensions: dimensions, mimeType: item.stickerItem.mimeType)) + stickers.append(ImportSticker(resource: resource, emojis: item.stickerItem.emojis, dimensions: dimensions, mimeType: item.stickerItem.mimeType, keywords: item.stickerItem.keywords)) } else if let resource = item.stickerItem.resource { - stickers.append(ImportSticker(resource: resource, emojis: item.stickerItem.emojis, dimensions: dimensions, mimeType: item.stickerItem.mimeType)) + stickers.append(ImportSticker(resource: resource, emojis: item.stickerItem.emojis, dimensions: dimensions, mimeType: item.stickerItem.mimeType, keywords: item.stickerItem.keywords)) } } var thumbnailSticker: ImportSticker? @@ -618,7 +618,7 @@ final class ImportStickerPackControllerNode: ViewControllerTracingNode, UIScroll } let resource = LocalFileMediaResource(fileId: Int64.random(in: Int64.min ... Int64.max)) self.context.account.postbox.mediaBox.storeResourceData(resource.id, data: thumbnail.data) - thumbnailSticker = ImportSticker(resource: resource, emojis: [], dimensions: dimensions, mimeType: thumbnail.mimeType) + thumbnailSticker = ImportSticker(resource: resource, emojis: [], dimensions: dimensions, mimeType: thumbnail.mimeType, keywords: thumbnail.keywords) } let firstStickerItem = thumbnailSticker ?? stickers.first @@ -636,7 +636,7 @@ final class ImportStickerPackControllerNode: ViewControllerTracingNode, UIScroll if let (_, _, count) = strongSelf.progress { strongSelf.progress = (1.0, count, count) var animated = false - if case .image = stickerPack.type { + if case .image = stickerPack.type.contentType { animated = true } strongSelf.radialStatus.transitionToState(.progress(color: strongSelf.presentationData.theme.list.itemAccentColor, lineWidth: 6.0, value: 1.0, cancelEnabled: false, animateRotation: false), animated: animated, synchronous: true, completion: {}) @@ -804,7 +804,7 @@ final class ImportStickerPackControllerNode: ViewControllerTracingNode, UIScroll } self.pendingItems = updatedItems - if case .image = stickerPack.type { + if case .image = stickerPack.type.contentType { } else { self.stickerPackReady = stickerPack.stickers.count == (verifiedStickers.count + declinedStickers.count) && updatedItems.count > 0 } diff --git a/submodules/TelegramApi/Sources/Api0.swift b/submodules/TelegramApi/Sources/Api0.swift index c032d2dc00..4f43d13390 100644 --- a/submodules/TelegramApi/Sources/Api0.swift +++ b/submodules/TelegramApi/Sources/Api0.swift @@ -379,7 +379,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-1645763991] = { return Api.InputStickerSet.parse_inputStickerSetID($0) } dict[-930399486] = { return Api.InputStickerSet.parse_inputStickerSetPremiumGifts($0) } dict[-2044933984] = { return Api.InputStickerSet.parse_inputStickerSetShortName($0) } - dict[-6249322] = { return Api.InputStickerSetItem.parse_inputStickerSetItem($0) } + dict[853188252] = { return Api.InputStickerSetItem.parse_inputStickerSetItem($0) } dict[70813275] = { return Api.InputStickeredMedia.parse_inputStickeredMediaDocument($0) } dict[1251549527] = { return Api.InputStickeredMedia.parse_inputStickeredMediaPhoto($0) } dict[1634697192] = { return Api.InputStorePaymentPurpose.parse_inputStorePaymentGiftPremium($0) } diff --git a/submodules/TelegramApi/Sources/Api10.swift b/submodules/TelegramApi/Sources/Api10.swift index bcfe97264d..22bf87c5cb 100644 --- a/submodules/TelegramApi/Sources/Api10.swift +++ b/submodules/TelegramApi/Sources/Api10.swift @@ -392,26 +392,27 @@ public extension Api { } public extension Api { enum InputStickerSetItem: TypeConstructorDescription { - case inputStickerSetItem(flags: Int32, document: Api.InputDocument, emoji: String, maskCoords: Api.MaskCoords?) + case inputStickerSetItem(flags: Int32, document: Api.InputDocument, emoji: String, maskCoords: Api.MaskCoords?, keywords: String?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { - case .inputStickerSetItem(let flags, let document, let emoji, let maskCoords): + case .inputStickerSetItem(let flags, let document, let emoji, let maskCoords, let keywords): if boxed { - buffer.appendInt32(-6249322) + buffer.appendInt32(853188252) } serializeInt32(flags, buffer: buffer, boxed: false) document.serialize(buffer, true) serializeString(emoji, buffer: buffer, boxed: false) if Int(flags) & Int(1 << 0) != 0 {maskCoords!.serialize(buffer, true)} + if Int(flags) & Int(1 << 1) != 0 {serializeString(keywords!, buffer: buffer, boxed: false)} break } } public func descriptionFields() -> (String, [(String, Any)]) { switch self { - case .inputStickerSetItem(let flags, let document, let emoji, let maskCoords): - return ("inputStickerSetItem", [("flags", flags as Any), ("document", document as Any), ("emoji", emoji as Any), ("maskCoords", maskCoords as Any)]) + case .inputStickerSetItem(let flags, let document, let emoji, let maskCoords, let keywords): + return ("inputStickerSetItem", [("flags", flags as Any), ("document", document as Any), ("emoji", emoji as Any), ("maskCoords", maskCoords as Any), ("keywords", keywords as Any)]) } } @@ -428,12 +429,15 @@ public extension Api { if Int(_1!) & Int(1 << 0) != 0 {if let signature = reader.readInt32() { _4 = Api.parse(reader, signature: signature) as? Api.MaskCoords } } + var _5: String? + if Int(_1!) & Int(1 << 1) != 0 {_5 = parseString(reader) } let _c1 = _1 != nil let _c2 = _2 != nil let _c3 = _3 != nil let _c4 = (Int(_1!) & Int(1 << 0) == 0) || _4 != nil - if _c1 && _c2 && _c3 && _c4 { - return Api.InputStickerSetItem.inputStickerSetItem(flags: _1!, document: _2!, emoji: _3!, maskCoords: _4) + let _c5 = (Int(_1!) & Int(1 << 1) == 0) || _5 != nil + if _c1 && _c2 && _c3 && _c4 && _c5 { + return Api.InputStickerSetItem.inputStickerSetItem(flags: _1!, document: _2!, emoji: _3!, maskCoords: _4, keywords: _5) } else { return nil diff --git a/submodules/TelegramApi/Sources/Api30.swift b/submodules/TelegramApi/Sources/Api30.swift index a7b2b34a3e..49cfdf277e 100644 --- a/submodules/TelegramApi/Sources/Api30.swift +++ b/submodules/TelegramApi/Sources/Api30.swift @@ -7919,6 +7919,25 @@ public extension Api.functions.stickers { }) } } +public extension Api.functions.stickers { + static func changeSticker(flags: Int32, sticker: Api.InputDocument, emoji: String?, maskCoords: Api.MaskCoords?, keywords: String?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + let buffer = Buffer() + buffer.appendInt32(-179077444) + serializeInt32(flags, buffer: buffer, boxed: false) + sticker.serialize(buffer, true) + if Int(flags) & Int(1 << 0) != 0 {serializeString(emoji!, buffer: buffer, boxed: false)} + if Int(flags) & Int(1 << 1) != 0 {maskCoords!.serialize(buffer, true)} + if Int(flags) & Int(1 << 2) != 0 {serializeString(keywords!, buffer: buffer, boxed: false)} + return (FunctionDescription(name: "stickers.changeSticker", parameters: [("flags", String(describing: flags)), ("sticker", String(describing: sticker)), ("emoji", String(describing: emoji)), ("maskCoords", String(describing: maskCoords)), ("keywords", String(describing: keywords))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.messages.StickerSet? in + let reader = BufferReader(buffer) + var result: Api.messages.StickerSet? + if let signature = reader.readInt32() { + result = Api.parse(reader, signature: signature) as? Api.messages.StickerSet + } + return result + }) + } +} public extension Api.functions.stickers { static func changeStickerPosition(sticker: Api.InputDocument, position: Int32) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() @@ -7991,12 +8010,30 @@ public extension Api.functions.stickers { } } public extension Api.functions.stickers { - static func setStickerSetThumb(stickerset: Api.InputStickerSet, thumb: Api.InputDocument) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + static func renameStickerSet(stickerset: Api.InputStickerSet, title: String) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { let buffer = Buffer() - buffer.appendInt32(-1707717072) + buffer.appendInt32(306912256) stickerset.serialize(buffer, true) - thumb.serialize(buffer, true) - return (FunctionDescription(name: "stickers.setStickerSetThumb", parameters: [("stickerset", String(describing: stickerset)), ("thumb", String(describing: thumb))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.messages.StickerSet? in + serializeString(title, buffer: buffer, boxed: false) + return (FunctionDescription(name: "stickers.renameStickerSet", parameters: [("stickerset", String(describing: stickerset)), ("title", String(describing: title))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.messages.StickerSet? in + let reader = BufferReader(buffer) + var result: Api.messages.StickerSet? + if let signature = reader.readInt32() { + result = Api.parse(reader, signature: signature) as? Api.messages.StickerSet + } + return result + }) + } +} +public extension Api.functions.stickers { + static func setStickerSetThumb(flags: Int32, stickerset: Api.InputStickerSet, thumb: Api.InputDocument?, thumbDocumentId: Int64?) -> (FunctionDescription, Buffer, DeserializeFunctionResponse) { + let buffer = Buffer() + buffer.appendInt32(-1486204014) + serializeInt32(flags, buffer: buffer, boxed: false) + stickerset.serialize(buffer, true) + if Int(flags) & Int(1 << 0) != 0 {thumb!.serialize(buffer, true)} + if Int(flags) & Int(1 << 1) != 0 {serializeInt64(thumbDocumentId!, buffer: buffer, boxed: false)} + return (FunctionDescription(name: "stickers.setStickerSetThumb", parameters: [("flags", String(describing: flags)), ("stickerset", String(describing: stickerset)), ("thumb", String(describing: thumb)), ("thumbDocumentId", String(describing: thumbDocumentId))]), buffer, DeserializeFunctionResponse { (buffer: Buffer) -> Api.messages.StickerSet? in let reader = BufferReader(buffer) var result: Api.messages.StickerSet? if let signature = reader.readInt32() { diff --git a/submodules/TelegramCore/Sources/State/Serialization.swift b/submodules/TelegramCore/Sources/State/Serialization.swift index 65f96a9d8b..5d7148df3a 100644 --- a/submodules/TelegramCore/Sources/State/Serialization.swift +++ b/submodules/TelegramCore/Sources/State/Serialization.swift @@ -210,7 +210,7 @@ public class BoxedMessage: NSObject { public class Serialization: NSObject, MTSerialization { public func currentLayer() -> UInt { - return 152 + return 153 } public func parseMessage(_ data: Data!) -> Any! { diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift index 1c43133c1f..9e139f7c9a 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift @@ -81,12 +81,14 @@ public struct ImportSticker { let emojis: [String] public let dimensions: PixelDimensions public let mimeType: String + public let keywords: String - public init(resource: MediaResource, emojis: [String], dimensions: PixelDimensions, mimeType: String) { + public init(resource: MediaResource, emojis: [String], dimensions: PixelDimensions, mimeType: String, keywords: String) { self.resource = resource self.emojis = emojis self.dimensions = dimensions self.mimeType = mimeType + self.keywords = keywords } } @@ -96,9 +98,21 @@ public enum CreateStickerSetStatus { } public enum CreateStickerSetType { - case image - case animation - case video + public enum ContentType { + case image + case animation + case video + } + + case stickers(content: ContentType) + case emoji(content: ContentType, textColored: Bool) + + var contentType: ContentType { + switch self { + case let .stickers(content), let .emoji(content, _): + return content + } + } } func _internal_createStickerSet(account: Account, title: String, shortName: String, stickers: [ImportSticker], thumbnail: ImportSticker?, type: CreateStickerSetType, software: String?) -> Signal { @@ -133,7 +147,7 @@ func _internal_createStickerSet(account: Account, title: String, shortName: Stri } if resources.count == stickers.count { var flags: Int32 = 0 - switch type { + switch type.contentType { case .animation: flags |= (1 << 1) case .video: @@ -141,12 +155,24 @@ func _internal_createStickerSet(account: Account, title: String, shortName: Stri default: break } + if case let .emoji(_, textColored) = type { + flags |= (1 << 5) + if textColored { + flags |= (1 << 6) + } + } var inputStickers: [Api.InputStickerSetItem] = [] let stickerDocuments = thumbnail != nil ? resources.dropLast() : resources for i in 0 ..< stickerDocuments.count { let sticker = stickers[i] let resource = resources[i] - inputStickers.append(.inputStickerSetItem(flags: 0, document: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference ?? Data())), emoji: sticker.emojis.first ?? "", maskCoords: nil)) + + var flags: Int32 = 0 + if sticker.keywords.count > 0 { + flags |= (1 << 1) + } + + inputStickers.append(.inputStickerSetItem(flags: flags, document: .inputDocument(id: resource.fileId, accessHash: resource.accessHash, fileReference: Buffer(data: resource.fileReference ?? Data())), emoji: sticker.emojis.first ?? "", maskCoords: nil, keywords: sticker.keywords)) } var thumbnailDocument: Api.InputDocument? if thumbnail != nil, let resource = resources.last {