2021-12-08 02:58:02 +00:00
|
|
|
//
|
|
|
|
// FervorController.swift
|
|
|
|
// Reader
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 11/25/21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import Fervor
|
2022-01-09 22:13:30 +00:00
|
|
|
import OSLog
|
2021-12-08 02:58:02 +00:00
|
|
|
|
|
|
|
class FervorController {
|
|
|
|
|
|
|
|
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-01-12 18:48:52 +00:00
|
|
|
private(set) var account: LocalData.Account?
|
2021-12-08 02:58:02 +00:00
|
|
|
private(set) var clientID: String?
|
|
|
|
private(set) var clientSecret: String?
|
|
|
|
private(set) var accessToken: String?
|
|
|
|
|
2021-12-25 19:04:45 +00:00
|
|
|
private(set) var persistentContainer: PersistentContainer!
|
|
|
|
|
2021-12-08 02:58:02 +00:00
|
|
|
init(instanceURL: URL) {
|
|
|
|
self.instanceURL = instanceURL
|
|
|
|
self.client = FervorClient(instanceURL: instanceURL, accessToken: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
convenience init(account: LocalData.Account) {
|
|
|
|
self.init(instanceURL: account.instanceURL)
|
2022-01-12 18:48:52 +00:00
|
|
|
self.account = account
|
2021-12-08 02:58:02 +00:00
|
|
|
self.clientID = account.clientID
|
|
|
|
self.clientSecret = account.clientSecret
|
|
|
|
self.accessToken = account.accessToken
|
2021-12-25 19:04:45 +00:00
|
|
|
|
|
|
|
self.client.accessToken = account.accessToken
|
|
|
|
|
2022-01-09 22:13:30 +00:00
|
|
|
self.persistentContainer = PersistentContainer(account: account, fervorController: self)
|
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 {
|
|
|
|
let token = try await client.token(authCode: authCode, redirectURI: FervorController.oauthRedirectURI, clientID: clientID!, clientSecret: clientSecret!)
|
|
|
|
client.accessToken = token.accessToken
|
|
|
|
accessToken = token.accessToken
|
|
|
|
}
|
|
|
|
|
2022-01-12 18:56:03 +00:00
|
|
|
func syncAll() async throws {
|
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-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)
|
|
|
|
try await persistentContainer.syncItems(update)
|
|
|
|
try await persistentContainer.updateLastSyncDate(update.syncTimestamp)
|
2021-12-25 19:04:45 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 16:54:57 +00:00
|
|
|
@MainActor
|
|
|
|
func syncReadToServer() async throws {
|
|
|
|
var count = 0
|
2022-01-12 18:56:03 +00:00
|
|
|
// todo: there should be a batch update api endpoint
|
|
|
|
for case let item as Item in persistentContainer.viewContext.updatedObjects {
|
2022-01-11 16:54:57 +00:00
|
|
|
let f = item.read ? client.read(item:) : client.unread(item:)
|
2022-01-12 18:56:03 +00:00
|
|
|
do {
|
|
|
|
let _ = try await f(item.id!)
|
|
|
|
count += 1
|
|
|
|
} catch {
|
|
|
|
logger.error("Failed to sync read state: \(error.localizedDescription, privacy: .public)")
|
|
|
|
item.needsReadStateSync = true
|
|
|
|
}
|
2022-01-11 16:54:57 +00:00
|
|
|
}
|
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")
|
|
|
|
if let needsSync = try? persistentContainer.viewContext.fetch(req) {
|
|
|
|
for item in needsSync {
|
|
|
|
let f = item.read ? client.read(item:) : client.unread(item:)
|
|
|
|
do {
|
|
|
|
let _ = try await f(item.id!)
|
|
|
|
count += 1
|
|
|
|
item.needsReadStateSync = false
|
|
|
|
} catch {
|
|
|
|
logger.error("Failed to sync read state again: \(error.localizedDescription, privacy: .public)")
|
|
|
|
item.needsReadStateSync = true
|
|
|
|
// todo: this should probably fail after a certain number of attempts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
logger.error("Failed to save view context: \(error.localizedDescription, privacy: .public)")
|
|
|
|
}
|
2022-01-11 16:54:57 +00:00
|
|
|
}
|
|
|
|
|
2021-12-08 02:58:02 +00:00
|
|
|
}
|