2021-04-13 13:46:42 +02:00
|
|
|
//
|
|
|
|
// RemoteThreadViewModel.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-4-12.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import CoreDataStack
|
2022-10-08 07:43:06 +02:00
|
|
|
import MastodonCore
|
2021-04-13 13:46:42 +02:00
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
final class RemoteThreadViewModel: ThreadViewModel {
|
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
init(
|
|
|
|
context: AppContext,
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: AuthContext,
|
2022-01-27 14:23:39 +01:00
|
|
|
statusID: Mastodon.Entity.Status.ID
|
|
|
|
) {
|
|
|
|
super.init(
|
|
|
|
context: context,
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: authContext,
|
2022-01-27 14:23:39 +01:00
|
|
|
optionalRoot: nil
|
|
|
|
)
|
2021-04-13 13:46:42 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
Task { @MainActor in
|
2022-10-09 14:07:57 +02:00
|
|
|
let domain = authContext.mastodonAuthenticationBox.domain
|
2022-01-27 14:23:39 +01:00
|
|
|
let response = try await context.apiService.status(
|
|
|
|
statusID: statusID,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
|
2021-04-13 13:46:42 +02:00
|
|
|
let managedObjectContext = context.managedObjectContext
|
|
|
|
let request = Status.sortedFetchRequest
|
|
|
|
request.fetchLimit = 1
|
|
|
|
request.predicate = Status.predicate(domain: domain, id: response.value.id)
|
|
|
|
guard let status = managedObjectContext.safeFetch(request).first else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
let threadContext = StatusItem.Thread.Context(status: .init(objectID: status.objectID))
|
|
|
|
self.root = .root(context: threadContext)
|
|
|
|
|
|
|
|
} // end Task
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|
2021-04-27 11:45:11 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
init(
|
|
|
|
context: AppContext,
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: AuthContext,
|
2022-01-27 14:23:39 +01:00
|
|
|
notificationID: Mastodon.Entity.Notification.ID
|
|
|
|
) {
|
|
|
|
super.init(
|
|
|
|
context: context,
|
2022-10-09 14:07:57 +02:00
|
|
|
authContext: authContext,
|
2022-01-27 14:23:39 +01:00
|
|
|
optionalRoot: nil
|
|
|
|
)
|
2021-04-27 11:45:11 +02:00
|
|
|
|
2022-01-27 14:23:39 +01:00
|
|
|
Task { @MainActor in
|
2022-10-09 14:07:57 +02:00
|
|
|
let domain = authContext.mastodonAuthenticationBox.domain
|
2022-01-27 14:23:39 +01:00
|
|
|
let response = try await context.apiService.notification(
|
|
|
|
notificationID: notificationID,
|
2022-10-09 14:07:57 +02:00
|
|
|
authenticationBox: authContext.mastodonAuthenticationBox
|
2022-01-27 14:23:39 +01:00
|
|
|
)
|
|
|
|
|
2021-04-27 11:45:11 +02:00
|
|
|
guard let statusID = response.value.status?.id else { return }
|
|
|
|
|
|
|
|
let managedObjectContext = context.managedObjectContext
|
|
|
|
let request = Status.sortedFetchRequest
|
|
|
|
request.fetchLimit = 1
|
|
|
|
request.predicate = Status.predicate(domain: domain, id: statusID)
|
|
|
|
guard let status = managedObjectContext.safeFetch(request).first else {
|
|
|
|
assertionFailure()
|
|
|
|
return
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
let threadContext = StatusItem.Thread.Context(status: .init(objectID: status.objectID))
|
|
|
|
self.root = .root(context: threadContext)
|
|
|
|
} // end Task
|
2021-04-27 11:45:11 +02:00
|
|
|
}
|
|
|
|
|
2021-04-13 13:46:42 +02:00
|
|
|
}
|