68 lines
2.5 KiB
TypeScript
68 lines
2.5 KiB
TypeScript
import { observable } from "@trpc/server/observable";
|
|
|
|
import type { Modify } from "@homarr/common/types";
|
|
import type { Integration } from "@homarr/db/schema";
|
|
import type { IntegrationKindByCategory } from "@homarr/definitions";
|
|
import { getIntegrationKindsByCategory } from "@homarr/definitions";
|
|
import type { MediaRelease } from "@homarr/integrations/types";
|
|
import { mediaReleaseRequestHandler } from "@homarr/request-handler/media-release";
|
|
|
|
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
|
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
|
|
|
export const mediaReleaseRouter = createTRPCRouter({
|
|
getMediaReleases: publicProcedure
|
|
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRelease")))
|
|
.query(async ({ ctx }) => {
|
|
const results = await Promise.all(
|
|
ctx.integrations.map(async (integration) => {
|
|
const innerHandler = mediaReleaseRequestHandler.handler(integration, {});
|
|
const { data, timestamp } = await innerHandler.getCachedOrUpdatedDataAsync({ forceUpdate: false });
|
|
|
|
return {
|
|
integration: {
|
|
id: integration.id,
|
|
name: integration.name,
|
|
kind: integration.kind,
|
|
updatedAt: timestamp,
|
|
},
|
|
releases: data,
|
|
};
|
|
}),
|
|
);
|
|
return results.flatMap((result) =>
|
|
result.releases.map((release) => ({
|
|
...release,
|
|
integration: result.integration,
|
|
})),
|
|
);
|
|
}),
|
|
|
|
subscribeToReleases: publicProcedure
|
|
.concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("mediaRelease")))
|
|
.subscription(({ ctx }) => {
|
|
return observable<{
|
|
integration: Modify<Integration, { kind: IntegrationKindByCategory<"mediaRelease"> }>;
|
|
releases: MediaRelease[];
|
|
}>((emit) => {
|
|
const unsubscribes: (() => void)[] = [];
|
|
for (const integrationWithSecrets of ctx.integrations) {
|
|
const { decryptedSecrets: _, ...integration } = integrationWithSecrets;
|
|
const innerHandler = mediaReleaseRequestHandler.handler(integrationWithSecrets, {});
|
|
const unsubscribe = innerHandler.subscribe((releases) => {
|
|
emit.next({
|
|
integration,
|
|
releases,
|
|
});
|
|
});
|
|
unsubscribes.push(unsubscribe);
|
|
}
|
|
return () => {
|
|
unsubscribes.forEach((unsubscribe) => {
|
|
unsubscribe();
|
|
});
|
|
};
|
|
});
|
|
}),
|
|
});
|