Stop an expired event pin from filing later shots into its folder.

When the pin is gone from the agenda and its ingest window has closed, arrivals follow the capture time instead of the old event.
This commit is contained in:
spark committed 2026-11-26 22:39:55 +00:00
1 parent 0a845f8bc6
commit 5794660694
4 files changed
+77 -12

No files matched your search

+12 -3
View File
@@ -3,7 +3,7 @@ import type { EventSummary } from "@photoreport/sdk";
import { useAuth } from "../auth.js";
import { planLocalAutopilot } from "../offline/autopilot.js";
import { cache } from "../offline/cache.js";
import { eventForArrival, exifDateTime, exifLens, lisbonInstant, localPhoto, localPhotos } from "../offline/local.js";
import { eventForArrival, exifDateTime, exifLens, lisbonDay, lisbonInstant, localPhoto, localPhotos } from "../offline/local.js";
import { localId, offlineQueue } from "../offline/queue.js";
import { useCallback, useEffect, useRef, useState } from "react";
import { t } from "../i18n.js";
@@ -89,6 +89,12 @@ type Add = (
export const eventLabel = (e: Pick<EventSummary, "title" | "home" | "away">) => e.title || [e.home, e.away].filter(Boolean).join(" x ");
/** The pin's own ingest window, when the agenda no longer lists that event. */
export function pinWindow(to: FtpTarget | null): { ingestFrom: string; ingestUntil: string } | null {
if (!to?.ingestFrom || !to.ingestUntil) return null;
return { ingestFrom: to.ingestFrom, ingestUntil: to.ingestUntil };
}
function savedTarget(): FtpTarget | null {
try {
const v = JSON.parse(prefs.get("ftp.target") ?? "null") as FtpTarget | null;
@@ -177,7 +183,7 @@ export function useLocalFtp(add: Add, enabled: boolean, invoke: Invoke = tauriIn
const events = cached?.events ?? [];
const capturedAt = lisbonInstant(exifDateTime(preview));
// Open FTP target always; eventForArrival drops an archived leftover and keeps shots inside the window.
const eventId = eventForArrival(to?.id ?? null, events, capturedAt);
const eventId = eventForArrival(to?.id ?? null, events, capturedAt, pinWindow(to));
const filed = events.find((e) => e.id === eventId);
if (!localPhotos.bySha(info.sha256)) {
let url = "";
@@ -239,9 +245,12 @@ export function useLocalFtp(add: Add, enabled: boolean, invoke: Invoke = tauriIn
sentEvent = eventLabel(filed);
sentDate = filed.startsAt.slice(0, 10);
} else if (to && eventId === to.id) {
// The pin is not in the cached list (no capture time, or this computer has not seen the event).
// The pin is not in the cached list and its window still covers this shot.
sentEvent = to.title;
sentDate = to.date;
} else if (capturedAt) {
// No event for this time: file under the shoot day (Desktop/2026/20261126), not the expired pin.
sentDate = lisbonDay(capturedAt);
}
invoke("ftp_sent", { path: f.path, event: sentEvent, date: sentDate })
.then(() => {
+16 -5
View File
@@ -2,10 +2,10 @@ import type { EventSummary } from "@photoreport/sdk";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { t } from "../i18n.js";
import { cache } from "../offline/cache.js";
import { eventForArrival, exifDateTime, lisbonInstant } from "../offline/local.js";
import { eventForArrival, exifDateTime, lisbonDay, lisbonInstant } from "../offline/local.js";
import { retryPhotoFile } from "../desktop/lightroomSidecar.js";
import { asArrayBuffer, prefs } from "../platform.js";
import { type FtpTarget, type IncomingFile, type Invoke, eventLabel } from "./LocalFtp.js";
import { type FtpTarget, type IncomingFile, type Invoke, eventLabel, pinWindow } from "./LocalFtp.js";
import type { PreviewOf, UploadStatus } from "./uploads.js";
/** What the desktop shell reports for a PTP body on the USB cable (`camera.rs`). */
@@ -103,16 +103,27 @@ export function useUsbCamera(add: Add, enabled: boolean, target: FtpTarget | nul
const events = cached?.events ?? [];
const capturedAt = lisbonInstant(exifDateTime(bytes));
// Always the open USB target — never drop a live pull into Sem evento because the agenda cache is stale.
const eventId = eventForArrival(to?.id ?? null, events, capturedAt);
const eventId = eventForArrival(to?.id ?? null, events, capturedAt, pinWindow(to));
const filed = events.find((e) => e.id === eventId);
let sentEvent = "";
let sentDate = "";
if (filed) {
sentEvent = eventLabel(filed);
sentDate = filed.startsAt.slice(0, 10);
} else if (to && eventId === to.id) {
sentEvent = to.title;
sentDate = to.date;
} else if (capturedAt) {
sentDate = lisbonDay(capturedAt);
}
add([file], eventId ?? undefined, (_file, result) => {
inFlight.current--;
if (result === "done" || result === "duplicate") {
setCounts((c) => ({ ...c, sent: c.sent + 1 }));
invoke("ftp_sent", {
path: f.path,
event: filed ? eventLabel(filed) : (to?.title ?? ""),
date: filed ? filed.startsAt.slice(0, 10) : (to?.date ?? ""),
event: sentEvent,
date: sentDate,
})
.then(() => {
retryPhotoFile(original.sha256);
+29 -3
View File
@@ -102,6 +102,16 @@ function readTiff(v: DataView, start: number, end: number): { date: string | nul
};
}
/** Lisbon calendar day of an ISO instant, "YYYY-MM-DD" (the work-folder date). */
export function lisbonDay(iso: string): string {
return new Intl.DateTimeFormat("en-CA", {
timeZone: "Europe/Lisbon",
year: "numeric",
month: "2-digit",
day: "2-digit",
}).format(new Date(iso));
}
/** "2026:10:09 20:26:47" (Lisbon wall time, as the API reads it) to an ISO instant. */
export function lisbonInstant(exif: string | null): string | null {
const m = exif?.match(/^(\d{4}):(\d{2}):(\d{2}) (\d{2}):(\d{2}):(\d{2})/);
@@ -137,19 +147,35 @@ export function routeLocally(events: RoutedEvent[], at: string | null): string |
return bestEventForCapture(events, at)?.id ?? null;
}
function windowCovers(window: { ingestFrom: string; ingestUntil: string } | null | undefined, at: string): boolean {
if (!window) return true;
const t = Date.parse(at);
const from = Date.parse(window.ingestFrom);
const until = Date.parse(window.ingestUntil);
if (!Number.isFinite(t) || !Number.isFinite(from) || !Number.isFinite(until)) return true;
return from <= t && t <= until;
}
/**
* Where a photo arrives on this computer. With a capture time, the pin (FTP target / open event) wins only
* inside that event's window — leftover pins from the morning studio must not swallow the afternoon training.
* Without a capture time, keep the pin (same as the server): USB auto-pull already filtered by the window via
* the card clock, and inventing "now" is what we must not do.
* A pin missing from the agenda is kept only while its own window still covers the shot. An expired pin
* (the event dropped off the list) must not keep filing the next day into that folder.
*/
export function eventForArrival(pinnedId: string | null, events: RoutedEvent[], at: string | null): string | null {
export function eventForArrival(
pinnedId: string | null,
events: RoutedEvent[],
at: string | null,
pinWindow?: { ingestFrom: string; ingestUntil: string } | null,
): string | null {
if (!at) return pinnedId;
if (!pinnedId) return routeLocally(events, at);
const pinned = events.find((e) => e.id === pinnedId);
if (!pinned) {
// Agenda cache miss: keep the open pin so USB/FTP does not dump live shots into Sem evento.
return pinnedId;
if (windowCovers(pinWindow, at)) return pinnedId;
return routeLocally(events, at);
}
if (pinned.archivedAt) return routeLocally(events, at);
const t = Date.parse(at);
+20 -1
View File
@@ -150,8 +150,27 @@ describe("photos arriving on this computer", () => {
// No capture time: keep the pin (USB already filtered by the card clock; server keeps the named folder too).
expect(eventForArrival("braga", events, null)).toBe("braga");
expect(eventForArrival(null, events, null)).toBeNull();
// Cache miss for the pin id: keep it (do not dump into Sem evento); archived pins fall through to the clock.
// Cache miss for the pin id: keep it while its window is unknown or still open.
expect(eventForArrival("missing", events, "2026-10-10T15:30:00Z")).toBe("missing");
expect(
eventForArrival("missing", events, "2026-10-09T19:26:47.000Z", {
ingestFrom: "2026-10-09T17:15:00Z",
ingestUntil: "2026-10-09T23:15:00Z",
}),
).toBe("missing");
// The pin left the agenda and its window has closed: file by the clock, not into that old folder.
expect(
eventForArrival("studio", events, "2026-11-26T20:20:00Z", {
ingestFrom: "2026-11-24T11:08:00.000Z",
ingestUntil: "2026-11-24T17:08:00.000Z",
}),
).toBeNull();
expect(
eventForArrival("studio", events, "2026-10-10T15:30:00Z", {
ingestFrom: "2026-10-09T17:15:00Z",
ingestUntil: "2026-10-09T23:15:00Z",
}),
).toBe("other");
expect(eventForArrival("braga", [{ ...events[0]!, archivedAt: "2026-10-10T00:00:00Z" }, events[1]!], "2026-10-10T15:30:00Z")).toBe("other");
});