From 9a1ccd46bcaeb2450778556024bcaeafca9fb907 Mon Sep 17 00:00:00 2001 From: Jackson Harper Date: Tue, 24 Oct 2023 12:11:28 +0800 Subject: [PATCH] Bump version, attempt to refetch PDFs if download signed URL has expired --- .../App/PDFSupport/PDFViewerViewModel.swift | 13 +++++++++- .../Models/DataModels/ArticleContent.swift | 5 +++- .../Services/DataService/ContentLoading.swift | 6 +++-- packages/api/src/server.ts | 25 +++++++++++++++++++ 4 files changed, 45 insertions(+), 4 deletions(-) diff --git a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift index 4643ef46d..af5f05915 100644 --- a/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift +++ b/apple/OmnivoreKit/Sources/App/PDFSupport/PDFViewerViewModel.swift @@ -96,7 +96,18 @@ final class PDFViewerViewModel: ObservableObject { } } - return try await dataService.loadPDFData(slug: pdfItem.slug, downloadURL: pdfItem.downloadURL) + if let result = try? await dataService.loadPDFData(slug: pdfItem.slug, downloadURL: pdfItem.downloadURL) { + return result + } + + // Downloading failed, try to get the article again, and then download + if let content = try? await dataService.loadArticleContentWithRetries(itemID: pdfItem.itemID, username: "me") { + // refetched the content, now try one more time then throw + if let result = try await dataService.loadPDFData(slug: pdfItem.slug, downloadURL: content.downloadURL) { + return result + } + } + return nil } catch { print("error downloading PDF", error) return nil diff --git a/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContent.swift b/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContent.swift index 2a453a9ac..4ce4eb657 100644 --- a/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContent.swift +++ b/apple/OmnivoreKit/Sources/Models/DataModels/ArticleContent.swift @@ -16,19 +16,22 @@ public struct ArticleContent { public let highlightsJSONString: String public let contentStatus: ArticleContentStatus public let objectID: NSManagedObjectID? + public let downloadURL: String public init( title: String, htmlContent: String, highlightsJSONString: String, contentStatus: ArticleContentStatus, - objectID: NSManagedObjectID? + objectID: NSManagedObjectID?, + downloadURL: String ) { self.title = title self.htmlContent = htmlContent self.highlightsJSONString = highlightsJSONString self.contentStatus = contentStatus self.objectID = objectID + self.downloadURL = downloadURL } } diff --git a/apple/OmnivoreKit/Sources/Services/DataService/ContentLoading.swift b/apple/OmnivoreKit/Sources/Services/DataService/ContentLoading.swift index e96469e3a..a5427549b 100644 --- a/apple/OmnivoreKit/Sources/Services/DataService/ContentLoading.swift +++ b/apple/OmnivoreKit/Sources/Services/DataService/ContentLoading.swift @@ -63,7 +63,8 @@ extension DataService { htmlContent: fetchResult.htmlContent, highlightsJSONString: fetchResult.highlights.asJSONString, contentStatus: fetchResult.item.isPDF ? .succeeded : fetchResult.item.state, - objectID: objectID + objectID: objectID, + downloadURL: fetchResult.item.downloadURL ) } @@ -91,7 +92,8 @@ extension DataService { .filter { $0.serverSyncStatus != ServerSyncStatus.needsDeletion.rawValue } .map { InternalHighlight.make(from: $0) }.asJSONString, contentStatus: .succeeded, - objectID: linkedItem.objectID + objectID: linkedItem.objectID, + downloadURL: linkedItem.downloadURL ?? "" ) } } diff --git a/packages/api/src/server.ts b/packages/api/src/server.ts index b40a82bb8..5c6c27d87 100755 --- a/packages/api/src/server.ts +++ b/packages/api/src/server.ts @@ -38,6 +38,7 @@ import { sentryConfig } from './sentry' import { getClaimsByToken, getTokenByRequest } from './utils/auth' import { corsConfig } from './utils/corsConfig' import { buildLogger, buildLoggerTransport } from './utils/logger' +import { FirefishClient, createFirefishUserClient } from './activitypub' const PORT = process.env.PORT || 4000 @@ -178,4 +179,28 @@ const main = async (): Promise => { // only call main if the file was called from the CLI and wasn't required from another module if (require.main === module) { main() + ;(async () => { + // console.log( + // 'creating user with firefish token: ', + // process.env.FIREFISH_TOKEN + // ) + // const client = new FirefishClient( + // 'http://localhost:8000/api', + // process.env.FIREFISH_TOKEN || 'firefish-token', + // 'abc123' + // ) + // // const token = await client.createUserActor( + // // 'a03a7396-909b-11ed-9075-c3f3cf07eed9', + // // 'jacksonh@gmail.com', + // // 'jacksonharper' + // // ) + // // console.log('user token: ', token) + // // if (token) { + // // const secret = client.createAppForActor(token) + // // console.log('created secretL: ', secret) + // // } + // const appClient = createFirefishUserClient('UEpw5pStVG5hFhrX') + // appClient.setupUserActor('a03a7396-909b-11ed-9075-c3f3cf07eed9') + // appClient.getUserInfo() + })() }