Merge pull request #631 from stonerl/comments-piped

hopefully fixes #629
This commit is contained in:
Arkadiusz Fal 2024-04-02 22:51:07 +02:00 committed by GitHub
commit 576a993faf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 26 additions and 19 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)
@ -724,15 +727,23 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
let commentorUrl = details["commentorUrl"]?.string let commentorUrl = details["commentorUrl"]?.string
let channelId = commentorUrl?.components(separatedBy: "/")[2] ?? "" 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( return Comment(
id: details["commentId"]?.string ?? UUID().uuidString, id: commentId,
author: author, author: author,
authorAvatarURL: details["thumbnail"]?.string ?? "", authorAvatarURL: details["thumbnail"]?.string ?? "",
time: details["commentedTime"]?.string ?? "", time: details["commentedTime"]?.string ?? "",
pinned: details["pinned"]?.bool ?? false, pinned: details["pinned"]?.bool ?? false,
hearted: details["hearted"]?.bool ?? false, hearted: details["hearted"]?.bool ?? false,
likeCount: details["likeCount"]?.int ?? 0, likeCount: details["likeCount"]?.int ?? 0,
text: extractCommentText(from: details["commentText"]?.stringValue), text: commentText,
repliesPage: details["repliesPage"]?.string, repliesPage: details["repliesPage"]?.string,
channel: Channel(app: .piped, id: channelId, name: author) channel: Channel(app: .piped, id: channelId, name: author)
) )

View File

@ -35,26 +35,22 @@ 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
} }
} }
.onFailure { [weak self] requestError in .onFailure { [weak self] _ in
self?.disabled = !requestError.json.dictionaryValue["error"].isNil self?.disabled = true
} }
.onCompletion { [weak self] _ in .onCompletion { [weak self] _ in
self?.loaded = true self?.loaded = true