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
|
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)
|
|
|
|
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-09 22:13:30 +00:00
|
|
|
func syncAll() async {
|
|
|
|
logger.info("Syncing groups and feeds")
|
2021-12-25 19:04:45 +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
|
|
|
|
|
|
|
let lastSync = try! await persistentContainer.lastSyncDate()
|
|
|
|
logger.info("Syncing items with last sync date: \(String(describing: lastSync), privacy: .public)")
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-08 02:58:02 +00:00
|
|
|
}
|