diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-03-02 03:16:39 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-03-02 03:16:39 +0900 |
| commit | a53edc96bec3503517daa24837def5d3ecd82624 (patch) | |
| tree | e0a1b5081c137664d87d8ffa1f49160793edabb3 /src/api/endpoints | |
| parent | Custom validator support (diff) | |
| download | sharkey-a53edc96bec3503517daa24837def5d3ecd82624.tar.gz sharkey-a53edc96bec3503517daa24837def5d3ecd82624.tar.bz2 sharkey-a53edc96bec3503517daa24837def5d3ecd82624.zip | |
wip
Diffstat (limited to 'src/api/endpoints')
| -rw-r--r-- | src/api/endpoints/posts/create.js | 40 |
1 files changed, 13 insertions, 27 deletions
diff --git a/src/api/endpoints/posts/create.js b/src/api/endpoints/posts/create.js index 57e95bd712..2f03ebd8e9 100644 --- a/src/api/endpoints/posts/create.js +++ b/src/api/endpoints/posts/create.js @@ -4,8 +4,9 @@ * Module dependencies */ import * as mongo from 'mongodb'; +import validate from '../../validator'; import parse from '../../../common/text'; -import Post from '../../models/post'; +import { Post, isValidText } from '../../models/post'; import User from '../../models/user'; import Following from '../../models/following'; import DriveFile from '../../models/drive-file'; @@ -16,15 +17,14 @@ import event from '../../event'; import config from '../../../conf'; /** - * 最大文字数 - */ -const maxTextLength = 1000; - -/** * 添付できるファイルの数 */ const maxMediaCount = 4; +function hasDuplicates(array) { + return (new Set(array)).size !== array.length; +} + /** * Create a post * @@ -37,30 +37,16 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { // Get 'text' parameter - let text = params.text; - if (text !== undefined && text !== null) { - if (typeof text != 'string') { - return rej('text must be a string'); - } - text = text.trim(); - if (text.length == 0) { - text = null; - } else if (text.length > maxTextLength) { - return rej('too long text'); - } - } else { - text = null; - } + const [text, textErr] = validate(params.text, 'string', false, isValidText); + if (textErr) return rej('invalid text'); // Get 'media_ids' parameter - let medias = params.media_ids; - let files = []; - if (medias !== undefined && medias !== null) { - if (!Array.isArray(medias)) { - return rej('media_ids must be an array'); - } + const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'array', false, x => !hasDuplicates(x)); + if (mediaIdsErr) return rej('invalid media_ids'); - if (medias.length > maxMediaCount) { + let files = []; + if (mediaIds !== null) { + if (mediaIds.length > maxMediaCount) { return rej('too many media'); } |