diff --git a/apps/api/src/imports.ts b/apps/api/src/imports.ts index 725814b..6a5f1b0 100644 --- a/apps/api/src/imports.ts +++ b/apps/api/src/imports.ts @@ -75,13 +75,17 @@ export class Imports { if (known) return known.found ? known : undefined; throw new ImvuUnavailable("no IMVU client"); } - let p; + let answer; try { - p = await this.imvu.product(id); + answer = await this.imvu.lookup(id); } catch (e) { if (known && e instanceof ImvuUnavailable) return known.found ? known : undefined; throw e; } + // IMVU hides Access Pass products from anonymous callers (401, as for deleted and made-up IDs): keep what was + // last seen rather than treat a product that became AP as gone. + if (answer === "hidden" && known?.found) return known; + const p = typeof answer === "string" ? null : answer; const row = { id, found: p !== null, diff --git a/apps/api/src/imvu.ts b/apps/api/src/imvu.ts index 41c406e..a5744b6 100644 --- a/apps/api/src/imvu.ts +++ b/apps/api/src/imvu.ts @@ -39,7 +39,7 @@ export type ImvuProduct = { }; export class NoSuchImvuUser extends Error { - constructor() { + constructor(readonly status = 404) { super("no such IMVU account"); } } @@ -83,13 +83,24 @@ export class ImvuClient { * Access Pass ones). Throws ImvuUnavailable if IMVU can't be reached. */ async product(id: number): Promise { - if (!Number.isSafeInteger(id) || id <= 0) return null; + const p = await this.lookup(id); + return typeof p === "string" ? null : p; + } + + /** + * Like product(), but tells "missing" (404) from "hidden": IMVU answers 401 alike for Access Pass products, + * deleted ones and IDs that never existed, so a hidden product may still exist. + */ + async lookup(id: number): Promise { + if (!Number.isSafeInteger(id) || id <= 0) return "missing"; + let answer: unknown; try { - return parseProduct(await this.get(`/product/product-${id}`, [401, 403]), id); + answer = await this.get(`/product/product-${id}`, [401, 403]); } catch (e) { - if (e instanceof NoSuchImvuUser) return null; + if (e instanceof NoSuchImvuUser) return e.status === 404 ? "missing" : "hidden"; throw e; } + return parseProduct(answer, id) ?? "missing"; } private async wait() { @@ -111,7 +122,7 @@ export class ImvuClient { } catch (e) { throw new ImvuUnavailable(String(e)); } - if (response.status === 404 || missing.includes(response.status)) throw new NoSuchImvuUser(); + if (response.status === 404 || missing.includes(response.status)) throw new NoSuchImvuUser(response.status); if (!response.ok) throw new ImvuUnavailable(`IMVU answered ${response.status}`); return response.json(); } diff --git a/apps/api/test/imports.test.ts b/apps/api/test/imports.test.ts index 5ea3c04..96d6190 100644 --- a/apps/api/test/imports.test.ts +++ b/apps/api/test/imports.test.ts @@ -6,17 +6,20 @@ import { openDatabase, type Database } from "@t5de/db"; import { createApp } from "../src/app"; import { createAuth } from "../src/auth"; import { ImvuClient, type ImvuUser } from "../src/imvu"; +import type { Imports } from "../src/imports"; import { twoFactorOn } from "./two-factor"; const webURL = "http://web.test"; const apiURL = "http://api.test"; const accounts = new Map(); const imvuProducts = new Map>(); +const hiddenProducts = new Set(); // answered 401, as IMVU does for Access Pass products const fakeIMVU = (async (input: RequestInfo | URL) => { const url = new URL(String(input)); const pm = url.pathname.match(/^\/product\/product-(\d+)$/); if (pm) { const p = imvuProducts.get(Number(pm[1])); + if (hiddenProducts.has(Number(pm[1]))) return Response.json({ status: "failure", error: "AUTHENTICATION-005" }, { status: 401 }); if (!p) return Response.json({ status: "failure" }, { status: 404 }); return Response.json({ denormalized: { [`https://api.imvu.com/product/product-${pm[1]}`]: { data: p } } }); } @@ -30,6 +33,7 @@ const fakeIMVU = (async (input: RequestInfo | URL) => { }); }) as unknown as typeof fetch; +let imports: Imports; let db: Database; let close: () => Promise; let base: string; @@ -39,6 +43,7 @@ beforeAll(async () => { ({ db, close } = await openDatabase("memory")); const auth = createAuth(db, { apiURL, webURL, secret: "test-secret-test-secret-test-secret-1234", rateLimit: false }); const hub = createApp({ db, auth, imvu: new ImvuClient("http://imvu.test", fakeIMVU), apiURL, webURL, catalogHealthy: async () => true }); + imports = hub.imports; hub.app.listen({ port: 0, hostname: "127.0.0.1" }); const server = hub.app.server as unknown as { port: number; stop: () => void }; stop = () => server.stop(); @@ -200,3 +205,15 @@ test("grants, requests, the importer's jobs, and withdrawal", async () => { expect((await call(maker, "DELETE", "/api/v1/account/links/501")).status).toBe(204); expect((await call(maker, "GET", "/api/v1/imports/grants")).json.grants[0]).toMatchObject({ id: again.json.grant.id, status: "suspended" }); }); + +test("IMVU's 401 keeps what was last seen; 404 is gone", async () => { + imvuProducts.set(9500, imvuProduct("Turned AP", 501)); + expect(await imports.info(9500, true)).toMatchObject({ found: true, creatorId: 501 }); + hiddenProducts.add(9500); + expect(await imports.info(9500, true)).toMatchObject({ found: true, creatorId: 501, name: "Turned AP" }); + hiddenProducts.add(9501); // never seen: nothing to keep + expect(await imports.info(9501, true)).toBeUndefined(); + hiddenProducts.delete(9500); + imvuProducts.delete(9500); + expect(await imports.info(9500, true)).toBeUndefined(); +}); diff --git a/docs/imports.md b/docs/imports.md index 1d12a51..06e8e01 100644 --- a/docs/imports.md +++ b/docs/imports.md @@ -160,8 +160,11 @@ account is unlinked or linked to someone else, an hourly batch of daily rechecks answers a day), notifications (`import_grant_decided`, `import_grant_ended`), the website's `/imports` page (ask for a product; bring products; change or withdraw; what people asked for) and the console's `/console/catalogs/:id/imports` (importer on, off, paused, key; accept and decline; the queue with retries). -IMVU's product fields (`product_name`, `creator_cid`, `creator_name`, `rating`, `is_purchasable`) were not checked -against the live API from the dev machine (it can't reach api.imvu.com); check them with the spike. +IMVU's product fields (`product_name`, `creator_cid`, `creator_name`, `rating`, `is_purchasable`) were checked +against the live API (2026-11-06). Anonymous callers get 401 (`AUTHENTICATION-005`) alike for Access Pass products, +deleted ones and IDs that never existed (404 only for a few very old IDs), so the hub keeps what it last saw on a +401 rather than withdraw a product that became AP, and can't import AP products it never saw. The importer, which +has an IMVU session, could tell the hub what it sees: decide with the spike. Still to do: admin-recorded grants, proofs and unclaimed creators (and claiming); `GET /imports/available` for the proxy's shop button; t5de-catalog accepting `t5de-import` tokens on uploads; the spike and `t5de-importer`.