Vernissage/MastodonKit/Sources/MastodonKit/MastodonClient+Account.swift

87 lines
2.9 KiB
Swift
Raw Normal View History

2023-01-04 20:56:26 +01:00
//
// https://mczachurski.dev
// Copyright © 2023 Marcin Czachurski and the repository contributors.
// Licensed under the MIT License.
//
import Foundation
2023-01-10 08:04:25 +01:00
public extension MastodonClientAuthenticated {
2023-01-04 20:56:26 +01:00
func getAccount(for accountId: String) async throws -> Account {
let request = try Self.request(
for: baseURL,
target: Mastodon.Account.account(accountId),
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
return try await downloadJson(Account.self, request: request)
2023-01-04 20:56:26 +01:00
}
2023-01-05 11:55:20 +01:00
func getRelationship(for accountId: String) async throws -> Relationship? {
let request = try Self.request(
for: baseURL,
target: Mastodon.Account.relationships([accountId]),
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
let relationships = try await downloadJson([Relationship].self, request: request)
2023-01-05 11:55:20 +01:00
return relationships.first
}
2023-01-09 08:29:55 +01:00
func getStatuses(for accountId: String,
onlyMedia: Bool = true,
excludeReplies: Bool = true,
maxId: String? = nil,
sinceId: String? = nil,
minId: String? = nil,
limit: Int = 40) async throws -> [Status] {
2023-01-05 11:55:20 +01:00
let request = try Self.request(
for: baseURL,
2023-01-09 08:29:55 +01:00
target: Mastodon.Account.statuses(accountId, onlyMedia, excludeReplies, maxId, sinceId, minId, limit),
2023-01-05 11:55:20 +01:00
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
return try await downloadJson([Status].self, request: request)
2023-01-05 11:55:20 +01:00
}
2023-01-05 14:50:48 +01:00
func follow(for accountId: String) async throws -> Relationship {
let request = try Self.request(
for: baseURL,
target: Mastodon.Account.follow(accountId),
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
return try await downloadJson(Relationship.self, request: request)
2023-01-05 14:50:48 +01:00
}
2023-01-05 21:08:19 +01:00
func unfollow(for accountId: String) async throws -> Relationship {
let request = try Self.request(
for: baseURL,
target: Mastodon.Account.unfollow(accountId),
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
return try await downloadJson(Relationship.self, request: request)
}
2023-01-05 21:08:19 +01:00
func getFollowers(for accountId: String, page: Int = 1) async throws -> [Account] {
let request = try Self.request(
for: baseURL,
target: Mastodon.Account.followers(accountId, nil, nil, nil, nil, page),
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
return try await downloadJson([Account].self, request: request)
2023-01-05 21:08:19 +01:00
}
func getFollowing(for accountId: String, page: Int = 1) async throws -> [Account] {
let request = try Self.request(
for: baseURL,
target: Mastodon.Account.following(accountId, nil, nil, nil, nil, page),
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
return try await downloadJson([Account].self, request: request)
2023-01-05 21:08:19 +01:00
}
2023-01-04 20:56:26 +01:00
}