Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route): Add Discourse notifications fulltext support. #13168

Merged
merged 5 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/v2/discourse/maintainer.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module.exports = {
'/:configId/posts': ['dzx-dzx'],
'/:configId/notification': ['dzx-dzx'],
'/:configId/notification/:fulltext?': ['dzx-dzx'],
};
19 changes: 18 additions & 1 deletion lib/v2/discourse/notifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
Expand Down
2 changes: 1 addition & 1 deletion lib/v2/discourse/router.js
Original file line number Diff line number Diff line change
@@ -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'));
};
8 changes: 7 additions & 1 deletion website/docs/routes/bbs.md
Original file line number Diff line number Diff line change
Expand Up @@ -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. Set it to `1` to enable it.']} selfhost="1"/>
dzx-dzx marked this conversation as resolved.
Show resolved Hide resolved

:::caution

If you opt to enable `fulltext` feature, consider adding `limit` parameter to your query to avoid sending too many request.

:::

## Discuz {#discuz}

Expand Down