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 {
urlRequest.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
}
urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
return urlRequest
}

View File

@ -22,13 +22,47 @@ public class Account: Decodable {
public let url: URL
public let avatar: URL
public let avatarStatic: URL
public let header: URL
public let headerStatic: URL
public let header: URL?
public let headerStatic: URL?
public private(set) var emojis: [Emoji]
public let moved: Bool?
public let fields: [Field]?
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> {
return Request<Empty>(method: .post, path: "/api/v1/follow_requests/\(account.id)/authorize")
}

View File

@ -64,6 +64,17 @@ extension Attachment {
case gifv
case audio
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 let accessToken: String
private let scope: String
private let scope: String?
public var scopes: [Scope] {
guard let scope = scope else { return [] }
return scope.components(separatedBy: .whitespaces).compactMap(Scope.init)
}

View File

@ -13,6 +13,27 @@ public class RegisteredApplication: Decodable {
public let clientID: 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 {
case id
case clientID = "client_id"

View File

@ -157,8 +157,8 @@ class ProfileTableViewController: EnhancedTableViewController {
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 {
guard let older = older else { return }
let older = self.older ?? RequestRange.after(id: timelineSegments.last!.last!, count: nil)
getStatuses(for: older) { response in
guard case let .success(newStatuses, pagination) = response else { fatalError() }
@ -183,8 +183,8 @@ class ProfileTableViewController: EnhancedTableViewController {
}
@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
guard case let .success(newStatuses, pagination) = response else { fatalError() }

View File

@ -36,7 +36,7 @@ class TimelineTableViewController: EnhancedTableViewController {
var newer: RequestRange?
var older: RequestRange?
init(for timeline: Timeline) {
self.timeline = timeline
@ -105,7 +105,7 @@ class TimelineTableViewController: EnhancedTableViewController {
override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
if indexPath.section == timelineSegments.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)
MastodonController.client.run(request) { response in
@ -130,7 +130,7 @@ class TimelineTableViewController: EnhancedTableViewController {
}
@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)
MastodonController.client.run(request) { response in

View File

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

View File

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