Create addNewFeed function to replace FeedlyAddNewFeedOperation.

This commit is contained in:
Brent Simmons 2024-04-28 22:03:51 -07:00
parent 2cde31a523
commit e85e449dcc

View File

@ -29,34 +29,34 @@ final class FeedlyAccountDelegate: AccountDelegate {
// TODO: Kiel, if you decide not to support OPML import you will have to disallow it in the behaviors // TODO: Kiel, if you decide not to support OPML import you will have to disallow it in the behaviors
// See https://developer.feedly.com/v3/opml/ // See https://developer.feedly.com/v3/opml/
var behaviors: AccountBehaviors = [.disallowFeedInRootFolder, .disallowMarkAsUnreadAfterPeriod(31)] var behaviors: AccountBehaviors = [.disallowFeedInRootFolder, .disallowMarkAsUnreadAfterPeriod(31)]
let isOPMLImportSupported = false let isOPMLImportSupported = false
var isOPMLImportInProgress = false var isOPMLImportInProgress = false
var server: String? { var server: String? {
return caller.server return caller.server
} }
var credentials: Credentials? { var credentials: Credentials? {
didSet { didSet {
#if DEBUG #if DEBUG
// https://developer.feedly.com/v3/developer/ // https://developer.feedly.com/v3/developer/
if let devToken = ProcessInfo.processInfo.environment["FEEDLY_DEV_ACCESS_TOKEN"], !devToken.isEmpty { if let devToken = ProcessInfo.processInfo.environment["FEEDLY_DEV_ACCESS_TOKEN"], !devToken.isEmpty {
caller.credentials = Credentials(type: .oauthAccessToken, username: "Developer", secret: devToken) caller.credentials = Credentials(type: .oauthAccessToken, username: "Developer", secret: devToken)
return return
} }
#endif #endif
caller.credentials = credentials caller.credentials = credentials
} }
} }
let oauthAuthorizationClient: OAuthAuthorizationClient let oauthAuthorizationClient: OAuthAuthorizationClient
var accountMetadata: AccountMetadata? var accountMetadata: AccountMetadata?
var refreshProgress = DownloadProgress(numberOfTasks: 0) var refreshProgress = DownloadProgress(numberOfTasks: 0)
private var userID: String? { private var userID: String? {
credentials?.username credentials?.username
} }
@ -66,7 +66,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
private weak var account: Account? private weak var account: Account?
internal let caller: FeedlyAPICaller internal let caller: FeedlyAPICaller
private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Feedly") private let log = OSLog(subsystem: Bundle.main.bundleIdentifier!, category: "Feedly")
private let syncDatabase: SyncDatabase private let syncDatabase: SyncDatabase
@ -78,12 +78,12 @@ final class FeedlyAccountDelegate: AccountDelegate {
// Making this a serial queue at this higher level of abstraction means we can ensure, // Making this a serial queue at this higher level of abstraction means we can ensure,
// for example, a `FeedlyRefreshAccessTokenOperation` occurs before a `FeedlySyncAllOperation`, // for example, a `FeedlyRefreshAccessTokenOperation` occurs before a `FeedlySyncAllOperation`,
// improving our ability to debug, reason about and predict the behaviour of the code. // improving our ability to debug, reason about and predict the behaviour of the code.
if let transport = transport { if let transport = transport {
self.caller = FeedlyAPICaller(transport: transport, api: api, secretsProvider: secretsProvider) self.caller = FeedlyAPICaller(transport: transport, api: api, secretsProvider: secretsProvider)
} else { } else {
let sessionConfiguration = URLSessionConfiguration.default let sessionConfiguration = URLSessionConfiguration.default
sessionConfiguration.requestCachePolicy = .reloadIgnoringLocalCacheData sessionConfiguration.requestCachePolicy = .reloadIgnoringLocalCacheData
sessionConfiguration.timeoutIntervalForRequest = 60.0 sessionConfiguration.timeoutIntervalForRequest = 60.0
@ -92,24 +92,24 @@ final class FeedlyAccountDelegate: AccountDelegate {
sessionConfiguration.httpMaximumConnectionsPerHost = 1 sessionConfiguration.httpMaximumConnectionsPerHost = 1
sessionConfiguration.httpCookieStorage = nil sessionConfiguration.httpCookieStorage = nil
sessionConfiguration.urlCache = nil sessionConfiguration.urlCache = nil
if let userAgentHeaders = UserAgent.headers() { if let userAgentHeaders = UserAgent.headers() {
sessionConfiguration.httpAdditionalHeaders = userAgentHeaders sessionConfiguration.httpAdditionalHeaders = userAgentHeaders
} }
let session = URLSession(configuration: sessionConfiguration) let session = URLSession(configuration: sessionConfiguration)
self.caller = FeedlyAPICaller(transport: session, api: api, secretsProvider: secretsProvider) self.caller = FeedlyAPICaller(transport: session, api: api, secretsProvider: secretsProvider)
} }
let databasePath = (dataFolder as NSString).appendingPathComponent("Sync.sqlite3") let databasePath = (dataFolder as NSString).appendingPathComponent("Sync.sqlite3")
self.syncDatabase = SyncDatabase(databasePath: databasePath) self.syncDatabase = SyncDatabase(databasePath: databasePath)
self.oauthAuthorizationClient = api.oauthAuthorizationClient(secretsProvider: secretsProvider) self.oauthAuthorizationClient = api.oauthAuthorizationClient(secretsProvider: secretsProvider)
self.caller.delegate = self self.caller.delegate = self
} }
// MARK: Account API // MARK: Account API
func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async { func receiveRemoteNotification(for account: Account, userInfo: [AnyHashable : Any]) async {
} }
@ -129,41 +129,41 @@ final class FeedlyAccountDelegate: AccountDelegate {
private func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) { private func refreshAll(for account: Account, completion: @escaping (Result<Void, Error>) -> Void) {
assert(Thread.isMainThread) assert(Thread.isMainThread)
guard currentSyncAllOperation == nil else { guard currentSyncAllOperation == nil else {
os_log(.debug, log: log, "Ignoring refreshAll: Feedly sync already in progress.") os_log(.debug, log: log, "Ignoring refreshAll: Feedly sync already in progress.")
completion(.success(())) completion(.success(()))
return return
} }
guard let credentials = credentials else { guard let credentials = credentials else {
os_log(.debug, log: log, "Ignoring refreshAll: Feedly account has no credentials.") os_log(.debug, log: log, "Ignoring refreshAll: Feedly account has no credentials.")
completion(.failure(FeedlyAccountDelegateError.notLoggedIn)) completion(.failure(FeedlyAccountDelegateError.notLoggedIn))
return return
} }
let log = self.log let log = self.log
let syncAllOperation = FeedlySyncAllOperation(account: account, feedlyUserID: credentials.username, caller: caller, database: syncDatabase, lastSuccessfulFetchStartDate: accountMetadata?.lastArticleFetchStartTime, downloadProgress: refreshProgress, log: log) let syncAllOperation = FeedlySyncAllOperation(account: account, feedlyUserID: credentials.username, caller: caller, database: syncDatabase, lastSuccessfulFetchStartDate: accountMetadata?.lastArticleFetchStartTime, downloadProgress: refreshProgress, log: log)
syncAllOperation.downloadProgress = refreshProgress syncAllOperation.downloadProgress = refreshProgress
let date = Date() let date = Date()
syncAllOperation.syncCompletionHandler = { [weak self] result in syncAllOperation.syncCompletionHandler = { [weak self] result in
if case .success = result { if case .success = result {
self?.accountMetadata?.lastArticleFetchStartTime = date self?.accountMetadata?.lastArticleFetchStartTime = date
self?.accountMetadata?.lastArticleFetchEndTime = Date() self?.accountMetadata?.lastArticleFetchEndTime = Date()
} }
os_log(.debug, log: log, "Sync took %{public}.3f seconds", -date.timeIntervalSinceNow) os_log(.debug, log: log, "Sync took %{public}.3f seconds", -date.timeIntervalSinceNow)
completion(result) completion(result)
} }
currentSyncAllOperation = syncAllOperation currentSyncAllOperation = syncAllOperation
operationQueue.add(syncAllOperation) operationQueue.add(syncAllOperation)
} }
@MainActor func syncArticleStatus(for account: Account) async throws { @MainActor func syncArticleStatus(for account: Account) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -184,7 +184,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
} }
} }
} }
public func sendArticleStatus(for account: Account) async throws { public func sendArticleStatus(for account: Account) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -211,7 +211,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
} }
operationQueue.add(send) operationQueue.add(send)
} }
func refreshArticleStatus(for account: Account) async throws { func refreshArticleStatus(for account: Account) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -236,31 +236,31 @@ final class FeedlyAccountDelegate: AccountDelegate {
guard let credentials = credentials else { guard let credentials = credentials else {
return completion(.success(())) return completion(.success(()))
} }
let group = DispatchGroup() let group = DispatchGroup()
let ingestUnread = FeedlyIngestUnreadArticleIDsOperation(account: account, userID: credentials.username, service: caller, database: syncDatabase, newerThan: nil, log: log) let ingestUnread = FeedlyIngestUnreadArticleIDsOperation(account: account, userID: credentials.username, service: caller, database: syncDatabase, newerThan: nil, log: log)
group.enter() group.enter()
ingestUnread.completionBlock = { _ in ingestUnread.completionBlock = { _ in
group.leave() group.leave()
} }
let ingestStarred = FeedlyIngestStarredArticleIDsOperation(account: account, userID: credentials.username, service: caller, database: syncDatabase, newerThan: nil, log: log) let ingestStarred = FeedlyIngestStarredArticleIDsOperation(account: account, userID: credentials.username, service: caller, database: syncDatabase, newerThan: nil, log: log)
group.enter() group.enter()
ingestStarred.completionBlock = { _ in ingestStarred.completionBlock = { _ in
group.leave() group.leave()
} }
group.notify(queue: .main) { group.notify(queue: .main) {
completion(.success(())) completion(.success(()))
} }
operationQueue.addOperations([ingestUnread, ingestStarred]) operationQueue.addOperations([ingestUnread, ingestStarred])
} }
func importOPML(for account: Account, opmlFile: URL) async throws { func importOPML(for account: Account, opmlFile: URL) async throws {
let data = try Data(contentsOf: opmlFile) let data = try Data(contentsOf: opmlFile)
@ -309,7 +309,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
} }
let nameBefore = folder.name let nameBefore = folder.name
do { do {
let collection = try await caller.renameCollection(with: id, to: name) let collection = try await caller.renameCollection(with: id, to: name)
folder.name = collection.label folder.name = collection.label
@ -318,20 +318,20 @@ final class FeedlyAccountDelegate: AccountDelegate {
throw error throw error
} }
} }
func removeFolder(for account: Account, with folder: Folder) async throws { func removeFolder(for account: Account, with folder: Folder) async throws {
guard let id = folder.externalID else { guard let id = folder.externalID else {
throw FeedlyAccountDelegateError.unableToRemoveFolder(folder.nameForDisplay) throw FeedlyAccountDelegateError.unableToRemoveFolder(folder.nameForDisplay)
} }
refreshProgress.addTask() refreshProgress.addTask()
defer { refreshProgress.completeTask() } defer { refreshProgress.completeTask() }
try await caller.deleteCollection(with: id) try await caller.deleteCollection(with: id)
account.removeFolder(folder: folder) account.removeFolder(folder: folder)
} }
func createFeed(for account: Account, url: String, name: String?, container: Container, validateFeed: Bool) async throws -> Feed { func createFeed(for account: Account, url: String, name: String?, container: Container, validateFeed: Bool) async throws -> Feed {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -352,7 +352,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
guard let credentials = credentials else { guard let credentials = credentials else {
throw FeedlyAccountDelegateError.notLoggedIn throw FeedlyAccountDelegateError.notLoggedIn
} }
let addNewFeed = try FeedlyAddNewFeedOperation(account: account, let addNewFeed = try FeedlyAddNewFeedOperation(account: account,
credentials: credentials, credentials: credentials,
url: url, url: url,
@ -365,30 +365,30 @@ final class FeedlyAccountDelegate: AccountDelegate {
container: container, container: container,
progress: refreshProgress, progress: refreshProgress,
log: log) log: log)
addNewFeed.addCompletionHandler = { result in addNewFeed.addCompletionHandler = { result in
completion(result) completion(result)
} }
operationQueue.add(addNewFeed) operationQueue.add(addNewFeed)
} catch { } catch {
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func renameFeed(for account: Account, with feed: Feed, to name: String) async throws { func renameFeed(for account: Account, with feed: Feed, to name: String) async throws {
let folderCollectionIDs = account.folders?.filter { $0.has(feed) }.compactMap { $0.externalID } let folderCollectionIDs = account.folders?.filter { $0.has(feed) }.compactMap { $0.externalID }
guard let collectionIDs = folderCollectionIDs, let collectionID = collectionIDs.first else { guard let collectionIDs = folderCollectionIDs, let collectionID = collectionIDs.first else {
throw FeedlyAccountDelegateError.unableToRenameFeed(feed.nameForDisplay, name) throw FeedlyAccountDelegateError.unableToRenameFeed(feed.nameForDisplay, name)
} }
let feedID = FeedlyFeedResourceID(id: feed.feedID) let feedID = FeedlyFeedResourceID(id: feed.feedID)
let editedNameBefore = feed.editedName let editedNameBefore = feed.editedName
// Optimistically set the name // Optimistically set the name
feed.editedName = name feed.editedName = name
@ -400,7 +400,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
feed.editedName = editedNameBefore feed.editedName = editedNameBefore
} }
} }
func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws { func addFeed(for account: Account, with feed: Feed, to container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -422,47 +422,47 @@ final class FeedlyAccountDelegate: AccountDelegate {
guard let credentials = credentials else { guard let credentials = credentials else {
throw FeedlyAccountDelegateError.notLoggedIn throw FeedlyAccountDelegateError.notLoggedIn
} }
let resource = FeedlyFeedResourceID(id: feed.feedID) let resource = FeedlyFeedResourceID(id: feed.feedID)
let addExistingFeed = try FeedlyAddExistingFeedOperation(account: account, let addExistingFeed = try FeedlyAddExistingFeedOperation(account: account,
credentials: credentials, credentials: credentials,
resource: resource, resource: resource,
service: caller, service: caller,
container: container, container: container,
progress: refreshProgress, progress: refreshProgress,
log: log, log: log,
customFeedName: feed.editedName) customFeedName: feed.editedName)
addExistingFeed.addCompletionHandler = { result in addExistingFeed.addCompletionHandler = { result in
completion(result) completion(result)
} }
operationQueue.add(addExistingFeed) operationQueue.add(addExistingFeed)
} catch { } catch {
DispatchQueue.main.async { DispatchQueue.main.async {
completion(.failure(error)) completion(.failure(error))
} }
} }
} }
func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws { func removeFeed(for account: Account, with feed: Feed, from container: any Container) async throws {
guard let folder = container as? Folder, let collectionID = folder.externalID else { guard let folder = container as? Folder, let collectionID = folder.externalID else {
throw FeedlyAccountDelegateError.unableToRemoveFeed(feed.nameForDisplay) throw FeedlyAccountDelegateError.unableToRemoveFeed(feed.nameForDisplay)
} }
try await caller.removeFeed(feed.feedID, fromCollectionWith: collectionID) try await caller.removeFeed(feed.feedID, fromCollectionWith: collectionID)
folder.removeFeed(feed) folder.removeFeed(feed)
} }
func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container) async throws { func moveFeed(for account: Account, with feed: Feed, from: Container, to: Container) async throws {
guard let sourceFolder = from as? Folder, let destinationFolder = to as? Folder else { guard let sourceFolder = from as? Folder, let destinationFolder = to as? Folder else {
throw FeedlyAccountDelegateError.addFeedChooseFolder throw FeedlyAccountDelegateError.addFeedChooseFolder
} }
// Optimistically move the feed, undoing as appropriate to the failure // Optimistically move the feed, undoing as appropriate to the failure
sourceFolder.removeFeed(feed) sourceFolder.removeFeed(feed)
destinationFolder.addFeed(feed) destinationFolder.addFeed(feed)
@ -482,7 +482,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
throw FeedlyAccountDelegateError.unableToMoveFeedBetweenFolders(feed.nameForDisplay, sourceFolder.nameForDisplay, destinationFolder.nameForDisplay) throw FeedlyAccountDelegateError.unableToMoveFeedBetweenFolders(feed.nameForDisplay, sourceFolder.nameForDisplay, destinationFolder.nameForDisplay)
} }
} }
func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws { func restoreFeed(for account: Account, feed: Feed, container: any Container) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -521,7 +521,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
} }
} }
} }
func restoreFolder(for account: Account, folder: Folder) async throws { func restoreFolder(for account: Account, folder: Folder) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -538,11 +538,11 @@ final class FeedlyAccountDelegate: AccountDelegate {
private func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) { private func restoreFolder(for account: Account, folder: Folder, completion: @escaping (Result<Void, Error>) -> Void) {
let group = DispatchGroup() let group = DispatchGroup()
for feed in folder.topLevelFeeds { for feed in folder.topLevelFeeds {
folder.topLevelFeeds.remove(feed) folder.topLevelFeeds.remove(feed)
group.enter() group.enter()
restoreFeed(for: account, feed: feed, container: folder) { result in restoreFeed(for: account, feed: feed, container: folder) { result in
group.leave() group.leave()
@ -553,15 +553,15 @@ final class FeedlyAccountDelegate: AccountDelegate {
os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription) os_log(.error, log: self.log, "Restore folder feed error: %@.", error.localizedDescription)
} }
} }
} }
group.notify(queue: .main) { group.notify(queue: .main) {
account.addFolder(folder) account.addFolder(folder)
completion(.success(())) completion(.success(()))
} }
} }
func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws { func markArticles(for account: Account, articles: Set<Article>, statusKey: ArticleStatus.Key, flag: Bool) async throws {
try await withCheckedThrowingContinuation { continuation in try await withCheckedThrowingContinuation { continuation in
@ -605,13 +605,13 @@ final class FeedlyAccountDelegate: AccountDelegate {
self.account = account self.account = account
credentials = try? account.retrieveCredentials(type: .oauthAccessToken) credentials = try? account.retrieveCredentials(type: .oauthAccessToken)
} }
@MainActor func accountWillBeDeleted(_ account: Account) { @MainActor func accountWillBeDeleted(_ account: Account) {
let logout = FeedlyLogoutOperation(service: caller, log: log) let logout = FeedlyLogoutOperation(service: caller, log: log)
// Dispatch on the shared queue because the lifetime of the account delegate is uncertain. // Dispatch on the shared queue because the lifetime of the account delegate is uncertain.
MainThreadOperationQueue.shared.add(logout) MainThreadOperationQueue.shared.add(logout)
} }
static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?, secretsProvider: SecretsProvider) async throws -> Credentials? { static func validateCredentials(transport: Transport, credentials: Credentials, endpoint: URL?, secretsProvider: SecretsProvider) async throws -> Credentials? {
assertionFailure("An `account` instance should refresh the access token first instead.") assertionFailure("An `account` instance should refresh the access token first instead.")
@ -678,6 +678,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
} }
} }
@discardableResult
func addFeedToCollection(feedResource: FeedlyFeedResourceID, feedName: String? = nil, collectionID: String, folder: Folder) async throws -> [([FeedlyFeed], Folder)] { func addFeedToCollection(feedResource: FeedlyFeedResourceID, feedName: String? = nil, collectionID: String, folder: Folder) async throws -> [([FeedlyFeed], Folder)] {
// To replace FeedlyAddFeedToCollectionOperation // To replace FeedlyAddFeedToCollectionOperation
@ -718,7 +719,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
func downloadArticles(missingArticleIDs: Set<String>, updatedArticleIDs: Set<String>) async throws { func downloadArticles(missingArticleIDs: Set<String>, updatedArticleIDs: Set<String>) async throws {
// To replace FeedlyDownloadArticlesOperation // To replace FeedlyDownloadArticlesOperation
let allArticleIDs = missingArticleIDs.union(updatedArticleIDs) let allArticleIDs = missingArticleIDs.union(updatedArticleIDs)
os_log(.debug, log: log, "Requesting %{public}i articles.", allArticleIDs.count) os_log(.debug, log: log, "Requesting %{public}i articles.", allArticleIDs.count)
@ -744,7 +745,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
throw error throw error
} }
} }
func parsedItems(with entries: Set<FeedlyEntry>) -> Set<ParsedItem> { func parsedItems(with entries: Set<FeedlyEntry>) -> Set<ParsedItem> {
// TODO: convert directly from FeedlyEntry to ParsedItem without having to use FeedlyEntryParser. // TODO: convert directly from FeedlyEntry to ParsedItem without having to use FeedlyEntryParser.
@ -863,7 +864,7 @@ final class FeedlyAccountDelegate: AccountDelegate {
func processStarredArticleIDs(remoteArticleIDs: Set<String>) async throws { func processStarredArticleIDs(remoteArticleIDs: Set<String>) async throws {
guard let account else { return } guard let account else { return }
var remoteArticleIDs = remoteArticleIDs var remoteArticleIDs = remoteArticleIDs
func removeEntryIDsWithPendingStatus() async throws { func removeEntryIDsWithPendingStatus() async throws {
@ -953,6 +954,28 @@ final class FeedlyAccountDelegate: AccountDelegate {
try await updateAccountFeeds(parsedItems: parsedItems) try await updateAccountFeeds(parsedItems: parsedItems)
} }
func addNewFeed(url: String, feedName: String?) async throws {
// To replace FeedlyAddNewFeedOperation
let validator = FeedlyFeedContainerValidator(container: container)
let (folder, collectionID) = try validator.getValidContainer()
let searchResponse = try await searchForFeed(url: url)
guard let firstFeed = response.results.first else {
throw AccountError.createErrorNotFound
}
let feedResourceID = FeedlyFeedResourceID(id: firstFeed.feedID)
try await addFeedToCollection(feedResource: feedResourceID, feedName: feedName, collectionID: collectionID, folder: folder)
// TODO: FeedlyCreateFeedsForCollectionFoldersOperation replacement
// let createFeeds = TODO
try await fetchAndProcessUnreadArticleIDs()
try await syncStreamContents(feedResourceID: feedResourceID)
}
// MARK: Suspend and Resume (for iOS) // MARK: Suspend and Resume (for iOS)
/// Suspend all network activity /// Suspend all network activity