From 1a1b2d44a4ce35d2cb4697f126920ea1882d4cd7 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Mon, 22 May 2023 14:11:57 +0200 Subject: [PATCH] Download (and cache) requested follows (IOS-157) Thanks to @kimar for pointing this out! --- .../Provider/DataSourceFacade+Follow.swift | 2 +- .../API/APIService+FollowRequest.swift | 15 ++++++- .../Service/AuthenticationService.swift | 7 ++- .../Mastodon+API+Account+FollowRequest.swift | 45 +++++++++++++++++-- 4 files changed, 62 insertions(+), 7 deletions(-) diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift index 2c221bc9b..88503ae7b 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Follow.swift @@ -32,7 +32,7 @@ extension DataSourceFacade { static func responseToUserFollowRequestAction( dependency: NeedsDependency & AuthContextProvider, notification: ManagedObjectRecord, - query: Mastodon.API.Account.FollowReqeustQuery + query: Mastodon.API.Account.FollowRequestQuery ) async throws { let selectionFeedbackGenerator = await UISelectionFeedbackGenerator() await selectionFeedbackGenerator.selectionChanged() diff --git a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+FollowRequest.swift b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+FollowRequest.swift index f0f0bfb7b..74650131d 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/API/APIService+FollowRequest.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/API/APIService+FollowRequest.swift @@ -16,7 +16,7 @@ extension APIService { public func followRequest( userID: Mastodon.Entity.Account.ID, - query: Mastodon.API.Account.FollowReqeustQuery, + query: Mastodon.API.Account.FollowRequestQuery, authenticationBox: MastodonAuthenticationBox ) async throws -> Mastodon.Response.Content { let response = try await Mastodon.API.Account.followRequest( @@ -51,4 +51,17 @@ extension APIService { return response } + public func pendingFollowRequest( + userID: Mastodon.Entity.Account.ID, + authenticationBox: MastodonAuthenticationBox + ) async throws -> Mastodon.Response.Content<[Mastodon.Entity.Account]> { + let response = try await Mastodon.API.Account.pendingFollowRequest( + session: session, + domain: authenticationBox.domain, + userID: userID, + authorization: authenticationBox.userAuthorization + ).singleOutput() + + return response + } } diff --git a/MastodonSDK/Sources/MastodonCore/Service/AuthenticationService.swift b/MastodonSDK/Sources/MastodonCore/Service/AuthenticationService.swift index 933fd1c6c..8faac74d9 100644 --- a/MastodonSDK/Sources/MastodonCore/Service/AuthenticationService.swift +++ b/MastodonSDK/Sources/MastodonCore/Service/AuthenticationService.swift @@ -45,7 +45,12 @@ public final class AuthenticationService: NSObject { let blockedIds = try await apiService.getBlocked( authenticationBox: authBox ).value.map { $0.id } - + + let followRequestIds = try await apiService.pendingFollowRequest(userID: authBox.userID, + authenticationBox: authBox) + .value.map { $0.id } + + authBox.inMemoryCache.followRequestedUserIDs = followRequestIds authBox.inMemoryCache.followingUserIds = followingIds authBox.inMemoryCache.blockedUserIds = blockedIds } diff --git a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FollowRequest.swift b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FollowRequest.swift index ddde4ffdc..fdf091c86 100644 --- a/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FollowRequest.swift +++ b/MastodonSDK/Sources/MastodonSDK/API/Mastodon+API+Account+FollowRequest.swift @@ -10,6 +10,11 @@ import Combine // MARK: - Account credentials extension Mastodon.API.Account { + + static func pendingFollowRequestEndpointURL(domain: String) -> URL { + return Mastodon.API.endpointURL(domain: domain) + .appendingPathComponent("follow_requests") + } static func acceptFollowRequestEndpointURL(domain: String, userID: Mastodon.Entity.Account.ID) -> URL { return Mastodon.API.endpointURL(domain: domain) @@ -25,7 +30,7 @@ extension Mastodon.API.Account { .appendingPathComponent("reject") } - /// Accept Follow + /// Pending Follow Requests /// /// /// - Since: 0.0.0 @@ -37,6 +42,38 @@ extension Mastodon.API.Account { /// - domain: Mastodon instance domain. e.g. "example.com" /// - userID: ID of the account in the database /// - authorization: User token + /// - Returns: `AnyPublisher` contains `[Account]` nested in the response + public static func pendingFollowRequest( + session: URLSession, + domain: String, + userID: Mastodon.Entity.Account.ID, + authorization: Mastodon.API.OAuth.Authorization + ) -> AnyPublisher, Error> { + let request = Mastodon.API.get( + url: pendingFollowRequestEndpointURL(domain: domain), + 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() + } + + + /// Accept Follow + /// + /// + /// - Since: 0.0.0 + /// - Version: 3.3.0 + /// # Reference + /// [Document](https://docs.joinmastodon.org/methods/accounts/follow_requests/#allow) + /// - Parameters: + /// - session: `URLSession` + /// - domain: Mastodon instance domain. e.g. "example.com" + /// - userID: ID of the account in the database + /// - authorization: User token /// - Returns: `AnyPublisher` contains `Relationship` nested in the response public static func acceptFollowRequest( session: URLSession, @@ -63,7 +100,7 @@ extension Mastodon.API.Account { /// - Since: 0.0.0 /// - Version: 3.3.0 /// # Reference - /// [Document](https://docs.joinmastodon.org/methods/accounts/follow_requests/) + /// [Document](https://docs.joinmastodon.org/methods/accounts/follow_requests/#reject) /// - Parameters: /// - session: `URLSession` /// - domain: Mastodon instance domain. e.g. "example.com" @@ -92,7 +129,7 @@ extension Mastodon.API.Account { extension Mastodon.API.Account { - public enum FollowReqeustQuery { + public enum FollowRequestQuery { case accept case reject } @@ -101,7 +138,7 @@ extension Mastodon.API.Account { session: URLSession, domain: String, userID: Mastodon.Entity.Account.ID, - query: FollowReqeustQuery, + query: FollowRequestQuery, authorization: Mastodon.API.OAuth.Authorization ) -> AnyPublisher, Error> { switch query {