This commit is contained in:
syuilo 2018-10-29 10:52:36 +09:00
parent 22d0d11895
commit e74c0df6c6
No known key found for this signature in database
GPG Key ID: BDC4C49D06AB9D69

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 = {
const reactions = await Reaction noteId: note._id
.find({ } as any;
noteId: note._id,
deletedAt: { $exists: false } const sort = {
}, { _id: -1
limit: limit, };
skip: offset,
sort: { if (ps.sinceId) {
_id: sort == 'asc' ? 1 : -1 sort._id = 1;
query._id = {
$gt: ps.sinceId
};
} else if (ps.untilId) {
query._id = {
$lt: ps.untilId
};
} }
const reactions = await Reaction
.find(query, {
limit: ps.limit,
skip: ps.offset,
sort: sort
}); });
// Serialize // Serialize