Download (and cache) requested follows (IOS-157)

Thanks to @kimar for pointing this out!
This commit is contained in:
Nathan Mattes 2023-05-22 14:11:57 +02:00
parent a1315b9006
commit 1a1b2d44a4
4 changed files with 62 additions and 7 deletions

View File

@ -32,7 +32,7 @@ extension DataSourceFacade {
static func responseToUserFollowRequestAction(
dependency: NeedsDependency & AuthContextProvider,
notification: ManagedObjectRecord<Notification>,
query: Mastodon.API.Account.FollowReqeustQuery
query: Mastodon.API.Account.FollowRequestQuery
) async throws {
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
await selectionFeedbackGenerator.selectionChanged()

View File

@ -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<Mastodon.Entity.Relationship> {
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
}
}

View File

@ -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
}

View File

@ -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<Mastodon.Response.Content<[Mastodon.Entity.Account]>, 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<Mastodon.Response.Content<Mastodon.Entity.Relationship>, Error> {
switch query {