2022-01-27 14:23:39 +01:00
|
|
|
//
|
|
|
|
// DataSourceFacade+Follow.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK on 2022-1-24.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import CoreDataStack
|
2022-06-28 13:00:39 +02:00
|
|
|
import class CoreDataStack.Notification
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2022-06-28 13:00:39 +02:00
|
|
|
import MastodonSDK
|
2022-06-30 09:02:24 +02:00
|
|
|
import MastodonLocalization
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
extension DataSourceFacade {
|
|
|
|
static func responseToUserFollowAction(
|
2022-10-09 14:07:57 +02:00
|
|
|
dependency: NeedsDependency & AuthContextProvider,
|
|
|
|
user: ManagedObjectRecord<MastodonUser>
|
2022-01-27 14:23:39 +01:00
|
|
|
) async throws {
|
2022-06-28 13:00:39 +02:00
|
|
|
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
|
2022-01-27 14:23:39 +01:00
|
|
|
await selectionFeedbackGenerator.selectionChanged()
|
|
|
|
|
|
|
|
_ = try await dependency.context.apiService.toggleFollow(
|
|
|
|
user: user,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: dependency.authContext.mastodonAuthenticationBox
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
} // end func
|
|
|
|
}
|
2022-06-28 13:00:39 +02:00
|
|
|
|
|
|
|
extension DataSourceFacade {
|
|
|
|
static func responseToUserFollowRequestAction(
|
2022-10-09 14:07:57 +02:00
|
|
|
dependency: NeedsDependency & AuthContextProvider,
|
2022-06-28 13:00:39 +02:00
|
|
|
notification: ManagedObjectRecord<Notification>,
|
2022-10-09 14:07:57 +02:00
|
|
|
query: Mastodon.API.Account.FollowReqeustQuery
|
2022-06-28 13:00:39 +02:00
|
|
|
) async throws {
|
|
|
|
let selectionFeedbackGenerator = await UISelectionFeedbackGenerator()
|
|
|
|
await selectionFeedbackGenerator.selectionChanged()
|
|
|
|
|
|
|
|
let managedObjectContext = dependency.context.managedObjectContext
|
|
|
|
let _userID: MastodonUser.ID? = try await managedObjectContext.perform {
|
|
|
|
guard let notification = notification.object(in: managedObjectContext) else { return nil }
|
|
|
|
return notification.account.id
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let userID = _userID else {
|
|
|
|
assertionFailure()
|
|
|
|
throw APIService.APIError.implicit(.badRequest)
|
|
|
|
}
|
|
|
|
|
2022-06-30 09:02:24 +02:00
|
|
|
let state: MastodonFollowRequestState = try await managedObjectContext.perform {
|
|
|
|
guard let notification = notification.object(in: managedObjectContext) else { return .init(state: .none) }
|
|
|
|
return notification.followRequestState
|
|
|
|
}
|
|
|
|
|
|
|
|
guard state.state == .none else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try? await managedObjectContext.performChanges {
|
|
|
|
guard let notification = notification.object(in: managedObjectContext) else { return }
|
|
|
|
switch query {
|
|
|
|
case .accept:
|
|
|
|
notification.transientFollowRequestState = .init(state: .isAccepting)
|
|
|
|
case .reject:
|
|
|
|
notification.transientFollowRequestState = .init(state: .isRejecting)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
_ = try await dependency.context.apiService.followRequest(
|
|
|
|
userID: userID,
|
|
|
|
query: query,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: dependency.authContext.mastodonAuthenticationBox
|
2022-06-30 09:02:24 +02:00
|
|
|
)
|
|
|
|
} catch {
|
2022-07-13 11:44:47 +02:00
|
|
|
// reset state when failure
|
2022-06-30 09:02:24 +02:00
|
|
|
try? await managedObjectContext.performChanges {
|
|
|
|
guard let notification = notification.object(in: managedObjectContext) else { return }
|
|
|
|
notification.transientFollowRequestState = .init(state: .none)
|
|
|
|
}
|
|
|
|
|
|
|
|
if let error = error as? Mastodon.API.Error {
|
|
|
|
switch error.httpResponseStatus {
|
|
|
|
case .notFound:
|
|
|
|
let backgroundManagedObjectContext = dependency.context.backgroundManagedObjectContext
|
|
|
|
try await backgroundManagedObjectContext.performChanges {
|
|
|
|
guard let notification = notification.object(in: backgroundManagedObjectContext) else { return }
|
|
|
|
for feed in notification.feeds {
|
|
|
|
backgroundManagedObjectContext.delete(feed)
|
|
|
|
}
|
|
|
|
backgroundManagedObjectContext.delete(notification)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
let alertController = await UIAlertController(for: error, title: nil, preferredStyle: .alert)
|
|
|
|
let okAction = await UIAlertAction(title: L10n.Common.Controls.Actions.ok, style: .default)
|
|
|
|
await alertController.addAction(okAction)
|
|
|
|
await dependency.coordinator.present(
|
|
|
|
scene: .alertController(alertController: alertController),
|
|
|
|
from: nil,
|
|
|
|
transition: .alertController(animated: true, completion: nil)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
try? await managedObjectContext.performChanges {
|
|
|
|
guard let notification = notification.object(in: managedObjectContext) else { return }
|
|
|
|
switch query {
|
|
|
|
case .accept:
|
|
|
|
notification.transientFollowRequestState = .init(state: .isAccept)
|
|
|
|
case .reject:
|
2022-07-13 11:44:47 +02:00
|
|
|
// do nothing due to will delete notification
|
|
|
|
break
|
2022-06-30 09:02:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let backgroundManagedObjectContext = dependency.context.backgroundManagedObjectContext
|
|
|
|
try? await backgroundManagedObjectContext.performChanges {
|
|
|
|
guard let notification = notification.object(in: backgroundManagedObjectContext) else { return }
|
|
|
|
switch query {
|
|
|
|
case .accept:
|
|
|
|
notification.followRequestState = .init(state: .isAccept)
|
|
|
|
case .reject:
|
2022-07-13 11:44:47 +02:00
|
|
|
// delete notification
|
|
|
|
for feed in notification.feeds {
|
|
|
|
backgroundManagedObjectContext.delete(feed)
|
|
|
|
}
|
|
|
|
backgroundManagedObjectContext.delete(notification)
|
2022-06-30 09:02:24 +02:00
|
|
|
}
|
|
|
|
}
|
2022-06-28 13:00:39 +02:00
|
|
|
} // end func
|
|
|
|
}
|