Refactoring, Clean up and bug fixes
This commit is contained in:
parent
b4b9e76c8d
commit
931bdc6aac
108 changed files with 1722 additions and 1539 deletions
|
|
@ -1,26 +1,41 @@
|
|||
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
|
||||
import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id';
|
||||
import Note, { packMany, INote } from '../../../../models/note';
|
||||
import { ILocalUser } from '../../../../models/user';
|
||||
import getParams from '../../get-params';
|
||||
|
||||
export const meta = {
|
||||
desc: {
|
||||
'ja-JP': '指定した投稿の文脈を取得します。',
|
||||
'en-US': 'Show conversation of a note.'
|
||||
},
|
||||
|
||||
requireCredential: false,
|
||||
|
||||
params: {
|
||||
noteId: {
|
||||
validator: $.type(ID),
|
||||
transform: transform,
|
||||
},
|
||||
|
||||
limit: {
|
||||
validator: $.num.optional.range(1, 100),
|
||||
default: 10
|
||||
},
|
||||
|
||||
offset: {
|
||||
validator: $.num.optional.min(0),
|
||||
default: 0
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Show conversation of a note
|
||||
*/
|
||||
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
|
||||
// Get 'noteId' parameter
|
||||
const [noteId, noteIdErr] = $.type(ID).get(params.noteId);
|
||||
if (noteIdErr) return rej('invalid noteId param');
|
||||
|
||||
// Get 'limit' parameter
|
||||
const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
|
||||
if (limitErr) return rej('invalid limit param');
|
||||
|
||||
// Get 'offset' parameter
|
||||
const [offset = 0, offsetErr] = $.num.optional.min(0).get(params.offset);
|
||||
if (offsetErr) return rej('invalid offset param');
|
||||
const [ps, psErr] = getParams(meta, params);
|
||||
if (psErr) return rej(psErr);
|
||||
|
||||
// Lookup note
|
||||
const note = await Note.findOne({
|
||||
_id: noteId
|
||||
_id: ps.noteId
|
||||
});
|
||||
|
||||
if (note === null) {
|
||||
|
|
@ -34,11 +49,11 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
|
|||
i++;
|
||||
const p = await Note.findOne({ _id: id });
|
||||
|
||||
if (i > offset) {
|
||||
if (i > ps.offset) {
|
||||
conversation.push(p);
|
||||
}
|
||||
|
||||
if (conversation.length == limit) {
|
||||
if (conversation.length == ps.limit) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -51,6 +66,5 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
|
|||
await get(note.replyId);
|
||||
}
|
||||
|
||||
// Serialize
|
||||
res(await packMany(conversation, user));
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue