From eebd99c85a897edfbbb5cf84e2f5f641ef37fa3b Mon Sep 17 00:00:00 2001
From: Andvari <31068367+dzx-dzx@users.noreply.github.com>
Date: Fri, 25 Aug 2023 00:34:19 +0800
Subject: [PATCH] feat(route): Add Discourse notifications support (#13114)
* Adding discourse notifications support.
* Refactor & add doc.
---
lib/v2/discourse/maintainer.js | 1 +
lib/v2/discourse/notifications.js | 22 ++++++++++++++++++++++
lib/v2/discourse/posts.js | 7 ++-----
lib/v2/discourse/router.js | 1 +
lib/v2/discourse/utils.js | 12 ++++++++++++
website/docs/routes/bbs.md | 6 +++++-
6 files changed, 43 insertions(+), 6 deletions(-)
create mode 100644 lib/v2/discourse/notifications.js
create mode 100644 lib/v2/discourse/utils.js
diff --git a/lib/v2/discourse/maintainer.js b/lib/v2/discourse/maintainer.js
index 6d6c640d8..559fca350 100644
--- a/lib/v2/discourse/maintainer.js
+++ b/lib/v2/discourse/maintainer.js
@@ -1,3 +1,4 @@
module.exports = {
'/:configId/posts': ['dzx-dzx'],
+ '/:configId/notification': ['dzx-dzx'],
};
diff --git a/lib/v2/discourse/notifications.js b/lib/v2/discourse/notifications.js
new file mode 100644
index 000000000..b781f3d04
--- /dev/null
+++ b/lib/v2/discourse/notifications.js
@@ -0,0 +1,22 @@
+const { getConfig } = require('./utils');
+const got = require('@/utils/got');
+
+module.exports = async (ctx) => {
+ const { link, key } = getConfig(ctx);
+
+ const response = await got(`${link}/notifications.json`, { headers: { 'User-Api-Key': key } }).json();
+ const items = response.notifications.map((e) => ({
+ title: e.fancy_title ?? e.data.badge_name,
+ link: `${link}/${e.data.hasOwnProperty('badge_id') ? `badges/${e.data.badge_id}/${e.data.badge_slug}?username=${e.data.username}` : `${e.topic_id}/${e.post_number}`}`,
+ pubDate: new Date(e.created_at),
+ author: e.data.display_username ?? e.data.username,
+ category: `notification_type:${e.notification_type}`,
+ }));
+
+ const { about } = await got(`${link}/about.json`, { headers: { 'User-Api-Key': key } }).json();
+ ctx.state.data = {
+ title: `${about.title} - Notifications`,
+ description: about.description,
+ item: items,
+ };
+};
diff --git a/lib/v2/discourse/posts.js b/lib/v2/discourse/posts.js
index 0dc8317be..8d7ed2092 100644
--- a/lib/v2/discourse/posts.js
+++ b/lib/v2/discourse/posts.js
@@ -1,12 +1,9 @@
-const config = require('@/config').value;
+const { getConfig } = require('./utils');
const got = require('@/utils/got');
const RSSParser = require('@/utils/rss-parser');
module.exports = async (ctx) => {
- if (!config.discourse.config[ctx.params.configId]) {
- throw Error('Discourse RSS is disabled due to the lack of relevant config');
- }
- const { link, key } = config.discourse.config[ctx.params.configId];
+ const { link, key } = getConfig(ctx);
const feed = await RSSParser.parseString(
(
diff --git a/lib/v2/discourse/router.js b/lib/v2/discourse/router.js
index b59d0004f..e538d1ef8 100644
--- a/lib/v2/discourse/router.js
+++ b/lib/v2/discourse/router.js
@@ -1,3 +1,4 @@
module.exports = (router) => {
router.get('/:configId/posts', require('./posts'));
+ router.get('/:configId/notifications', require('./notifications'));
};
diff --git a/lib/v2/discourse/utils.js b/lib/v2/discourse/utils.js
new file mode 100644
index 000000000..1b56e7b10
--- /dev/null
+++ b/lib/v2/discourse/utils.js
@@ -0,0 +1,12 @@
+const config = require('@/config').value;
+
+function getConfig(ctx) {
+ if (!config.discourse.config[ctx.params.configId]) {
+ throw Error('Discourse RSS is disabled due to the lack of relevant config');
+ }
+ return config.discourse.config[ctx.params.configId];
+}
+
+module.exports = {
+ getConfig,
+};
diff --git a/website/docs/routes/bbs.md b/website/docs/routes/bbs.md
index 9136f178c..a98f47f40 100644
--- a/website/docs/routes/bbs.md
+++ b/website/docs/routes/bbs.md
@@ -150,7 +150,11 @@ You need to set the environment variable `DISCOURSE_CONFIG_{id}` before using it
### Latest posts {#discourse-latest-posts}
-
+
+
+### Notifications {#discourse-notifications}
+
+
## Discuz {#discuz}