From 945f05703bd2983e9605e8a57d1ae84287c1c7f3 Mon Sep 17 00:00:00 2001 From: CMK Date: Fri, 13 May 2022 18:39:18 +0800 Subject: [PATCH] feat: add familiar followers endpoint --- .../APIService/APIService+Recommend.swift | 33 +++++++++ ...stodon+API+Account+FamiliarFollowers.swift | 71 +++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FamiliarFollowers.swift diff --git a/Mastodon/Service/APIService/APIService+Recommend.swift b/Mastodon/Service/APIService/APIService+Recommend.swift index 6e457ad0..87471250 100644 --- a/Mastodon/Service/APIService/APIService+Recommend.swift +++ b/Mastodon/Service/APIService/APIService+Recommend.swift @@ -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 + } + +} diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FamiliarFollowers.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FamiliarFollowers.swift new file mode 100644 index 00000000..a1f0b4f6 --- /dev/null +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FamiliarFollowers.swift @@ -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, 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 + } + } + +}