From 94b1d73c7448e00aad894cdbfc6c75da57b74592 Mon Sep 17 00:00:00 2001 From: dzx-dzx Date: Wed, 30 Aug 2023 16:05:44 +0800 Subject: [PATCH 1/5] feat(route): Add Discourse notifications fulltext support. --- lib/v2/discourse/notifications.js | 19 ++++++++++++++++++- lib/v2/discourse/router.js | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/v2/discourse/notifications.js b/lib/v2/discourse/notifications.js index 45c35f1161c757..d877baa0d53ae4 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 === 'Yes') { + 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 e538d1ef8238ad..93424ee23d9a0c 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')); }; From 396491a0b2e5938e749b319dfb79224a2b8085fa Mon Sep 17 00:00:00 2001 From: dzx-dzx Date: Wed, 30 Aug 2023 16:15:53 +0800 Subject: [PATCH 2/5] Update doc. --- lib/v2/discourse/maintainer.js | 2 +- website/docs/routes/bbs.md | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/v2/discourse/maintainer.js b/lib/v2/discourse/maintainer.js index 559fca350ffa4c..51feca9428c7cd 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/website/docs/routes/bbs.md b/website/docs/routes/bbs.md index a98f47f40bb14c..13f0407fdba8d1 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} From 0cc0c4ada66046ca123064293b9fa0ebc06313e4 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Thu, 31 Aug 2023 08:36:52 +0800 Subject: [PATCH 3/5] Update notifications.js --- lib/v2/discourse/notifications.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/v2/discourse/notifications.js b/lib/v2/discourse/notifications.js index d877baa0d53ae4..7024ed48462143 100644 --- a/lib/v2/discourse/notifications.js +++ b/lib/v2/discourse/notifications.js @@ -14,7 +14,7 @@ module.exports = async (ctx) => { original_post_id: e.data.original_post_id, })); - if (ctx.params.fulltext === 'Yes') { + if (ctx.params.fulltext === '1') { items = await Promise.all( items.map((e) => { if (e.original_post_id) { From 9a1bfb7e865c52fd79ec1d533ab7bc518572dc88 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Thu, 31 Aug 2023 08:38:21 +0800 Subject: [PATCH 4/5] Update bbs.md --- website/docs/routes/bbs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/routes/bbs.md b/website/docs/routes/bbs.md index 13f0407fdba8d1..2f67f4eda29e0b 100644 --- a/website/docs/routes/bbs.md +++ b/website/docs/routes/bbs.md @@ -154,7 +154,7 @@ You need to set the environment variable `DISCOURSE_CONFIG_{id}` before using it ### Notifications {#discourse-notifications} - + :::caution From e8d169096424c77f6769fa8fa0e5241b87237f73 Mon Sep 17 00:00:00 2001 From: Andvari <31068367+dzx-dzx@users.noreply.github.com> Date: Thu, 31 Aug 2023 18:21:04 +0800 Subject: [PATCH 5/5] Update bbs.md --- website/docs/routes/bbs.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/routes/bbs.md b/website/docs/routes/bbs.md index 2f67f4eda29e0b..57101c8c4e097e 100644 --- a/website/docs/routes/bbs.md +++ b/website/docs/routes/bbs.md @@ -154,7 +154,7 @@ You need to set the environment variable `DISCOURSE_CONFIG_{id}` before using it ### Notifications {#discourse-notifications} - + :::caution