This commit is contained in:
syuilo
2017-03-09 03:50:09 +09:00
parent 3d536ddb3f
commit 31f36fa618
64 changed files with 241 additions and 256 deletions

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Post from '../../models/post';
import serialize from '../../serializers/post';
@@ -14,15 +14,15 @@ import serialize from '../../serializers/post';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get 'limit' parameter
const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get();
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] = it(params.offset).expect.number().min(0).get();
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
// Lookup post

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
const parse = require('../../../common/text');
import Post from '../../models/post';
import { isValidText } from '../../models/post';
@@ -23,11 +23,11 @@ import config from '../../../conf';
*/
module.exports = (params, user, app) => new Promise(async (res, rej) => {
// Get 'text' parameter
const [text, textErr] = it(params.text).must.be.a.string().validate(isValidText).get();
const [text, textErr] = $(params.text).optional.string().pipe(isValidText).$;
if (textErr) return rej('invalid text');
// Get 'media_ids' parameter
const [mediaIds, mediaIdsErr] = it(params.media_ids).must.be.an.array().unique().range(1, 4).get();
const [mediaIds, mediaIdsErr] = $(params.media_ids).optional.array('id').unique().range(1, 4).$;
if (mediaIdsErr) return rej('invalid media_ids');
let files = [];
@@ -36,8 +36,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
// forEach だと途中でエラーなどがあっても return できないので
// 敢えて for を使っています。
for (let i = 0; i < mediaIds.length; i++) {
const [mediaId, mediaIdErr] = it(mediaIds[i]).must.be.an.id().required().get();
if (mediaIdErr) return rej('invalid media id');
const mediaId = mediaIds[i];
// Fetch file
// SELECT _id
@@ -59,7 +58,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
}
// Get 'repost_id' parameter
const [repostId, repostIdErr] = it(params.repost_id).must.be.an.id().get();
const [repostId, repostIdErr] = $(params.repost_id).optional.id().$;
if (repostIdErr) return rej('invalid repost_id');
let repost = null;
@@ -101,7 +100,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
}
// Get 'in_reply_to_post_id' parameter
const [inReplyToPostId, inReplyToPostIdErr] = it(params.reply_to_id, 'id').get();
const [inReplyToPostId, inReplyToPostIdErr] = $(params.reply_to_id).optional.id().$;
if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id');
let inReplyToPost = null;
@@ -122,37 +121,24 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
}
// Get 'poll' parameter
const [_poll, pollErr] = it(params.poll, 'object').get();
const [poll, pollErr] = $(params.poll).optional.object()
.have('choices', $().array('string')
.unique()
.range(2, 10)
.each(c => c.length > 0 && c.length < 50))
.$;
if (pollErr) return rej('invalid poll');
let poll = null;
if (_poll !== undefined) {
const [pollChoices, pollChoicesErr] =
it(params.poll.choices).expect.array()
.required()
.unique()
.allString()
.range(2, 10)
.validate(choices => !choices.some(choice => {
if (typeof choice != 'string') return true;
if (choice.trim().length == 0) return true;
if (choice.trim().length > 50) return true;
return false;
}))
.get();
if (pollChoicesErr) return rej('invalid poll choices');
_poll.choices = pollChoices.map((choice, i) => ({
if (poll) {
(poll as any).choices = (poll as any).choices.map((choice, i) => ({
id: i, // IDを付与
text: choice.trim(),
votes: 0
}));
poll = _poll;
}
// テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー
if (text === undefined && files === null && repost === null && poll === null) {
if (text === undefined && files === null && repost === null && poll === undefined) {
return rej('text, media_ids, repost_id or poll is required');
}
@@ -162,7 +148,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
media_ids: files ? files.map(file => file._id) : undefined,
reply_to_id: inReplyToPost ? inReplyToPost._id : undefined,
repost_id: repost ? repost._id : undefined,
poll: poll ? poll : undefined,
poll: poll,
text: text,
user_id: user._id,
app_id: app ? app._id : null
@@ -235,7 +221,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
addMention(inReplyToPost.user_id, 'reply');
}
// If it is repost
// If $ is repost
if (repost) {
// Notify
const type = text ? 'quote' : 'repost';
@@ -243,7 +229,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
post_id: post._id
});
// If it is quote repost
// If $ is quote repost
if (text) {
// Add mention
addMention(repost.user_id, 'quote');

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Favorite from '../../../models/favorite';
import Post from '../../../models/post';
@@ -14,7 +14,7 @@ import Post from '../../../models/post';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get favoritee

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Favorite from '../../../models/favorite';
import Post from '../../../models/post';
@@ -14,7 +14,7 @@ import Post from '../../../models/post';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get favoritee

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Post from '../../models/post';
import Like from '../../models/like';
import serialize from '../../serializers/user';
@@ -15,19 +15,19 @@ import serialize from '../../serializers/user';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get 'limit' parameter
const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get();
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] = it(params.offset).expect.number().min(0).get();
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
const [sort = 'desc', sortError] = it(params.sort).expect.string().or('desc asc').get();
const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
if (sortError) return rej('invalid sort param');
// Lookup post

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Like from '../../../models/like';
import Post from '../../../models/post';
import User from '../../../models/user';
@@ -16,7 +16,7 @@ import notify from '../../../common/notify';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get likee

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Like from '../../../models/like';
import Post from '../../../models/post';
import User from '../../../models/user';
@@ -16,7 +16,7 @@ import User from '../../../models/user';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get likee

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Post from '../../models/post';
import getFriends from '../../common/get-friends';
import serialize from '../../serializers/post';
@@ -16,19 +16,19 @@ import serialize from '../../serializers/post';
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'following' parameter
const [following = false, followingError] =
it(params.following).expect.boolean().get();
$(params.following).optional.boolean().$;
if (followingError) return rej('invalid following param');
// Get 'limit' parameter
const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get();
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get();
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'max_id' parameter
const [maxId, maxIdErr] = it(params.max_id).expect.id().get();
const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
if (maxIdErr) return rej('invalid max_id param');
// Check if both of since_id and max_id is specified

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Vote from '../../../models/poll-vote';
import Post from '../../../models/post';
import notify from '../../../common/notify';
@@ -15,7 +15,7 @@ import notify from '../../../common/notify';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get votee
@@ -33,10 +33,9 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'choice' parameter
const [choice, choiceError] =
it(params.choice).expect.string()
.required()
.validate(c => post.poll.choices.some(x => x.id == c))
.get();
$(params.choice).string()
.pipe(c => post.poll.choices.some(x => x.id == c))
.$;
if (choiceError) return rej('invalid choice param');
// if already voted

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Post from '../../models/post';
import serialize from '../../serializers/post';
@@ -14,19 +14,19 @@ import serialize from '../../serializers/post';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get 'limit' parameter
const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get();
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] = it(params.offset).expect.number().min(0).get();
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter
const [sort = 'desc', sortError] = it(params.sort).expect.string().or('desc asc').get();
const [sort = 'desc', sortError] = $(params.sort).optional.string().or('desc asc').$;
if (sortError) return rej('invalid sort param');
// Lookup post

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Post from '../../models/post';
import serialize from '../../serializers/post';
@@ -14,19 +14,19 @@ import serialize from '../../serializers/post';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get 'limit' parameter
const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get();
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get();
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'max_id' parameter
const [maxId, maxIdErr] = it(params.max_id).expect.id().get();
const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
if (maxIdErr) return rej('invalid max_id param');
// Check if both of since_id and max_id is specified

View File

@@ -2,7 +2,7 @@
* Module dependencies
*/
import * as mongo from 'mongodb';
import it from 'cafy';
import $ from 'cafy';
const escapeRegexp = require('escape-regexp');
import Post from '../../models/post';
import serialize from '../../serializers/post';
@@ -17,18 +17,18 @@ import config from '../../../conf';
*/
module.exports = (params, me) => new Promise(async (res, rej) => {
// Get 'query' parameter
const [query, queryError] = it(params.query).expect.string().required().trim().validate(x => x != '').get();
const [query, queryError] = $(params.query).string().pipe(x => x != '').$;
if (queryError) return rej('invalid query param');
// Get 'offset' parameter
const [offset = 0, offsetErr] = it(params.offset).expect.number().min(0).get();
const [offset = 0, offsetErr] = $(params.offset).optional.number().min(0).$;
if (offsetErr) return rej('invalid offset param');
// Get 'max' parameter
const [max = 10, maxErr] = it(params.max).expect.number().range(1, 30).get();
const [max = 10, maxErr] = $(params.max).optional.number().range(1, 30).$;
if (maxErr) return rej('invalid max param');
// If Elasticsearch is available, search by it
// If Elasticsearch is available, search by $
// If not, search by MongoDB
(config.elasticsearch.enable ? byElasticsearch : byNative)
(res, rej, me, query, offset, max);

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Post from '../../models/post';
import serialize from '../../serializers/post';
@@ -14,7 +14,7 @@ import serialize from '../../serializers/post';
*/
module.exports = (params, user) => new Promise(async (res, rej) => {
// Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id!').get();
const [postId, postIdErr] = $(params.post_id).id().$;
if (postIdErr) return rej('invalid post_id param');
// Get post

View File

@@ -1,7 +1,7 @@
/**
* Module dependencies
*/
import it from 'cafy';
import $ from 'cafy';
import Post from '../../models/post';
import getFriends from '../../common/get-friends';
import serialize from '../../serializers/post';
@@ -16,15 +16,15 @@ import serialize from '../../serializers/post';
*/
module.exports = (params, user, app) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = it(params.limit).expect.number().range(1, 100).get();
const [limit = 10, limitErr] = $(params.limit).optional.number().range(1, 100).$;
if (limitErr) return rej('invalid limit param');
// Get 'since_id' parameter
const [sinceId, sinceIdErr] = it(params.since_id).expect.id().get();
const [sinceId, sinceIdErr] = $(params.since_id).optional.id().$;
if (sinceIdErr) return rej('invalid since_id param');
// Get 'max_id' parameter
const [maxId, maxIdErr] = it(params.max_id).expect.id().get();
const [maxId, maxIdErr] = $(params.max_id).optional.id().$;
if (maxIdErr) return rej('invalid max_id param');
// Check if both of since_id and max_id is specified
@@ -32,7 +32,7 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => {
return rej('cannot set since_id and max_id');
}
// ID list of the user itself and other users who the user follows
// ID list of the user $self and other users who the user follows
const followingIds = await getFriends(user._id);
// Construct query