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

142 lines
4.8 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)
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
2023-01-10 07:24:05 +00:00
case updateCredentials(displayName: String,
note: String,
privacy: Visibility,
isSensitive: Bool,
isBot: Bool,
isLocked: Bool,
isDiscoverable: Bool)
2023-01-03 17:22:08 +00:00
case statuses(id: String,
sinceId: String?,
tag: String?,
onlyMedia: Bool?,
excludeReplies: Bool?,
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)
case unmute(id: String)
2023-01-17 10:36:01 +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):
2022-11-29 11:18:06 +00:00
return "accounts/\(id)"
case .favorites:
2023-01-24 06:19:09 +00:00
return "favourites"
2023-01-09 18:26:56 +00:00
case .bookmarks:
return "bookmarks"
case .followedTags:
return "followed_tags"
2023-01-17 10:36:01 +00:00
case let .featuredTags(id):
2022-12-21 19:26:38 +00:00
return "accounts/\(id)/featured_tags"
2022-12-01 08:05:26 +00:00
case .verifyCredentials:
return "accounts/verify_credentials"
2023-01-10 07:24:05 +00:00
case .updateCredentials:
return "accounts/update_credentials"
2023-01-17 10:36:01 +00:00
case let .statuses(id, _, _, _, _, _):
2022-12-18 19:30:19 +00:00
return "accounts/\(id)/statuses"
2022-12-20 16:11:12 +00:00
case .relationships:
return "accounts/relationships"
case let .follow(id, _, _):
2022-12-20 16:11:12 +00:00
return "accounts/\(id)/follow"
2023-01-17 10:36:01 +00:00
case let .unfollow(id):
2022-12-20 16:11:12 +00:00
return "accounts/\(id)/unfollow"
case .familiarFollowers:
return "accounts/familiar_followers"
case .suggestions:
return "suggestions"
2023-01-17 10:36:01 +00:00
case let .following(id, _):
2022-12-23 17:47:19 +00:00
return "accounts/\(id)/following"
2023-01-17 10:36:01 +00:00
case let .followers(id, _):
2022-12-23 17:47:19 +00:00
return "accounts/\(id)/followers"
2023-01-17 10:36:01 +00:00
case let .lists(id):
return "accounts/\(id)/lists"
2023-01-09 18:47:54 +00:00
case .preferences:
return "preferences"
case let .block(id):
return "accounts/\(id)/block"
case let .unblock(id):
return "accounts/\(id)/unblock"
case let .mute(id):
return "accounts/\(id)/mute"
case let .unmute(id):
return "accounts/\(id)/unmute"
2022-11-29 11:18:06 +00:00
}
}
2023-01-17 10:36:01 +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-01-17 10:36:01 +00:00
case let .statuses(_, sinceId, tag, onlyMedia, excludeReplies, 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))
}
if let onlyMedia {
params.append(.init(name: "only_media", value: onlyMedia ? "true" : "false"))
}
if let excludeReplies {
params.append(.init(name: "exclude_replies", value: excludeReplies ? "true" : "false"))
}
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)]
2023-01-10 07:24:05 +00:00
case let .updateCredentials(displayName, note, privacy,
isSensitive, isBot, isLocked, isDiscoverable):
var params: [URLQueryItem] = []
params.append(.init(name: "display_name", value: displayName))
params.append(.init(name: "note", value: note))
params.append(.init(name: "source[privacy]", value: privacy.rawValue))
params.append(.init(name: "source[sensitive]", value: isSensitive ? "true" : "false"))
params.append(.init(name: "bot", value: isBot ? "true" : "false"))
params.append(.init(name: "locked", value: isLocked ? "true" : "false"))
params.append(.init(name: "discoverable", value: isDiscoverable ? "true" : "false"))
return params
2022-12-18 19:30:19 +00:00
default:
return nil
}
2022-11-29 11:18:06 +00:00
}
}