summaryrefslogtreecommitdiff
path: root/src/server/api/endpoints/notes/conversation.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-11-02 03:32:24 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-11-02 03:32:24 +0900
commit931bdc6aace5e7aa71ffdfb470e208ead78a2a53 (patch)
treeeee6d7bf5f5480b883bb601517b4d9db03f31e9f /src/server/api/endpoints/notes/conversation.ts
parentRefactoring (diff)
downloadsharkey-931bdc6aace5e7aa71ffdfb470e208ead78a2a53.tar.gz
sharkey-931bdc6aace5e7aa71ffdfb470e208ead78a2a53.tar.bz2
sharkey-931bdc6aace5e7aa71ffdfb470e208ead78a2a53.zip
Refactoring, Clean up and bug fixes
Diffstat (limited to 'src/server/api/endpoints/notes/conversation.ts')
-rw-r--r--src/server/api/endpoints/notes/conversation.ts50
1 files changed, 32 insertions, 18 deletions
diff --git a/src/server/api/endpoints/notes/conversation.ts b/src/server/api/endpoints/notes/conversation.ts
index 0c23f9e5fc..c7be6d6e3f 100644
--- a/src/server/api/endpoints/notes/conversation.ts
+++ b/src/server/api/endpoints/notes/conversation.ts
@@ -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';
-/**
- * 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');
+export const meta = {
+ desc: {
+ 'ja-JP': '指定した投稿の文脈を取得します。',
+ 'en-US': 'Show conversation of a note.'
+ },
+
+ requireCredential: false,
+
+ params: {
+ noteId: {
+ validator: $.type(ID),
+ transform: transform,
+ },
- // Get 'limit' parameter
- const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
- if (limitErr) return rej('invalid limit param');
+ limit: {
+ validator: $.num.optional.range(1, 100),
+ default: 10
+ },
- // Get 'offset' parameter
- const [offset = 0, offsetErr] = $.num.optional.min(0).get(params.offset);
- if (offsetErr) return rej('invalid offset param');
+ offset: {
+ validator: $.num.optional.min(0),
+ default: 0
+ },
+ }
+};
+
+export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
+ 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));
});