summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-03-25 16:43:55 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-03-25 16:43:55 +0900
commitcaf945765335c81d4785b7a108c1944aa0f2af26 (patch)
treed241d785258b3ab24e22fde5e2f481b07cfc0388 /src/api
parentImprove readability (diff)
downloadsharkey-caf945765335c81d4785b7a108c1944aa0f2af26.tar.gz
sharkey-caf945765335c81d4785b7a108c1944aa0f2af26.tar.bz2
sharkey-caf945765335c81d4785b7a108c1944aa0f2af26.zip
#321
Diffstat (limited to 'src/api')
-rw-r--r--src/api/endpoints.ts4
-rw-r--r--src/api/endpoints/posts/polls/recommendation.ts57
2 files changed, 61 insertions, 0 deletions
diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts
index 2d3716bb85..f0031b82a2 100644
--- a/src/api/endpoints.ts
+++ b/src/api/endpoints.ts
@@ -409,6 +409,10 @@ const endpoints: Endpoint[] = [
},
kind: 'vote-write'
},
+ {
+ name: 'posts/polls/recommendation',
+ withCredential: true
+ },
{
name: 'messaging/history',
diff --git a/src/api/endpoints/posts/polls/recommendation.ts b/src/api/endpoints/posts/polls/recommendation.ts
new file mode 100644
index 0000000000..d3fccf1233
--- /dev/null
+++ b/src/api/endpoints/posts/polls/recommendation.ts
@@ -0,0 +1,57 @@
+/**
+ * Module dependencies
+ */
+import $ from 'cafy';
+import Vote from '../../../models/poll-vote';
+import Post from '../../../models/post';
+import serialize from '../../../serializers/post';
+
+/**
+ * Get recommended polls
+ *
+ * @param {any} params
+ * @param {any} user
+ * @return {Promise<any>}
+ */
+module.exports = (params, user) => new Promise(async (res, rej) => {
+ // Get 'limit' parameter
+ const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
+ if (limitErr) return rej('invalid limit param');
+
+ // Get 'offset' parameter
+ const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
+ if (offsetErr) return rej('invalid offset param');
+
+ // Get votes
+ const votes = await Vote.find({
+ user_id: user._id
+ }, {
+ fields: {
+ _id: false,
+ post_id: true
+ }
+ });
+
+ const nin = votes && votes.length != 0 ? votes.map(v => v.post_id) : [];
+
+ const posts = await Post
+ .find({
+ _id: {
+ $nin: nin
+ },
+ poll: {
+ $exists: true,
+ $ne: null
+ }
+ }, {
+ limit: limit,
+ skip: offset,
+ sort: {
+ _id: -1
+ }
+ });
+
+ // Serialize
+ res(await Promise.all(posts.map(async post =>
+ await serialize(post, user, { detail: true }))));
+});