You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.0 KiB
32 lines
1.0 KiB
5 years ago
|
function htmlEncode (str) {
|
||
|
return str ? str.replace(/&/g, '&')
|
||
|
.replace(/</g, '<')
|
||
|
.replace(/>/g, '>')
|
||
|
.replace(/"/g, '"')
|
||
|
.replace(/'/g, ''')
|
||
|
.replace(/\//g, '/') : '';
|
||
|
}
|
||
|
|
||
|
module.exports = async (ctx) => {
|
||
|
const { id, limit } = ctx.request.query;
|
||
|
|
||
|
let data = await ctx.redis.get(`danmaku${id}`);
|
||
|
if (data) {
|
||
|
data = JSON.parse(data);
|
||
|
if (limit) {
|
||
|
data = data.slice(-1 * parseInt(limit));
|
||
|
}
|
||
|
ctx.response.set('X-Koa-Redis', 'true');
|
||
|
} else {
|
||
|
data = await ctx.mongodb.find({ player: id }) || [];
|
||
|
ctx.redis.set(`danmaku${id}`, JSON.stringify(data));
|
||
|
if (limit) {
|
||
|
data = data.slice(-1 * parseInt(limit));
|
||
|
}
|
||
|
ctx.response.set('X-Koa-Mongodb', 'true');
|
||
|
}
|
||
|
ctx.body = JSON.stringify({
|
||
|
code: 0,
|
||
|
data: data.map((item) => [item.time || 0, item.type || 0, item.color || 16777215, htmlEncode(item.author) || 'DPlayer', htmlEncode(item.text) || '']),
|
||
|
});
|
||
|
};
|