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}