Refactoring, Clean up and bug fixes

This commit is contained in:
syuilo 2018-11-02 03:32:24 +09:00
parent b4b9e76c8d
commit 931bdc6aac
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
108 changed files with 1722 additions and 1539 deletions

View file

@ -1,19 +1,31 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id';
import Note from '../../../../models/note';
import User, { pack, ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
export const meta = {
requireCredential: false,
params: {
userId: {
validator: $.type(ID),
transform: transform,
},
limit: {
validator: $.num.optional.range(1, 100),
default: 10
},
}
};
export default (params: any, me: ILocalUser) => new Promise(async (res, rej) => {
// Get 'userId' parameter
const [userId, userIdErr] = $.type(ID).get(params.userId);
if (userIdErr) return rej('invalid userId param');
// Get 'limit' parameter
const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
if (limitErr) return rej('invalid limit param');
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
// Lookup user
const user = await User.findOne({
_id: userId
_id: ps.userId
}, {
fields: {
_id: true
@ -83,7 +95,7 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
const repliedUsersSorted = Object.keys(repliedUsers).sort((a, b) => repliedUsers[b] - repliedUsers[a]);
// Extract top replied users
const topRepliedUsers = repliedUsersSorted.slice(0, limit);
const topRepliedUsers = repliedUsersSorted.slice(0, ps.limit);
// Make replies object (includes weights)
const repliesObj = await Promise.all(topRepliedUsers.map(async (user) => ({
@ -91,6 +103,5 @@ export default (params: any, me: ILocalUser) => new Promise(async (res, rej) =>
weight: repliedUsers[user] / peak
})));
// Response
res(repliesObj);
});