diff --git a/apps/api/src/delivery/zerozero.test.ts b/apps/api/src/delivery/zerozero.test.ts index ec2d513..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 () => { @@ -133,6 +151,11 @@ 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", + "/api/photo-sync/match/12627743", + ]); }); }); diff --git a/apps/api/src/delivery/zerozero.ts b/apps/api/src/delivery/zerozero.ts index d3cc0de..169a84b 100644 --- a/apps/api/src/delivery/zerozero.ts +++ b/apps/api/src/delivery/zerozero.ts @@ -292,12 +292,29 @@ export class ZerozeroClient { 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 8fbe32d..8be7480 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -477,8 +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 photographer account that is refused (403) still counts as delivered: the file - is on the desk, and that account cannot publish. Any other publish failure fails the delivery. 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).