2021-12-08 02:58:02 +00:00
|
|
|
//
|
|
|
|
// FervorController.swift
|
|
|
|
// Reader
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 11/25/21.
|
|
|
|
//
|
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
@preconcurrency import Foundation
|
2021-12-08 02:58:02 +00:00
|
|
|
import Fervor
|
2022-03-06 20:05:33 +00:00
|
|
|
@preconcurrency import OSLog
|
2022-01-27 03:37:10 +00:00
|
|
|
import Combine
|
2021-12-08 02:58:02 +00:00
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
actor FervorController {
|
2021-12-08 02:58:02 +00:00
|
|
|
|
|
|
|
static let oauthRedirectURI = URL(string: "frenzy://oauth-callback")!
|
|
|
|
|
|
|
|
let instanceURL: URL
|
|
|
|
|
2022-01-09 22:13:30 +00:00
|
|
|
private let logger = Logger(subsystem: Bundle.main.bundleIdentifier!, category: "FervorController")
|
|
|
|
|
|
|
|
let client: FervorClient
|
2022-03-06 20:05:33 +00:00
|
|
|
nonisolated let account: LocalData.Account?
|
2021-12-08 02:58:02 +00:00
|
|
|
private(set) var clientID: String?
|
|
|
|
private(set) var clientSecret: String?
|
2022-03-06 20:34:30 +00:00
|
|
|
private(set) var token: Token?
|
2021-12-08 02:58:02 +00:00
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
nonisolated let persistentContainer: PersistentContainer!
|
2021-12-25 19:04:45 +00:00
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
nonisolated let syncState = PassthroughSubject<SyncState, Never>()
|
|
|
|
private var lastSyncState = SyncState.done
|
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2022-01-27 03:37:10 +00:00
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
init(instanceURL: URL, account: LocalData.Account?) async {
|
2021-12-08 02:58:02 +00:00
|
|
|
self.instanceURL = instanceURL
|
2022-03-06 20:34:30 +00:00
|
|
|
self.client = FervorClient(instanceURL: instanceURL, accessToken: account?.token.accessToken)
|
2022-01-12 18:48:52 +00:00
|
|
|
self.account = account
|
2022-03-06 20:05:33 +00:00
|
|
|
self.clientID = account?.clientID
|
|
|
|
self.clientSecret = account?.clientSecret
|
2021-12-25 19:04:45 +00:00
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
if let account = account {
|
|
|
|
self.persistentContainer = PersistentContainer(account: account)
|
|
|
|
} else {
|
|
|
|
self.persistentContainer = nil
|
|
|
|
}
|
2021-12-25 19:04:45 +00:00
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
persistentContainer?.fervorController = self
|
|
|
|
}
|
|
|
|
|
|
|
|
convenience init(account: LocalData.Account) async {
|
|
|
|
await self.init(instanceURL: account.instanceURL, account: account)
|
2021-12-08 02:58:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 03:37:10 +00:00
|
|
|
private func setSyncState(_ state: SyncState) {
|
2022-03-06 20:05:33 +00:00
|
|
|
lastSyncState = state
|
|
|
|
syncState.send(state)
|
2022-01-27 03:37:10 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 02:58:02 +00:00
|
|
|
func register() async throws -> ClientRegistration {
|
|
|
|
let registration = try await client.register(clientName: "Frenzy iOS", website: nil, redirectURI: FervorController.oauthRedirectURI)
|
|
|
|
clientID = registration.clientID
|
|
|
|
clientSecret = registration.clientSecret
|
|
|
|
return registration
|
|
|
|
}
|
|
|
|
|
|
|
|
func getToken(authCode: String) async throws {
|
2022-03-06 20:34:30 +00:00
|
|
|
token = try await client.token(authCode: authCode, redirectURI: FervorController.oauthRedirectURI, clientID: clientID!, clientSecret: clientSecret!)
|
2021-12-08 02:58:02 +00:00
|
|
|
}
|
|
|
|
|
2022-01-12 18:56:03 +00:00
|
|
|
func syncAll() async throws {
|
2022-03-06 20:05:33 +00:00
|
|
|
guard lastSyncState == .done else {
|
2022-01-27 04:13:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// always return to .done, even if we throw and stop syncing early
|
|
|
|
defer { setSyncState(.done) }
|
|
|
|
|
2022-01-27 03:37:10 +00:00
|
|
|
setSyncState(.groupsAndFeeds)
|
|
|
|
|
2022-01-09 22:13:30 +00:00
|
|
|
logger.info("Syncing groups and feeds")
|
2022-01-12 18:56:03 +00:00
|
|
|
async let groups = try client.groups()
|
|
|
|
async let feeds = try client.feeds()
|
|
|
|
try await persistentContainer.sync(serverGroups: groups, serverFeeds: feeds)
|
2022-01-09 22:13:30 +00:00
|
|
|
|
2022-01-27 03:37:10 +00:00
|
|
|
setSyncState(.items)
|
|
|
|
|
2022-01-12 18:56:03 +00:00
|
|
|
let lastSync = try await persistentContainer.lastSyncDate()
|
2022-01-09 22:13:30 +00:00
|
|
|
logger.info("Syncing items with last sync date: \(String(describing: lastSync), privacy: .public)")
|
2022-01-12 18:56:03 +00:00
|
|
|
let update = try await client.syncItems(lastSync: lastSync)
|
2022-01-27 03:37:10 +00:00
|
|
|
try await persistentContainer.syncItems(update, setSyncState: setSyncState(_:))
|
2022-01-12 18:56:03 +00:00
|
|
|
try await persistentContainer.updateLastSyncDate(update.syncTimestamp)
|
2022-01-27 03:37:10 +00:00
|
|
|
|
|
|
|
setSyncState(.excerpts)
|
|
|
|
await ExcerptGenerator.generateAll(self)
|
2021-12-25 19:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-03-06 20:34:30 +00:00
|
|
|
@MainActor
|
2022-01-27 04:07:35 +00:00
|
|
|
func syncReadToServer() async {
|
2022-01-11 16:54:57 +00:00
|
|
|
var count = 0
|
2022-01-12 18:56:03 +00:00
|
|
|
|
|
|
|
// try to sync items which failed last time
|
|
|
|
let req = Item.fetchRequest()
|
|
|
|
req.predicate = NSPredicate(format: "needsReadStateSync = YES")
|
2022-01-27 04:07:35 +00:00
|
|
|
if var needsSync = try? persistentContainer.viewContext.fetch(req) {
|
|
|
|
let firstReadIndex = needsSync.partition(by: \.read)
|
|
|
|
let unreadIDs = needsSync[..<firstReadIndex].map(\.id.unsafelyUnwrapped)
|
|
|
|
let readIDs = needsSync[firstReadIndex...].map(\.id.unsafelyUnwrapped)
|
|
|
|
var updatedIDs = Set<FervorID>()
|
|
|
|
if !unreadIDs.isEmpty {
|
2022-01-12 18:56:03 +00:00
|
|
|
do {
|
2022-01-27 04:07:35 +00:00
|
|
|
let ids = try await client.unread(ids: unreadIDs)
|
|
|
|
updatedIDs.formUnion(ids)
|
2022-01-12 18:56:03 +00:00
|
|
|
} catch {
|
2022-01-27 04:07:35 +00:00
|
|
|
logger.error("Failed to sync unread state: \(String(describing: error), privacy: .public)")
|
2022-01-12 18:56:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-01-27 04:07:35 +00:00
|
|
|
if !readIDs.isEmpty {
|
|
|
|
do {
|
|
|
|
let ids = try await client.read(ids: readIDs)
|
|
|
|
updatedIDs.formUnion(ids)
|
|
|
|
} catch {
|
|
|
|
logger.error("Failed to sync read state: \(String(describing: error), privacy: .public)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count += updatedIDs.count
|
|
|
|
for item in needsSync where updatedIDs.contains(item.id!) {
|
|
|
|
item.needsReadStateSync = false
|
|
|
|
}
|
2022-01-12 18:56:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 16:54:57 +00:00
|
|
|
logger.info("Synced \(count, privacy: .public) read/unread to server")
|
2022-01-12 18:56:03 +00:00
|
|
|
|
|
|
|
do {
|
|
|
|
try persistentContainer.viewContext.save()
|
|
|
|
} catch {
|
2022-01-27 02:22:15 +00:00
|
|
|
logger.error("Failed to save view context: \(String(describing: error), privacy: .public)")
|
2022-01-12 18:56:03 +00:00
|
|
|
}
|
2022-01-11 16:54:57 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 19:22:13 +00:00
|
|
|
@MainActor
|
|
|
|
func markItem(_ item: Item, read: Bool) async {
|
|
|
|
item.read = read
|
|
|
|
do {
|
2022-03-06 20:05:33 +00:00
|
|
|
let f = read ? client.read(item:) : client.unread(item:)
|
2022-01-15 19:22:13 +00:00
|
|
|
_ = try await f(item.id!)
|
|
|
|
item.needsReadStateSync = false
|
|
|
|
} catch {
|
2022-01-27 02:22:15 +00:00
|
|
|
logger.error("Failed to mark item (un)read: \(String(describing: error), privacy: .public)")
|
2022-01-15 19:22:13 +00:00
|
|
|
item.needsReadStateSync = true
|
|
|
|
}
|
|
|
|
|
2022-03-06 20:05:33 +00:00
|
|
|
do {
|
|
|
|
try self.persistentContainer.saveViewContext()
|
|
|
|
} catch {
|
|
|
|
logger.error("Failed to save view context: \(String(describing: error), privacy: .public)")
|
2022-01-15 19:22:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-08 02:58:02 +00:00
|
|
|
}
|
2022-01-27 03:37:10 +00:00
|
|
|
|
|
|
|
extension FervorController {
|
|
|
|
enum SyncState: Equatable {
|
|
|
|
case groupsAndFeeds
|
|
|
|
case items
|
|
|
|
case updateItems(current: Int, total: Int)
|
|
|
|
case excerpts
|
|
|
|
case done
|
|
|
|
}
|
|
|
|
}
|