summaryrefslogtreecommitdiff
path: root/src/api/endpoints
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-02-14 13:59:26 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-02-14 13:59:26 +0900
commit2b4c5ecff4e4457c49a14d3ed0095cc9f0e1f758 (patch)
tree216793638b28dd1de209561ac79544e4a27f407a /src/api/endpoints
parent#133 (diff)
downloadsharkey-2b4c5ecff4e4457c49a14d3ed0095cc9f0e1f758.tar.gz
sharkey-2b4c5ecff4e4457c49a14d3ed0095cc9f0e1f758.tar.bz2
sharkey-2b4c5ecff4e4457c49a14d3ed0095cc9f0e1f758.zip
Implement the poll feature
Closes #164
Diffstat (limited to 'src/api/endpoints')
-rw-r--r--src/api/endpoints/posts/create.js57
-rw-r--r--src/api/endpoints/posts/polls/vote.js101
-rw-r--r--src/api/endpoints/posts/show.js3
3 files changed, 156 insertions, 5 deletions
diff --git a/src/api/endpoints/posts/create.js b/src/api/endpoints/posts/create.js
index e7c1d0ceca..61f8e714fb 100644
--- a/src/api/endpoints/posts/create.js
+++ b/src/api/endpoints/posts/create.js
@@ -161,9 +161,59 @@ module.exports = (params, user, app) =>
replyTo = null;
}
- // テキストが無いかつ添付ファイルが無いかつRepostも無かったらエラー
- if (text === null && files === null && repost === null) {
- return rej('text, media_ids or repost_id is required');
+ // Get 'poll' parameter
+ let poll = params.poll;
+ if (poll !== undefined && poll !== null) {
+ // 選択肢が無かったらエラー
+ if (poll.choices == null) {
+ return rej('poll choices is required');
+ }
+
+ // 選択肢が配列でなかったらエラー
+ if (!Array.isArray(poll.choices)) {
+ return rej('poll choices must be an array');
+ }
+
+ // Validate each choices
+ const shouldReject = poll.choices.some(choice => {
+ if (typeof choice !== 'string') return true;
+ if (choice.trim().length === 0) return true;
+ if (choice.trim().length > 100) return true;
+ });
+
+ if (shouldReject) {
+ return rej('invalid poll choices');
+ }
+
+ // Trim choices
+ poll.choices = poll.choices.map(choice => choice.trim());
+
+ // Drop duplicates
+ poll.choices = poll.choices.filter((x, i, s) => s.indexOf(x) == i);
+
+ // 選択肢がひとつならエラー
+ if (poll.choices.length == 1) {
+ return rej('poll choices must be ひとつ以上');
+ }
+
+ // 選択肢が多すぎてもエラー
+ if (poll.choices.length > 10) {
+ return rej('many poll choices');
+ }
+
+ // serialize
+ poll.choices = poll.choices.map((choice, i) => ({
+ id: i, // IDを付与
+ text: choice,
+ votes: 0
+ }));
+ } else {
+ poll = null;
+ }
+
+ // テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー
+ if (text === null && files === null && repost === null && poll === null) {
+ return rej('text, media_ids, repost_id or poll is required');
}
// 投稿を作成
@@ -172,6 +222,7 @@ module.exports = (params, user, app) =>
media_ids: media ? files.map(file => file._id) : undefined,
reply_to_id: replyTo ? replyTo._id : undefined,
repost_id: repost ? repost._id : undefined,
+ poll: poll ? poll : undefined,
text: text,
user_id: user._id,
app_id: app ? app._id : null
diff --git a/src/api/endpoints/posts/polls/vote.js b/src/api/endpoints/posts/polls/vote.js
new file mode 100644
index 0000000000..f1842069d4
--- /dev/null
+++ b/src/api/endpoints/posts/polls/vote.js
@@ -0,0 +1,101 @@
+'use strict';
+
+/**
+ * Module dependencies
+ */
+import * as mongo from 'mongodb';
+import Vote from '../../../models/poll-vote';
+import Post from '../../../models/post';
+import notify from '../../../common/notify';
+
+/**
+ * Vote poll of a post
+ *
+ * @param {Object} params
+ * @param {Object} user
+ * @return {Promise<object>}
+ */
+module.exports = (params, user) =>
+ new Promise(async (res, rej) =>
+{
+ // Get 'post_id' parameter
+ const postId = params.post_id;
+ if (postId === undefined || postId === null) {
+ return rej('post_id is required');
+ }
+
+ // Validate id
+ if (!mongo.ObjectID.isValid(postId)) {
+ return rej('incorrect post_id');
+ }
+
+ // Get votee
+ const post = await Post.findOne({
+ _id: new mongo.ObjectID(postId)
+ });
+
+ if (post === null) {
+ return rej('post not found');
+ }
+
+ if (post.poll == null) {
+ return rej('poll not found');
+ }
+
+ // Get 'choice' parameter
+ const choice = params.choice;
+ if (choice == null) {
+ return rej('choice is required');
+ }
+
+ // Validate choice
+ if (!post.poll.choices.some(x => x.id == choice)) {
+ return rej('invalid choice');
+ }
+
+ // Check arleady voted
+ const exist = await Vote.findOne({
+ post_id: post._id,
+ user_id: user._id
+ });
+
+ if (exist !== null) {
+ return rej('already voted');
+ }
+
+ // Create vote
+ await Vote.insert({
+ created_at: new Date(),
+ post_id: post._id,
+ user_id: user._id,
+ choice: choice
+ });
+
+ // Send response
+ res();
+
+ const inc = {};
+ inc[`poll.choices.${ findWithAttr(post.poll.choices, 'id', choice) }.votes`] = 1;
+
+ console.log(inc);
+
+ // Increment likes count
+ Post.update({ _id: post._id }, {
+ $inc: inc
+ });
+
+ // Notify
+ notify(post.user_id, user._id, 'poll_vote', {
+ post_id: post._id,
+ choice: choice
+ });
+});
+
+function findWithAttr(array, attr, value) {
+ for (let i = 0; i < array.length; i += 1) {
+ if(array[i][attr] === value) {
+ return i;
+ }
+ }
+ return -1;
+}
diff --git a/src/api/endpoints/posts/show.js b/src/api/endpoints/posts/show.js
index f399d86c8a..1b9a747a8d 100644
--- a/src/api/endpoints/posts/show.js
+++ b/src/api/endpoints/posts/show.js
@@ -39,7 +39,6 @@ module.exports = (params, user) =>
// Serialize
res(await serialize(post, user, {
- serializeReplyTo: true,
- includeIsLiked: true
+ detail: true
}));
});