feat(rss-feed): add periodic live updates (#5449)
This commit is contained in:
1 parent
bdf9439e49
commit
faffb818f9
4 files changed
+83
-45
No files matched your search
@@ -1,3 +1,4 @@
|
|||||||
|
import { observable } from "@trpc/server/observable";
|
||||||
import SuperJSON from "superjson";
|
import SuperJSON from "superjson";
|
||||||
import { z } from "zod/v4";
|
import { z } from "zod/v4";
|
||||||
|
|
||||||
@@ -5,45 +6,59 @@ import type { Session } from "@homarr/auth";
|
|||||||
import type { Database } from "@homarr/db";
|
import type { Database } from "@homarr/db";
|
||||||
import { eq } from "@homarr/db";
|
import { eq } from "@homarr/db";
|
||||||
import { items, users } from "@homarr/db/schema";
|
import { items, users } from "@homarr/db/schema";
|
||||||
|
import type { ExtendedFeedEntry } from "@homarr/request-handler/rss-feeds";
|
||||||
import { rssFeedsRequestHandler } from "@homarr/request-handler/rss-feeds";
|
import { rssFeedsRequestHandler } from "@homarr/request-handler/rss-feeds";
|
||||||
|
|
||||||
import type { WidgetComponentProps } from "../../../../widgets/src";
|
import type { WidgetComponentProps } from "../../../../widgets/src";
|
||||||
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
import { createTRPCRouter, publicProcedure } from "../../trpc";
|
||||||
|
|
||||||
|
const feedsInput = z.object({
|
||||||
|
urls: z.array(z.string()).max(100),
|
||||||
|
maximumAmountPosts: z.number(),
|
||||||
|
});
|
||||||
|
|
||||||
export const rssFeedRouter = createTRPCRouter({
|
export const rssFeedRouter = createTRPCRouter({
|
||||||
getFeeds: publicProcedure
|
getFeeds: publicProcedure.input(feedsInput).query(async ({ ctx, input }) => {
|
||||||
.input(
|
const urls = (await canAccessAllFeedsAsync(ctx.db, ctx.session))
|
||||||
z.object({
|
? input.urls
|
||||||
urls: z.array(z.string()).max(100),
|
: await restrictUrlsAsync(ctx.db, input.urls);
|
||||||
maximumAmountPosts: z.number(),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
.query(async ({ ctx, input }) => {
|
|
||||||
const urls = (await canAccessAllFeedsAsync(ctx.db, ctx.session))
|
|
||||||
? input.urls
|
|
||||||
: await restrictUrlsAsync(ctx.db, input.urls);
|
|
||||||
|
|
||||||
const rssFeeds = await Promise.all(
|
const rssFeeds = await Promise.all(
|
||||||
urls.map(async (url) => {
|
urls.map(async (url) => {
|
||||||
const innerHandler = rssFeedsRequestHandler.handler({
|
const innerHandler = rssFeedsRequestHandler.handler({
|
||||||
url,
|
url,
|
||||||
count: input.maximumAmountPosts,
|
count: input.maximumAmountPosts,
|
||||||
});
|
|
||||||
return await innerHandler.getCachedOrUpdatedDataAsync({
|
|
||||||
forceUpdate: false,
|
|
||||||
});
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
return rssFeeds
|
|
||||||
.flatMap((rssFeed) => rssFeed.data.entries)
|
|
||||||
.slice(0, input.maximumAmountPosts)
|
|
||||||
.sort((entryA, entryB) => {
|
|
||||||
return entryA.published && entryB.published
|
|
||||||
? new Date(entryB.published).getTime() - new Date(entryA.published).getTime()
|
|
||||||
: 0;
|
|
||||||
});
|
});
|
||||||
}),
|
return await innerHandler.getCachedOrUpdatedDataAsync({
|
||||||
|
forceUpdate: false,
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
return rssFeeds
|
||||||
|
.flatMap((rssFeed) => rssFeed.data.entries)
|
||||||
|
.slice(0, input.maximumAmountPosts)
|
||||||
|
.sort((entryA, entryB) => {
|
||||||
|
return entryA.published && entryB.published
|
||||||
|
? new Date(entryB.published).getTime() - new Date(entryA.published).getTime()
|
||||||
|
: 0;
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
subscribeFeeds: publicProcedure.input(feedsInput).subscription(({ input }) => {
|
||||||
|
// It is not necessary to check access to all feeds here, as the client will only subscribe to feeds which already exist
|
||||||
|
return observable<{ url: string; entries: ExtendedFeedEntry[] }>((emit) => {
|
||||||
|
const unsubscribes = input.urls.map((url) => {
|
||||||
|
const innerHandler = rssFeedsRequestHandler.handler({ url, count: input.maximumAmountPosts });
|
||||||
|
return innerHandler.subscribe((data) => {
|
||||||
|
emit.next({ url, entries: data.entries });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
unsubscribes.forEach((unsubscribe) => unsubscribe());
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
export async function canAccessAllFeedsAsync(db: Database, session: Session | null) {
|
export async function canAccessAllFeedsAsync(db: Database, session: Session | null) {
|
||||||
|
|||||||
@@ -5,9 +5,9 @@ import { ErrorWithMetadata } from "@homarr/core/infrastructure/logs/error";
|
|||||||
import { EVERY_10_MINUTES } from "@homarr/cron-jobs-core/expressions";
|
import { EVERY_10_MINUTES } from "@homarr/cron-jobs-core/expressions";
|
||||||
import { db, eq } from "@homarr/db";
|
import { db, eq } from "@homarr/db";
|
||||||
import { items } from "@homarr/db/schema";
|
import { items } from "@homarr/db/schema";
|
||||||
// This import is done that way to avoid circular dependencies.
|
|
||||||
import { rssFeedsRequestHandler } from "@homarr/request-handler/rss-feeds";
|
import { rssFeedsRequestHandler } from "@homarr/request-handler/rss-feeds";
|
||||||
|
|
||||||
|
// This import is done that way to avoid circular dependencies.
|
||||||
import type { WidgetComponentProps } from "../../../widgets";
|
import type { WidgetComponentProps } from "../../../widgets";
|
||||||
import { createCronJob } from "../lib";
|
import { createCronJob } from "../lib";
|
||||||
|
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ export const rssFeedsRequestHandler = createCachedWidgetRequestHandler({
|
|||||||
|
|
||||||
return {
|
return {
|
||||||
...result,
|
...result,
|
||||||
entries: result.entries?.slice(0, input.count) ?? [],
|
entries: result.entries?.map((entry) => ({ ...entry, feedUrl: input.url })).slice(0, input.count) ?? [],
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
cacheDuration: dayjs.duration(5, "minutes"),
|
cacheDuration: dayjs.duration(5, "minutes"),
|
||||||
@@ -109,6 +109,7 @@ const getFirstMediaProperty = (feedObject: object) => {
|
|||||||
* This interface adds properties on top of the default ones.
|
* This interface adds properties on top of the default ones.
|
||||||
*/
|
*/
|
||||||
export interface ExtendedFeedEntry extends FeedEntry {
|
export interface ExtendedFeedEntry extends FeedEntry {
|
||||||
|
feedUrl: string;
|
||||||
enclosure?: string;
|
enclosure?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,25 +4,47 @@ import { Card, Flex, Group, Image, ScrollArea, Stack, Text } from "@mantine/core
|
|||||||
import { IconClock } from "@tabler/icons-react";
|
import { IconClock } from "@tabler/icons-react";
|
||||||
import dayjs from "dayjs";
|
import dayjs from "dayjs";
|
||||||
|
|
||||||
|
import type { RouterInputs } from "@homarr/api";
|
||||||
import { clientApi } from "@homarr/api/client";
|
import { clientApi } from "@homarr/api/client";
|
||||||
import { useRequiredBoard } from "@homarr/boards/context";
|
import { useRequiredBoard } from "@homarr/boards/context";
|
||||||
|
|
||||||
import type { WidgetComponentProps } from "../definition";
|
import type { WidgetComponentProps } from "../definition";
|
||||||
import classes from "./component.module.scss";
|
import classes from "./component.module.scss";
|
||||||
|
|
||||||
|
const useLiveFeedEntries = (input: RouterInputs["widget"]["rssFeed"]["getFeeds"]) => {
|
||||||
|
const [feedEntries] = clientApi.widget.rssFeed.getFeeds.useSuspenseQuery(input, {
|
||||||
|
refetchOnMount: false,
|
||||||
|
refetchOnWindowFocus: false,
|
||||||
|
refetchOnReconnect: false,
|
||||||
|
retry: false,
|
||||||
|
});
|
||||||
|
const utils = clientApi.useUtils();
|
||||||
|
|
||||||
|
clientApi.widget.rssFeed.subscribeFeeds.useSubscription(input, {
|
||||||
|
onData(updatedData) {
|
||||||
|
utils.widget.rssFeed.getFeeds.setData(input, (oldData) => {
|
||||||
|
return oldData
|
||||||
|
?.filter((entry) => entry.feedUrl !== updatedData.url)
|
||||||
|
.concat(updatedData.entries)
|
||||||
|
.sort((entryA, entryB) => {
|
||||||
|
return entryA.published && entryB.published
|
||||||
|
? new Date(entryB.published).getTime() - new Date(entryA.published).getTime()
|
||||||
|
: 0;
|
||||||
|
})
|
||||||
|
.slice(0, input.maximumAmountPosts);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return feedEntries;
|
||||||
|
};
|
||||||
|
|
||||||
export default function RssFeed({ options }: WidgetComponentProps<"rssFeed">) {
|
export default function RssFeed({ options }: WidgetComponentProps<"rssFeed">) {
|
||||||
const [feedEntries] = clientApi.widget.rssFeed.getFeeds.useSuspenseQuery(
|
const feedEntries = useLiveFeedEntries({
|
||||||
{
|
urls: options.feedUrls,
|
||||||
urls: options.feedUrls,
|
maximumAmountPosts: typeof options.maximumAmountPosts === "number" ? options.maximumAmountPosts : 100,
|
||||||
maximumAmountPosts: typeof options.maximumAmountPosts === "number" ? options.maximumAmountPosts : 100,
|
});
|
||||||
},
|
|
||||||
{
|
|
||||||
refetchOnMount: false,
|
|
||||||
refetchOnWindowFocus: false,
|
|
||||||
refetchOnReconnect: false,
|
|
||||||
retry: false,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
const board = useRequiredBoard();
|
const board = useRequiredBoard();
|
||||||
|
|
||||||
const languageDir = options.enableRtl ? "RTL" : "LTR";
|
const languageDir = options.enableRtl ? "RTL" : "LTR";
|
||||||
|
|||||||
Reference in new issue
Block a user