IceCubesApp/Packages/Network/Sources/Network/Endpoint/Accounts.swift

217 lines
6 KiB
Swift
Raw Normal View History

2022-11-29 11:18:06 +00:00
import Foundation
2023-01-10 07:24:05 +00:00
import Models
2022-11-29 11:18:06 +00:00
2022-12-01 08:05:26 +00:00
public enum Accounts: Endpoint {
2022-11-29 11:18:06 +00:00
case accounts(id: String)
2023-12-08 07:04:35 +00:00
case lookup(name: String)
case favorites(sinceId: String?)
2023-01-09 18:26:56 +00:00
case bookmarks(sinceId: String?)
case followedTags
2022-12-21 19:26:38 +00:00
case featuredTags(id: String)
2022-12-01 08:05:26 +00:00
case verifyCredentials
2024-01-02 20:16:27 +00:00
case updateCredentialsMedia
2023-02-24 06:55:24 +00:00
case updateCredentials(json: UpdateCredentialsData)
2023-01-03 17:22:08 +00:00
case statuses(id: String,
sinceId: String?,
tag: String?,
2024-01-08 17:22:44 +00:00
onlyMedia: Bool,
excludeReplies: Bool,
excludeReblogs: Bool,
2023-01-03 17:22:08 +00:00
pinned: Bool?)
case relationships(ids: [String])
case follow(id: String, notify: Bool, reblogs: Bool)
2022-12-20 16:11:12 +00:00
case unfollow(id: String)
case familiarFollowers(withAccount: String)
case suggestions
2022-12-24 12:41:25 +00:00
case followers(id: String, maxId: String?)
case following(id: String, maxId: String?)
case lists(id: String)
2023-01-09 18:47:54 +00:00
case preferences
case block(id: String)
case unblock(id: String)
case mute(id: String, json: MuteData)
case unmute(id: String)
case relationshipNote(id: String, json: RelationshipNoteData)
2023-02-12 15:29:41 +00:00
2022-11-29 11:18:06 +00:00
public func path() -> String {
switch self {
2023-01-17 10:36:01 +00:00
case let .accounts(id):
2023-09-16 12:15:03 +00:00
"accounts/\(id)"
2023-12-08 07:04:35 +00:00
case .lookup:
"accounts/lookup"
case .favorites:
2023-09-16 12:15:03 +00:00
"favourites"
2023-01-09 18:26:56 +00:00
case .bookmarks:
2023-09-16 12:15:03 +00:00
"bookmarks"
case .followedTags:
2023-09-16 12:15:03 +00:00
"followed_tags"
2023-01-17 10:36:01 +00:00
case let .featuredTags(id):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/featured_tags"
2022-12-01 08:05:26 +00:00
case .verifyCredentials:
2023-09-16 12:15:03 +00:00
"accounts/verify_credentials"
2024-01-02 20:16:27 +00:00
case .updateCredentials, .updateCredentialsMedia:
2023-09-16 12:15:03 +00:00
"accounts/update_credentials"
2024-01-08 17:22:44 +00:00
case let .statuses(id, _, _, _, _, _, _):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/statuses"
2022-12-20 16:11:12 +00:00
case .relationships:
2023-09-16 12:15:03 +00:00
"accounts/relationships"
case let .follow(id, _, _):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/follow"
2023-01-17 10:36:01 +00:00
case let .unfollow(id):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/unfollow"
case .familiarFollowers:
2023-09-16 12:15:03 +00:00
"accounts/familiar_followers"
case .suggestions:
2023-09-16 12:15:03 +00:00
"suggestions"
2023-01-17 10:36:01 +00:00
case let .following(id, _):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/following"
2023-01-17 10:36:01 +00:00
case let .followers(id, _):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/followers"
2023-01-17 10:36:01 +00:00
case let .lists(id):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/lists"
2023-01-09 18:47:54 +00:00
case .preferences:
2023-09-16 12:15:03 +00:00
"preferences"
case let .block(id):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/block"
case let .unblock(id):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/unblock"
case let .mute(id, _):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/mute"
case let .unmute(id):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/unmute"
case let .relationshipNote(id, _):
2023-09-16 12:15:03 +00:00
"accounts/\(id)/note"
2022-11-29 11:18:06 +00:00
}
}
2023-02-12 15:29:41 +00:00
2022-11-29 11:18:06 +00:00
public func queryItems() -> [URLQueryItem]? {
2022-12-18 19:30:19 +00:00
switch self {
2023-12-08 07:04:35 +00:00
case let .lookup(name):
return [
.init(name: "acct", value: name),
]
2024-01-08 17:22:44 +00:00
case let .statuses(_, sinceId, tag, onlyMedia, excludeReplies, excludeReblogs, pinned):
2022-12-21 19:26:38 +00:00
var params: [URLQueryItem] = []
if let tag {
params.append(.init(name: "tagged", value: tag))
}
if let sinceId {
params.append(.init(name: "max_id", value: sinceId))
}
2024-02-14 11:48:14 +00:00
2024-01-08 17:22:44 +00:00
params.append(.init(name: "only_media", value: onlyMedia ? "true" : "false"))
params.append(.init(name: "exclude_replies", value: excludeReplies ? "true" : "false"))
params.append(.init(name: "exclude_reblogs", value: excludeReblogs ? "true" : "false"))
2024-02-14 11:48:14 +00:00
2023-01-03 17:22:08 +00:00
if let pinned {
params.append(.init(name: "pinned", value: pinned ? "true" : "false"))
}
2022-12-21 19:26:38 +00:00
return params
case let .relationships(ids):
return ids.map {
URLQueryItem(name: "id[]", value: $0)
}
case let .follow(_, notify, reblogs):
return [
.init(name: "notify", value: notify ? "true" : "false"),
.init(name: "reblogs", value: reblogs ? "true" : "false"),
]
case let .familiarFollowers(withAccount):
return [.init(name: "id[]", value: withAccount)]
2022-12-24 12:41:25 +00:00
case let .followers(_, maxId):
return makePaginationParam(sinceId: nil, maxId: maxId, mindId: nil)
2022-12-24 12:41:25 +00:00
case let .following(_, maxId):
return makePaginationParam(sinceId: nil, maxId: maxId, mindId: nil)
case let .favorites(sinceId):
guard let sinceId else { return nil }
return [.init(name: "max_id", value: sinceId)]
2023-01-09 18:26:56 +00:00
case let .bookmarks(sinceId):
guard let sinceId else { return nil }
return [.init(name: "max_id", value: sinceId)]
2022-12-18 19:30:19 +00:00
default:
return nil
}
2022-11-29 11:18:06 +00:00
}
2023-02-12 15:29:41 +00:00
public var jsonValue: Encodable? {
switch self {
case let .mute(_, json):
2023-09-16 12:15:03 +00:00
json
case let .relationshipNote(_, json):
2023-09-16 12:15:03 +00:00
json
2023-02-24 06:55:24 +00:00
case let .updateCredentials(json):
2023-09-16 12:15:03 +00:00
json
default:
2023-09-16 12:15:03 +00:00
nil
}
}
}
public struct MuteData: Encodable, Sendable {
public let duration: Int
2023-02-12 15:29:41 +00:00
public init(duration: Int) {
self.duration = duration
}
2022-11-29 11:18:06 +00:00
}
public struct RelationshipNoteData: Encodable, Sendable {
public let comment: String
2023-02-21 06:23:42 +00:00
public init(note comment: String) {
self.comment = comment
}
}
2023-02-24 06:55:24 +00:00
public struct UpdateCredentialsData: Encodable, Sendable {
public struct SourceData: Encodable, Sendable {
public let privacy: Visibility
public let sensitive: Bool
2023-02-26 05:45:57 +00:00
2023-02-24 06:55:24 +00:00
public init(privacy: Visibility, sensitive: Bool) {
self.privacy = privacy
self.sensitive = sensitive
}
}
2023-02-26 05:45:57 +00:00
2023-02-24 06:55:24 +00:00
public struct FieldData: Encodable, Sendable {
public let name: String
public let value: String
2023-02-26 05:45:57 +00:00
2023-02-24 06:55:24 +00:00
public init(name: String, value: String) {
self.name = name
self.value = value
}
}
2023-02-26 05:45:57 +00:00
2023-02-24 06:55:24 +00:00
public let displayName: String
public let note: String
public let source: SourceData
public let bot: Bool
public let locked: Bool
public let discoverable: Bool
public let fieldsAttributes: [String: FieldData]
2023-02-26 05:45:57 +00:00
2023-02-24 06:55:24 +00:00
public init(displayName: String,
note: String,
source: UpdateCredentialsData.SourceData,
bot: Bool,
locked: Bool,
discoverable: Bool,
2023-02-26 05:45:57 +00:00
fieldsAttributes: [FieldData])
{
2023-02-24 06:55:24 +00:00
self.displayName = displayName
self.note = note
self.source = source
self.bot = bot
self.locked = locked
self.discoverable = discoverable
2023-02-26 05:45:57 +00:00
2023-02-24 06:55:24 +00:00
var fieldAttributes: [String: FieldData] = [:]
2023-02-24 08:23:16 +00:00
for (index, field) in fieldsAttributes.enumerated() {
fieldAttributes[String(index)] = field
2023-02-24 06:55:24 +00:00
}
self.fieldsAttributes = fieldAttributes
}
}