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}