feat: auto-assign new Immich albums via keyword rules
Auto-assignment rules were only applied when an admin clicked "Regeln jetzt anwenden". New albums created in Immich therefore never reached editorials automatically. - applyAllRules() syncs every active editorial's keywords against the full album list in one pass and grants matches as system actor - in-process scheduler (src/instrumentation.ts) runs the sync every AUTO_RULE_SYNC_INTERVAL_SECONDS (default 120s) - POST /api/cron/album-sync allows external triggers via CRON_SECRET - overlapping sync runs coalesce to avoid duplicate grants/emails
This commit is contained in:
1 parent
a3dc56778d
commit
dd60fd6ee6
14 files changed
+613
-36
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import "server-only";
|
||||
|
||||
import type { autoRuleService } from "@/modules/albums/auto-rules";
|
||||
|
||||
const noStoreHeaders = { "cache-control": "private, no-store" } as const;
|
||||
|
||||
export type AlbumSyncHandlerDependencies = {
|
||||
applyAllRules: typeof autoRuleService.applyAllRules;
|
||||
cronSecret: string | undefined;
|
||||
};
|
||||
|
||||
/**
|
||||
* Manual/external trigger for the album auto-rule sync (e.g. an uptime
|
||||
* monitor cron hitting this endpoint). Protected via CRON_SECRET bearer
|
||||
* token; disabled when no secret is configured.
|
||||
*/
|
||||
export function createAlbumSyncHandler({
|
||||
applyAllRules,
|
||||
cronSecret,
|
||||
}: AlbumSyncHandlerDependencies) {
|
||||
return async function POST(request: Request): Promise<Response> {
|
||||
if (!cronSecret) {
|
||||
return Response.json(
|
||||
{ code: "SYNC_DISABLED" },
|
||||
{ status: 404, headers: noStoreHeaders },
|
||||
);
|
||||
}
|
||||
|
||||
const authorization = request.headers.get("authorization");
|
||||
if (authorization !== `Bearer ${cronSecret}`) {
|
||||
return Response.json(
|
||||
{ code: "FORBIDDEN" },
|
||||
{ status: 403, headers: noStoreHeaders },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const result = await applyAllRules();
|
||||
return Response.json(result, { headers: noStoreHeaders });
|
||||
} catch (error) {
|
||||
console.error("[album-sync] failed:", error);
|
||||
return Response.json(
|
||||
{ code: "SYNC_FAILED" },
|
||||
{ status: 503, headers: noStoreHeaders },
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import { expect, test, vi } from "vitest";
|
||||
|
||||
import { createAlbumSyncHandler } from "./album-sync-handler";
|
||||
|
||||
const secret = "cron-secret-at-least-16-chars";
|
||||
|
||||
function post(handler: ReturnType<typeof createAlbumSyncHandler>, headers: Record<string, string>) {
|
||||
return handler(
|
||||
new Request("http://portal.test/api/cron/album-sync", {
|
||||
method: "POST",
|
||||
headers,
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
test("runs the sync and reports the granted count with a valid bearer token", async () => {
|
||||
const applyAllRules = vi.fn().mockResolvedValue({ granted: 3 });
|
||||
const handler = createAlbumSyncHandler({ applyAllRules, cronSecret: secret });
|
||||
|
||||
const response = await post(handler, { authorization: `Bearer ${secret}` });
|
||||
|
||||
expect(response.status).toBe(200);
|
||||
await expect(response.json()).resolves.toEqual({ granted: 3 });
|
||||
expect(applyAllRules).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
test("rejects a missing or wrong bearer token without running the sync", async () => {
|
||||
const applyAllRules = vi.fn();
|
||||
const handler = createAlbumSyncHandler({ applyAllRules, cronSecret: secret });
|
||||
|
||||
const missing = await post(handler, {});
|
||||
const wrong = await post(handler, { authorization: "Bearer nope" });
|
||||
|
||||
expect(missing.status).toBe(403);
|
||||
expect(wrong.status).toBe(403);
|
||||
expect(applyAllRules).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("reports the endpoint as disabled when no cron secret is configured", async () => {
|
||||
const applyAllRules = vi.fn();
|
||||
const handler = createAlbumSyncHandler({ applyAllRules, cronSecret: undefined });
|
||||
|
||||
const response = await post(handler, { authorization: `Bearer ${secret}` });
|
||||
|
||||
expect(response.status).toBe(404);
|
||||
expect(applyAllRules).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
test("maps sync failures to a retryable upstream error", async () => {
|
||||
const applyAllRules = vi.fn().mockRejectedValue(new Error("db down"));
|
||||
vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
const handler = createAlbumSyncHandler({ applyAllRules, cronSecret: secret });
|
||||
|
||||
const response = await post(handler, { authorization: `Bearer ${secret}` });
|
||||
|
||||
expect(response.status).toBe(503);
|
||||
await expect(response.json()).resolves.toEqual({ code: "SYNC_FAILED" });
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
@@ -0,0 +1,11 @@
|
||||
import "server-only";
|
||||
|
||||
import { autoRuleService } from "@/modules/albums/auto-rules";
|
||||
import { env } from "@/config/env";
|
||||
|
||||
import { createAlbumSyncHandler } from "./album-sync-handler";
|
||||
|
||||
export const POST = createAlbumSyncHandler({
|
||||
applyAllRules: (...args) => autoRuleService.applyAllRules(...args),
|
||||
cronSecret: env.CRON_SECRET,
|
||||
});
|
||||
Reference in new issue
Block a user