Kurdtvs-Live-Kurdish-TV-Kur.../Mastodon/Service/APIService/APIService+Status.swift

83 lines
2.7 KiB
Swift
Raw Normal View History

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-10 12:12:53 +01:00
func status(
statusID: Mastodon.Entity.Status.ID,
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
).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
)
}
return response
2021-03-18 10:33:07 +01:00
}
func deleteStatus(
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(
session: session,
domain: authenticationBox.domain,
query: query,
authorization: authorization
).singleOutput()
try await managedObjectContext.performChanges {
guard let status = status.object(in: managedObjectContext) else { return }
managedObjectContext.delete(status)
}
return response
}
2021-03-18 10:33:07 +01:00
}