Tusker/Pachyderm/Model/Account.swift

131 lines
4.6 KiB
Swift
Raw Normal View History

2018-09-11 14:52:21 +00:00
//
// Account.swift
// Pachyderm
//
// Created by Shadowfacts on 9/8/18.
// Copyright © 2018 Shadowfacts. All rights reserved.
//
import Foundation
public class Account: Decodable {
2018-09-11 14:52:21 +00:00
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 static func authorizeFollowRequest(_ account: Account) -> Request<Empty> {
return Request<Empty>(method: .post, path: "/api/v1/follow_requests/\(account.id)/authorize")
2018-09-11 14:52:21 +00:00
}
public static func rejectFollowRequest(_ account: Account) -> Request<Empty> {
return Request<Empty>(method: .post, path: "/api/v1/follow_requests/\(account.id)/reject")
2018-09-11 14:52:21 +00:00
}
public static func removeFromFollowRequests(_ account: Account) -> Request<Empty> {
return Request<Empty>(method: .delete, path: "/api/v1/suggestions/\(account.id)")
2018-09-11 14:52:21 +00:00
}
public static func getFollowers(_ account: Account, range: RequestRange = .default) -> Request<[Account]> {
var request = Request<[Account]>(method: .get, path: "/api/v1/accounts/\(account.id)/followers")
2018-09-11 14:52:21 +00:00
request.range = range
return request
2018-09-11 14:52:21 +00:00
}
public static func getFollowing(_ account: Account, range: RequestRange = .default) -> Request<[Account]> {
var request = Request<[Account]>(method: .get, path: "/api/v1/accounts/\(account.id)/following")
2018-09-11 14:52:21 +00:00
request.range = range
return request
2018-09-11 14:52:21 +00:00
}
public static func getStatuses(_ account: Account, range: RequestRange = .default, onlyMedia: Bool? = nil, pinned: Bool? = nil, excludeReplies: Bool? = nil) -> Request<[Status]> {
var request = Request<[Status]>(method: .get, path: "/api/v1/accounts/\(account.id)/statuses", queryParameters: [
2018-09-11 14:52:21 +00:00
"only_media" => onlyMedia,
"pinned" => pinned,
"exclude_replies" => excludeReplies
])
request.range = range
return request
2018-09-11 14:52:21 +00:00
}
public static func follow(_ account: Account) -> Request<Relationship> {
return Request<Relationship>(method: .post, path: "/api/v1/accounts/\(account.id)/follow")
2018-09-11 14:52:21 +00:00
}
public static func unfollow(_ account: Account) -> Request<Relationship> {
return Request<Relationship>(method: .post, path: "/api/v1/accounts/\(account.id)/unfollow")
2018-09-11 14:52:21 +00:00
}
public static func block(_ account: Account) -> Request<Relationship> {
return Request<Relationship>(method: .post, path: "/api/v1/accounts/\(account.id)/block")
2018-09-11 14:52:21 +00:00
}
public static func unblock(_ account: Account) -> Request<Relationship> {
return Request<Relationship>(method: .post, path: "/api/v1/accounts/\(account.id)/unblock")
2018-09-11 14:52:21 +00:00
}
public static func mute(_ account: Account, notifications: Bool? = nil) -> Request<Relationship> {
return Request<Relationship>(method: .post, path: "/api/v1/accounts/\(account.id)/mute", body: .parameters([
2018-09-11 14:52:21 +00:00
"notifications" => notifications
]))
}
public static func unmute(_ account: Account) -> Request<Relationship> {
return Request<Relationship>(method: .post, path: "/api/v1/accounts/\(account.id)/unmute")
2018-09-11 14:52:21 +00:00
}
public static func getLists(_ account: Account) -> Request<[List]> {
return Request<[List]>(method: .get, path: "/api/v1/accounts/\(account.id)/lists")
2018-09-11 14:52:21 +00:00
}
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
}
}