From f84c6d319a424967cad30a3b8cd42d1ff130a759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Mon, 1 Apr 2024 15:08:08 +0200 Subject: [PATCH] iv: use html comments instead of plain text It now correctly displays emojis hyphens --- Model/Applications/InvidiousAPI.swift | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/Model/Applications/InvidiousAPI.swift b/Model/Applications/InvidiousAPI.swift index 2a3c191b..e22445ad 100644 --- a/Model/Applications/InvidiousAPI.swift +++ b/Model/Applications/InvidiousAPI.swift @@ -123,7 +123,7 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI { content.json.dictionaryValue["videos"]?.arrayValue.map(self.extractVideo) ?? [] } - ["latest", "playlists", "streams", "shorts", "channels", "videos", "releases", "podcasts"].forEach { type in + for type in ["latest", "playlists", "streams", "shorts", "channels", "videos", "releases", "podcasts"] { configureTransformer(pathPattern("channels/*/\(type)"), requestMethods: [.get]) { (content: Entity) -> ChannelPage in self.extractChannelPage(from: content.json) } @@ -691,6 +691,8 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI { let author = details["author"]?.string ?? "" let channelId = details["authorId"]?.string ?? UUID().uuidString let authorAvatarURL = details["authorThumbnails"]?.arrayValue.last?.dictionaryValue["url"]?.string ?? "" + let htmlContent = details["contentHtml"]?.string ?? "" + let decodedContent = decodeHtml(htmlContent) return Comment( id: UUID().uuidString, author: author, @@ -699,12 +701,25 @@ final class InvidiousAPI: Service, ObservableObject, VideosAPI { pinned: false, hearted: false, likeCount: details["likeCount"]?.int ?? 0, - text: details["content"]?.string ?? "", + text: decodedContent, repliesPage: details["replies"]?.dictionaryValue["continuation"]?.string, channel: Channel(app: .invidious, id: channelId, name: author) ) } + private func decodeHtml(_ htmlEncodedString: String) -> String { + if let data = htmlEncodedString.data(using: .utf8) { + let options: [NSAttributedString.DocumentReadingOptionKey: Any] = [ + .documentType: NSAttributedString.DocumentType.html, + .characterEncoding: String.Encoding.utf8.rawValue + ] + if let attributedString = try? NSAttributedString(data: data, options: options, documentAttributes: nil) { + return attributedString.string + } + } + return htmlEncodedString + } + private func extractCaptions(from content: JSON) -> [Captions] { content["captions"].arrayValue.compactMap { details in guard let url = URL(string: details["url"].stringValue, relativeTo: account.url) else { return nil }