Vernissage/PixelfedKit/Sources/PixelfedKit/PixelfedClient+Account.swift

183 lines
6.3 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.
2023-03-28 10:35:38 +02:00
// Licensed under the Apache License 2.0.
2023-01-04 20:56:26 +01:00
//
import Foundation
2023-02-19 10:43:37 +01:00
public extension PixelfedClientAuthenticated {
2023-01-18 18:41:42 +01:00
func verifyCredentials() async throws -> Account {
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.verifyCredentials,
2023-01-18 18:41:42 +01:00
withBearerToken: token
)
return try await downloadJson(Account.self, request: request)
}
2023-01-22 21:44:07 +01:00
func account(for accountId: String) async throws -> Account {
2023-01-04 20:56:26 +01:00
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.account(accountId),
2023-01-04 20:56:26 +01:00
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
2023-01-22 21:44:07 +01:00
func relationships(for accountId: String) async throws -> Relationship? {
2023-01-05 11:55:20 +01:00
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.relationships([accountId]),
2023-01-05 11:55:20 +01:00
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-22 21:44:07 +01:00
func statuses(for accountId: String,
2023-01-09 08:29:55 +01:00
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-02-19 10:43:37 +01:00
target: Pixelfed.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,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.follow(accountId),
2023-01-05 14:50:48 +01:00
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,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.unfollow(accountId),
withBearerToken: token
)
2023-01-15 12:41:55 +01:00
return try await downloadJson(Relationship.self, request: request)
}
2023-01-22 15:56:35 +01:00
func mute(for accountId: String) async throws -> Relationship {
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.mute(accountId),
2023-01-22 15:56:35 +01:00
withBearerToken: token
)
return try await downloadJson(Relationship.self, request: request)
}
func unmute(for accountId: String) async throws -> Relationship {
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.unmute(accountId),
2023-01-22 15:56:35 +01:00
withBearerToken: token
)
return try await downloadJson(Relationship.self, request: request)
}
func block(for accountId: String) async throws -> Relationship {
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.block(accountId),
2023-01-22 15:56:35 +01:00
withBearerToken: token
)
return try await downloadJson(Relationship.self, request: request)
}
func unblock(for accountId: String) async throws -> Relationship {
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.unblock(accountId),
2023-01-22 15:56:35 +01:00
withBearerToken: token
)
return try await downloadJson(Relationship.self, request: request)
}
2023-01-22 21:44:07 +01:00
func followers(for accountId: String, page: Int = 1) async throws -> [Account] {
2023-01-05 21:08:19 +01:00
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.followers(accountId, nil, nil, nil, nil, page),
2023-01-05 21:08:19 +01:00
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-22 21:44:07 +01:00
func following(for accountId: String, page: Int = 1) async throws -> [Account] {
2023-01-05 21:08:19 +01:00
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Account.following(accountId, nil, nil, nil, nil, page),
2023-01-05 21:08:19 +01:00
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-23 08:43:04 +01:00
func favourites(maxId: EntityId? = nil,
2023-02-01 20:01:18 +01:00
sinceId: EntityId? = nil,
minId: EntityId? = nil,
limit: Int? = nil,
page: Page? = nil) async throws -> [Status] {
2023-01-23 08:43:04 +01:00
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Favourites.favourites(maxId, sinceId, minId, limit, page),
2023-01-23 08:43:04 +01:00
withBearerToken: token
)
return try await downloadJson([Status].self, request: request)
}
func bookmarks(maxId: EntityId? = nil,
sinceId: EntityId? = nil,
minId: EntityId? = nil,
2023-02-01 20:01:18 +01:00
limit: Int? = nil,
page: Page? = nil) async throws -> [Status] {
2023-01-23 08:43:04 +01:00
let request = try Self.request(
for: baseURL,
2023-02-19 10:43:37 +01:00
target: Pixelfed.Bookmarks.bookmarks(maxId, sinceId, minId, limit, page),
2023-01-23 08:43:04 +01:00
withBearerToken: token
)
return try await downloadJson([Status].self, request: request)
}
2023-03-24 17:45:27 +01:00
2023-03-25 17:45:50 +01:00
func update(displayName: String, bio: String, website: String, locked: Bool, image: Data?) async throws -> Account {
2023-03-24 17:45:27 +01:00
let request = try Self.request(
for: baseURL,
2023-03-25 17:45:50 +01:00
target: Pixelfed.Account.updateCredentials(displayName, bio, website, locked, image),
2023-03-24 17:45:27 +01:00
withBearerToken: token)
return try await downloadJson(Account.self, request: request)
}
2023-03-25 17:01:28 +01:00
func avatar(image: Data?) async throws -> Account {
let request = try Self.request(
for: baseURL,
target: Pixelfed.Account.updateAvatar(image),
withBearerToken: token)
return try await downloadJson(Account.self, request: request)
}
2023-01-04 20:56:26 +01:00
}