hopefully fixes #629

should also get rid of empty comments if they couldn't be loaded
This commit is contained in:
Toni Förster 2024-04-01 13:49:09 +02:00
parent 96a2119a05
commit 807c0a1e2e
No known key found for this signature in database
GPG Key ID: 292F3E5086C83FC7
2 changed files with 23 additions and 17 deletions

View File

@ -113,8 +113,11 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
content.json.arrayValue.compactMap { self.extractVideo(from: $0) } content.json.arrayValue.compactMap { self.extractVideo(from: $0) }
} }
configureTransformer(pathPattern("comments/*")) { (content: Entity<JSON>) -> CommentsPage in configureTransformer(pathPattern("comments/*")) { (content: Entity<JSON>?) -> CommentsPage in
let details = content.json.dictionaryValue 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 comments = details["comments"]?.arrayValue.compactMap { self.extractComment(from: $0) } ?? []
let nextPage = details["nextpage"]?.string let nextPage = details["nextpage"]?.string
let disabled = details["disabled"]?.bool ?? false let disabled = details["disabled"]?.bool ?? false
@ -663,16 +666,16 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
let videoStreams = content.dictionaryValue["videoStreams"]?.arrayValue ?? [] let videoStreams = content.dictionaryValue["videoStreams"]?.arrayValue ?? []
videoStreams.forEach { videoStream in for videoStream in videoStreams {
let videoCodec = videoStream.dictionaryValue["codec"]?.string ?? "" let videoCodec = videoStream.dictionaryValue["codec"]?.string ?? ""
if Self.disallowedVideoCodecs.contains(where: videoCodec.contains) { if Self.disallowedVideoCodecs.contains(where: videoCodec.contains) {
return continue
} }
guard let audioAssetUrl = audioStream.dictionaryValue["url"]?.url, guard let audioAssetUrl = audioStream.dictionaryValue["url"]?.url,
let videoAssetUrl = videoStream.dictionaryValue["url"]?.url let videoAssetUrl = videoStream.dictionaryValue["url"]?.url
else { else {
return continue
} }
let audioAsset = AVURLAsset(url: audioAssetUrl) let audioAsset = AVURLAsset(url: audioAssetUrl)

View File

@ -35,32 +35,35 @@ final class CommentsModel: ObservableObject {
func load(page: String? = nil) { func load(page: String? = nil) {
guard let video = player.currentVideo else { return } guard let video = player.currentVideo else { return }
guard firstPage || nextPageAvailable else { return }
if !firstPage && !nextPageAvailable {
return
}
firstPage = page.isNil || page!.isEmpty
player player
.playerAPI(video)? .playerAPI(video)?
.comments(video.videoID, page: page)? .comments(video.videoID, page: page)?
.load() .load()
.onSuccess { [weak self] response in .onSuccess { [weak self] response in
if let page: CommentsPage = response.typedContent() { guard let self = self else { return }
self?.all += page.comments if let commentsPage: CommentsPage = response.typedContent() {
self?.nextPage = page.nextPage self.all += commentsPage.comments
self?.disabled = page.disabled self.nextPage = commentsPage.nextPage
self.disabled = commentsPage.disabled
} else {
print("Failed to parse response: \(response)")
} }
} }
.onFailure { [weak self] requestError in .onFailure { [weak self] error in
self?.disabled = !requestError.json.dictionaryValue["error"].isNil self?.handleFailure(error: error)
} }
.onCompletion { [weak self] _ in .onCompletion { [weak self] _ in
self?.loaded = true self?.loaded = true
} }
} }
func handleFailure(error: Error) {
disabled = true
print("Encountered an error: \(error)")
}
func loadNextPageIfNeeded(current comment: Comment) { func loadNextPageIfNeeded(current comment: Comment) {
let thresholdIndex = all.index(all.endIndex, offsetBy: -5) let thresholdIndex = all.index(all.endIndex, offsetBy: -5)
if all.firstIndex(where: { $0 == comment }) == thresholdIndex { if all.firstIndex(where: { $0 == comment }) == thresholdIndex {