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
This commit is contained in:
Andvari 2023-09-02 00:31:32 +08:00 committed by GitHub
parent 8e8bed5981
commit 26a5d268e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 4 deletions

View File

@ -1,4 +1,4 @@
module.exports = {
'/:configId/posts': ['dzx-dzx'],
'/:configId/notification': ['dzx-dzx'],
'/:configId/notification/:fulltext?': ['dzx-dzx'],
};

View File

@ -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`,

View File

@ -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'));
};

View File

@ -154,7 +154,13 @@ You need to set the environment variable `DISCOURSE_CONFIG_{id}` before using it
### Notifications {#discourse-notifications}
<Route author="dzx-dzx" example="/discourse/0/notifications" path="/discourse/:configId/notifications" paramsDesc={['Environment variable configuration id, see above']} selfhost="1"/>
<Route author="dzx-dzx" example="/discourse/0/notifications" path="/discourse/:configId/notifications/:fulltext?" paramsDesc={['Environment variable configuration id, see above','Fetch the content if the notification points to a post. This is disabled by default, set it to `1` to enable it.']} selfhost="1"/>
:::caution
If you opt to enable `fulltext` feature, consider adding `limit` parameter to your query to avoid sending too many request.
:::
## Discuz {#discuz}