-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
media-organizer.ts
60 lines (51 loc) · 2.1 KB
/
media-organizer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import dayjs from "dayjs";
import SuperJSON from "superjson";
import { decryptSecret } from "@homarr/common";
import { EVERY_MINUTE } from "@homarr/cron-jobs-core/expressions";
import { db, eq } from "@homarr/db";
import { items } from "@homarr/db/schema/sqlite";
import { integrationCreatorByKind } from "@homarr/integrations";
import type { CalendarEvent } from "@homarr/integrations/types";
import { createItemAndIntegrationChannel } from "@homarr/redis";
// This import is done that way to avoid circular dependencies.
import type { WidgetComponentProps } from "../../../../widgets";
import { createCronJob } from "../../lib";
export const mediaOrganizerJob = createCronJob("mediaOrganizer", EVERY_MINUTE).withCallback(async () => {
const itemsForIntegration = await db.query.items.findMany({
where: eq(items.kind, "calendar"),
with: {
integrations: {
with: {
integration: {
with: {
secrets: {
columns: {
kind: true,
value: true,
},
},
},
},
},
},
},
});
for (const itemForIntegration of itemsForIntegration) {
for (const integration of itemForIntegration.integrations) {
const options = SuperJSON.parse<WidgetComponentProps<"calendar">["options"]>(itemForIntegration.options);
const start = dayjs().subtract(Number(options.filterPastMonths), "months").toDate();
const end = dayjs().add(Number(options.filterFutureMonths), "months").toDate();
const decryptedSecrets = integration.integration.secrets.map((secret) => ({
...secret,
value: decryptSecret(secret.value),
}));
const integrationInstance = integrationCreatorByKind(integration.integration.kind as "radarr" | "sonarr", {
...integration.integration,
decryptedSecrets,
});
const events = await integrationInstance.getCalendarEventsAsync(start, end);
const cache = createItemAndIntegrationChannel<CalendarEvent[]>("calendar", integration.integrationId);
await cache.setAsync(events);
}
}
});