feat: downloads widget (#844)

Usenet and Torrent downloads in 1 widget.
sabNZBd, NzbGet, Deluge, qBitTorrent, and transmission support.
Columns can be reordered in Edit mode.
Sorting enabled.
Time uses Dayjs with auto translation.
Can pause/resume single items, clients, or all.
Can delete items (With option to delete assossiated files).
Clients list and details.
Include all filtering and processing for ratio from oldmarr torrent widget.
Invalidation of old data (older than 30 seconds) to show an integration is not responding anymore.

Misc (So many miscs):
Fixed validation error with multiText.
Fixed translation application for multiSelect to behave the same as select.
Added background to gitignore (I needed to add a background to visually test opacity, probably will in the future too)
Added setOptions to frontend components so potential updates made from the Dashboard can be saved.
Extracted background and border color to use in widgets.
humanFileSize function based on the si format (powers of 1024, not 1000).
Improved integrationCreatorByKind by @Meierschlumpf.
Changed integrationCreatorByKind to integrationCreator so it functions directly from the integration.
Added integrationCreatorFromSecrets to directly work with secrets from db.
Added getIntegrationKindsByCategory to get a list of integrations sharing categories.
Added IntegrationKindByCategory type to get the types possible for a category (Great to cast on integration.kind that isn't already properly limited/typed but for which we know the limitation)
Added a common AtLeastOneOf type. Applied to TKind and IntegrationSecretKind[] where it was already being used and Added to the getIntegrationKindsByCategory's output to be more freely used.
Added the Modify type, instead of omiting to then add again just to change a parameters type, use the modify instead. Applied code wide already.
Hook to get list of integration depending on permission level of user. (By @Meierschlumpf)
This commit is contained in:
Manuel
2024-09-11 17:30:21 +02:00
committed by GitHub
parent 3d4e607a9d
commit 2535192b2c
81 changed files with 4176 additions and 390 deletions

View File

@@ -3,7 +3,8 @@ import { TRPCError } from "@trpc/server";
import type { Session } from "@homarr/auth";
import { hasQueryAccessToIntegrationsAsync } from "@homarr/auth/server";
import { constructIntegrationPermissions } from "@homarr/auth/shared";
import { decryptSecret } from "@homarr/common";
import { decryptSecret } from "@homarr/common/server";
import type { AtLeastOneOf } from "@homarr/common/types";
import type { Database } from "@homarr/db";
import { and, eq, inArray } from "@homarr/db";
import { integrations } from "@homarr/db/schema/sqlite";
@@ -12,7 +13,7 @@ import { z } from "@homarr/validation";
import { publicProcedure } from "../trpc";
type IntegrationAction = "query" | "interact";
export type IntegrationAction = "query" | "interact";
/**
* Creates a middleware that provides the integration in the context that is of the specified kinds
@@ -25,7 +26,7 @@ type IntegrationAction = "query" | "interact";
*/
export const createOneIntegrationMiddleware = <TKind extends IntegrationKind>(
action: IntegrationAction,
...kinds: [TKind, ...TKind[]] // Ensure at least one kind is provided
...kinds: AtLeastOneOf<TKind> // Ensure at least one kind is provided
) => {
return publicProcedure.input(z.object({ integrationId: z.string() })).use(async ({ input, ctx, next }) => {
const integration = await ctx.db.query.integrations.findFirst({
@@ -95,7 +96,7 @@ export const createOneIntegrationMiddleware = <TKind extends IntegrationKind>(
*/
export const createManyIntegrationMiddleware = <TKind extends IntegrationKind>(
action: IntegrationAction,
...kinds: [TKind, ...TKind[]] // Ensure at least one kind is provided
...kinds: AtLeastOneOf<TKind> // Ensure at least one kind is provided
) => {
return publicProcedure
.input(z.object({ integrationIds: z.array(z.string()).min(1) }))
@@ -161,7 +162,7 @@ export const createManyIntegrationMiddleware = <TKind extends IntegrationKind>(
*/
export const createManyIntegrationOfOneItemMiddleware = <TKind extends IntegrationKind>(
action: IntegrationAction,
...kinds: [TKind, ...TKind[]] // Ensure at least one kind is provided
...kinds: AtLeastOneOf<TKind> // Ensure at least one kind is provided
) => {
return publicProcedure
.input(z.object({ integrationIds: z.array(z.string()).min(1), itemId: z.string() }))

View File

@@ -1,6 +1,6 @@
import { TRPCError } from "@trpc/server";
import { decryptSecret, encryptSecret } from "@homarr/common";
import { decryptSecret, encryptSecret } from "@homarr/common/server";
import type { Database } from "@homarr/db";
import { and, createId, eq, inArray } from "@homarr/db";
import {

View File

@@ -1,8 +1,8 @@
import { decryptSecret } from "@homarr/common";
import { decryptSecret } from "@homarr/common/server";
import type { Integration } from "@homarr/db/schema/sqlite";
import type { IntegrationKind, IntegrationSecretKind } from "@homarr/definitions";
import { getAllSecretKindOptions } from "@homarr/definitions";
import { integrationCreatorByKind, IntegrationTestConnectionError } from "@homarr/integrations";
import { integrationCreator, IntegrationTestConnectionError } from "@homarr/integrations";
type FormIntegration = Integration & {
secrets: {
@@ -37,23 +37,25 @@ export const testConnectionAsync = async (
const sourcedSecrets = [...formSecrets, ...decryptedDbSecrets];
const secretKinds = getSecretKindOption(integration.kind, sourcedSecrets);
const filteredSecrets = secretKinds.map((kind) => {
const secrets = sourcedSecrets.filter((secret) => secret.kind === kind);
// Will never be undefined because of the check before
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (secrets.length === 1) return secrets[0]!;
const decryptedSecrets = secretKinds
.map((kind) => {
const secrets = sourcedSecrets.filter((secret) => secret.kind === kind);
// Will never be undefined because of the check before
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
if (secrets.length === 1) return secrets[0]!;
// There will always be a matching secret because of the getSecretKindOption function
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return secrets.find((secret) => secret.source === "form") ?? secrets[0]!;
});
// There will always be a matching secret because of the getSecretKindOption function
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return secrets.find((secret) => secret.source === "form") ?? secrets[0]!;
})
.map(({ source: _, ...secret }) => secret);
// @ts-expect-error - For now we expect an error here as not all integerations have been implemented
const integrationInstance = integrationCreatorByKind(integration.kind, {
id: integration.id,
name: integration.name,
url: integration.url,
decryptedSecrets: filteredSecrets,
const { secrets: _, ...baseIntegration } = integration;
// @ts-expect-error - For now we expect an error here as not all integrations have been implemented
const integrationInstance = integrationCreator({
...baseIntegration,
decryptedSecrets,
});
await integrationInstance.testConnectionAsync();

View File

@@ -2,7 +2,7 @@
import { describe, expect, test, vi } from "vitest";
import type { Session } from "@homarr/auth";
import { encryptSecret } from "@homarr/common";
import { encryptSecret } from "@homarr/common/server";
import { createId } from "@homarr/db";
import { integrations, integrationSecrets } from "@homarr/db/schema/sqlite";
import { createDb } from "@homarr/db/test";

View File

@@ -5,9 +5,9 @@ import * as homarrIntegrations from "@homarr/integrations";
import { testConnectionAsync } from "../../integration/integration-test-connection";
vi.mock("@homarr/common", async (importActual) => {
vi.mock("@homarr/common/server", async (importActual) => {
// eslint-disable-next-line @typescript-eslint/consistent-type-imports
const actual = await importActual<typeof import("@homarr/common")>();
const actual = await importActual<typeof import("@homarr/common/server")>();
return {
...actual,
@@ -18,7 +18,7 @@ vi.mock("@homarr/common", async (importActual) => {
describe("testConnectionAsync should run test connection of integration", () => {
test("with input of only form secrets matching api key kind it should use form apiKey", async () => {
// Arrange
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreatorByKind");
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreator");
const optionsSpy = vi.spyOn(homarrDefinitions, "getAllSecretKindOptions");
factorySpy.mockReturnValue({
testConnectionAsync: async () => await Promise.resolve(),
@@ -42,10 +42,11 @@ describe("testConnectionAsync should run test connection of integration", () =>
await testConnectionAsync(integration);
// Assert
expect(factorySpy).toHaveBeenCalledWith("piHole", {
expect(factorySpy).toHaveBeenCalledWith({
id: "new",
name: "Pi Hole",
url: "http://pi.hole",
kind: "piHole",
decryptedSecrets: [
expect.objectContaining({
kind: "apiKey",
@@ -57,7 +58,7 @@ describe("testConnectionAsync should run test connection of integration", () =>
test("with input of only null form secrets and the required db secrets matching api key kind it should use db apiKey", async () => {
// Arrange
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreatorByKind");
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreator");
const optionsSpy = vi.spyOn(homarrDefinitions, "getAllSecretKindOptions");
factorySpy.mockReturnValue({
testConnectionAsync: async () => await Promise.resolve(),
@@ -88,10 +89,11 @@ describe("testConnectionAsync should run test connection of integration", () =>
await testConnectionAsync(integration, dbSecrets);
// Assert
expect(factorySpy).toHaveBeenCalledWith("piHole", {
expect(factorySpy).toHaveBeenCalledWith({
id: "new",
name: "Pi Hole",
url: "http://pi.hole",
kind: "piHole",
decryptedSecrets: [
expect.objectContaining({
kind: "apiKey",
@@ -103,7 +105,7 @@ describe("testConnectionAsync should run test connection of integration", () =>
test("with input of form and db secrets matching api key kind it should use form apiKey", async () => {
// Arrange
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreatorByKind");
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreator");
const optionsSpy = vi.spyOn(homarrDefinitions, "getAllSecretKindOptions");
factorySpy.mockReturnValue({
testConnectionAsync: async () => await Promise.resolve(),
@@ -134,10 +136,11 @@ describe("testConnectionAsync should run test connection of integration", () =>
await testConnectionAsync(integration, dbSecrets);
// Assert
expect(factorySpy).toHaveBeenCalledWith("piHole", {
expect(factorySpy).toHaveBeenCalledWith({
id: "new",
name: "Pi Hole",
url: "http://pi.hole",
kind: "piHole",
decryptedSecrets: [
expect.objectContaining({
kind: "apiKey",
@@ -149,7 +152,7 @@ describe("testConnectionAsync should run test connection of integration", () =>
test("with input of form apiKey and db secrets for username and password it should use form apiKey when both is allowed", async () => {
// Arrange
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreatorByKind");
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreator");
const optionsSpy = vi.spyOn(homarrDefinitions, "getAllSecretKindOptions");
factorySpy.mockReturnValue({
testConnectionAsync: async () => await Promise.resolve(),
@@ -184,10 +187,11 @@ describe("testConnectionAsync should run test connection of integration", () =>
await testConnectionAsync(integration, dbSecrets);
// Assert
expect(factorySpy).toHaveBeenCalledWith("piHole", {
expect(factorySpy).toHaveBeenCalledWith({
id: "new",
name: "Pi Hole",
url: "http://pi.hole",
kind: "piHole",
decryptedSecrets: [
expect.objectContaining({
kind: "apiKey",
@@ -199,7 +203,7 @@ describe("testConnectionAsync should run test connection of integration", () =>
test("with input of null form apiKey and db secrets for username and password it should use db username and password when both is allowed", async () => {
// Arrange
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreatorByKind");
const factorySpy = vi.spyOn(homarrIntegrations, "integrationCreator");
const optionsSpy = vi.spyOn(homarrDefinitions, "getAllSecretKindOptions");
factorySpy.mockReturnValue({
testConnectionAsync: async () => await Promise.resolve(),
@@ -234,10 +238,11 @@ describe("testConnectionAsync should run test connection of integration", () =>
await testConnectionAsync(integration, dbSecrets);
// Assert
expect(factorySpy).toHaveBeenCalledWith("piHole", {
expect(factorySpy).toHaveBeenCalledWith({
id: "new",
name: "Pi Hole",
url: "http://pi.hole",
kind: "piHole",
decryptedSecrets: [
expect.objectContaining({
kind: "username",

View File

@@ -1,3 +1,4 @@
import { getIntegrationKindsByCategory } from "@homarr/definitions";
import type { CalendarEvent } from "@homarr/integrations/types";
import { createItemAndIntegrationChannel } from "@homarr/redis";
@@ -6,7 +7,7 @@ import { createTRPCRouter, publicProcedure } from "../../trpc";
export const calendarRouter = createTRPCRouter({
findAllEvents: publicProcedure
.unstable_concat(createManyIntegrationOfOneItemMiddleware("query", "sonarr", "radarr", "readarr", "lidarr"))
.unstable_concat(createManyIntegrationOfOneItemMiddleware("query", ...getIntegrationKindsByCategory("calendar")))
.query(async ({ ctx }) => {
return await Promise.all(
ctx.integrations.flatMap(async (integration) => {

View File

@@ -1,6 +1,7 @@
import { TRPCError } from "@trpc/server";
import { AdGuardHomeIntegration, PiHoleIntegration } from "@homarr/integrations";
import { getIntegrationKindsByCategory } from "@homarr/definitions";
import { integrationCreator } from "@homarr/integrations";
import type { DnsHoleSummary } from "@homarr/integrations/types";
import { logger } from "@homarr/log";
import { createCacheChannel } from "@homarr/redis";
@@ -11,21 +12,13 @@ import { createTRPCRouter, publicProcedure } from "../../trpc";
export const dnsHoleRouter = createTRPCRouter({
summary: publicProcedure
.unstable_concat(createManyIntegrationMiddleware("query", "piHole", "adGuardHome"))
.unstable_concat(createManyIntegrationMiddleware("query", ...getIntegrationKindsByCategory("dnsHole")))
.query(async ({ ctx }) => {
const results = await Promise.all(
ctx.integrations.map(async (integration) => {
const cache = createCacheChannel<DnsHoleSummary>(`dns-hole-summary:${integration.id}`);
const { data } = await cache.consumeAsync(async () => {
let client;
switch (integration.kind) {
case "piHole":
client = new PiHoleIntegration(integration);
break;
case "adGuardHome":
client = new AdGuardHomeIntegration(integration);
break;
}
const client = integrationCreator(integration);
return await client.getSummaryAsync().catch((err) => {
logger.error("dns-hole router - ", err);
@@ -47,33 +40,17 @@ export const dnsHoleRouter = createTRPCRouter({
}),
enable: publicProcedure
.unstable_concat(createOneIntegrationMiddleware("interact", "piHole", "adGuardHome"))
.mutation(async ({ ctx }) => {
let client;
switch (ctx.integration.kind) {
case "piHole":
client = new PiHoleIntegration(ctx.integration);
break;
case "adGuardHome":
client = new AdGuardHomeIntegration(ctx.integration);
break;
}
.unstable_concat(createOneIntegrationMiddleware("interact", ...getIntegrationKindsByCategory("dnsHole")))
.mutation(async ({ ctx: { integration } }) => {
const client = integrationCreator(integration);
await client.enableAsync();
}),
disable: publicProcedure
.input(controlsInputSchema)
.unstable_concat(createOneIntegrationMiddleware("interact", "piHole", "adGuardHome"))
.mutation(async ({ ctx, input }) => {
let client;
switch (ctx.integration.kind) {
case "piHole":
client = new PiHoleIntegration(ctx.integration);
break;
case "adGuardHome":
client = new AdGuardHomeIntegration(ctx.integration);
break;
}
.unstable_concat(createOneIntegrationMiddleware("interact", ...getIntegrationKindsByCategory("dnsHole")))
.mutation(async ({ ctx: { integration }, input }) => {
const client = integrationCreator(integration);
await client.disableAsync(input.duration);
}),
});

View File

@@ -0,0 +1,110 @@
import { observable } from "@trpc/server/observable";
import type { Integration } from "@homarr/db/schema/sqlite";
import { getIntegrationKindsByCategory } from "@homarr/definitions";
import type { DownloadClientJobsAndStatus } from "@homarr/integrations";
import { downloadClientItemSchema, integrationCreator } from "@homarr/integrations";
import { createItemAndIntegrationChannel } from "@homarr/redis";
import { z } from "@homarr/validation";
import type { IntegrationAction } from "../../middlewares/integration";
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trpc";
const createDownloadClientIntegrationMiddleware = (action: IntegrationAction) =>
createManyIntegrationMiddleware(action, ...getIntegrationKindsByCategory("downloadClient"));
export const downloadsRouter = createTRPCRouter({
getJobsAndStatuses: publicProcedure
.unstable_concat(createDownloadClientIntegrationMiddleware("query"))
.query(async ({ ctx }) => {
return await Promise.all(
ctx.integrations.map(async ({ decryptedSecrets: _, ...integration }) => {
const channel = createItemAndIntegrationChannel<DownloadClientJobsAndStatus>("downloads", integration.id);
const { data, timestamp } = (await channel.getAsync()) ?? { data: null, timestamp: new Date(0) };
return {
integration,
timestamp,
data,
};
}),
);
}),
subscribeToJobsAndStatuses: publicProcedure
.unstable_concat(createDownloadClientIntegrationMiddleware("query"))
.subscription(({ ctx }) => {
return observable<{ integration: Integration; timestamp: Date; data: DownloadClientJobsAndStatus }>((emit) => {
const unsubscribes: (() => void)[] = [];
for (const integrationWithSecrets of ctx.integrations) {
const { decryptedSecrets: _, ...integration } = integrationWithSecrets;
const channel = createItemAndIntegrationChannel<DownloadClientJobsAndStatus>("downloads", integration.id);
const unsubscribe = channel.subscribe((data) => {
emit.next({
integration,
timestamp: new Date(),
data,
});
});
unsubscribes.push(unsubscribe);
}
return () => {
unsubscribes.forEach((unsubscribe) => {
unsubscribe();
});
};
});
}),
pause: protectedProcedure
.unstable_concat(createDownloadClientIntegrationMiddleware("interact"))
.mutation(async ({ ctx }) => {
await Promise.all(
ctx.integrations.map(async (integration) => {
const integrationInstance = integrationCreator(integration);
await integrationInstance.pauseQueueAsync();
}),
);
}),
pauseItem: protectedProcedure
.unstable_concat(createDownloadClientIntegrationMiddleware("interact"))
.input(z.object({ item: downloadClientItemSchema }))
.mutation(async ({ ctx, input }) => {
await Promise.all(
ctx.integrations.map(async (integration) => {
const integrationInstance = integrationCreator(integration);
await integrationInstance.pauseItemAsync(input.item);
}),
);
}),
resume: protectedProcedure
.unstable_concat(createDownloadClientIntegrationMiddleware("interact"))
.mutation(async ({ ctx }) => {
await Promise.all(
ctx.integrations.map(async (integration) => {
const integrationInstance = integrationCreator(integration);
await integrationInstance.resumeQueueAsync();
}),
);
}),
resumeItem: protectedProcedure
.unstable_concat(createDownloadClientIntegrationMiddleware("interact"))
.input(z.object({ item: downloadClientItemSchema }))
.mutation(async ({ ctx, input }) => {
await Promise.all(
ctx.integrations.map(async (integration) => {
const integrationInstance = integrationCreator(integration);
await integrationInstance.resumeItemAsync(input.item);
}),
);
}),
deleteItem: protectedProcedure
.unstable_concat(createDownloadClientIntegrationMiddleware("interact"))
.input(z.object({ item: downloadClientItemSchema, fromDisk: z.boolean() }))
.mutation(async ({ ctx, input }) => {
await Promise.all(
ctx.integrations.map(async (integration) => {
const integrationInstance = integrationCreator(integration);
await integrationInstance.deleteItemAsync(input.item, input.fromDisk);
}),
);
}),
});

View File

@@ -2,6 +2,7 @@ import { createTRPCRouter } from "../../trpc";
import { appRouter } from "./app";
import { calendarRouter } from "./calendar";
import { dnsHoleRouter } from "./dns-hole";
import { downloadsRouter } from "./downloads";
import { indexerManagerRouter } from "./indexer-manager";
import { mediaRequestsRouter } from "./media-requests";
import { mediaServerRouter } from "./media-server";
@@ -18,6 +19,7 @@ export const widgetRouter = createTRPCRouter({
smartHome: smartHomeRouter,
mediaServer: mediaServerRouter,
calendar: calendarRouter,
downloads: downloadsRouter,
mediaRequests: mediaRequestsRouter,
rssFeed: rssFeedRouter,
indexerManager: indexerManagerRouter,

View File

@@ -1,21 +1,26 @@
import { TRPCError } from "@trpc/server";
import { observable } from "@trpc/server/observable";
import { integrationCreatorByKind } from "@homarr/integrations";
import { getIntegrationKindsByCategory } from "@homarr/definitions";
import { integrationCreator } from "@homarr/integrations";
import type { Indexer } from "@homarr/integrations/types";
import { logger } from "@homarr/log";
import { createItemAndIntegrationChannel } from "@homarr/redis";
import type { IntegrationAction } from "../../middlewares/integration";
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
import { createTRPCRouter, publicProcedure } from "../../trpc";
const createIndexerManagerIntegrationMiddleware = (action: IntegrationAction) =>
createManyIntegrationMiddleware(action, ...getIntegrationKindsByCategory("indexerManager"));
export const indexerManagerRouter = createTRPCRouter({
getIndexersStatus: publicProcedure
.unstable_concat(createManyIntegrationMiddleware("query", "prowlarr"))
.unstable_concat(createIndexerManagerIntegrationMiddleware("query"))
.query(async ({ ctx }) => {
const results = await Promise.all(
ctx.integrations.map(async (integration) => {
const client = integrationCreatorByKind(integration.kind, integration);
const client = integrationCreator(integration);
const indexers = await client.getIndexersAsync().catch((err) => {
logger.error("indexer-manager router - ", err);
throw new TRPCError({
@@ -34,7 +39,7 @@ export const indexerManagerRouter = createTRPCRouter({
}),
subscribeIndexersStatus: publicProcedure
.unstable_concat(createManyIntegrationMiddleware("query", "prowlarr"))
.unstable_concat(createIndexerManagerIntegrationMiddleware("query"))
.subscription(({ ctx }) => {
return observable<{ integrationId: string; indexers: Indexer[] }>((emit) => {
const unsubscribes: (() => void)[] = [];
@@ -57,11 +62,11 @@ export const indexerManagerRouter = createTRPCRouter({
}),
testAllIndexers: publicProcedure
.unstable_concat(createManyIntegrationMiddleware("interact", "prowlarr"))
.unstable_concat(createIndexerManagerIntegrationMiddleware("interact"))
.mutation(async ({ ctx }) => {
await Promise.all(
ctx.integrations.map(async (integration) => {
const client = integrationCreatorByKind(integration.kind, integration);
const client = integrationCreator(integration);
await client.testAllAsync().catch((err) => {
logger.error("indexer-manager router - ", err);
throw new TRPCError({

View File

@@ -1,5 +1,6 @@
import { getIntegrationKindsByCategory } from "@homarr/definitions";
import type { MediaRequestList, MediaRequestStats } from "@homarr/integrations";
import { integrationCreatorByKind } from "@homarr/integrations";
import { integrationCreator } from "@homarr/integrations";
import { createItemAndIntegrationChannel } from "@homarr/redis";
import { z } from "@homarr/validation";
@@ -11,7 +12,9 @@ import { createTRPCRouter, protectedProcedure, publicProcedure } from "../../trp
export const mediaRequestsRouter = createTRPCRouter({
getLatestRequests: publicProcedure
.unstable_concat(createManyIntegrationOfOneItemMiddleware("query", "overseerr", "jellyseerr"))
.unstable_concat(
createManyIntegrationOfOneItemMiddleware("query", ...getIntegrationKindsByCategory("mediaRequest")),
)
.query(async ({ input }) => {
return await Promise.all(
input.integrationIds.map(async (integrationId) => {
@@ -21,7 +24,9 @@ export const mediaRequestsRouter = createTRPCRouter({
);
}),
getStats: publicProcedure
.unstable_concat(createManyIntegrationOfOneItemMiddleware("query", "overseerr", "jellyseerr"))
.unstable_concat(
createManyIntegrationOfOneItemMiddleware("query", ...getIntegrationKindsByCategory("mediaRequest")),
)
.query(async ({ input }) => {
return await Promise.all(
input.integrationIds.map(async (integrationId) => {
@@ -34,15 +39,15 @@ export const mediaRequestsRouter = createTRPCRouter({
);
}),
answerRequest: protectedProcedure
.unstable_concat(createOneIntegrationMiddleware("interact", "overseerr", "jellyseerr"))
.unstable_concat(createOneIntegrationMiddleware("interact", ...getIntegrationKindsByCategory("mediaRequest")))
.input(z.object({ requestId: z.number(), answer: z.enum(["approve", "decline"]) }))
.mutation(async ({ ctx, input }) => {
const integration = integrationCreatorByKind(ctx.integration.kind, ctx.integration);
.mutation(async ({ ctx: { integration }, input }) => {
const integrationInstance = integrationCreator(integration);
if (input.answer === "approve") {
await integration.approveRequestAsync(input.requestId);
await integrationInstance.approveRequestAsync(input.requestId);
return;
}
await integration.declineRequestAsync(input.requestId);
await integrationInstance.declineRequestAsync(input.requestId);
}),
});

View File

@@ -1,14 +1,19 @@
import { observable } from "@trpc/server/observable";
import { getIntegrationKindsByCategory } from "@homarr/definitions";
import type { StreamSession } from "@homarr/integrations";
import { createItemAndIntegrationChannel } from "@homarr/redis";
import type { IntegrationAction } from "../../middlewares/integration";
import { createManyIntegrationMiddleware } from "../../middlewares/integration";
import { createTRPCRouter, publicProcedure } from "../../trpc";
const createMediaServerIntegrationMiddleware = (action: IntegrationAction) =>
createManyIntegrationMiddleware(action, ...getIntegrationKindsByCategory("mediaService"));
export const mediaServerRouter = createTRPCRouter({
getCurrentStreams: publicProcedure
.unstable_concat(createManyIntegrationMiddleware("query", "jellyfin", "plex"))
.unstable_concat(createMediaServerIntegrationMiddleware("query"))
.query(async ({ ctx }) => {
return await Promise.all(
ctx.integrations.map(async (integration) => {
@@ -22,7 +27,7 @@ export const mediaServerRouter = createTRPCRouter({
);
}),
subscribeToCurrentStreams: publicProcedure
.unstable_concat(createManyIntegrationMiddleware("query", "jellyfin", "plex"))
.unstable_concat(createMediaServerIntegrationMiddleware("query"))
.subscription(({ ctx }) => {
return observable<{ integrationId: string; data: StreamSession[] }>((emit) => {
const unsubscribes: (() => void)[] = [];

View File

@@ -1,12 +1,17 @@
import { observable } from "@trpc/server/observable";
import { HomeAssistantIntegration } from "@homarr/integrations";
import { getIntegrationKindsByCategory } from "@homarr/definitions";
import { integrationCreator } from "@homarr/integrations";
import { homeAssistantEntityState } from "@homarr/redis";
import { z } from "@homarr/validation";
import type { IntegrationAction } from "../../middlewares/integration";
import { createOneIntegrationMiddleware } from "../../middlewares/integration";
import { createTRPCRouter, publicProcedure } from "../../trpc";
const createSmartHomeIntegrationMiddleware = (action: IntegrationAction) =>
createOneIntegrationMiddleware(action, ...getIntegrationKindsByCategory("smartHomeServer"));
export const smartHomeRouter = createTRPCRouter({
subscribeEntityState: publicProcedure.input(z.object({ entityId: z.string() })).subscription(({ input }) => {
return observable<{
@@ -26,17 +31,17 @@ export const smartHomeRouter = createTRPCRouter({
});
}),
switchEntity: publicProcedure
.unstable_concat(createOneIntegrationMiddleware("interact", "homeAssistant"))
.unstable_concat(createSmartHomeIntegrationMiddleware("interact"))
.input(z.object({ entityId: z.string() }))
.mutation(async ({ ctx, input }) => {
const client = new HomeAssistantIntegration(ctx.integration);
.mutation(async ({ ctx: { integration }, input }) => {
const client = integrationCreator(integration);
return await client.triggerToggleAsync(input.entityId);
}),
executeAutomation: publicProcedure
.unstable_concat(createOneIntegrationMiddleware("interact", "homeAssistant"))
.unstable_concat(createSmartHomeIntegrationMiddleware("interact"))
.input(z.object({ automationId: z.string() }))
.mutation(async ({ input, ctx }) => {
const client = new HomeAssistantIntegration(ctx.integration);
.mutation(async ({ ctx: { integration }, input }) => {
const client = integrationCreator(integration);
await client.triggerAutomationAsync(input.automationId);
}),
});