feat: add familiar followers endpoint

This commit is contained in:
CMK 2022-05-13 18:39:18 +08:00
parent 5611c67479
commit 945f05703b
2 changed files with 104 additions and 0 deletions

View File

@ -74,3 +74,36 @@ extension APIService {
}
}
extension APIService {
func familiarFollowers(
query: Mastodon.API.Account.FamiliarFollowersQuery,
authenticationBox: MastodonAuthenticationBox
) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Account]> {
let response = try await Mastodon.API.Account.familiarFollowers(
session: session,
domain: authenticationBox.domain,
query: query,
authorization: authenticationBox.userAuthorization
).singleOutput()
// let managedObjectContext = backgroundManagedObjectContext
// try await managedObjectContext.performChanges {
// for entity in response.value {
// _ = Persistence.MastodonUser.createOrMerge(
// in: managedObjectContext,
// context: Persistence.MastodonUser.PersistContext(
// domain: authenticationBox.domain,
// entity: entity.account,
// cache: nil,
// networkDate: response.networkDate
// )
// )
// } // end for in
// }
return response
}
}

View File

@ -0,0 +1,71 @@
//
// Mastodon+API+Account+FamiliarFollowers.swift
//
//
// Created by MainasuK on 2022-5-13.
//
import Foundation
import Combine
// https://github.com/mastodon/mastodon/pull/17700
extension Mastodon.API.Account {
private static func familiarFollowersEndpointURL(domain: String) -> URL {
return Mastodon.API.endpointURL(domain: domain)
.appendingPathComponent("accounts")
.appendingPathComponent("familiar_followers")
}
/// Fetch familiar followers
///
/// - Since: 3.5.?
/// - Version: 3.5.2
/// # Last Update
/// 2022/5/13
/// # Reference
/// [Document](none)
/// - Parameters:
/// - session: `URLSession`
/// - domain: Mastodon instance domain. e.g. "example.com"
/// - query: `FamiliarFollowersQuery`
/// - authorization: User token
/// - Returns: `AnyPublisher` contains `[Mastodon.Entity.Account]` nested in the response
public static func familiarFollowers(
session: URLSession,
domain: String,
query: FamiliarFollowersQuery,
authorization: Mastodon.API.OAuth.Authorization
) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Account]>, Error> {
let request = Mastodon.API.get(
url: familiarFollowersEndpointURL(domain: domain),
query: query,
authorization: authorization
)
return session.dataTaskPublisher(for: request)
.tryMap { data, response in
let value = try Mastodon.API.decode(type: [Mastodon.Entity.Account].self, from: data, response: response)
return Mastodon.Response.Content(value: value, response: response)
}
.eraseToAnyPublisher()
}
public struct FamiliarFollowersQuery: GetQuery {
public let accounts: [Mastodon.Entity.Account.ID]
public init(accounts: [Mastodon.Entity.Account.ID]) {
self.accounts = accounts
}
var queryItems: [URLQueryItem]? {
var items: [URLQueryItem] = []
let accountsValue = accounts.joined(separator: ",")
if !accountsValue.isEmpty {
items.append(URLQueryItem(name: "accounts", value: accountsValue))
}
guard !items.isEmpty else { return nil }
return items
}
}
}