Comments (fixes #4)

This commit is contained in:
Arkadiusz Fal
2021-12-04 20:35:41 +01:00
parent eb537676e6
commit 19a3f08336
29 changed files with 688 additions and 68 deletions

View File

@@ -71,6 +71,13 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
content.json.arrayValue.map { PipedAPI.extractVideo(from: $0)! }
}
configureTransformer(pathPattern("comments/*")) { (content: Entity<JSON>) -> CommentsPage in
let comments = content.json.dictionaryValue["comments"]?.arrayValue.map { PipedAPI.extractComment(from: $0)! } ?? []
let nextPage = content.json.dictionaryValue["nextpage"]?.stringValue
return CommentsPage(comments: comments, nextPage: nextPage)
}
if account.token.isNil {
updateToken()
}
@@ -80,9 +87,14 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
PipedAPI.authorizedEndpoints.contains { url.absoluteString.contains($0) }
}
@discardableResult func updateToken() -> Request {
func updateToken() {
guard !account.anonymous else {
return
}
account.token = nil
return login.request(
login.request(
.post,
json: ["username": account.username, "password": account.password]
)
@@ -161,6 +173,17 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
func playlistVideo(_: String, _: String) -> Resource? { nil }
func playlistVideos(_: String) -> Resource? { nil }
func comments(_ id: Video.ID, page: String?) -> Resource? {
let path = page.isNil ? "comments/\(id)" : "nextpage/comments/\(id)"
let resource = resource(baseURL: account.url, path: path)
if page.isNil {
return resource
}
return resource.withParam("nextpage", page)
}
private func pathPattern(_ path: String) -> String {
"**\(path)"
}
@@ -395,4 +418,23 @@ final class PipedAPI: Service, ObservableObject, VideosAPI {
.arrayValue
.filter { $0.dictionaryValue["format"] == "MPEG_4" } ?? []
}
private static func extractComment(from content: JSON) -> Comment? {
let details = content.dictionaryValue
let author = details["author"]?.stringValue ?? ""
let commentorUrl = details["commentorUrl"]?.stringValue
let channelId = commentorUrl?.components(separatedBy: "/")[2] ?? ""
return Comment(
id: details["commentId"]?.stringValue ?? UUID().uuidString,
author: author,
authorAvatarURL: details["thumbnail"]?.stringValue ?? "",
time: details["commentedTime"]?.stringValue ?? "",
pinned: details["pinned"]?.boolValue ?? false,
hearted: details["hearted"]?.boolValue ?? false,
likeCount: details["likeCount"]?.intValue ?? 0,
text: details["commentText"]?.stringValue ?? "",
repliesPage: details["repliesPage"]?.stringValue,
channel: Channel(id: channelId, name: author)
)
}
}