From 807c0a1e2eef1558d6e94ecafe88ea328822b59a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Mon, 1 Apr 2024 13:49:09 +0200 Subject: [PATCH 1/3] hopefully fixes #629 should also get rid of empty comments if they couldn't be loaded --- Model/Applications/PipedAPI.swift | 13 ++++++++----- Model/CommentsModel.swift | 27 +++++++++++++++------------ 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/Model/Applications/PipedAPI.swift b/Model/Applications/PipedAPI.swift index bdb23372..1a1567e3 100644 --- a/Model/Applications/PipedAPI.swift +++ b/Model/Applications/PipedAPI.swift @@ -113,8 +113,11 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { content.json.arrayValue.compactMap { self.extractVideo(from: $0) } } - configureTransformer(pathPattern("comments/*")) { (content: Entity) -> CommentsPage in - let details = content.json.dictionaryValue + configureTransformer(pathPattern("comments/*")) { (content: Entity?) -> CommentsPage in + guard let details = content?.json.dictionaryValue else { + return CommentsPage(comments: [], nextPage: nil, disabled: true) + } + let comments = details["comments"]?.arrayValue.compactMap { self.extractComment(from: $0) } ?? [] let nextPage = details["nextpage"]?.string let disabled = details["disabled"]?.bool ?? false @@ -663,16 +666,16 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { let videoStreams = content.dictionaryValue["videoStreams"]?.arrayValue ?? [] - videoStreams.forEach { videoStream in + for videoStream in videoStreams { let videoCodec = videoStream.dictionaryValue["codec"]?.string ?? "" if Self.disallowedVideoCodecs.contains(where: videoCodec.contains) { - return + continue } guard let audioAssetUrl = audioStream.dictionaryValue["url"]?.url, let videoAssetUrl = videoStream.dictionaryValue["url"]?.url else { - return + continue } let audioAsset = AVURLAsset(url: audioAssetUrl) diff --git a/Model/CommentsModel.swift b/Model/CommentsModel.swift index 8340467c..9e713199 100644 --- a/Model/CommentsModel.swift +++ b/Model/CommentsModel.swift @@ -35,32 +35,35 @@ final class CommentsModel: ObservableObject { func load(page: String? = nil) { guard let video = player.currentVideo else { return } - - if !firstPage && !nextPageAvailable { - return - } - - firstPage = page.isNil || page!.isEmpty + guard firstPage || nextPageAvailable else { return } player .playerAPI(video)? .comments(video.videoID, page: page)? .load() .onSuccess { [weak self] response in - if let page: CommentsPage = response.typedContent() { - self?.all += page.comments - self?.nextPage = page.nextPage - self?.disabled = page.disabled + guard let self = self else { return } + if let commentsPage: CommentsPage = response.typedContent() { + self.all += commentsPage.comments + self.nextPage = commentsPage.nextPage + self.disabled = commentsPage.disabled + } else { + print("Failed to parse response: \(response)") } } - .onFailure { [weak self] requestError in - self?.disabled = !requestError.json.dictionaryValue["error"].isNil + .onFailure { [weak self] error in + self?.handleFailure(error: error) } .onCompletion { [weak self] _ in self?.loaded = true } } + func handleFailure(error: Error) { + disabled = true + print("Encountered an error: \(error)") + } + func loadNextPageIfNeeded(current comment: Comment) { let thresholdIndex = all.index(all.endIndex, offsetBy: -5) if all.firstIndex(where: { $0 == comment }) == thresholdIndex { From ae16680fc205e37f3c271397417edd4f1bbc3e46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Mon, 1 Apr 2024 14:00:02 +0200 Subject: [PATCH 2/3] removed some unnecessary print() --- Model/CommentsModel.swift | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/Model/CommentsModel.swift b/Model/CommentsModel.swift index 9e713199..7ca43851 100644 --- a/Model/CommentsModel.swift +++ b/Model/CommentsModel.swift @@ -47,23 +47,16 @@ final class CommentsModel: ObservableObject { self.all += commentsPage.comments self.nextPage = commentsPage.nextPage self.disabled = commentsPage.disabled - } else { - print("Failed to parse response: \(response)") } } - .onFailure { [weak self] error in - self?.handleFailure(error: error) + .onFailure { [weak self] _ in + self?.disabled = true } .onCompletion { [weak self] _ in self?.loaded = true } } - func handleFailure(error: Error) { - disabled = true - print("Encountered an error: \(error)") - } - func loadNextPageIfNeeded(current comment: Comment) { let thresholdIndex = all.index(all.endIndex, offsetBy: -5) if all.firstIndex(where: { $0 == comment }) == thresholdIndex { From c77c5a6d21723c58ab58e29e36bb4dd7c73ed8b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toni=20F=C3=B6rster?= Date: Tue, 2 Apr 2024 15:08:36 +0200 Subject: [PATCH 3/3] don't add empty comments --- Model/Applications/PipedAPI.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/Model/Applications/PipedAPI.swift b/Model/Applications/PipedAPI.swift index 1a1567e3..f456d343 100644 --- a/Model/Applications/PipedAPI.swift +++ b/Model/Applications/PipedAPI.swift @@ -727,15 +727,23 @@ final class PipedAPI: Service, ObservableObject, VideosAPI { let commentorUrl = details["commentorUrl"]?.string let channelId = commentorUrl?.components(separatedBy: "/")[2] ?? "" + let commentText = extractCommentText(from: details["commentText"]?.stringValue) + let commentId = details["commentId"]?.string ?? UUID().uuidString + + // Sanity checks: return nil if required data is missing + if commentText.isEmpty || commentId.isEmpty || author.isEmpty { + return nil + } + return Comment( - id: details["commentId"]?.string ?? UUID().uuidString, + id: commentId, author: author, authorAvatarURL: details["thumbnail"]?.string ?? "", time: details["commentedTime"]?.string ?? "", pinned: details["pinned"]?.bool ?? false, hearted: details["hearted"]?.bool ?? false, likeCount: details["likeCount"]?.int ?? 0, - text: extractCommentText(from: details["commentText"]?.stringValue), + text: commentText, repliesPage: details["repliesPage"]?.string, channel: Channel(app: .piped, id: channelId, name: author) )