From a0359f17faa06b344e9ee44480fde63eb0fe53c4 Mon Sep 17 00:00:00 2001 From: Brent Simmons Date: Fri, 29 May 2026 18:51:51 -0700 Subject: [PATCH] Add activity reporting to Feedly operations. --- .../FeedlyDownloadArticlesOperation.swift | 1 + ...yFetchIdsForMissingArticlesOperation.swift | 1 + .../FeedlyGetCollectionsOperation.swift | 1 + .../FeedlyGetUpdatedArticleIdsOperation.swift | 1 + ...edlyIngestStarredArticleIdsOperation.swift | 1 + ...eedlyIngestStreamArticleIdsOperation.swift | 1 + ...eedlyIngestUnreadArticleIdsOperation.swift | 1 + .../Feedly/Operations/FeedlyOperation.swift | 48 ++++++++++++++++++- .../FeedlySendArticleStatusesOperation.swift | 1 + .../Operations/FeedlySyncAllOperation.swift | 27 +++++++++++ 10 files changed, 82 insertions(+), 1 deletion(-) diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyDownloadArticlesOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyDownloadArticlesOperation.swift index 21d0449c1..49742b836 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyDownloadArticlesOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyDownloadArticlesOperation.swift @@ -33,6 +33,7 @@ final class FeedlyDownloadArticlesOperation: FeedlyOperation { } override func run() { + startActivityIfNeeded() var articleIds = missingArticleEntryIdProvider.entryIDs articleIds.formUnion(updatedArticleEntryIdProvider.entryIDs) diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyFetchIdsForMissingArticlesOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyFetchIdsForMissingArticlesOperation.swift index b98a736c8..4715c2833 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyFetchIdsForMissingArticlesOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyFetchIdsForMissingArticlesOperation.swift @@ -20,6 +20,7 @@ final class FeedlyFetchIdsForMissingArticlesOperation: FeedlyOperation, FeedlyEn } override func run() { + startActivityIfNeeded() Task { do { let articleIDs = try await account.fetchArticleIDsForStatusesWithoutArticlesNewerThanCutoffDateAsync() diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetCollectionsOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetCollectionsOperation.swift index 6bec901c7..1b271f82f 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetCollectionsOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetCollectionsOperation.swift @@ -26,6 +26,7 @@ final class FeedlyGetCollectionsOperation: FeedlyOperation, FeedlyCollectionProv override func run() { Feedly.logger.info("Feedly: Requesting collections") + startActivityIfNeeded() service.getCollections { result in Task { @MainActor in diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetUpdatedArticleIdsOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetUpdatedArticleIdsOperation.swift index edf3d9e9a..82b7530d0 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetUpdatedArticleIdsOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyGetUpdatedArticleIdsOperation.swift @@ -40,6 +40,7 @@ final class FeedlyGetUpdatedArticleIdsOperation: FeedlyOperation, FeedlyEntryIde private var storedUpdatedArticleIds = Set() override func run() { + startActivityIfNeeded() getStreamIds(nil) } diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift index 683c2fd97..9f96f52e0 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStarredArticleIdsOperation.swift @@ -38,6 +38,7 @@ final class FeedlyIngestStarredArticleIdsOperation: FeedlyOperation { } override func run() { + startActivityIfNeeded() getStreamIds(nil) } diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStreamArticleIdsOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStreamArticleIdsOperation.swift index 35b73a5d8..f20ea6a95 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStreamArticleIdsOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestStreamArticleIdsOperation.swift @@ -33,6 +33,7 @@ final class FeedlyIngestStreamArticleIdsOperation: FeedlyOperation { } override func run() { + startActivityIfNeeded() getStreamIds(nil) } diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift index c55eaaadc..d42ff1d98 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyIngestUnreadArticleIdsOperation.swift @@ -39,6 +39,7 @@ final class FeedlyIngestUnreadArticleIdsOperation: FeedlyOperation { } override func run() { + startActivityIfNeeded() getStreamIds(nil) } diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyOperation.swift index 371807204..2201d4beb 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlyOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlyOperation.swift @@ -7,6 +7,7 @@ // import Foundation +import ActivityLog import RSWeb import RSCore @@ -28,6 +29,14 @@ protocol FeedlyOperationDelegate: AnyObject { } } + /// Set these before adding the op to its queue to have it report an + /// Activity Log entry — started on `run()`, completed in `didFinish()`, + /// failed in `didFinish(with:)`. + var activityKind: ActivityKind? + var activityDetail: String? + var activityAccountID: String? + private var activityID: Int? + // FeedlyMainThreadOperation public var isCanceled = false { didSet { @@ -44,7 +53,20 @@ protocol FeedlyOperationDelegate: AnyObject { public func run() { } + /// Starts the configured activity (if any). Subclasses call this at the + /// top of their `run()` override. + func startActivityIfNeeded() { + guard activityID == nil, let kind = activityKind, let accountID = activityAccountID else { + return + } + let log = ActivityLog.shared + let id = log.createActivity(owner: .account(accountID), kind: kind, detail: activityDetail) + log.didStart(id: id) + activityID = id + } + func didFinish() { + completeActivityIfNeeded() if !isCanceled { operationDelegate?.operationDidComplete(self) } @@ -52,11 +74,35 @@ protocol FeedlyOperationDelegate: AnyObject { } func didFinish(with error: Error) { + failActivityIfNeeded(error: error) delegate?.feedlyOperation(self, didFailWith: error) - didFinish() + if !isCanceled { + operationDelegate?.operationDidComplete(self) + } + downloadProgress?.completeTask() } public func didCancel() { + // On cancel, mark the activity failed so it doesn't linger as "started". + if activityID != nil { + failActivityIfNeeded(error: CocoaError(.userCancelled)) + } didFinish() } + + private func completeActivityIfNeeded() { + guard let id = activityID else { + return + } + ActivityLog.shared.didComplete(id: id) + activityID = nil + } + + private func failActivityIfNeeded(error: Error) { + guard let id = activityID else { + return + } + ActivityLog.shared.didFail(id: id, error: error) + activityID = nil + } } diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlySendArticleStatusesOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlySendArticleStatusesOperation.swift index 54d6bf088..d260a468e 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlySendArticleStatusesOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlySendArticleStatusesOperation.swift @@ -25,6 +25,7 @@ final class FeedlySendArticleStatusesOperation: FeedlyOperation { override func run() { Feedly.logger.info("Feedly: Sending article statuses") + startActivityIfNeeded() Task { do { diff --git a/Modules/Account/Sources/Account/Feedly/Operations/FeedlySyncAllOperation.swift b/Modules/Account/Sources/Account/Feedly/Operations/FeedlySyncAllOperation.swift index 6e33e0a41..327e1243c 100644 --- a/Modules/Account/Sources/Account/Feedly/Operations/FeedlySyncAllOperation.swift +++ b/Modules/Account/Sources/Account/Feedly/Operations/FeedlySyncAllOperation.swift @@ -12,6 +12,7 @@ import SyncDatabase import RSWeb import RSCore import Secrets +import ActivityLog /// Compose the operations necessary to get the entire set of articles, feeds and folders with the statuses the user expects between now and a certain date in the past. final class FeedlySyncAllOperation: FeedlyOperation { @@ -41,10 +42,15 @@ final class FeedlySyncAllOperation: FeedlyOperation { self.downloadProgress = downloadProgress + let accountID = account.accountID + // Send any read/unread/starred article statuses to Feedly before anything else. let sendArticleStatuses = FeedlySendArticleStatusesOperation(database: database, service: markArticlesService) sendArticleStatuses.delegate = self sendArticleStatuses.downloadProgress = downloadProgress + sendArticleStatuses.activityKind = .sendArticleStatuses + sendArticleStatuses.activityDetail = "Sending Feedly statuses" + sendArticleStatuses.activityAccountID = accountID self.operationQueue.add(sendArticleStatuses) // Get all the Collections the user has. @@ -52,6 +58,9 @@ final class FeedlySyncAllOperation: FeedlyOperation { getCollections.delegate = self getCollections.downloadProgress = downloadProgress getCollections.addDependency(sendArticleStatuses) + getCollections.activityKind = .refreshFeedList + getCollections.activityDetail = "Fetching collections" + getCollections.activityAccountID = accountID self.operationQueue.add(getCollections) // Ensure a folder exists for each Collection, removing Folders without a corresponding Collection. @@ -70,6 +79,9 @@ final class FeedlySyncAllOperation: FeedlyOperation { getAllArticleIds.delegate = self getAllArticleIds.downloadProgress = downloadProgress getAllArticleIds.addDependency(createFeedsOperation) + getAllArticleIds.activityKind = .refreshArticleStatuses + getAllArticleIds.activityDetail = "Fetching all article IDs" + getAllArticleIds.activityAccountID = accountID self.operationQueue.add(getAllArticleIds) // Get each page of unread article ids in the global.all stream for the last 31 days (nil = Feedly API default). @@ -77,6 +89,9 @@ final class FeedlySyncAllOperation: FeedlyOperation { getUnread.delegate = self getUnread.addDependency(getAllArticleIds) getUnread.downloadProgress = downloadProgress + getUnread.activityKind = .refreshArticleStatuses + getUnread.activityDetail = "Fetching unread article IDs" + getUnread.activityAccountID = accountID self.operationQueue.add(getUnread) // Get each page of the article ids which have been update since the last successful fetch start date. @@ -85,6 +100,9 @@ final class FeedlySyncAllOperation: FeedlyOperation { getUpdated.delegate = self getUpdated.downloadProgress = downloadProgress getUpdated.addDependency(createFeedsOperation) + getUpdated.activityKind = .refreshArticleStatuses + getUpdated.activityDetail = "Fetching updated article IDs" + getUpdated.activityAccountID = accountID self.operationQueue.add(getUpdated) // Get each page of the article ids for starred articles. @@ -92,6 +110,9 @@ final class FeedlySyncAllOperation: FeedlyOperation { getStarred.delegate = self getStarred.downloadProgress = downloadProgress getStarred.addDependency(createFeedsOperation) + getStarred.activityKind = .refreshArticleStatuses + getStarred.activityDetail = "Fetching starred article IDs" + getStarred.activityAccountID = accountID self.operationQueue.add(getStarred) // Now all the possible article ids we need have a status, fetch the article ids for missing articles. @@ -102,6 +123,9 @@ final class FeedlySyncAllOperation: FeedlyOperation { getMissingIds.addDependency(getUnread) getMissingIds.addDependency(getStarred) getMissingIds.addDependency(getUpdated) + getMissingIds.activityKind = .refreshMissingArticles + getMissingIds.activityDetail = "Finding missing article IDs" + getMissingIds.activityAccountID = accountID self.operationQueue.add(getMissingIds) // Download all the missing and updated articles @@ -115,6 +139,9 @@ final class FeedlySyncAllOperation: FeedlyOperation { downloadMissingArticles.downloadProgress = downloadProgress downloadMissingArticles.addDependency(getMissingIds) downloadMissingArticles.addDependency(getUpdated) + downloadMissingArticles.activityKind = .refreshMissingArticles + downloadMissingArticles.activityDetail = "Downloading articles" + downloadMissingArticles.activityAccountID = accountID self.operationQueue.add(downloadMissingArticles) // Once this operation's dependencies, their dependencies etc finish, we can finish.