From caf945765335c81d4785b7a108c1944aa0f2af26 Mon Sep 17 00:00:00 2001 From: syuilo Date: Sat, 25 Mar 2017 16:43:55 +0900 Subject: [PATCH] #321 --- package.json | 2 +- src/api/endpoints.ts | 4 + .../endpoints/posts/polls/recommendation.ts | 57 ++++++++++ .../tags/home-widgets/recommended-polls.tag | 103 ++++++++++++++++++ src/web/app/desktop/tags/home.tag | 1 + src/web/app/desktop/tags/index.js | 1 + 6 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 src/api/endpoints/posts/polls/recommendation.ts create mode 100644 src/web/app/desktop/tags/home-widgets/recommended-polls.tag diff --git a/package.json b/package.json index 67b8015fa..8a7071a18 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "misskey", "author": "syuilo ", - "version": "0.0.1434", + "version": "0.0.1460", "license": "MIT", "description": "A miniblog-based SNS", "bugs": "https://github.com/syuilo/misskey/issues", diff --git a/src/api/endpoints.ts b/src/api/endpoints.ts index 2d3716bb8..f0031b82a 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 000000000..d3fccf123 --- /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} + */ +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 })))); +}); diff --git a/src/web/app/desktop/tags/home-widgets/recommended-polls.tag b/src/web/app/desktop/tags/home-widgets/recommended-polls.tag new file mode 100644 index 000000000..4ef7701c4 --- /dev/null +++ b/src/web/app/desktop/tags/home-widgets/recommended-polls.tag @@ -0,0 +1,103 @@ + +

投票

+ +
+

{ poll.text }

+ +
+

ありません!

+

読み込んでいます

+ + +
diff --git a/src/web/app/desktop/tags/home.tag b/src/web/app/desktop/tags/home.tag index 6c218d93c..93840bdf0 100644 --- a/src/web/app/desktop/tags/home.tag +++ b/src/web/app/desktop/tags/home.tag @@ -76,6 +76,7 @@ 'broadcast', 'notifications', 'user-recommendation', + 'recommended-polls', 'donation', 'nav', 'tips' diff --git a/src/web/app/desktop/tags/index.js b/src/web/app/desktop/tags/index.js index 1dd279430..3032c9c54 100644 --- a/src/web/app/desktop/tags/index.js +++ b/src/web/app/desktop/tags/index.js @@ -42,6 +42,7 @@ require('./home-widgets/rss-reader.tag'); require('./home-widgets/photo-stream.tag'); require('./home-widgets/broadcast.tag'); require('./home-widgets/version.tag'); +require('./home-widgets/recommended-polls.tag'); require('./timeline.tag'); require('./messaging/window.tag'); require('./messaging/room-window.tag');