This commit is contained in:
syuilo 2018-10-29 10:52:36 +09:00
parent ffcb6aacf5
commit c9e634aad3

View file

@ -2,6 +2,7 @@ import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import Note from '../../../../models/note'; import Note from '../../../../models/note';
import Reaction, { pack } from '../../../../models/note-reaction'; import Reaction, { pack } from '../../../../models/note-reaction';
import { ILocalUser } from '../../../../models/user'; import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
export const meta = { export const meta = {
desc: { desc: {
@ -9,46 +10,70 @@ export const meta = {
'en-US': 'Show reactions of a note.' 'en-US': 'Show reactions of a note.'
}, },
requireCredential: true requireCredential: false,
params: {
noteId: $.type(ID).note({
}),
limit: $.num.optional.range(1, 100).note({
default: 10
}),
offset: $.num.optional.note({
default: 0
}),
sinceId: $.type(ID).optional.note({
}),
untilId: $.type(ID).optional.note({
}),
}
}; };
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => { export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'noteId' parameter const [ps, psErr] = getParams(meta, params);
const [noteId, noteIdErr] = $.type(ID).get(params.noteId); if (psErr) return rej(psErr);
if (noteIdErr) return rej('invalid noteId param');
// Get 'limit' parameter // Check if both of sinceId and untilId is specified
const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit); if (ps.sinceId && ps.untilId) {
if (limitErr) return rej('invalid limit param'); return rej('cannot set sinceId and untilId');
}
// Get 'offset' parameter
const [offset = 0, offsetErr] = $.num.optional.min(0).get(params.offset);
if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
const [sort = 'desc', sortError] = $.str.optional.or('desc asc').get(params.sort);
if (sortError) return rej('invalid sort param');
// Lookup note // Lookup note
const note = await Note.findOne({ const note = await Note.findOne({
_id: noteId _id: ps.noteId
}); });
if (note === null) { if (note === null) {
return rej('note not found'); return rej('note not found');
} }
// Issue query const query = {
noteId: note._id
} as any;
const sort = {
_id: -1
};
if (ps.sinceId) {
sort._id = 1;
query._id = {
$gt: ps.sinceId
};
} else if (ps.untilId) {
query._id = {
$lt: ps.untilId
};
}
const reactions = await Reaction const reactions = await Reaction
.find({ .find(query, {
noteId: note._id, limit: ps.limit,
deletedAt: { $exists: false } skip: ps.offset,
}, { sort: sort
limit: limit,
skip: offset,
sort: {
_id: sort == 'asc' ? 1 : -1
}
}); });
// Serialize // Serialize