@ -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
|
||||
|
101
src/api/endpoints/posts/polls/vote.js
Normal file
101
src/api/endpoints/posts/polls/vote.js
Normal file
@ -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;
|
||||
}
|
@ -39,7 +39,6 @@ module.exports = (params, user) =>
|
||||
|
||||
// Serialize
|
||||
res(await serialize(post, user, {
|
||||
serializeReplyTo: true,
|
||||
includeIsLiked: true
|
||||
detail: true
|
||||
}));
|
||||
});
|
||||
|
Reference in New Issue
Block a user