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(
|
|
|
|
domain: String,
|
|
|
|
statusID: Mastodon.Entity.Status.ID,
|
2021-07-20 10:40:04 +02:00
|
|
|
authorizationBox: MastodonAuthenticationBox
|
2021-03-10 12:12:53 +01:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> {
|
|
|
|
let authorization = authorizationBox.userAuthorization
|
|
|
|
return Mastodon.API.Statuses.status(
|
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
statusID: statusID,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> in
|
2021-03-17 04:33:25 +01:00
|
|
|
return APIService.Persist.persistStatus(
|
2021-03-10 12:12:53 +01:00
|
|
|
managedObjectContext: self.backgroundManagedObjectContext,
|
|
|
|
domain: domain,
|
|
|
|
query: nil,
|
|
|
|
response: response.map { [$0] },
|
|
|
|
persistType: .lookUp,
|
|
|
|
requestMastodonUserID: nil,
|
|
|
|
log: OSLog.api
|
|
|
|
)
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.tryMap { result -> Mastodon.Response.Content<Mastodon.Entity.Status> in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
return response
|
|
|
|
case .failure(let error):
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
2021-03-18 10:33:07 +01:00
|
|
|
}
|
|
|
|
|
2021-05-07 10:08:07 +02:00
|
|
|
func deleteStatus(
|
|
|
|
domain: String,
|
|
|
|
statusID: Mastodon.Entity.Status.ID,
|
2021-07-20 10:40:04 +02:00
|
|
|
authorizationBox: MastodonAuthenticationBox
|
2021-05-07 10:08:07 +02:00
|
|
|
) -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> {
|
|
|
|
let authorization = authorizationBox.userAuthorization
|
|
|
|
let query = Mastodon.API.Statuses.DeleteStatusQuery(id: statusID)
|
|
|
|
return Mastodon.API.Statuses.deleteStatus(
|
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
query: query,
|
|
|
|
authorization: authorization
|
|
|
|
)
|
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<Mastodon.Entity.Status>, Error> in
|
|
|
|
return self.backgroundManagedObjectContext.performChanges{
|
|
|
|
// fetch old Status
|
|
|
|
let oldStatus: Status? = {
|
|
|
|
let request = Status.sortedFetchRequest
|
|
|
|
request.predicate = Status.predicate(domain: domain, id: response.value.id)
|
|
|
|
request.fetchLimit = 1
|
|
|
|
request.returnsObjectsAsFaults = false
|
|
|
|
do {
|
|
|
|
return try self.backgroundManagedObjectContext.fetch(request).first
|
|
|
|
} catch {
|
|
|
|
assertionFailure(error.localizedDescription)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
if let status = oldStatus {
|
2021-06-21 10:37:28 +02:00
|
|
|
let homeTimelineIndexes = status.homeTimelineIndexes ?? Set()
|
|
|
|
for homeTimelineIndex in homeTimelineIndexes {
|
|
|
|
self.backgroundManagedObjectContext.delete(homeTimelineIndex)
|
|
|
|
}
|
|
|
|
let inNotifications = status.inNotifications ?? Set()
|
|
|
|
for notification in inNotifications {
|
|
|
|
self.backgroundManagedObjectContext.delete(notification)
|
|
|
|
}
|
2021-05-07 10:08:07 +02:00
|
|
|
self.backgroundManagedObjectContext.delete(status)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.tryMap { result -> Mastodon.Response.Content<Mastodon.Entity.Status> in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
return response
|
|
|
|
case .failure(let error):
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
|
2021-03-18 10:33:07 +01:00
|
|
|
}
|