From a0864b4df03996499b54b877f25745dce4732386 Mon Sep 17 00:00:00 2001 From: Isaac <> Date: Tue, 16 Jul 2024 20:53:56 +0800 Subject: [PATCH 1/2] Update API --- submodules/TelegramApi/Sources/Api0.swift | 2 +- submodules/TelegramApi/Sources/Api5.swift | 18 +++++++++++------- .../Sources/ApiUtils/TelegramMediaFile.swift | 2 +- .../PendingMessageUploadedContent.swift | 2 +- .../TelegramEngine/Messages/Stories.swift | 3 +++ .../Stickers/ImportStickers.swift | 2 +- 6 files changed, 18 insertions(+), 11 deletions(-) diff --git a/submodules/TelegramApi/Sources/Api0.swift b/submodules/TelegramApi/Sources/Api0.swift index a534054d76..3cea4f2ec1 100644 --- a/submodules/TelegramApi/Sources/Api0.swift +++ b/submodules/TelegramApi/Sources/Api0.swift @@ -242,7 +242,7 @@ fileprivate let parsers: [Int32 : (BufferReader) -> Any?] = { dict[-1744710921] = { return Api.DocumentAttribute.parse_documentAttributeHasStickers($0) } dict[1815593308] = { return Api.DocumentAttribute.parse_documentAttributeImageSize($0) } dict[1662637586] = { return Api.DocumentAttribute.parse_documentAttributeSticker($0) } - dict[-745541182] = { return Api.DocumentAttribute.parse_documentAttributeVideo($0) } + dict[389652397] = { return Api.DocumentAttribute.parse_documentAttributeVideo($0) } dict[761606687] = { return Api.DraftMessage.parse_draftMessage($0) } dict[453805082] = { return Api.DraftMessage.parse_draftMessageEmpty($0) } dict[-1764723459] = { return Api.EmailVerification.parse_emailVerificationApple($0) } diff --git a/submodules/TelegramApi/Sources/Api5.swift b/submodules/TelegramApi/Sources/Api5.swift index b8ae224638..bd121a4a0b 100644 --- a/submodules/TelegramApi/Sources/Api5.swift +++ b/submodules/TelegramApi/Sources/Api5.swift @@ -1473,7 +1473,7 @@ public extension Api { case documentAttributeHasStickers case documentAttributeImageSize(w: Int32, h: Int32) case documentAttributeSticker(flags: Int32, alt: String, stickerset: Api.InputStickerSet, maskCoords: Api.MaskCoords?) - case documentAttributeVideo(flags: Int32, duration: Double, w: Int32, h: Int32, preloadPrefixSize: Int32?) + case documentAttributeVideo(flags: Int32, duration: Double, w: Int32, h: Int32, preloadPrefixSize: Int32?, videoStartTs: Double?) public func serialize(_ buffer: Buffer, _ boxed: Swift.Bool) { switch self { @@ -1529,15 +1529,16 @@ public extension Api { stickerset.serialize(buffer, true) if Int(flags) & Int(1 << 0) != 0 {maskCoords!.serialize(buffer, true)} break - case .documentAttributeVideo(let flags, let duration, let w, let h, let preloadPrefixSize): + case .documentAttributeVideo(let flags, let duration, let w, let h, let preloadPrefixSize, let videoStartTs): if boxed { - buffer.appendInt32(-745541182) + buffer.appendInt32(389652397) } serializeInt32(flags, buffer: buffer, boxed: false) serializeDouble(duration, buffer: buffer, boxed: false) serializeInt32(w, buffer: buffer, boxed: false) serializeInt32(h, buffer: buffer, boxed: false) if Int(flags) & Int(1 << 2) != 0 {serializeInt32(preloadPrefixSize!, buffer: buffer, boxed: false)} + if Int(flags) & Int(1 << 4) != 0 {serializeDouble(videoStartTs!, buffer: buffer, boxed: false)} break } } @@ -1558,8 +1559,8 @@ public extension Api { return ("documentAttributeImageSize", [("w", w as Any), ("h", h as Any)]) case .documentAttributeSticker(let flags, let alt, let stickerset, let maskCoords): return ("documentAttributeSticker", [("flags", flags as Any), ("alt", alt as Any), ("stickerset", stickerset as Any), ("maskCoords", maskCoords as Any)]) - case .documentAttributeVideo(let flags, let duration, let w, let h, let preloadPrefixSize): - return ("documentAttributeVideo", [("flags", flags as Any), ("duration", duration as Any), ("w", w as Any), ("h", h as Any), ("preloadPrefixSize", preloadPrefixSize as Any)]) + case .documentAttributeVideo(let flags, let duration, let w, let h, let preloadPrefixSize, let videoStartTs): + return ("documentAttributeVideo", [("flags", flags as Any), ("duration", duration as Any), ("w", w as Any), ("h", h as Any), ("preloadPrefixSize", preloadPrefixSize as Any), ("videoStartTs", videoStartTs as Any)]) } } @@ -1671,13 +1672,16 @@ public extension Api { _4 = reader.readInt32() var _5: Int32? if Int(_1!) & Int(1 << 2) != 0 {_5 = reader.readInt32() } + var _6: Double? + if Int(_1!) & Int(1 << 4) != 0 {_6 = reader.readDouble() } let _c1 = _1 != nil let _c2 = _2 != nil let _c3 = _3 != nil let _c4 = _4 != nil let _c5 = (Int(_1!) & Int(1 << 2) == 0) || _5 != nil - if _c1 && _c2 && _c3 && _c4 && _c5 { - return Api.DocumentAttribute.documentAttributeVideo(flags: _1!, duration: _2!, w: _3!, h: _4!, preloadPrefixSize: _5) + let _c6 = (Int(_1!) & Int(1 << 4) == 0) || _6 != nil + if _c1 && _c2 && _c3 && _c4 && _c5 && _c6 { + return Api.DocumentAttribute.documentAttributeVideo(flags: _1!, duration: _2!, w: _3!, h: _4!, preloadPrefixSize: _5, videoStartTs: _6) } else { return nil diff --git a/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaFile.swift b/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaFile.swift index 9daed9bc01..2d3c10d386 100644 --- a/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaFile.swift +++ b/submodules/TelegramCore/Sources/ApiUtils/TelegramMediaFile.swift @@ -99,7 +99,7 @@ func telegramMediaFileAttributesFromApiAttributes(_ attributes: [Api.DocumentAtt result.append(.ImageSize(size: PixelDimensions(width: w, height: h))) case .documentAttributeAnimated: result.append(.Animated) - case let .documentAttributeVideo(flags, duration, w, h, preloadSize): + case let .documentAttributeVideo(flags, duration, w, h, preloadSize, _): var videoFlags = TelegramMediaVideoFlags() if (flags & (1 << 0)) != 0 { videoFlags.insert(.instantRoundVideo) diff --git a/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift b/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift index f76bc6d185..0ec84b8642 100644 --- a/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift +++ b/submodules/TelegramCore/Sources/PendingMessages/PendingMessageUploadedContent.swift @@ -716,7 +716,7 @@ func inputDocumentAttributesFromFileAttributes(_ fileAttributes: [TelegramMediaF flags |= (1 << 3) } - attributes.append(.documentAttributeVideo(flags: flags, duration: duration, w: Int32(size.width), h: Int32(size.height), preloadPrefixSize: preloadSize)) + attributes.append(.documentAttributeVideo(flags: flags, duration: duration, w: Int32(size.width), h: Int32(size.height), preloadPrefixSize: preloadSize, videoStartTs: nil)) case let .Audio(isVoice, duration, title, performer, waveform): var flags: Int32 = 0 if isVoice { diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift index 21d995f956..11663bbd71 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Messages/Stories.swift @@ -1327,6 +1327,9 @@ func _internal_uploadBotPreviewImpl( return current } var media = currentBotPreview.media + if let index = media.firstIndex(where: { $0.id == resultMediaValue.id }) { + media.remove(at: index) + } media.append(resultMediaValue) let botPreview = CachedUserData.BotPreview(media: media) current = current.withUpdatedBotPreview(botPreview) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift index fb0878a5ae..ac6ed07c33 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/ImportStickers.swift @@ -80,7 +80,7 @@ func _internal_uploadSticker(account: Account, peer: Peer, resource: MediaResour var attributes: [Api.DocumentAttribute] = [] attributes.append(.documentAttributeSticker(flags: 0, alt: alt, stickerset: .inputStickerSetEmpty, maskCoords: nil)) if let duration { - attributes.append(.documentAttributeVideo(flags: 0, duration: duration, w: dimensions.width, h: dimensions.height, preloadPrefixSize: nil)) + attributes.append(.documentAttributeVideo(flags: 0, duration: duration, w: dimensions.width, h: dimensions.height, preloadPrefixSize: nil, videoStartTs: nil)) } attributes.append(.documentAttributeImageSize(w: dimensions.width, h: dimensions.height)) return account.network.request(Api.functions.messages.uploadMedia(flags: 0, businessConnectionId: nil, peer: inputPeer, media: Api.InputMedia.inputMediaUploadedDocument(flags: flags, file: file, thumb: thumbnailFile, mimeType: mimeType, attributes: attributes, stickers: nil, ttlSeconds: nil))) From 798b0b9c067584afbad88499b6b076879597a973 Mon Sep 17 00:00:00 2001 From: Isaac <> Date: Tue, 16 Jul 2024 20:54:13 +0800 Subject: [PATCH 2/2] Proxy server from the server --- .../BrowserUI/Sources/BrowserWebContent.swift | 88 +++---------------- 1 file changed, 14 insertions(+), 74 deletions(-) diff --git a/submodules/BrowserUI/Sources/BrowserWebContent.swift b/submodules/BrowserUI/Sources/BrowserWebContent.swift index 0cc8f49bf3..e49c7fb54a 100644 --- a/submodules/BrowserUI/Sources/BrowserWebContent.swift +++ b/submodules/BrowserUI/Sources/BrowserWebContent.swift @@ -18,83 +18,13 @@ import UndoUI import LottieComponent import MultilineTextComponent -/*private final class IpfsSchemeHandler: NSObject, WKURLSchemeHandler { - private final class PendingTask { - let sourceTask: any WKURLSchemeTask - var urlSessionTask: URLSessionTask? - let isCompleted = Atomic(value: false) - - init(sourceTask: any WKURLSchemeTask) { - self.sourceTask = sourceTask - - var cleanUrl = sourceTask.request.url!.absoluteString - if let range = cleanUrl.range(of: "/ipfs/") { - cleanUrl = "ipfs://" + String(cleanUrl[range.upperBound...]) - } else if let range = cleanUrl.range(of: "/ipns/") { - cleanUrl = "ipns://" + String(cleanUrl[range.upperBound...]) - } - print("Load: \(cleanUrl)") - cleanUrl = cleanUrl.replacingOccurrences(of: "ipns://", with: "ipns/") - cleanUrl = cleanUrl.replacingOccurrences(of: "ipfs://", with: "ipfs/") - let mappedUrl = "https://cloudflare-ipfs.com/\(cleanUrl)" - let isCompleted = self.isCompleted - self.urlSessionTask = URLSession.shared.dataTask(with: URLRequest(url: URL(string: mappedUrl)!), completionHandler: { data, response, error in - if isCompleted.swap(true) { - return - } - - if let error { - sourceTask.didFailWithError(error) - } else { - if let response { - sourceTask.didReceive(response) - } - if let data { - sourceTask.didReceive(data) - } - sourceTask.didFinish() - } - }) - self.urlSessionTask?.resume() - } - - func cancel() { - if let urlSessionTask = self.urlSessionTask { - self.urlSessionTask = nil - if !self.isCompleted.swap(true) { - switch urlSessionTask.state { - case .running, .suspended: - urlSessionTask.cancel() - default: - break - } - } - } - } - } - - private var pendingTasks: [PendingTask] = [] - - func webView(_ webView: WKWebView, start urlSchemeTask: any WKURLSchemeTask) { - self.pendingTasks.append(PendingTask(sourceTask: urlSchemeTask)) - } - - func webView(_ webView: WKWebView, stop urlSchemeTask: any WKURLSchemeTask) { - if let index = self.pendingTasks.firstIndex(where: { $0.sourceTask === urlSchemeTask }) { - let task = self.pendingTasks[index] - self.pendingTasks.remove(at: index) - task.cancel() - } - } -}*/ - private final class TonSchemeHandler: NSObject, WKURLSchemeHandler { private final class PendingTask { let sourceTask: any WKURLSchemeTask var urlSessionTask: URLSessionTask? let isCompleted = Atomic(value: false) - init(sourceTask: any WKURLSchemeTask) { + init(proxyServerHost: String, sourceTask: any WKURLSchemeTask) { self.sourceTask = sourceTask var mappedHost: String = "" @@ -111,7 +41,7 @@ private final class TonSchemeHandler: NSObject, WKURLSchemeHandler { mappedPath = "/\(mappedPath)" } } - let mappedUrl = "https://\(mappedHost).magic.org\(mappedPath)" + let mappedUrl = "https://\(mappedHost).\(proxyServerHost)\(mappedPath)" let isCompleted = self.isCompleted self.urlSessionTask = URLSession.shared.dataTask(with: URLRequest(url: URL(string: mappedUrl)!), completionHandler: { data, response, error in if isCompleted.swap(true) { @@ -148,10 +78,16 @@ private final class TonSchemeHandler: NSObject, WKURLSchemeHandler { } } + private let proxyServerHost: String + private var pendingTasks: [PendingTask] = [] + init(proxyServerHost: String) { + self.proxyServerHost = proxyServerHost + } + func webView(_ webView: WKWebView, start urlSchemeTask: any WKURLSchemeTask) { - self.pendingTasks.append(PendingTask(sourceTask: urlSchemeTask)) + self.pendingTasks.append(PendingTask(proxyServerHost: self.proxyServerHost, sourceTask: urlSchemeTask)) } func webView(_ webView: WKWebView, stop urlSchemeTask: any WKURLSchemeTask) { @@ -200,7 +136,11 @@ final class BrowserWebContent: UIView, BrowserContent, WKNavigationDelegate, WKU let configuration = WKWebViewConfiguration() - configuration.setURLSchemeHandler(TonSchemeHandler(), forURLScheme: "tonsite") + var proxyServerHost = "magic.org" + if let data = context.currentAppConfiguration.with({ $0 }).data, let hostValue = data["ton_proxy_address"] as? String { + proxyServerHost = hostValue + } + configuration.setURLSchemeHandler(TonSchemeHandler(proxyServerHost: proxyServerHost), forURLScheme: "tonsite") self.webView = WKWebView(frame: CGRect(), configuration: configuration) self.webView.allowsLinkPreview = true