Compare commits

...

4 Commits

9 changed files with 88 additions and 17 deletions

View File

@ -88,6 +88,7 @@ public class Client {
if let accessToken = accessToken { if let accessToken = accessToken {
urlRequest.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization") urlRequest.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
} }
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
return urlRequest return urlRequest
} }

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

@ -157,7 +157,7 @@ class ProfileTableViewController: EnhancedTableViewController {
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if timelineSegments.count > 0 && indexPath.section == timelineSegments.count && indexPath.row == timelineSegments[indexPath.section - 1].count - 1 { if timelineSegments.count > 0 && indexPath.section == timelineSegments.count && indexPath.row == timelineSegments[indexPath.section - 1].count - 1 {
guard let older = older else { return } let older = self.older ?? RequestRange.after(id: timelineSegments.last!.last!, count: nil)
getStatuses(for: older) { response in getStatuses(for: older) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() } guard case let .success(newStatuses, pagination) = response else { fatalError() }
@ -183,7 +183,7 @@ class ProfileTableViewController: EnhancedTableViewController {
} }
@objc func refreshStatuses(_ sender: Any) { @objc func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return } let newer = self.newer ?? RequestRange.after(id: timelineSegments.first!.first!, count: nil)
getStatuses(for: newer) { response in getStatuses(for: newer) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() } guard case let .success(newStatuses, pagination) = response else { fatalError() }

View File

@ -105,7 +105,7 @@ class TimelineTableViewController: EnhancedTableViewController {
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) { override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == timelineSegments.count - 1, if indexPath.section == timelineSegments.count - 1,
indexPath.row == timelineSegments[indexPath.section].count - 1 { indexPath.row == timelineSegments[indexPath.section].count - 1 {
guard let older = older else { return } let older = self.older ?? RequestRange.before(id: timelineSegments.last!.last!, count: nil)
let request = MastodonController.client.getStatuses(timeline: timeline, range: older) let request = MastodonController.client.getStatuses(timeline: timeline, range: older)
MastodonController.client.run(request) { response in MastodonController.client.run(request) { response in
@ -130,7 +130,7 @@ class TimelineTableViewController: EnhancedTableViewController {
} }
@objc func refreshStatuses(_ sender: Any) { @objc func refreshStatuses(_ sender: Any) {
guard let newer = newer else { return } let newer = self.newer ?? RequestRange.after(id: timelineSegments.first!.first!, count: nil)
let request = MastodonController.client.getStatuses(timeline: timeline, range: newer) let request = MastodonController.client.getStatuses(timeline: timeline, range: newer)
MastodonController.client.run(request) { response in MastodonController.client.run(request) { response in

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

@ -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
]) ])
} }