feat: add rss widget (#760)
Co-authored-by: SeDemal <demal.sebastien@bluewin.ch>
This commit is contained in:
29
packages/api/src/middlewares/item.ts
Normal file
29
packages/api/src/middlewares/item.ts
Normal file
@@ -0,0 +1,29 @@
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
import { and, eq } from "@homarr/db";
|
||||
import { items } from "@homarr/db/schema/sqlite";
|
||||
import type { WidgetKind } from "@homarr/definitions";
|
||||
import { z } from "@homarr/validation";
|
||||
|
||||
import { publicProcedure } from "../trpc";
|
||||
|
||||
export const createOneItemMiddleware = (kind: WidgetKind) => {
|
||||
return publicProcedure.input(z.object({ itemId: z.string() })).use(async ({ input, ctx, next }) => {
|
||||
const item = await ctx.db.query.items.findFirst({
|
||||
where: and(eq(items.id, input.itemId), eq(items.kind, kind)),
|
||||
});
|
||||
|
||||
if (!item) {
|
||||
throw new TRPCError({
|
||||
code: "NOT_FOUND",
|
||||
message: `Item with id ${input.itemId} not found`,
|
||||
});
|
||||
}
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
item,
|
||||
},
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -4,6 +4,7 @@ import { calendarRouter } from "./calendar";
|
||||
import { dnsHoleRouter } from "./dns-hole";
|
||||
import { mediaServerRouter } from "./media-server";
|
||||
import { notebookRouter } from "./notebook";
|
||||
import { rssFeedRouter } from "./rssFeed";
|
||||
import { smartHomeRouter } from "./smart-home";
|
||||
import { weatherRouter } from "./weather";
|
||||
|
||||
@@ -15,4 +16,5 @@ export const widgetRouter = createTRPCRouter({
|
||||
smartHome: smartHomeRouter,
|
||||
mediaServer: mediaServerRouter,
|
||||
calendar: calendarRouter,
|
||||
rssFeed: rssFeedRouter,
|
||||
});
|
||||
|
||||
12
packages/api/src/router/widgets/rssFeed.ts
Normal file
12
packages/api/src/router/widgets/rssFeed.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { RssFeed } from "@homarr/cron-jobs";
|
||||
import { createItemChannel } from "@homarr/redis";
|
||||
|
||||
import { createOneItemMiddleware } from "../../middlewares/item";
|
||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||
|
||||
export const rssFeedRouter = createTRPCRouter({
|
||||
getFeeds: publicProcedure.unstable_concat(createOneItemMiddleware("rssFeed")).query(async ({ input }) => {
|
||||
const channel = createItemChannel<RssFeed[]>(input.itemId);
|
||||
return await channel.getAsync();
|
||||
}),
|
||||
});
|
||||
Reference in New Issue
Block a user