feat: add linux patchwork comments (#1327)

This commit is contained in:
Richard Yu 2018-12-31 01:02:50 +08:00 committed by DIYgod
parent a94c087f53
commit 76103963aa
4 changed files with 56 additions and 0 deletions

View File

@ -794,6 +794,10 @@ GitHub 官方也提供了一些 RSS:
<route name="最新发布" author="xyqfer" example="/testerhome/newest" path="/testerhome/newest"/>
### Linux Patchwork
<route name="Patch Comments" author="ysc3839" example="/patchwork.kernel.org/comments/10723629" path="/patchwork.kernel.org/comments/:id" :paramsDesc="['Patch ID']"/>
## 直播
### 哔哩哔哩直播

View File

@ -944,4 +944,7 @@ router.get('/36kr/search/article/:keyword', require('./routes/36kr/search/articl
// icourse163
router.get('/icourse163/newest', require('./routes/icourse163/newest'));
// patchwork.kernel.org
router.get('/patchwork.kernel.org/comments/:id', require('./routes/patchwork.kernel.org/comments'));
module.exports = router;

View File

@ -0,0 +1,17 @@
const axios = require('../../utils/axios');
module.exports = {
getPatchnameFromID: async (ctx, id) => {
const key = 'patchwork-kernel-org-patchname-from-id-' + id;
let name = await ctx.cache.get(key);
if (!name) {
const patchDetail = await axios({
method: 'get',
url: `https://patchwork.kernel.org/api/patches/${id}/`,
});
name = patchDetail.data.name;
ctx.cache.set(key, name, 24 * 60 * 60);
}
return name;
},
};

View File

@ -0,0 +1,32 @@
const axios = require('../../utils/axios');
const cache = require('./cache');
const he = require('he');
module.exports = async (ctx) => {
const id = ctx.params.id;
const name = await cache.getPatchnameFromID(ctx, id);
const host = `https://patchwork.kernel.org/patch/${id}/`;
const url = `https://patchwork.kernel.org/api/patches/${id}/comments/`;
const response = await axios({
method: 'get',
url,
params: {
order: '-date',
},
});
const data = response.data;
ctx.state.data = {
title: `${name} - Comments`,
link: host,
item: data.map((item) => ({
title: item.subject,
description: he.escape(he.escape(item.content)).replace(/\n/g, '<br>'),
pubDate: new Date(item.date).toUTCString(),
link: item.web_url,
})),
};
};