feat(route): Add Discourse notifications support (#13114)

* Adding discourse notifications support.

* Refactor & add doc.
This commit is contained in:
Andvari 2023-08-25 00:34:19 +08:00 committed by GitHub
parent bf50612ff3
commit eebd99c85a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 6 deletions

View File

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

View File

@ -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,
};
};

View File

@ -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 <a href="https://docs.rsshub.app/install">relevant config</a>');
}
const { link, key } = config.discourse.config[ctx.params.configId];
const { link, key } = getConfig(ctx);
const feed = await RSSParser.parseString(
(

View File

@ -1,3 +1,4 @@
module.exports = (router) => {
router.get('/:configId/posts', require('./posts'));
router.get('/:configId/notifications', require('./notifications'));
};

12
lib/v2/discourse/utils.js Normal file
View File

@ -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 <a href="https://docs.rsshub.app/install">relevant config</a>');
}
return config.discourse.config[ctx.params.configId];
}
module.exports = {
getConfig,
};

View File

@ -150,7 +150,11 @@ You need to set the environment variable `DISCOURSE_CONFIG_{id}` before using it
### Latest posts {#discourse-latest-posts}
<Route author="dzx-dzx" example="/discourse/0/posts" path="/discuz/:configId/posts" paramsDesc={['Environment variable configuration id, see above']} selfhost="1"/>
<Route author="dzx-dzx" example="/discourse/0/posts" path="/discourse/:configId/posts" paramsDesc={['Environment variable configuration id, see above']} selfhost="1"/>
### Notifications {#discourse-notifications}
<Route author="dzx-dzx" example="/discourse/0/notifications" path="/discourse/:configId/notifications" paramsDesc={['Environment variable configuration id, see above']} selfhost="1"/>
## Discuz {#discuz}