From 3bef3dd6c6d79e3ed8304151f1045ada3c2d96a1 Mon Sep 17 00:00:00 2001 From: Heng-Yi Wu <2316687+henry40408@users.noreply.github.com> Date: Tue, 6 Dec 2022 21:33:45 +0800 Subject: [PATCH 1/2] feat: miniflux widget --- public/locales/en/common.json | 6 +++- src/widgets/components.js | 1 + src/widgets/miniflux/component.jsx | 33 ++++++++++++++++++++++ src/widgets/miniflux/proxy.js | 45 ++++++++++++++++++++++++++++++ src/widgets/miniflux/widget.js | 7 +++++ src/widgets/widgets.js | 2 ++ 6 files changed, 93 insertions(+), 1 deletion(-) create mode 100644 src/widgets/miniflux/component.jsx create mode 100644 src/widgets/miniflux/proxy.js create mode 100644 src/widgets/miniflux/widget.js diff --git a/public/locales/en/common.json b/public/locales/en/common.json index 6466727f..3681f9de 100644 --- a/public/locales/en/common.json +++ b/public/locales/en/common.json @@ -246,6 +246,10 @@ "status_count": "Posts", "domain_count": "Domains" }, + "miniflux": { + "read": "Read", + "unread": "Unread" + }, "authentik": { "users": "Users", "loginsLast24H": "Logins (24h)", @@ -379,4 +383,4 @@ "inbox": "Inbox", "total": "Total" } -} \ No newline at end of file +} diff --git a/src/widgets/components.js b/src/widgets/components.js index e6021f17..0ecce683 100644 --- a/src/widgets/components.js +++ b/src/widgets/components.js @@ -21,6 +21,7 @@ const components = { jellyseerr: dynamic(() => import("./jellyseerr/component")), lidarr: dynamic(() => import("./lidarr/component")), mastodon: dynamic(() => import("./mastodon/component")), + miniflux: dynamic(() => import("./miniflux/component")), navidrome: dynamic(() => import("./navidrome/component")), npm: dynamic(() => import("./npm/component")), nzbget: dynamic(() => import("./nzbget/component")), diff --git a/src/widgets/miniflux/component.jsx b/src/widgets/miniflux/component.jsx new file mode 100644 index 00000000..e68419e0 --- /dev/null +++ b/src/widgets/miniflux/component.jsx @@ -0,0 +1,33 @@ +import { useTranslation } from "next-i18next"; + +import Container from "components/services/widget/container"; +import Block from "components/services/widget/block"; +import useWidgetAPI from "utils/proxy/use-widget-api"; + +export default function Component({ service }) { + const { t } = useTranslation(); + + const { widget } = service; + + const { data: minifluxData, error: minifluxError } = useWidgetAPI(widget); + + if (minifluxError) { + return ; + } + + if (!minifluxData) { + return ( + + + + + ); + } + + return ( + + + + + ); +} diff --git a/src/widgets/miniflux/proxy.js b/src/widgets/miniflux/proxy.js new file mode 100644 index 00000000..9e6ac951 --- /dev/null +++ b/src/widgets/miniflux/proxy.js @@ -0,0 +1,45 @@ +import { formatApiCall } from "utils/proxy/api-helpers"; +import { httpProxy } from "utils/proxy/http"; +import getServiceWidget from "utils/config/service-helpers"; +import createLogger from "utils/logger"; + +const logger = createLogger("minifluxProxyHandler"); + +export default async function minifluxProxyHandler(req, res) { + const { group, service } = req.query; + + if (!group || !service) { + logger.debug("Invalid or missing service '%s' or group '%s'", service, group); + return res.status(400).json({ error: "Invalid proxy service type" }); + } + + const widget = await getServiceWidget(group, service); + if (!widget) { + logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); + return res.status(400).json({ error: "Invalid proxy service type" }); + } + + const url = new URL(formatApiCall("{url}/v1/feeds/counters", { ...widget })); + url.username = widget.username; + url.password = widget.password; + + const params = { + method: "GET", + headers: { + "X-Auth-Token": widget.token + } + }; + + // eslint-disable-next-line no-unused-vars + const [status, contentType, data] = await httpProxy(url, params); + + let read = 0; + let unread = 0; + if (status === 200) { + const parsed = JSON.parse(data.toString()); + read = Object.values(parsed.reads).reduce((acc, i) => acc + i, 0); + unread = Object.values(parsed.unreads).reduce((acc, i) => acc + i, 0); + } + + return res.status(status).send({ read, unread }); +} diff --git a/src/widgets/miniflux/widget.js b/src/widgets/miniflux/widget.js new file mode 100644 index 00000000..d98fee40 --- /dev/null +++ b/src/widgets/miniflux/widget.js @@ -0,0 +1,7 @@ +import minifluxProxyHandler from "./proxy"; + +const widget = { + proxyHandler: minifluxProxyHandler, +}; + +export default widget; diff --git a/src/widgets/widgets.js b/src/widgets/widgets.js index cda52cc0..7bb3c95d 100644 --- a/src/widgets/widgets.js +++ b/src/widgets/widgets.js @@ -16,6 +16,7 @@ import jackett from "./jackett/widget"; import jellyseerr from "./jellyseerr/widget"; import lidarr from "./lidarr/widget"; import mastodon from "./mastodon/widget"; +import miniflux from "./miniflux/widget"; import navidrome from "./navidrome/widget"; import npm from "./npm/widget"; import nzbget from "./nzbget/widget"; @@ -66,6 +67,7 @@ const widgets = { jellyseerr, lidarr, mastodon, + miniflux, navidrome, npm, nzbget, From 93445a28316fadd90e96f0f8a050ef52adb6e8a8 Mon Sep 17 00:00:00 2001 From: Michael Shamoon <4887959+shamoon@users.noreply.github.com> Date: Fri, 16 Dec 2022 22:33:15 -0800 Subject: [PATCH 2/2] Use credentialed proxy for miniflux --- src/utils/proxy/handlers/credentialed.js | 2 ++ src/widgets/miniflux/component.jsx | 2 +- src/widgets/miniflux/proxy.js | 45 ------------------------ src/widgets/miniflux/widget.js | 16 +++++++-- 4 files changed, 17 insertions(+), 48 deletions(-) delete mode 100644 src/widgets/miniflux/proxy.js diff --git a/src/utils/proxy/handlers/credentialed.js b/src/utils/proxy/handlers/credentialed.js index 4d2007ba..5d34264d 100644 --- a/src/utils/proxy/handlers/credentialed.js +++ b/src/utils/proxy/handlers/credentialed.js @@ -36,6 +36,8 @@ export default async function credentialedProxyHandler(req, res, map) { headers["X-API-Token"] = `${widget.key}`; } else if (widget.type === "tubearchivist") { headers.Authorization = `Token ${widget.key}`; + } else if (widget.type === "miniflux") { + headers["X-Auth-Token"] = `${widget.key}`; } else { headers["X-API-Key"] = `${widget.key}`; } diff --git a/src/widgets/miniflux/component.jsx b/src/widgets/miniflux/component.jsx index e68419e0..dbfd6048 100644 --- a/src/widgets/miniflux/component.jsx +++ b/src/widgets/miniflux/component.jsx @@ -9,7 +9,7 @@ export default function Component({ service }) { const { widget } = service; - const { data: minifluxData, error: minifluxError } = useWidgetAPI(widget); + const { data: minifluxData, error: minifluxError } = useWidgetAPI(widget, "counters"); if (minifluxError) { return ; diff --git a/src/widgets/miniflux/proxy.js b/src/widgets/miniflux/proxy.js deleted file mode 100644 index 9e6ac951..00000000 --- a/src/widgets/miniflux/proxy.js +++ /dev/null @@ -1,45 +0,0 @@ -import { formatApiCall } from "utils/proxy/api-helpers"; -import { httpProxy } from "utils/proxy/http"; -import getServiceWidget from "utils/config/service-helpers"; -import createLogger from "utils/logger"; - -const logger = createLogger("minifluxProxyHandler"); - -export default async function minifluxProxyHandler(req, res) { - const { group, service } = req.query; - - if (!group || !service) { - logger.debug("Invalid or missing service '%s' or group '%s'", service, group); - return res.status(400).json({ error: "Invalid proxy service type" }); - } - - const widget = await getServiceWidget(group, service); - if (!widget) { - logger.debug("Invalid or missing widget for service '%s' in group '%s'", service, group); - return res.status(400).json({ error: "Invalid proxy service type" }); - } - - const url = new URL(formatApiCall("{url}/v1/feeds/counters", { ...widget })); - url.username = widget.username; - url.password = widget.password; - - const params = { - method: "GET", - headers: { - "X-Auth-Token": widget.token - } - }; - - // eslint-disable-next-line no-unused-vars - const [status, contentType, data] = await httpProxy(url, params); - - let read = 0; - let unread = 0; - if (status === 200) { - const parsed = JSON.parse(data.toString()); - read = Object.values(parsed.reads).reduce((acc, i) => acc + i, 0); - unread = Object.values(parsed.unreads).reduce((acc, i) => acc + i, 0); - } - - return res.status(status).send({ read, unread }); -} diff --git a/src/widgets/miniflux/widget.js b/src/widgets/miniflux/widget.js index d98fee40..d4efbe2f 100644 --- a/src/widgets/miniflux/widget.js +++ b/src/widgets/miniflux/widget.js @@ -1,7 +1,19 @@ -import minifluxProxyHandler from "./proxy"; +import { asJson } from "utils/proxy/api-helpers"; +import credentialedProxyHandler from "utils/proxy/handlers/credentialed"; const widget = { - proxyHandler: minifluxProxyHandler, + api: "{url}/v1/{endpoint}", + proxyHandler: credentialedProxyHandler, + + mappings: { + counters: { + endpoint: "feeds/counters", + map: (data) => ({ + read: Object.values(asJson(data).reads).reduce((acc, i) => acc + i, 0), + unread: Object.values(asJson(data).unreads).reduce((acc, i) => acc + i, 0) + }), + }, + } }; export default widget;