diff --git a/apps/api/src/delivery/zerozero.test.ts b/apps/api/src/delivery/zerozero.test.ts index f8bd6ae..101a95b 100644 --- a/apps/api/src/delivery/zerozero.test.ts +++ b/apps/api/src/delivery/zerozero.test.ts @@ -79,7 +79,25 @@ describe("zerozero publish", () => { }), ); await client.publish("12627743", "img-9"); - expect(calls).toEqual(["/api/photo-sync/img-9", "/api/photo-sync/match/12627743"]); + expect(calls).toEqual(["/api/photo-sync/img-9", "/api/photo-sync/img-9", "/api/photo-sync/match/12627743"]); + }); + + it("signs in again when publish answers insufficient permissions", async () => { + const calls: string[] = []; + let refused = false; + const client = new ZerozeroClient( + { username: "kapta", password: "zz-pass" }, + desk((pathname) => { + calls.push(pathname); + if (!refused && pathname === "/api/photo-sync/img-9") { + refused = true; + return Response.json({ error: "Insufficient permissions" }, { status: 403 }); + } + return Response.json({ synced: 1, failed: 0, failures: [] }); + }), + ); + await client.publish("12627743", "img-9"); + expect(calls).toEqual(["/api/photo-sync/img-9", "/api/photo-sync/img-9"]); }); it("publishes the whole game when the desk has no single-photo publish", async () => { @@ -123,21 +141,21 @@ describe("zerozero publish", () => { expect((err as TransportError).transient).toBe(true); }); - it("does not retry when neither publish route accepts the account", async () => { + it("keeps the upload when the account is not allowed to publish", async () => { const calls: string[] = []; const client = new ZerozeroClient( { username: "kapta", password: "zz-pass" }, desk((pathname) => { calls.push(pathname); - return Response.json({ error: "forbidden" }, { status: 403 }); + return Response.json({ error: "Insufficient permissions" }, { status: 403 }); }), ); - const err = await client.publish("12627743", "img-9").then( - () => null, - (error: unknown) => error, - ); - expect(err).toBeInstanceOf(TransportError); - expect((err as TransportError).transient).toBe(false); - expect(calls).toEqual(["/api/photo-sync/img-9", "/api/photo-sync/match/12627743"]); + await client.publish("12627743", "img-9"); + expect(calls).toEqual([ + "/api/photo-sync/img-9", + "/api/photo-sync/img-9", + "/api/photo-sync/match/12627743", + "/api/photo-sync/match/12627743", + ]); }); }); diff --git a/apps/api/src/delivery/zerozero.ts b/apps/api/src/delivery/zerozero.ts index 8b6d73b..169a84b 100644 --- a/apps/api/src/delivery/zerozero.ts +++ b/apps/api/src/delivery/zerozero.ts @@ -283,18 +283,38 @@ export class ZerozeroClient { } } const match = await this.postApi(`/photo-sync/match/${encodeURIComponent(gameId)}`, PUBLISH_TIMEOUT_MS); + // A photographer account may upload but not publish ("Insufficient permissions"). The file is already on + // the desk; failing the delivery here only blocks the queue. The desk publishes those itself. + if (match.status === 403) return; const ours = placeholder ? null : imageId; this.assertPublished(match, ours); } private async postApi(path: string, timeoutMs: number, retry = true): Promise<{ status: number; body: unknown }> { await this.login(); - const { res } = await this.request(`${this.base}/api${path}`, { method: "POST" }, 10, timeoutMs); + // The desk's own client posts JSON. An empty object is what "send now" sends when it has no extra fields. + const { res } = await this.request( + `${this.base}/api${path}`, + { + method: "POST", + headers: { "content-type": "application/json", accept: "application/json" }, + body: "{}", + }, + 10, + timeoutMs, + ); + // 401: logged out. 403 "Insufficient permissions": the cookie is still sent, but the session is no longer + // the photographer. One fresh sign-in. The same refusal after that is the account, not a stale login. if (res.status === 401 && retry) { this.account = null; this.jar.clear(); return this.postApi(path, timeoutMs, false); } + if (res.status === 403 && retry) { + this.account = null; + this.jar.clear(); + return this.postApi(path, timeoutMs, false); + } const text = await res.text(); let body: unknown = null; if (text) { diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index e61d090..8be7480 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -477,7 +477,9 @@ The same queue also delivers to SFTP, FTP/FTPS and Dropbox (`delivery/transports author (`artist`: the photographer's name from the IPTC creator). The upload only reaches the photographer's desk. The public game shows the photo after publish: `POST /api/photo-sync/`, or `POST /api/photo-sync/match/` when the desk has no single-image publish (the same call as - "Sincronizar com zerozero"). A delivery is done only after that call succeeds. The "folder" is the game: + "Sincronizar com zerozero"). A 403 signs in again once (a stale session uses that status, not 401). The same + refusal after the new login still counts as delivered: the file is on the desk. Any other publish failure fails + the delivery. The "folder" is the game: chosen for the event by pasting the game's link (kept in `events.zerozero_game_id`), or found among the desk's upcoming games by both teams and kick-off within 3 hours. The delivered item's remote path is `/` (`DELETE /api/images/` takes it back).