From 31f36fa618e02bab9776a943366167e1009ede48 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 9 Mar 2017 03:50:09 +0900 Subject: :v: --- src/api/endpoints/posts/context.ts | 8 ++--- src/api/endpoints/posts/create.ts | 50 +++++++++++------------------ src/api/endpoints/posts/favorites/create.ts | 4 +-- src/api/endpoints/posts/favorites/delete.ts | 4 +-- src/api/endpoints/posts/likes.ts | 10 +++--- src/api/endpoints/posts/likes/create.ts | 4 +-- src/api/endpoints/posts/likes/delete.ts | 4 +-- src/api/endpoints/posts/mentions.ts | 10 +++--- src/api/endpoints/posts/polls/vote.ts | 11 +++---- src/api/endpoints/posts/replies.ts | 10 +++--- src/api/endpoints/posts/reposts.ts | 10 +++--- src/api/endpoints/posts/search.ts | 10 +++--- src/api/endpoints/posts/show.ts | 4 +-- src/api/endpoints/posts/timeline.ts | 10 +++--- 14 files changed, 67 insertions(+), 82 deletions(-) (limited to 'src/api/endpoints/posts') diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts index 03f5a95800..cd5f15f481 100644 --- a/src/api/endpoints/posts/context.ts +++ b/src/api/endpoints/posts/context.ts @@ -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 diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index 7f952e83be..4cb60705af 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -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'); diff --git a/src/api/endpoints/posts/favorites/create.ts b/src/api/endpoints/posts/favorites/create.ts index 96edf0eb27..f9dee271b5 100644 --- a/src/api/endpoints/posts/favorites/create.ts +++ b/src/api/endpoints/posts/favorites/create.ts @@ -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 diff --git a/src/api/endpoints/posts/favorites/delete.ts b/src/api/endpoints/posts/favorites/delete.ts index 5179fad9a5..c4fe7d3234 100644 --- a/src/api/endpoints/posts/favorites/delete.ts +++ b/src/api/endpoints/posts/favorites/delete.ts @@ -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 diff --git a/src/api/endpoints/posts/likes.ts b/src/api/endpoints/posts/likes.ts index 23933237a7..29aff1de38 100644 --- a/src/api/endpoints/posts/likes.ts +++ b/src/api/endpoints/posts/likes.ts @@ -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 diff --git a/src/api/endpoints/posts/likes/create.ts b/src/api/endpoints/posts/likes/create.ts index db8b6d0b20..3a7650dead 100644 --- a/src/api/endpoints/posts/likes/create.ts +++ b/src/api/endpoints/posts/likes/create.ts @@ -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 diff --git a/src/api/endpoints/posts/likes/delete.ts b/src/api/endpoints/posts/likes/delete.ts index 8d9fe273db..d90f2937e0 100644 --- a/src/api/endpoints/posts/likes/delete.ts +++ b/src/api/endpoints/posts/likes/delete.ts @@ -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 diff --git a/src/api/endpoints/posts/mentions.ts b/src/api/endpoints/posts/mentions.ts index 677df52b28..0ebe8be503 100644 --- a/src/api/endpoints/posts/mentions.ts +++ b/src/api/endpoints/posts/mentions.ts @@ -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 diff --git a/src/api/endpoints/posts/polls/vote.ts b/src/api/endpoints/posts/polls/vote.ts index 04e380807f..e345497905 100644 --- a/src/api/endpoints/posts/polls/vote.ts +++ b/src/api/endpoints/posts/polls/vote.ts @@ -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 diff --git a/src/api/endpoints/posts/replies.ts b/src/api/endpoints/posts/replies.ts index 4516dce15c..89f4d99841 100644 --- a/src/api/endpoints/posts/replies.ts +++ b/src/api/endpoints/posts/replies.ts @@ -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 diff --git a/src/api/endpoints/posts/reposts.ts b/src/api/endpoints/posts/reposts.ts index 5098d5af81..b701ff7574 100644 --- a/src/api/endpoints/posts/reposts.ts +++ b/src/api/endpoints/posts/reposts.ts @@ -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 diff --git a/src/api/endpoints/posts/search.ts b/src/api/endpoints/posts/search.ts index 04b845bc3a..b434f64342 100644 --- a/src/api/endpoints/posts/search.ts +++ b/src/api/endpoints/posts/search.ts @@ -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); diff --git a/src/api/endpoints/posts/show.ts b/src/api/endpoints/posts/show.ts index e0bed903b4..5bfe4f6605 100644 --- a/src/api/endpoints/posts/show.ts +++ b/src/api/endpoints/posts/show.ts @@ -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 diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts index 02fdf4a17c..314e992344 100644 --- a/src/api/endpoints/posts/timeline.ts +++ b/src/api/endpoints/posts/timeline.ts @@ -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 -- cgit v1.2.3-freya