2023-04-25 12:48:53 +02:00
|
|
|
// Copyright © 2023 Mastodon gGmbH. All rights reserved.
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import MastodonUI
|
|
|
|
import CoreDataStack
|
|
|
|
import MastodonCore
|
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension DataSourceFacade {
|
|
|
|
static func responseToUserViewButtonAction(
|
|
|
|
dependency: NeedsDependency & AuthContextProvider,
|
|
|
|
user: ManagedObjectRecord<MastodonUser>,
|
2023-05-09 11:33:31 +02:00
|
|
|
buttonState: UserView.ButtonState
|
2023-04-25 12:48:53 +02:00
|
|
|
) async throws {
|
|
|
|
switch buttonState {
|
2023-05-09 11:33:31 +02:00
|
|
|
case .follow:
|
2023-04-25 12:48:53 +02:00
|
|
|
try await DataSourceFacade.responseToUserFollowAction(
|
|
|
|
dependency: dependency,
|
|
|
|
user: user
|
|
|
|
)
|
2023-05-09 11:36:19 +02:00
|
|
|
|
2023-05-09 11:33:31 +02:00
|
|
|
if let userObject = user.object(in: dependency.context.managedObjectContext) {
|
|
|
|
dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followingUserIds.append(userObject.id)
|
|
|
|
}
|
2023-05-17 11:35:02 +02:00
|
|
|
|
|
|
|
case .request:
|
|
|
|
try await DataSourceFacade.responseToUserFollowAction(
|
|
|
|
dependency: dependency,
|
|
|
|
user: user
|
|
|
|
)
|
|
|
|
|
|
|
|
if let userObject = user.object(in: dependency.context.managedObjectContext) {
|
|
|
|
dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followRequestedUserIDs.append(userObject.id)
|
|
|
|
}
|
|
|
|
|
2023-05-09 11:33:31 +02:00
|
|
|
case .unfollow:
|
|
|
|
try await DataSourceFacade.responseToUserFollowAction(
|
|
|
|
dependency: dependency,
|
|
|
|
user: user
|
|
|
|
)
|
|
|
|
if let userObject = user.object(in: dependency.context.managedObjectContext) {
|
|
|
|
dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followingUserIds.removeAll(where: { $0 == userObject.id })
|
|
|
|
}
|
2023-04-25 12:48:53 +02:00
|
|
|
case .blocked:
|
|
|
|
try await DataSourceFacade.responseToUserBlockAction(
|
|
|
|
dependency: dependency,
|
|
|
|
user: user
|
|
|
|
)
|
2023-05-09 11:36:19 +02:00
|
|
|
|
2023-05-09 11:33:31 +02:00
|
|
|
if let userObject = user.object(in: dependency.context.managedObjectContext) {
|
|
|
|
dependency.authContext.mastodonAuthenticationBox.inMemoryCache.blockedUserIds.append(userObject.id)
|
|
|
|
}
|
2023-05-17 11:35:02 +02:00
|
|
|
|
|
|
|
case .pending:
|
|
|
|
try await DataSourceFacade.responseToUserFollowAction(
|
|
|
|
dependency: dependency,
|
|
|
|
user: user
|
|
|
|
)
|
|
|
|
|
|
|
|
if let userObject = user.object(in: dependency.context.managedObjectContext) {
|
|
|
|
dependency.authContext.mastodonAuthenticationBox.inMemoryCache.followRequestedUserIDs.removeAll(where: { $0 == userObject.id })
|
|
|
|
}
|
2023-05-08 15:24:01 +02:00
|
|
|
case .none, .loading:
|
2023-04-25 12:48:53 +02:00
|
|
|
break //no-op
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-09 15:44:39 +02:00
|
|
|
|
|
|
|
extension UserTableViewCellDelegate where Self: NeedsDependency & AuthContextProvider {
|
|
|
|
func userView(_ view: UserView, didTapButtonWith state: UserView.ButtonState, for user: MastodonUser) {
|
|
|
|
Task {
|
|
|
|
try await DataSourceFacade.responseToUserViewButtonAction(
|
|
|
|
dependency: self,
|
|
|
|
user: user.asRecord,
|
|
|
|
buttonState: state
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|