From 26a5d268e7d9cd1d854d1c160a2a2c8793bb026b Mon Sep 17 00:00:00 2001
From: Andvari <31068367+dzx-dzx@users.noreply.github.com>
Date: Sat, 2 Sep 2023 00:31:32 +0800
Subject: [PATCH] feat(route): Add Discourse notifications fulltext support.
(#13168)
* feat(route): Add Discourse notifications fulltext support.
* Update doc.
* Update notifications.js
* Update bbs.md
* Update bbs.md
---
lib/v2/discourse/maintainer.js | 2 +-
lib/v2/discourse/notifications.js | 19 ++++++++++++++++++-
lib/v2/discourse/router.js | 2 +-
website/docs/routes/bbs.md | 8 +++++++-
4 files changed, 27 insertions(+), 4 deletions(-)
diff --git a/lib/v2/discourse/maintainer.js b/lib/v2/discourse/maintainer.js
index 559fca350..51feca942 100644
--- a/lib/v2/discourse/maintainer.js
+++ b/lib/v2/discourse/maintainer.js
@@ -1,4 +1,4 @@
module.exports = {
'/:configId/posts': ['dzx-dzx'],
- '/:configId/notification': ['dzx-dzx'],
+ '/:configId/notification/:fulltext?': ['dzx-dzx'],
};
diff --git a/lib/v2/discourse/notifications.js b/lib/v2/discourse/notifications.js
index 45c35f116..7024ed484 100644
--- a/lib/v2/discourse/notifications.js
+++ b/lib/v2/discourse/notifications.js
@@ -5,14 +5,31 @@ 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) => ({
+ let items = response.notifications.slice(0, ctx.query.limit ? parseInt(ctx.query.limit) : 10).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}` : `t/topic/${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}`,
+ original_post_id: e.data.original_post_id,
}));
+ if (ctx.params.fulltext === '1') {
+ items = await Promise.all(
+ items.map((e) => {
+ if (e.original_post_id) {
+ const post_link = `${link}/posts/${e.original_post_id}.json`;
+ return ctx.cache.tryGet(post_link, async () => {
+ const { cooked } = await got(post_link, { headers: { 'User-Api-Key': key } }).json();
+ return { ...e, description: cooked };
+ });
+ } else {
+ return e;
+ }
+ })
+ );
+ }
+
const { about } = await got(`${link}/about.json`, { headers: { 'User-Api-Key': key } }).json();
ctx.state.data = {
title: `${about.title} - Notifications`,
diff --git a/lib/v2/discourse/router.js b/lib/v2/discourse/router.js
index e538d1ef8..93424ee23 100644
--- a/lib/v2/discourse/router.js
+++ b/lib/v2/discourse/router.js
@@ -1,4 +1,4 @@
module.exports = (router) => {
router.get('/:configId/posts', require('./posts'));
- router.get('/:configId/notifications', require('./notifications'));
+ router.get('/:configId/notifications/:fulltext?', require('./notifications'));
};
diff --git a/website/docs/routes/bbs.md b/website/docs/routes/bbs.md
index a98f47f40..57101c8c4 100644
--- a/website/docs/routes/bbs.md
+++ b/website/docs/routes/bbs.md
@@ -154,7 +154,13 @@ You need to set the environment variable `DISCOURSE_CONFIG_{id}` before using it
### Notifications {#discourse-notifications}
-
+
+
+:::caution
+
+If you opt to enable `fulltext` feature, consider adding `limit` parameter to your query to avoid sending too many request.
+
+:::
## Discuz {#discuz}