2021-03-18 10:33:07 +01:00
|
|
|
//
|
|
|
|
// APIService+Status.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
2021-03-10 12:12:53 +01:00
|
|
|
// Created by MainasuK Cirno on 2021-3-10.
|
2021-03-18 10:33:07 +01:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Combine
|
2021-03-10 12:12:53 +01:00
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
|
|
|
import CommonOSLog
|
|
|
|
import DateToolsSwift
|
2021-03-18 10:33:07 +01:00
|
|
|
import MastodonSDK
|
|
|
|
|
|
|
|
extension APIService {
|
2021-03-22 10:55:55 +01:00
|
|
|
|
2021-03-10 12:12:53 +01:00
|
|
|
func status(
|
|
|
|
statusID: Mastodon.Entity.Status.ID,
|
2022-01-27 14:23:39 +01:00
|
|
|
authenticationBox: MastodonAuthenticationBox
|
|
|
|
) async throws -> Mastodon.Response.Content<Mastodon.Entity.Status> {
|
|
|
|
let domain = authenticationBox.domain
|
|
|
|
let authorization = authenticationBox.userAuthorization
|
|
|
|
|
|
|
|
let response = try await Mastodon.API.Statuses.status(
|
2021-03-10 12:12:53 +01:00
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
statusID: statusID,
|
|
|
|
authorization: authorization
|
2022-01-27 14:23:39 +01:00
|
|
|
).singleOutput()
|
|
|
|
|
|
|
|
let managedObjectContext = self.backgroundManagedObjectContext
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
let me = authenticationBox.authenticationRecord.object(in: managedObjectContext)?.user
|
|
|
|
_ = Persistence.Status.createOrMerge(
|
|
|
|
in: managedObjectContext,
|
|
|
|
context: Persistence.Status.PersistContext(
|
|
|
|
domain: domain,
|
|
|
|
entity: response.value,
|
|
|
|
me: me,
|
|
|
|
statusCache: nil,
|
|
|
|
userCache: nil,
|
|
|
|
networkDate: response.networkDate
|
|
|
|
)
|
2021-03-10 12:12:53 +01:00
|
|
|
)
|
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
return response
|
2021-03-18 10:33:07 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 10:08:07 +02:00
|
|
|
func deleteStatus(
|
2022-01-27 14:23:39 +01:00
|
|
|
status: ManagedObjectRecord<Status>,
|
|
|
|
authenticationBox: MastodonAuthenticationBox
|
|
|
|
) async throws -> Mastodon.Response.Content<Mastodon.Entity.Status> {
|
|
|
|
let authorization = authenticationBox.userAuthorization
|
|
|
|
|
|
|
|
let managedObjectContext = backgroundManagedObjectContext
|
|
|
|
let _query: Mastodon.API.Statuses.DeleteStatusQuery? = try? await managedObjectContext.perform {
|
|
|
|
guard let _status = status.object(in: managedObjectContext) else { return nil }
|
|
|
|
let status = _status.reblog ?? _status
|
|
|
|
return Mastodon.API.Statuses.DeleteStatusQuery(id: status.id)
|
|
|
|
}
|
|
|
|
guard let query = _query else {
|
|
|
|
throw APIError.implicit(.badRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
let response = try await Mastodon.API.Statuses.deleteStatus(
|
2021-05-07 10:08:07 +02:00
|
|
|
session: session,
|
2022-01-27 14:23:39 +01:00
|
|
|
domain: authenticationBox.domain,
|
2021-05-07 10:08:07 +02:00
|
|
|
query: query,
|
|
|
|
authorization: authorization
|
2022-01-27 14:23:39 +01:00
|
|
|
).singleOutput()
|
|
|
|
|
|
|
|
try await managedObjectContext.performChanges {
|
|
|
|
guard let status = status.object(in: managedObjectContext) else { return }
|
|
|
|
managedObjectContext.delete(status)
|
2021-05-07 10:08:07 +02:00
|
|
|
}
|
2022-01-27 14:23:39 +01:00
|
|
|
|
|
|
|
return response
|
2021-05-07 10:08:07 +02:00
|
|
|
}
|
|
|
|
|
2021-03-18 10:33:07 +01:00
|
|
|
}
|