Decoding tweaks to support Pixelfed

This commit is contained in:
Shadowfacts 2019-09-24 15:48:57 -04:00
parent 5da357ee9c
commit a38f9df3af
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
10 changed files with 90 additions and 19 deletions

View File

@ -22,13 +22,47 @@ public class Account: Decodable {
public let url: URL public let url: URL
public let avatar: URL public let avatar: URL
public let avatarStatic: URL public let avatarStatic: URL
public let header: URL public let header: URL?
public let headerStatic: URL public let headerStatic: URL?
public private(set) var emojis: [Emoji] public private(set) var emojis: [Emoji]
public let moved: Bool? public let moved: Bool?
public let fields: [Field]? public let fields: [Field]?
public let bot: Bool? public let bot: Bool?
// we need a custom decoder, because all API-compatible implementations don't return some data in the same format
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.id = try container.decode(String.self, forKey: .id)
self.username = try container.decode(String.self, forKey: .username)
self.acct = try container.decode(String.self, forKey: .acct)
self.displayName = try container.decode(String.self, forKey: .displayName)
self.locked = try container.decode(Bool.self, forKey: .locked)
self.createdAt = try container.decode(Date.self, forKey: .createdAt)
self.followersCount = try container.decode(Int.self, forKey: .followersCount)
self.followingCount = try container.decode(Int.self, forKey: .followingCount)
self.statusesCount = try container.decode(Int.self, forKey: .statusesCount)
self.note = try container.decode(String.self, forKey: .note)
self.url = try container.decode(URL.self, forKey: .url)
self.avatar = try container.decode(URL.self, forKey: .avatar)
self.avatarStatic = try container.decode(URL.self, forKey: .avatarStatic)
if let header = try? container.decodeIfPresent(String.self, forKey: .header),
let url = URL(string: header) {
self.header = url
} else {
self.header = nil
}
if let headerStatic = try? container.decodeIfPresent(String.self, forKey: .headerStatic),
let url = URL(string: headerStatic) {
self.headerStatic = url
} else {
self.headerStatic = nil
}
self.emojis = try container.decode([Emoji].self, forKey: .emojis)
self.moved = try container.decodeIfPresent(Bool.self, forKey: .moved)
self.fields = try container.decodeIfPresent([Field].self, forKey: .fields)
self.bot = try container.decodeIfPresent(Bool.self, forKey: .bot)
}
public static func authorizeFollowRequest(_ account: Account) -> Request<Empty> { public static func authorizeFollowRequest(_ account: Account) -> Request<Empty> {
return Request<Empty>(method: .post, path: "/api/v1/follow_requests/\(account.id)/authorize") return Request<Empty>(method: .post, path: "/api/v1/follow_requests/\(account.id)/authorize")
} }

View File

@ -64,6 +64,17 @@ extension Attachment {
case gifv case gifv
case audio case audio
case unknown case unknown
// we need a custom decoder, because all API-compatible implementations don't return some data in the same format
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let str = try container.decode(String.self)
if let kind = Kind(rawValue: str.lowercased()) {
self = kind
} else {
throw DecodingError.dataCorruptedError(in: container, debugDescription: "Attachment type must be one of image, video, gifv, audio, or unknown.")
}
}
} }
} }

View File

@ -10,9 +10,10 @@ import Foundation
public class LoginSettings: Decodable { public class LoginSettings: Decodable {
public let accessToken: String public let accessToken: String
private let scope: String private let scope: String?
public var scopes: [Scope] { public var scopes: [Scope] {
guard let scope = scope else { return [] }
return scope.components(separatedBy: .whitespaces).compactMap(Scope.init) return scope.components(separatedBy: .whitespaces).compactMap(Scope.init)
} }

View File

@ -13,6 +13,27 @@ public class RegisteredApplication: Decodable {
public let clientID: String public let clientID: String
public let clientSecret: String public let clientSecret: String
// we need a custom decoder, because all API-compatible implementations don't return some data in the same format
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
if let id = try? container.decode(String.self, forKey: .id) {
self.id = id
} else if let id = try? container.decode(Int.self, forKey: .id) {
self.id = String(id)
} else {
throw DecodingError.dataCorruptedError(forKey: CodingKeys.id, in: container, debugDescription: "Expect application id to be string or number")
}
if let clientID = try? container.decode(String.self, forKey: .clientID) {
self.clientID = clientID
} else if let clientID = try? container.decode(Int.self, forKey: .clientID) {
self.clientID = String(clientID)
} else {
throw DecodingError.dataCorruptedError(forKey: CodingKeys.id, in: container, debugDescription: "Expect client id to be string or number")
}
self.clientSecret = try container.decode(String.self, forKey: .clientSecret)
}
private enum CodingKeys: String, CodingKey { private enum CodingKeys: String, CodingKey {
case id case id
case clientID = "client_id" case clientID = "client_id"

View File

@ -27,7 +27,7 @@ public class Status: Decodable {
public let favourited: Bool? public let favourited: Bool?
public let muted: Bool? public let muted: Bool?
public let sensitive: Bool public let sensitive: Bool
public let spoilerText: String public let spoilerText: String?
public let visibility: Visibility public let visibility: Visibility
public let attachments: [Attachment] public let attachments: [Attachment]
public let mentions: [Mention] public let mentions: [Mention]

View File

@ -131,13 +131,14 @@ class ComposeViewController: UIViewController {
contentWarningEnabled = false contentWarningEnabled = false
contentWarningContainerView.isHidden = true contentWarningContainerView.isHidden = true
} else { } else {
contentWarningEnabled = !inReplyTo.spoilerText.isEmpty contentWarningEnabled = inReplyTo.spoilerText != nil && !inReplyTo.spoilerText!.isEmpty
contentWarningContainerView.isHidden = !contentWarningEnabled contentWarningContainerView.isHidden = !contentWarningEnabled
if Preferences.shared.contentWarningCopyMode == .prependRe, if Preferences.shared.contentWarningCopyMode == .prependRe,
!inReplyTo.spoilerText.lowercased().starts(with: "re:") { let spoiler = inReplyTo.spoilerText,
contentWarningTextField.text = "re: \(inReplyTo.spoilerText)" !spoiler.lowercased().starts(with: "re:") {
contentWarningTextField.text = "re: \(spoiler)"
} else { } else {
contentWarningTextField.text = inReplyTo.spoilerText contentWarningTextField.text = inReplyTo.spoilerText != nil ? inReplyTo.spoilerText : ""
} }
} }

View File

@ -63,13 +63,16 @@ class ProfileHeaderTableViewCell: UITableViewCell {
self.avatarURL = nil self.avatarURL = nil
} }
} }
ImageCache.headers.get(account.header) { (data) in self.headerImageView.image = nil
if let header = account.header {
ImageCache.headers.get(header) { (data) in
guard let data = data else { return } guard let data = data else { return }
DispatchQueue.main.async { DispatchQueue.main.async {
self.headerImageView.image = UIImage(data: data) self.headerImageView.image = UIImage(data: data)
self.headerURL = nil self.headerURL = nil
} }
} }
}
noteLabel.navigationDelegate = delegate noteLabel.navigationDelegate = delegate
noteLabel.setTextFromHtml(account.note) noteLabel.setTextFromHtml(account.note)

View File

@ -126,10 +126,10 @@ class ConversationMainStatusTableViewCell: UITableViewCell {
contentLabel.statusID = statusID contentLabel.statusID = statusID
collapsible = !status.spoilerText.isEmpty collapsible = status.spoilerText != nil && !status.spoilerText!.isEmpty
setCollapsed(collapsible, animated: false) setCollapsed(collapsible, animated: false)
contentWarningLabel.text = status.spoilerText contentWarningLabel.text = status.spoilerText
contentWarningLabel.isHidden = status.spoilerText.isEmpty contentWarningLabel.isHidden = status.spoilerText != nil && status.spoilerText!.isEmpty
} }
private func updateStatusState(status: Status) { private func updateStatusState(status: Status) {

View File

@ -135,10 +135,10 @@ class StatusTableViewCell: UITableViewCell {
contentLabel.statusID = status.id contentLabel.statusID = status.id
collapsible = !status.spoilerText.isEmpty collapsible = status.spoilerText != nil && !status.spoilerText!.isEmpty
setCollapsed(collapsible, animated: false) setCollapsed(collapsible, animated: false)
contentWarningLabel.text = status.spoilerText contentWarningLabel.text = status.spoilerText
contentWarningLabel.isHidden = status.spoilerText.isEmpty contentWarningLabel.isHidden = status.spoilerText != nil && status.spoilerText!.isEmpty
} }
private func updateStatusState(status: Status) { private func updateStatusState(status: Status) {

View File

@ -263,7 +263,7 @@ struct XCBActions {
"following": account.followingCount.description, "following": account.followingCount.description,
"url": account.url.absoluteString, "url": account.url.absoluteString,
"avatarURL": account.avatar.absoluteString, "avatarURL": account.avatar.absoluteString,
"headerURL": account.header.absoluteString "headerURL": account.header?.absoluteString
]) ])
} }
} }
@ -278,7 +278,7 @@ struct XCBActions {
"following": account.followingCount.description, "following": account.followingCount.description,
"url": account.url.absoluteString, "url": account.url.absoluteString,
"avatarURL": account.avatar.absoluteString, "avatarURL": account.avatar.absoluteString,
"headerURL": account.header.absoluteString "headerURL": account.header?.absoluteString
]) ])
} }