summaryrefslogtreecommitdiff
path: root/src/api/endpoints/posts/timeline.ts
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-03-03 06:48:26 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-03-03 06:48:26 +0900
commit6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a (patch)
treed9319e37433f57c70cdf9e66b57b525fd7bfc881 /src/api/endpoints/posts/timeline.ts
parentwip (diff)
downloadsharkey-6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a.tar.gz
sharkey-6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a.tar.bz2
sharkey-6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a.zip
wip
Diffstat (limited to 'src/api/endpoints/posts/timeline.ts')
-rw-r--r--src/api/endpoints/posts/timeline.ts73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts
new file mode 100644
index 0000000000..5744084932
--- /dev/null
+++ b/src/api/endpoints/posts/timeline.ts
@@ -0,0 +1,73 @@
+'use strict';
+
+/**
+ * Module dependencies
+ */
+import it from '../../it';
+import Post from '../../models/post';
+import getFriends from '../../common/get-friends';
+import serialize from '../../serializers/post';
+
+/**
+ * Get timeline of myself
+ *
+ * @param {any} params
+ * @param {any} user
+ * @param {any} app
+ * @return {Promise<any>}
+ */
+module.exports = (params, user, app) =>
+ new Promise(async (res, rej) =>
+{
+ // 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 'since_id' parameter
+ const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
+ if (sinceIdErr) return rej('invalid since_id param');
+
+ // Get 'max_id' parameter
+ const [maxId, maxIdErr] = it(params.max_id).expect.id().qed();
+ if (maxIdErr) return rej('invalid max_id param');
+
+ // Check if both of since_id and max_id is specified
+ if (sinceId !== null && maxId !== null) {
+ return rej('cannot set since_id and max_id');
+ }
+
+ // ID list of the user itself and other users who the user follows
+ const followingIds = await getFriends(user._id);
+
+ // Construct query
+ const sort = {
+ _id: -1
+ };
+ const query = {
+ user_id: {
+ $in: followingIds
+ }
+ } as any;
+ if (sinceId) {
+ sort._id = 1;
+ query._id = {
+ $gt: sinceId
+ };
+ } else if (maxId) {
+ query._id = {
+ $lt: maxId
+ };
+ }
+
+ // Issue query
+ const timeline = await Post
+ .find(query, {
+ limit: limit,
+ sort: sort
+ });
+
+ // Serialize
+ res(await Promise.all(timeline.map(async post =>
+ await serialize(post, user)
+ )));
+});