2021-04-12 10:31:53 +02:00
|
|
|
//
|
|
|
|
// APIService+Notification.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by sxiaojian on 2021/4/13.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import CoreData
|
|
|
|
import CoreDataStack
|
2021-04-15 04:16:30 +02:00
|
|
|
import Foundation
|
2021-04-12 10:31:53 +02:00
|
|
|
import MastodonSDK
|
|
|
|
import OSLog
|
|
|
|
|
|
|
|
extension APIService {
|
|
|
|
func allNotifications(
|
|
|
|
domain: String,
|
|
|
|
query: Mastodon.API.Notifications.Query,
|
2021-04-15 04:16:30 +02:00
|
|
|
mastodonAuthenticationBox: AuthenticationService.MastodonAuthenticationBox) -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Notification]>, Error>
|
|
|
|
{
|
2021-04-12 10:31:53 +02:00
|
|
|
let authorization = mastodonAuthenticationBox.userAuthorization
|
|
|
|
return Mastodon.API.Notifications.getNotifications(
|
|
|
|
session: session,
|
|
|
|
domain: domain,
|
|
|
|
query: query,
|
|
|
|
authorization: authorization)
|
|
|
|
.flatMap { response -> AnyPublisher<Mastodon.Response.Content<[Mastodon.Entity.Notification]>, Error> in
|
|
|
|
let log = OSLog.api
|
|
|
|
return self.backgroundManagedObjectContext.performChanges {
|
|
|
|
response.value.forEach { notification in
|
2021-04-15 04:16:30 +02:00
|
|
|
let (mastodonUser, _) = APIService.CoreData.createOrMergeMastodonUser(into: self.backgroundManagedObjectContext, for: nil, in: domain, entity: notification.account, userCache: nil, networkDate: Date(), log: log)
|
2021-04-12 10:31:53 +02:00
|
|
|
var status: Status?
|
|
|
|
if let statusEntity = notification.status {
|
2021-04-15 04:16:30 +02:00
|
|
|
let (statusInCoreData, _, _) = APIService.CoreData.createOrMergeStatus(
|
2021-04-12 10:31:53 +02:00
|
|
|
into: self.backgroundManagedObjectContext,
|
|
|
|
for: nil,
|
|
|
|
domain: domain,
|
|
|
|
entity: statusEntity,
|
|
|
|
statusCache: nil,
|
|
|
|
userCache: nil,
|
|
|
|
networkDate: Date(),
|
|
|
|
log: log)
|
|
|
|
status = statusInCoreData
|
|
|
|
}
|
|
|
|
// use constrain to avoid repeated save
|
2021-04-14 11:13:23 +02:00
|
|
|
let notification = MastodonNotification.insert(into: self.backgroundManagedObjectContext, domain: domain, property: MastodonNotification.Property(id: notification.id, type: notification.type.rawValue, account: mastodonUser, status: status, createdAt: notification.createdAt))
|
2021-04-13 15:31:49 +02:00
|
|
|
os_log(.info, log: log, "%{public}s[%{public}ld], %{public}s: fetch mastodon user [%s](%s)", (#file as NSString).lastPathComponent, #line, #function, notification.type, notification.account.username)
|
2021-04-12 10:31:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
.setFailureType(to: Error.self)
|
|
|
|
.tryMap { result -> Mastodon.Response.Content<[Mastodon.Entity.Notification]> in
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
return response
|
|
|
|
case .failure(let error):
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
.eraseToAnyPublisher()
|
|
|
|
}
|
|
|
|
}
|