From 572a3cea12a3f41e7fe8efd405955fb91a59c72c Mon Sep 17 00:00:00 2001 From: sickerine Date: Tue, 24 Nov 2026 00:31:56 +0000 Subject: [PATCH] fix: a free copy found for a paper with no link is kept under the address it came from The assistant is sent the stored copy, so a copy found by DOI for a paper the index gives no PDF link was refused as one that couldn't be kept. Seen on shared dev with W103333418 after the downloader swap. Part of OpenDevEd/openscholar#1515 --- convex/lib/paperPdfs.ts | 6 +++--- convex/research/paperPdf.test.ts | 15 +++++++++++++++ lib/pdf/download.ts | 4 +++- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/convex/lib/paperPdfs.ts b/convex/lib/paperPdfs.ts index b09b2b813..0997e71f6 100644 --- a/convex/lib/paperPdfs.ts +++ b/convex/lib/paperPdfs.ts @@ -9,7 +9,7 @@ * * A copy bought from OpenAlex is kept under the paper's OpenAlex ID. One fetched by a link is kept under that link, * so it stands for what the link served and nothing more: nobody's file, and no wrong file a link led to, stands in - * for a paper. + * for a paper. A paper with no link of its own, found by its DOI, is kept under the address its copy came from. * A file from a person's own Zotero library is theirs: it never goes into the store, and the store is never read for it. * * The assistant pays for a copy here (`charge: "self"`). Extraction adds what a copy costs to the paper's own charge @@ -290,10 +290,10 @@ export async function paperPdf(ctx: ActionCtx, payer: PdfPayer, paper: PaperRef, if (paper.url || paper.doi) { // A file from a person's Zotero library is theirs to read, and a miss on it says nothing about the paper. const memory = isZotero(paper) ? undefined : searchMemory(ctx, paper.url, options); - const { pdfBlob, error } = await downloadPdf(paper.url ?? "", { doi: paper.doi, openalexId: work ?? undefined, zoteroConnection: paper.zotero, allowHttp: options.allowHttp, maxBytes: options.maxBytes, ...(memory ? { memory } : {}) }); + const { pdfBlob, url: foundAt, error } = await downloadPdf(paper.url ?? "", { doi: paper.doi, openalexId: work ?? undefined, zoteroConnection: paper.zotero, allowHttp: options.allowHttp, maxBytes: options.maxBytes, ...(memory ? { memory } : {}) }); const bytes = pdfBlob ? new Uint8Array(await pdfBlob.arrayBuffer()) : null; if (bytes && isPdf(bytes)) { - const key = isZotero(paper) ? null : await linkKey(paper.url); + const key = isZotero(paper) ? null : await linkKey(paper.url ?? foundAt); const kept = key !== null && (await keep(key, bytes)); // A caller that sends the stored copy can't go on without one. if (!options.bytes && !kept) return { error: NOT_KEPT }; diff --git a/convex/research/paperPdf.test.ts b/convex/research/paperPdf.test.ts index 3b3c11a2a..f4a4cce08 100644 --- a/convex/research/paperPdf.test.ts +++ b/convex/research/paperPdf.test.ts @@ -167,6 +167,21 @@ describe("extraction's PDFs from the store", () => { expect(read).toMatchObject({ error: expect.stringMatching(/too large/) }); }); + it("keeps a copy found by DOI for a paper with no link, under the address the copy came from", async () => { + const t = convexTest(schema); + const answer = fetchMock.getMockImplementation()!; + fetchMock.mockImplementation(async (input: RequestInfo | URL, init?: RequestInit) => { + const url = new URL(input instanceof Request ? input.url : String(input)); + if (url.hostname !== "api.crossref.org") return answer(input, init); + return new Response(JSON.stringify({ message: { DOI: "10.1/nolink", link: [{ URL: PDF, "content-type": "application/pdf" }] } }), { status: 200, headers: { "content-type": "application/json" } }); + }); + + // As the assistant reads: it is sent the stored copy, so a copy that can't be kept is no use to it. + const found = await paperPdf(actionsOf(t), payer("j1"), { openalexId: "W12", doi: "10.1/nolink" }, { ...EXTRACTION, bytes: false }); + + expect(found).toMatchObject({ key: await byLink(PDF), from: "link", credits: 0 }); + }); + it("remembers a paper with no free copy, and asks no site about it again until its time", async () => { const t = convexTest(schema); const gone = { openalexId: "W10", doi: "10.1/gone", url: "https://example.org/gone.pdf", hasCopy: false }; diff --git a/lib/pdf/download.ts b/lib/pdf/download.ts index a967f0970..43d01e41f 100644 --- a/lib/pdf/download.ts +++ b/lib/pdf/download.ts @@ -33,6 +33,8 @@ export interface DownloadPdfOptions { export interface DownloadPdfResult { success: boolean; pdfBlob?: Blob; + /** The address the search found the copy at. */ + url?: string; error?: string; } @@ -65,7 +67,7 @@ export async function downloadPdf( httpsOnly: !options.allowHttp, }, ); - if (result.ok) return { success: true, pdfBlob: new Blob([result.bytes.slice().buffer], { type: "application/pdf" }) }; + if (result.ok) return { success: true, pdfBlob: new Blob([result.bytes.slice().buffer], { type: "application/pdf" }), url: result.url }; // A copy that exists and is too large is not a paper with no PDF. const tooLarge = result.attempts.some((attempt) => attempt.outcome === "too_large"); return { success: false, error: tooLarge ? "This paper's PDF is too large to read here." : WHY[result.reason] };