// // Account.swift // Pachyderm // // Created by Shadowfacts on 9/8/18. // Copyright © 2018 Shadowfacts. All rights reserved. // import Foundation public class Account: Decodable, ClientModel { var client: Client! { didSet { emojis.client = client } } public let id: String public let username: String public let acct: String public let displayName: String public let locked: Bool public let createdAt: Date public let followersCount: Int public let followingCount: Int public let statusesCount: Int public let note: String public let url: URL public let avatar: URL public let avatarStatic: 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? public func authorizeFollowRequest(completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/follow_requests/\(id)/authorize") client.run(request, completion: completion) } public func rejectFollowRequest(completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/follow_requests/\(id)/reject") client.run(request, completion: completion) } public func removeFromFollowRequests(completion: @escaping Client.Callback) { let request = Request(method: .delete, path: "/api/v1/suggestions/\(id)") client.run(request, completion: completion) } public func getFollowers(range: RequestRange = .default, completion: @escaping Client.Callback<[Account]>) { var request = Request<[Account]>(method: .get, path: "/api/v1/accounts/\(id)/followers") request.range = range client.run(request, completion: completion) } public func getFollowing(range: RequestRange = .default, completion: @escaping Client.Callback<[Account]>) { var request = Request<[Account]>(method: .get, path: "/api/v1/accounts/\(id)/following") request.range = range client.run(request, completion: completion) } public func getStatuses(range: RequestRange = .default, onlyMedia: Bool? = nil, pinned: Bool? = nil, excludeReplies: Bool? = nil, completion: @escaping Client.Callback<[Status]>) { var request = Request<[Status]>(method: .get, path: "/api/v1/accounts/\(id)/statuses", queryParameters: [ "only_media" => onlyMedia, "pinned" => pinned, "exclude_replies" => excludeReplies ]) request.range = range client.run(request, completion: completion) } public func follow(completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/accounts/\(id)/follow") client.run(request, completion: completion) } public func unfollow(completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/accounts/\(id)/unfollow") client.run(request, completion: completion) } public func block(completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/accounts/\(id)/block") client.run(request, completion: completion) } public func unblock(completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/accounts/\(id)/unblock") client.run(request, completion: completion) } public func mute(notifications: Bool? = nil, completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/accounts/\(id)/mute", body: .parameters([ "notifications" => notifications ])) client.run(request, completion: completion) } public func unmute(completion: @escaping Client.Callback) { let request = Request(method: .post, path: "/api/v1/accounts/\(id)/unmute") client.run(request, completion: completion) } public func getLists(completion: @escaping Client.Callback<[List]>) { let request = Request<[List]>(method: .get, path: "/api/v1/accounts/\(id)/lists") client.run(request, completion: completion) } private enum CodingKeys: String, CodingKey { case id case username case acct case displayName = "display_name" case locked case createdAt = "created_at" case followersCount = "followers_count" case followingCount = "following_count" case statusesCount = "statuses_count" case note case url case avatar case avatarStatic = "avatar_static" case header case headerStatic = "header_static" case emojis case moved case fields case bot } } extension Account: CustomDebugStringConvertible { public var debugDescription: String { return "Account(\(id), \(acct))" } } extension Account { public struct Field: Codable { let name: String let value: String } }