diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-03-03 06:48:26 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-03-03 06:48:26 +0900 |
| commit | 6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a (patch) | |
| tree | d9319e37433f57c70cdf9e66b57b525fd7bfc881 /src/api/endpoints/posts/replies.ts | |
| parent | wip (diff) | |
| download | sharkey-6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a.tar.gz sharkey-6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a.tar.bz2 sharkey-6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a.zip | |
wip
Diffstat (limited to 'src/api/endpoints/posts/replies.ts')
| -rw-r--r-- | src/api/endpoints/posts/replies.ts | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/src/api/endpoints/posts/replies.ts b/src/api/endpoints/posts/replies.ts new file mode 100644 index 0000000000..3f448d1632 --- /dev/null +++ b/src/api/endpoints/posts/replies.ts @@ -0,0 +1,58 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import serialize from '../../serializers/post'; + +/** + * Show a replies of a post + * + * @param {any} params + * @param {any} user + * @return {Promise<any>} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // Get 'sort' parameter + const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + if (sortError) return rej('invalid sort param'); + + // Lookup post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + // Issue query + const replies = await Post + .find({ reply_to_id: post._id }, { + limit: limit, + skip: offset, + sort: { + _id: sort == 'asc' ? 1 : -1 + } + }); + + // Serialize + res(await Promise.all(replies.map(async post => + await serialize(post, user)))); +}); |