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

6 years ago
function htmlEncode (str) {
return str ? str.replace(/&/g, '&')
6 years ago
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
.replace(/'/g, '&#x27;')
.replace(/\//g, '&#x2f;') : '';
6 years ago
}
7 years ago
6 years ago
module.exports = async (ctx) => {
const { id, limit } = ctx.request.query;
7 years ago
6 years ago
let data = await ctx.redis.get(`danmaku${id}`);
if (data) {
data = JSON.parse(data);
if (limit) {
data = data.slice(-1 * parseInt(limit));
7 years ago
}
6 years ago
ctx.response.set('X-Koa-Redis', 'true');
} else {
data = await ctx.mongodb.find({ player: id }) || [];
6 years ago
ctx.redis.set(`danmaku${id}`, JSON.stringify(data));
if (limit) {
data = data.slice(-1 * parseInt(limit));
7 years ago
}
6 years ago
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) || '']),
7 years ago
});
};