From 73deef0ca7038f9a88f9cb36e6c1b17c354a3a35 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 01:06:16 +0900 Subject: wip --- src/api/validator.ts | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/api/validator.ts (limited to 'src/api') diff --git a/src/api/validator.ts b/src/api/validator.ts new file mode 100644 index 0000000000..d75fe6420b --- /dev/null +++ b/src/api/validator.ts @@ -0,0 +1,53 @@ +import * as mongo from 'mongodb'; + +type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object'; + +export default (value: any, isRequired: boolean, type: Type): [T, string] => { + if (value === undefined || value === null) { + if (isRequired) { + return [null, 'is-required'] + } else { + return [null, null] + } + } + + switch (type) { + case 'id': + if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { + return [null, 'incorrect-id']; + } + break; + + case 'string': + if (typeof value != 'string') { + return [null, 'must-be-a-string']; + } + break; + + case 'number': + if (!Number.isFinite(value)) { + return [null, 'must-be-a-number']; + } + break; + + case 'boolean': + if (typeof value != 'boolean') { + return [null, 'must-be-an-boolean']; + } + break; + + case 'array': + if (!Array.isArray(value)) { + return [null, 'must-be-an-array']; + } + break; + + case 'object': + if (typeof value != 'object') { + return [null, 'must-be-an-onject']; + } + break; + } + + return [value, null]; +}; -- cgit v1.2.3-freya From 6256a8bdfc009aca0d737a4c7b1e45f6450b6db4 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 01:11:30 +0900 Subject: Custom validator support --- src/api/validator.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/api') diff --git a/src/api/validator.ts b/src/api/validator.ts index d75fe6420b..2562535c02 100644 --- a/src/api/validator.ts +++ b/src/api/validator.ts @@ -2,7 +2,7 @@ import * as mongo from 'mongodb'; type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object'; -export default (value: any, isRequired: boolean, type: Type): [T, string] => { +export default (value: any, isRequired: boolean, type: Type, validator?: (any) => boolean): [T, string] => { if (value === undefined || value === null) { if (isRequired) { return [null, 'is-required'] @@ -49,5 +49,11 @@ export default (value: any, isRequired: boolean, type: Type): [T, string] => break; } + if (validator) { + if (!validator(value)) { + return [null, 'invalid-format']; + } + } + return [value, null]; }; -- cgit v1.2.3-freya From a53edc96bec3503517daa24837def5d3ecd82624 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 03:16:39 +0900 Subject: wip --- src/api/endpoints/posts/create.js | 40 +++++++++++++-------------------------- src/api/models/post.ts | 4 ++++ src/api/validator.ts | 26 +++++++++++++++++++++---- 3 files changed, 39 insertions(+), 31 deletions(-) (limited to 'src/api') 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'; @@ -15,16 +16,15 @@ import notify from '../../common/notify'; 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'); } diff --git a/src/api/models/post.ts b/src/api/models/post.ts index ab29187251..baab63f991 100644 --- a/src/api/models/post.ts +++ b/src/api/models/post.ts @@ -1,3 +1,7 @@ import db from '../../db/mongodb'; export default db.get('posts') as any; // fuck type definition + +export function isValidText(text: string): boolean { + return text.length <= 1000 && text.trim() != ''; +} diff --git a/src/api/validator.ts b/src/api/validator.ts index 2562535c02..830786a18d 100644 --- a/src/api/validator.ts +++ b/src/api/validator.ts @@ -2,7 +2,15 @@ import * as mongo from 'mongodb'; type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object'; -export default (value: any, isRequired: boolean, type: Type, validator?: (any) => boolean): [T, string] => { +type Validator = ((x: T) => boolean | string) | ((x: T) => boolean | string)[]; + +function validate(value: any, type: 'id', isRequired?: boolean): [mongo.ObjectID, string]; +function validate(value: any, type: 'string', isRequired?: boolean, validator?: Validator): [string, string]; +function validate(value: any, type: 'number', isRequired?: boolean, validator?: Validator): [number, string]; +function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, string]; +function validate(value: any, type: 'array', isRequired?: boolean, validator?: Validator): [any[], string]; +function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator): [Object, string]; +function validate(value: any, type: Type, isRequired?: boolean, validator?: Validator): [T, string] { if (value === undefined || value === null) { if (isRequired) { return [null, 'is-required'] @@ -49,11 +57,21 @@ export default (value: any, isRequired: boolean, type: Type, validator?: (any break; } + if (type == 'id') value = new mongo.ObjectID(value); + if (validator) { - if (!validator(value)) { - return [null, 'invalid-format']; + const validators = Array.isArray(validator) ? validator : [validator]; + for (let i = 0; i < validators.length; i++) { + const result = validators[i](value); + if (result === false) { + return [null, 'invalid-format']; + } else if (typeof result == 'string') { + return [null, result]; + } } } return [value, null]; -}; +} + +export default validate; -- cgit v1.2.3-freya From fd0ce15b1b08abc72f3b45de90d6a47657440983 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 05:11:37 +0900 Subject: wip --- src/api/endpoints/posts/create.js | 157 ++++++++++++-------------------------- 1 file changed, 50 insertions(+), 107 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.js b/src/api/endpoints/posts/create.js index 2f03ebd8e9..9070dcb413 100644 --- a/src/api/endpoints/posts/create.js +++ b/src/api/endpoints/posts/create.js @@ -3,7 +3,6 @@ /** * Module dependencies */ -import * as mongo from 'mongodb'; import validate from '../../validator'; import parse from '../../../common/text'; import { Post, isValidText } from '../../models/post'; @@ -16,11 +15,6 @@ import notify from '../../common/notify'; import event from '../../event'; import config from '../../../conf'; -/** - * 添付できるファイルの数 - */ -const maxMediaCount = 4; - function hasDuplicates(array) { return (new Set(array)).size !== array.length; } @@ -41,37 +35,25 @@ module.exports = (params, user, app) => if (textErr) return rej('invalid text'); // Get 'media_ids' parameter - const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'array', false, x => !hasDuplicates(x)); + const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'array', false, [ + x => !hasDuplicates(x), + x => x.length > 4 ? 'too many media' : true + ]); if (mediaIdsErr) return rej('invalid media_ids'); let files = []; if (mediaIds !== null) { - if (mediaIds.length > maxMediaCount) { - return rej('too many media'); - } - - // Drop duplications - medias = medias.filter((x, i, s) => s.indexOf(x) == i); - // Fetch files // forEach だと途中でエラーなどがあっても return できないので // 敢えて for を使っています。 - for (let i = 0; i < medias.length; i++) { - const media = medias[i]; - - if (typeof media != 'string') { - return rej('media id must be a string'); - } - - // Validate id - if (!mongo.ObjectID.isValid(media)) { - return rej('incorrect media id'); - } + for (let i = 0; i < mediaIds.length; i++) { + const [mediaId, mediaIdErr] = validate(mediaIds[i], 'id', true); + if (mediaIdErr) return rej('invalid media id'); // Fetch file // SELECT _id const entity = await DriveFile.findOne({ - _id: new mongo.ObjectID(media), + _id: mediaId, user_id: user._id }, { _id: true @@ -88,20 +70,14 @@ module.exports = (params, user, app) => } // Get 'repost_id' parameter - let repost = params.repost_id; - if (repost !== undefined && repost !== null) { - if (typeof repost != 'string') { - return rej('repost_id must be a string'); - } - - // Validate id - if (!mongo.ObjectID.isValid(repost)) { - return rej('incorrect repost_id'); - } + const [repostId, repostIdErr] = validate(params.repost_id, 'id'); + if (repostIdErr) return rej('invalid repost_id'); + let repost = null; + if (repostId !== null) { // Fetch repost to post repost = await Post.findOne({ - _id: new mongo.ObjectID(repost) + _id: repostId }); if (repost == null) { @@ -133,96 +109,63 @@ module.exports = (params, user, app) => text === null && files === null) { return rej('二重Repostです(NEED TRANSLATE)'); } - } else { - repost = null; } - // Get 'reply_to_id' parameter - let replyTo = params.reply_to_id; - if (replyTo !== undefined && replyTo !== null) { - if (typeof replyTo != 'string') { - return rej('reply_to_id must be a string'); - } - - // Validate id - if (!mongo.ObjectID.isValid(replyTo)) { - return rej('incorrect reply_to_id'); - } + // Get 'in_reply_to_post_id' parameter + const [inReplyToPostId, inReplyToPostIdErr] = validate(params.reply_to_id, 'id'); + if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id'); + let inReplyToPost = null; + if (inReplyToPostId !== null) { // Fetch reply - replyTo = await Post.findOne({ - _id: new mongo.ObjectID(replyTo) + inReplyToPost = await Post.findOne({ + _id: inReplyToPostId }); - if (replyTo === null) { - return rej('reply to post is not found'); + if (inReplyToPost === null) { + return rej('in reply to post is not found'); } // 返信対象が引用でないRepostだったらエラー - if (replyTo.repost_id && !replyTo.text && !replyTo.media_ids) { + if (inReplyToPost.repost_id && !inReplyToPost.text && !inReplyToPost.media_ids) { return rej('cannot reply to repost'); } - } else { - replyTo = null; } // 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'); - } - - // 選択肢が空の配列でエラー - if (poll.choices.length == 0) { - return rej('poll choices is required'); - } - - // 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 duplications - 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) => ({ + const [_poll, pollErr] = validate(params.poll, 'object'); + if (pollErr) return rej('invalid poll'); + + let poll = null; + if (_poll !== null) { + const [pollChoices, pollChoicesErr] = validate(params.poll, 'array', false, [ + choices => !hasDuplicates(choices), + choices => { + const shouldReject = choices.some(choice => { + if (typeof choice != 'string') return true; + if (choice.trim().length == 0) return true; + if (choice.trim().length > 50) return true; + }); + return shouldReject ? 'invalid poll choices' : true; + }, + // 選択肢がひとつならエラー + choices => choices.length == 1 ? 'poll choices must be ひとつ以上' : true, + // 選択肢が多すぎてもエラー + choices => choices.length > 10 ? 'many poll choices' : true, + ]); + if (pollChoicesErr) return rej('invalid poll choices'); + + _poll.choices = pollChoices.map((choice, i) => ({ id: i, // IDを付与 - text: choice, + text: choice.trim(), votes: 0 })); - } else { - poll = null; + + poll = _poll; } // テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー - if (text === null && files === null && repost === null && poll === null) { + if (text === null && files === null && repost === null && pollChoices === null) { return rej('text, media_ids, repost_id or poll is required'); } -- cgit v1.2.3-freya From 17c8969fd2ff51096fcde8d0ae272ca67202f55d Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 05:12:59 +0900 Subject: fix --- src/api/endpoints/posts/create.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.js b/src/api/endpoints/posts/create.js index 9070dcb413..151938004d 100644 --- a/src/api/endpoints/posts/create.js +++ b/src/api/endpoints/posts/create.js @@ -165,7 +165,7 @@ module.exports = (params, user, app) => } // テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー - if (text === null && files === null && repost === null && pollChoices === null) { + if (text === null && files === null && repost === null && poll === null) { return rej('text, media_ids, repost_id or poll is required'); } -- cgit v1.2.3-freya From b3455bf1cb30beb46859345c482e6585845ca388 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 05:24:20 +0900 Subject: Extract hasDuplicates function --- src/api/endpoints/posts/create.js | 5 +---- src/common/has-duplicates.ts | 1 + 2 files changed, 2 insertions(+), 4 deletions(-) create mode 100644 src/common/has-duplicates.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.js b/src/api/endpoints/posts/create.js index 151938004d..92aeb3d08e 100644 --- a/src/api/endpoints/posts/create.js +++ b/src/api/endpoints/posts/create.js @@ -4,6 +4,7 @@ * Module dependencies */ import validate from '../../validator'; +import hasDuplicates from '../../../common/has-duplicates'; import parse from '../../../common/text'; import { Post, isValidText } from '../../models/post'; import User from '../../models/user'; @@ -15,10 +16,6 @@ import notify from '../../common/notify'; import event from '../../event'; import config from '../../../conf'; -function hasDuplicates(array) { - return (new Set(array)).size !== array.length; -} - /** * Create a post * diff --git a/src/common/has-duplicates.ts b/src/common/has-duplicates.ts new file mode 100644 index 0000000000..dd5e6759f1 --- /dev/null +++ b/src/common/has-duplicates.ts @@ -0,0 +1 @@ +export default (array: any[]) => (new Set(array)).size !== array.length; -- cgit v1.2.3-freya From f3f5869f5214085637276b077ece739eac2e5dff Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 05:31:30 +0900 Subject: Add 'set' type --- src/api/endpoints/posts/create.js | 9 +++------ src/api/validator.ts | 12 +++++++++++- 2 files changed, 14 insertions(+), 7 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.js b/src/api/endpoints/posts/create.js index 92aeb3d08e..eadc886c5c 100644 --- a/src/api/endpoints/posts/create.js +++ b/src/api/endpoints/posts/create.js @@ -4,7 +4,6 @@ * Module dependencies */ import validate from '../../validator'; -import hasDuplicates from '../../../common/has-duplicates'; import parse from '../../../common/text'; import { Post, isValidText } from '../../models/post'; import User from '../../models/user'; @@ -32,10 +31,9 @@ module.exports = (params, user, app) => if (textErr) return rej('invalid text'); // Get 'media_ids' parameter - const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'array', false, [ - x => !hasDuplicates(x), + const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'set', false, x => x.length > 4 ? 'too many media' : true - ]); + ); if (mediaIdsErr) return rej('invalid media_ids'); let files = []; @@ -135,8 +133,7 @@ module.exports = (params, user, app) => let poll = null; if (_poll !== null) { - const [pollChoices, pollChoicesErr] = validate(params.poll, 'array', false, [ - choices => !hasDuplicates(choices), + const [pollChoices, pollChoicesErr] = validate(params.poll, 'set', false, [ choices => { const shouldReject = choices.some(choice => { if (typeof choice != 'string') return true; diff --git a/src/api/validator.ts b/src/api/validator.ts index 830786a18d..3c426054e8 100644 --- a/src/api/validator.ts +++ b/src/api/validator.ts @@ -1,6 +1,7 @@ import * as mongo from 'mongodb'; +import hasDuplicates from '../common/has-duplicates'; -type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'object'; +type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object'; type Validator = ((x: T) => boolean | string) | ((x: T) => boolean | string)[]; @@ -9,6 +10,7 @@ function validate(value: any, type: 'string', isRequired?: boolean, validator?: function validate(value: any, type: 'number', isRequired?: boolean, validator?: Validator): [number, string]; function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, string]; function validate(value: any, type: 'array', isRequired?: boolean, validator?: Validator): [any[], string]; +function validate(value: any, type: 'set', isRequired?: boolean, validator?: Validator>): [Set, string]; function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator): [Object, string]; function validate(value: any, type: Type, isRequired?: boolean, validator?: Validator): [T, string] { if (value === undefined || value === null) { @@ -50,6 +52,14 @@ function validate(value: any, type: Type, isRequired?: boolean, validator?: V } break; + case 'set': + if (!Array.isArray(value)) { + return [null, 'must-be-an-array']; + } else if (hasDuplicates(value)) { + return [null, 'duplicated-contents']; + } + break; + case 'object': if (typeof value != 'object') { return [null, 'must-be-an-onject']; -- cgit v1.2.3-freya From d009286b51456643ab311a0c30ffc95a24c48d76 Mon Sep 17 00:00:00 2001 From: syuilo⭐️ Date: Thu, 2 Mar 2017 06:25:05 +0900 Subject: typo --- src/api/validator.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/api') diff --git a/src/api/validator.ts b/src/api/validator.ts index 3c426054e8..bc50da3a37 100644 --- a/src/api/validator.ts +++ b/src/api/validator.ts @@ -42,7 +42,7 @@ function validate(value: any, type: Type, isRequired?: boolean, validator?: V case 'boolean': if (typeof value != 'boolean') { - return [null, 'must-be-an-boolean']; + return [null, 'must-be-a-boolean']; } break; @@ -62,7 +62,7 @@ function validate(value: any, type: Type, isRequired?: boolean, validator?: V case 'object': if (typeof value != 'object') { - return [null, 'must-be-an-onject']; + return [null, 'must-be-an-object']; } break; } -- cgit v1.2.3-freya From 168039e68299029b11161b53f90cc103788c0cd3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 15:25:27 +0900 Subject: :v: --- src/api/endpoints/posts/create.js | 355 ------------------------------------- src/api/endpoints/posts/create.ts | 356 ++++++++++++++++++++++++++++++++++++++ src/api/validator.ts | 4 +- 3 files changed, 358 insertions(+), 357 deletions(-) delete mode 100644 src/api/endpoints/posts/create.js create mode 100644 src/api/endpoints/posts/create.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.js b/src/api/endpoints/posts/create.js deleted file mode 100644 index eadc886c5c..0000000000 --- a/src/api/endpoints/posts/create.js +++ /dev/null @@ -1,355 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import validate from '../../validator'; -import parse from '../../../common/text'; -import { Post, isValidText } from '../../models/post'; -import User from '../../models/user'; -import Following from '../../models/following'; -import DriveFile from '../../models/drive-file'; -import serialize from '../../serializers/post'; -import createFile from '../../common/add-file-to-drive'; -import notify from '../../common/notify'; -import event from '../../event'; -import config from '../../../conf'; - -/** - * Create a post - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise} - */ -module.exports = (params, user, app) => - new Promise(async (res, rej) => -{ - // Get 'text' parameter - const [text, textErr] = validate(params.text, 'string', false, isValidText); - if (textErr) return rej('invalid text'); - - // Get 'media_ids' parameter - const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'set', false, - x => x.length > 4 ? 'too many media' : true - ); - if (mediaIdsErr) return rej('invalid media_ids'); - - let files = []; - if (mediaIds !== null) { - // Fetch files - // forEach だと途中でエラーなどがあっても return できないので - // 敢えて for を使っています。 - for (let i = 0; i < mediaIds.length; i++) { - const [mediaId, mediaIdErr] = validate(mediaIds[i], 'id', true); - if (mediaIdErr) return rej('invalid media id'); - - // Fetch file - // SELECT _id - const entity = await DriveFile.findOne({ - _id: mediaId, - user_id: user._id - }, { - _id: true - }); - - if (entity === null) { - return rej('file not found'); - } else { - files.push(entity); - } - } - } else { - files = null; - } - - // Get 'repost_id' parameter - const [repostId, repostIdErr] = validate(params.repost_id, 'id'); - if (repostIdErr) return rej('invalid repost_id'); - - let repost = null; - if (repostId !== null) { - // Fetch repost to post - repost = await Post.findOne({ - _id: repostId - }); - - if (repost == null) { - return rej('repostee is not found'); - } else if (repost.repost_id && !repost.text && !repost.media_ids) { - return rej('cannot repost to repost'); - } - - // Fetch recently post - const latestPost = await Post.findOne({ - user_id: user._id - }, { - sort: { - _id: -1 - } - }); - - // 直近と同じRepost対象かつ引用じゃなかったらエラー - if (latestPost && - latestPost.repost_id && - latestPost.repost_id.equals(repost._id) && - text === null && files === null) { - return rej('二重Repostです(NEED TRANSLATE)'); - } - - // 直近がRepost対象かつ引用じゃなかったらエラー - if (latestPost && - latestPost._id.equals(repost._id) && - text === null && files === null) { - return rej('二重Repostです(NEED TRANSLATE)'); - } - } - - // Get 'in_reply_to_post_id' parameter - const [inReplyToPostId, inReplyToPostIdErr] = validate(params.reply_to_id, 'id'); - if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id'); - - let inReplyToPost = null; - if (inReplyToPostId !== null) { - // Fetch reply - inReplyToPost = await Post.findOne({ - _id: inReplyToPostId - }); - - if (inReplyToPost === null) { - return rej('in reply to post is not found'); - } - - // 返信対象が引用でないRepostだったらエラー - if (inReplyToPost.repost_id && !inReplyToPost.text && !inReplyToPost.media_ids) { - return rej('cannot reply to repost'); - } - } - - // Get 'poll' parameter - const [_poll, pollErr] = validate(params.poll, 'object'); - if (pollErr) return rej('invalid poll'); - - let poll = null; - if (_poll !== null) { - const [pollChoices, pollChoicesErr] = validate(params.poll, 'set', false, [ - choices => { - const shouldReject = choices.some(choice => { - if (typeof choice != 'string') return true; - if (choice.trim().length == 0) return true; - if (choice.trim().length > 50) return true; - }); - return shouldReject ? 'invalid poll choices' : true; - }, - // 選択肢がひとつならエラー - choices => choices.length == 1 ? 'poll choices must be ひとつ以上' : true, - // 選択肢が多すぎてもエラー - choices => choices.length > 10 ? 'many poll choices' : true, - ]); - if (pollChoicesErr) return rej('invalid poll choices'); - - _poll.choices = pollChoices.map((choice, i) => ({ - id: i, // IDを付与 - text: choice.trim(), - votes: 0 - })); - - poll = _poll; - } - - // テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー - if (text === null && files === null && repost === null && poll === null) { - return rej('text, media_ids, repost_id or poll is required'); - } - - // 投稿を作成 - const post = await Post.insert({ - created_at: new Date(), - media_ids: files ? 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 - }); - - // Serialize - const postObj = await serialize(post); - - // Reponse - res(postObj); - - //-------------------------------- - // Post processes - - let mentions = []; - - function addMention(mentionee, type) { - // Reject if already added - if (mentions.some(x => x.equals(mentionee))) return; - - // Add mention - mentions.push(mentionee); - - // Publish event - if (!user._id.equals(mentionee)) { - event(mentionee, type, postObj); - } - } - - // Publish event to myself's stream - event(user._id, 'post', postObj); - - // Fetch all followers - const followers = await Following - .find({ - followee_id: user._id, - // 削除されたドキュメントは除く - deleted_at: { $exists: false } - }, { - follower_id: true, - _id: false - }); - - // Publish event to followers stream - followers.forEach(following => - event(following.follower_id, 'post', postObj)); - - // Increment my posts count - User.update({ _id: user._id }, { - $inc: { - posts_count: 1 - } - }); - - // If has in reply to post - if (replyTo) { - // Increment replies count - Post.update({ _id: replyTo._id }, { - $inc: { - replies_count: 1 - } - }); - - // 自分自身へのリプライでない限りは通知を作成 - notify(replyTo.user_id, user._id, 'reply', { - post_id: post._id - }); - - // Add mention - addMention(replyTo.user_id, 'reply'); - } - - // If it is repost - if (repost) { - // Notify - const type = text ? 'quote' : 'repost'; - notify(repost.user_id, user._id, type, { - post_id: post._id - }); - - // If it is quote repost - if (text) { - // Add mention - addMention(repost.user_id, 'quote'); - } else { - // Publish event - if (!user._id.equals(repost.user_id)) { - event(repost.user_id, 'repost', postObj); - } - } - - // 今までで同じ投稿をRepostしているか - const existRepost = await Post.findOne({ - user_id: user._id, - repost_id: repost._id, - _id: { - $ne: post._id - } - }); - - if (!existRepost) { - // Update repostee status - Post.update({ _id: repost._id }, { - $inc: { - repost_count: 1 - } - }); - } - } - - // If has text content - if (text) { - // Analyze - const tokens = parse(text); - - // Extract a hashtags - const hashtags = tokens - .filter(t => t.type == 'hashtag') - .map(t => t.hashtag) - // Drop dupulicates - .filter((v, i, s) => s.indexOf(v) == i); - - // ハッシュタグをデータベースに登録 - //registerHashtags(user, hashtags); - - // Extract an '@' mentions - const atMentions = tokens - .filter(t => t.type == 'mention') - .map(m => m.username) - // Drop dupulicates - .filter((v, i, s) => s.indexOf(v) == i); - - // Resolve all mentions - await Promise.all(atMentions.map(async (mention) => { - // Fetch mentioned user - // SELECT _id - const mentionee = await User - .findOne({ - username_lower: mention.toLowerCase() - }, { _id: true }); - - // When mentioned user not found - if (mentionee == null) return; - - // 既に言及されたユーザーに対する返信や引用repostの場合も無視 - if (replyTo && replyTo.user_id.equals(mentionee._id)) return; - if (repost && repost.user_id.equals(mentionee._id)) return; - - // Add mention - addMention(mentionee._id, 'mention'); - - // Create notification - notify(mentionee._id, user._id, 'mention', { - post_id: post._id - }); - - return; - })); - } - - // Register to search database - if (text && config.elasticsearch.enable) { - const es = require('../../../db/elasticsearch'); - - es.index({ - index: 'misskey', - type: 'post', - id: post._id.toString(), - body: { - text: post.text - } - }); - } - - // Append mentions data - if (mentions.length > 0) { - Post.update({ _id: post._id }, { - $set: { - mentions: mentions - } - }); - } -}); diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts new file mode 100644 index 0000000000..0ecce4e9ad --- /dev/null +++ b/src/api/endpoints/posts/create.ts @@ -0,0 +1,356 @@ +'use strict'; + +/** + * Module dependencies + */ +import validate from '../../validator'; +import parse from '../../../common/text'; +import Post from '../../models/post'; +import { isValidText } from '../../models/post'; +import User from '../../models/user'; +import Following from '../../models/following'; +import DriveFile from '../../models/drive-file'; +import serialize from '../../serializers/post'; +import notify from '../../common/notify'; +import event from '../../event'; +import config from '../../../conf'; + +/** + * Create a post + * + * @param {any} params + * @param {any} user + * @param {any} app + * @return {Promise} + */ +module.exports = (params, user, app) => + new Promise(async (res, rej) => +{ + // Get 'text' parameter + const [text, textErr] = validate(params.text, 'string', false, isValidText); + if (textErr) return rej('invalid text'); + + // Get 'media_ids' parameter + const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'set', false, + x => x.length > 4 ? 'too many media' : true + ); + if (mediaIdsErr) return rej('invalid media_ids'); + + let files = []; + if (mediaIds !== null) { + // Fetch files + // forEach だと途中でエラーなどがあっても return できないので + // 敢えて for を使っています。 + for (let i = 0; i < mediaIds.length; i++) { + const [mediaId, mediaIdErr] = validate(mediaIds[i], 'id', true); + if (mediaIdErr) return rej('invalid media id'); + + // Fetch file + // SELECT _id + const entity = await DriveFile.findOne({ + _id: mediaId, + user_id: user._id + }, { + _id: true + }); + + if (entity === null) { + return rej('file not found'); + } else { + files.push(entity); + } + } + } else { + files = null; + } + + // Get 'repost_id' parameter + const [repostId, repostIdErr] = validate(params.repost_id, 'id'); + if (repostIdErr) return rej('invalid repost_id'); + + let repost = null; + if (repostId !== null) { + // Fetch repost to post + repost = await Post.findOne({ + _id: repostId + }); + + if (repost == null) { + return rej('repostee is not found'); + } else if (repost.repost_id && !repost.text && !repost.media_ids) { + return rej('cannot repost to repost'); + } + + // Fetch recently post + const latestPost = await Post.findOne({ + user_id: user._id + }, { + sort: { + _id: -1 + } + }); + + // 直近と同じRepost対象かつ引用じゃなかったらエラー + if (latestPost && + latestPost.repost_id && + latestPost.repost_id.equals(repost._id) && + text === null && files === null) { + return rej('二重Repostです(NEED TRANSLATE)'); + } + + // 直近がRepost対象かつ引用じゃなかったらエラー + if (latestPost && + latestPost._id.equals(repost._id) && + text === null && files === null) { + return rej('二重Repostです(NEED TRANSLATE)'); + } + } + + // Get 'in_reply_to_post_id' parameter + const [inReplyToPostId, inReplyToPostIdErr] = validate(params.reply_to_id, 'id'); + if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id'); + + let inReplyToPost = null; + if (inReplyToPostId !== null) { + // Fetch reply + inReplyToPost = await Post.findOne({ + _id: inReplyToPostId + }); + + if (inReplyToPost === null) { + return rej('in reply to post is not found'); + } + + // 返信対象が引用でないRepostだったらエラー + if (inReplyToPost.repost_id && !inReplyToPost.text && !inReplyToPost.media_ids) { + return rej('cannot reply to repost'); + } + } + + // Get 'poll' parameter + const [_poll, pollErr] = validate(params.poll, 'object'); + if (pollErr) return rej('invalid poll'); + + let poll = null; + if (_poll !== null) { + const [pollChoices, pollChoicesErr] = validate(params.poll, 'set', false, [ + choices => { + const shouldReject = 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; + }); + return shouldReject ? 'invalid poll choices' : true; + }, + // 選択肢がひとつならエラー + choices => choices.length == 1 ? 'poll choices must be ひとつ以上' : true, + // 選択肢が多すぎてもエラー + choices => choices.length > 10 ? 'many poll choices' : true, + ]); + if (pollChoicesErr) return rej('invalid poll choices'); + + _poll.choices = pollChoices.map((choice, i) => ({ + id: i, // IDを付与 + text: choice.trim(), + votes: 0 + })); + + poll = _poll; + } + + // テキストが無いかつ添付ファイルが無いかつRepostも無いかつ投票も無かったらエラー + if (text === null && files === null && repost === null && poll === null) { + return rej('text, media_ids, repost_id or poll is required'); + } + + // 投稿を作成 + const post = await Post.insert({ + created_at: new Date(), + 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, + text: text, + user_id: user._id, + app_id: app ? app._id : null + }); + + // Serialize + const postObj = await serialize(post); + + // Reponse + res(postObj); + + //-------------------------------- + // Post processes + + let mentions = []; + + function addMention(mentionee, type) { + // Reject if already added + if (mentions.some(x => x.equals(mentionee))) return; + + // Add mention + mentions.push(mentionee); + + // Publish event + if (!user._id.equals(mentionee)) { + event(mentionee, type, postObj); + } + } + + // Publish event to myself's stream + event(user._id, 'post', postObj); + + // Fetch all followers + const followers = await Following + .find({ + followee_id: user._id, + // 削除されたドキュメントは除く + deleted_at: { $exists: false } + }, { + follower_id: true, + _id: false + }); + + // Publish event to followers stream + followers.forEach(following => + event(following.follower_id, 'post', postObj)); + + // Increment my posts count + User.update({ _id: user._id }, { + $inc: { + posts_count: 1 + } + }); + + // If has in reply to post + if (inReplyToPost) { + // Increment replies count + Post.update({ _id: inReplyToPost._id }, { + $inc: { + replies_count: 1 + } + }); + + // 自分自身へのリプライでない限りは通知を作成 + notify(inReplyToPost.user_id, user._id, 'reply', { + post_id: post._id + }); + + // Add mention + addMention(inReplyToPost.user_id, 'reply'); + } + + // If it is repost + if (repost) { + // Notify + const type = text ? 'quote' : 'repost'; + notify(repost.user_id, user._id, type, { + post_id: post._id + }); + + // If it is quote repost + if (text) { + // Add mention + addMention(repost.user_id, 'quote'); + } else { + // Publish event + if (!user._id.equals(repost.user_id)) { + event(repost.user_id, 'repost', postObj); + } + } + + // 今までで同じ投稿をRepostしているか + const existRepost = await Post.findOne({ + user_id: user._id, + repost_id: repost._id, + _id: { + $ne: post._id + } + }); + + if (!existRepost) { + // Update repostee status + Post.update({ _id: repost._id }, { + $inc: { + repost_count: 1 + } + }); + } + } + + // If has text content + if (text) { + // Analyze + const tokens = parse(text); +/* + // Extract a hashtags + const hashtags = tokens + .filter(t => t.type == 'hashtag') + .map(t => t.hashtag) + // Drop dupulicates + .filter((v, i, s) => s.indexOf(v) == i); + + // ハッシュタグをデータベースに登録 + registerHashtags(user, hashtags); +*/ + // Extract an '@' mentions + const atMentions = tokens + .filter(t => t.type == 'mention') + .map(m => m.username) + // Drop dupulicates + .filter((v, i, s) => s.indexOf(v) == i); + + // Resolve all mentions + await Promise.all(atMentions.map(async (mention) => { + // Fetch mentioned user + // SELECT _id + const mentionee = await User + .findOne({ + username_lower: mention.toLowerCase() + }, { _id: true }); + + // When mentioned user not found + if (mentionee == null) return; + + // 既に言及されたユーザーに対する返信や引用repostの場合も無視 + if (inReplyToPost && inReplyToPost.user_id.equals(mentionee._id)) return; + if (repost && repost.user_id.equals(mentionee._id)) return; + + // Add mention + addMention(mentionee._id, 'mention'); + + // Create notification + notify(mentionee._id, user._id, 'mention', { + post_id: post._id + }); + + return; + })); + } + + // Register to search database + if (text && config.elasticsearch.enable) { + const es = require('../../../db/elasticsearch'); + + es.index({ + index: 'misskey', + type: 'post', + id: post._id.toString(), + body: { + text: post.text + } + }); + } + + // Append mentions data + if (mentions.length > 0) { + Post.update({ _id: post._id }, { + $set: { + mentions: mentions + } + }); + } +}); diff --git a/src/api/validator.ts b/src/api/validator.ts index bc50da3a37..3f1678e35d 100644 --- a/src/api/validator.ts +++ b/src/api/validator.ts @@ -10,8 +10,8 @@ function validate(value: any, type: 'string', isRequired?: boolean, validator?: function validate(value: any, type: 'number', isRequired?: boolean, validator?: Validator): [number, string]; function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, string]; function validate(value: any, type: 'array', isRequired?: boolean, validator?: Validator): [any[], string]; -function validate(value: any, type: 'set', isRequired?: boolean, validator?: Validator>): [Set, string]; -function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator): [Object, string]; +function validate(value: any, type: 'set', isRequired?: boolean, validator?: Validator): [any[], string]; +function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator): [any, string]; function validate(value: any, type: Type, isRequired?: boolean, validator?: Validator): [T, string] { if (value === undefined || value === null) { if (isRequired) { -- cgit v1.2.3-freya From 7f4db37ff43e4a6efb4241cf84e328bbd3369b1b Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 17:07:34 +0900 Subject: wip --- src/api/validator2.ts | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/api/validator2.ts (limited to 'src/api') diff --git a/src/api/validator2.ts b/src/api/validator2.ts new file mode 100644 index 0000000000..057875f80b --- /dev/null +++ b/src/api/validator2.ts @@ -0,0 +1,99 @@ +import * as mongo from 'mongodb'; +import hasDuplicates from '../common/has-duplicates'; + +type CustomValidator = (value: T) => boolean | string; + +interface Validator { + get: () => [any, string]; + + required: () => Validator; + + validate: (validator: CustomValidator) => Validator; +} + +class ValidatorCore implements Validator { + value: any; + error: string; + + required() { + if (this.error === null && this.value === null) { + this.error = 'required'; + } + return this; + } + + get(): [any, string] { + return [this.value, this.error]; + } + + validate(validator: any) { + if (this.error || this.value === null) return this; + const result = validator(this.value); + if (result === false) { + this.error = 'invalid-format'; + } else if (typeof result == 'string') { + this.error = result; + } + return this; + } +} + +class NumberValidator extends ValidatorCore { + value: number; + error: string; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (!Number.isFinite(value)) { + this.error = 'must-be-a-number'; + } else { + this.value = value; + } + } + + range(min: number, max: number) { + if (this.error || this.value === null) return this; + if (this.value < min || this.value > max) { + this.error = 'invalid-range'; + } + return this; + } + + required() { + return super.required(); + } + + get(): [number, string] { + return super.get(); + } + + validate(validator: CustomValidator) { + return super.validate(validator); + } +} + +const it = (value) => { + return { + must: { + be: { + a: { + string: 0, + number: () => new NumberValidator(value), + boolean: 0, + set: 0 + }, + an: { + id: 0, + array: 0, + object: 0 + } + } + } + }; +}; + +export default it; + +const [n, e] = it(42).must.be.a.number().required().range(10, 70).validate(x => x != 21).get(); -- cgit v1.2.3-freya From f3d5c07ada08a089969176cdb7b7bfc3f0132fda Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 17:08:09 +0900 Subject: wip --- src/api/endpoints/posts/context.js | 83 -------------------------------------- src/api/endpoints/posts/context.ts | 81 +++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+), 83 deletions(-) delete mode 100644 src/api/endpoints/posts/context.js create mode 100644 src/api/endpoints/posts/context.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/context.js b/src/api/endpoints/posts/context.js deleted file mode 100644 index b843044642..0000000000 --- a/src/api/endpoints/posts/context.js +++ /dev/null @@ -1,83 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import serialize from '../../serializers/post'; - -/** - * Show a context of a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -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'); - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found', 'POST_NOT_FOUND'); - } - - const context = []; - let i = 0; - - async function get(id) { - i++; - const p = await Post.findOne({ _id: id }); - - if (i > offset) { - context.push(p); - } - - if (context.length == limit) { - return; - } - - if (p.reply_to_id) { - await get(p.reply_to_id); - } - } - - if (post.reply_to_id) { - await get(post.reply_to_id); - } - - // Serialize - res(await Promise.all(context.map(async post => - await serialize(post, user)))); -}); diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts new file mode 100644 index 0000000000..673da0fab8 --- /dev/null +++ b/src/api/endpoints/posts/context.ts @@ -0,0 +1,81 @@ +'use strict'; + +/** + * Module dependencies + */ +import validate from '../../validator'; +import Post from '../../models/post'; +import serialize from '../../serializers/post'; + +/** + * Show a context of a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = validate(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id'); + + // Get 'limit' parameter + let [limit, limitErr] = validate(params.limit, 'number'); + if (limitErr) return rej('invalid limit'); + + if (limit !== null) { + // From 1 to 100 + if (!(1 <= limit && limit <= 100)) { + return rej('invalid limit range'); + } + } else { + limit = 10; + } + + // Get 'offset' parameter + let offset = params.offset; + if (offset !== undefined && offset !== null) { + offset = parseInt(offset, 10); + } else { + offset = 0; + } + + // Lookup post + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); + + if (post === null) { + return rej('post not found', 'POST_NOT_FOUND'); + } + + const context = []; + let i = 0; + + async function get(id) { + i++; + const p = await Post.findOne({ _id: id }); + + if (i > offset) { + context.push(p); + } + + if (context.length == limit) { + return; + } + + if (p.reply_to_id) { + await get(p.reply_to_id); + } + } + + if (post.reply_to_id) { + await get(post.reply_to_id); + } + + // Serialize + res(await Promise.all(context.map(async post => + await serialize(post, user)))); +}); -- cgit v1.2.3-freya From b1195e5bfd9839f6ae3d8d375c948c8d0d5f7f20 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 17:14:46 +0900 Subject: Fix bug --- src/api/validator2.ts | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'src/api') diff --git a/src/api/validator2.ts b/src/api/validator2.ts index 057875f80b..a1c5ba164b 100644 --- a/src/api/validator2.ts +++ b/src/api/validator2.ts @@ -15,6 +15,11 @@ class ValidatorCore implements Validator { value: any; error: string; + constructor() { + this.value = null; + this.error = null; + } + required() { if (this.error === null && this.value === null) { this.error = 'required'; -- cgit v1.2.3-freya From 85f43b49cffc645019e23e996f3ce85413d5b25f Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 17:16:55 +0900 Subject: いい感じに MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/validator2.ts | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'src/api') diff --git a/src/api/validator2.ts b/src/api/validator2.ts index a1c5ba164b..923f102e42 100644 --- a/src/api/validator2.ts +++ b/src/api/validator2.ts @@ -79,25 +79,23 @@ class NumberValidator extends ValidatorCore { } } -const it = (value) => { - return { - must: { - be: { - a: { - string: 0, - number: () => new NumberValidator(value), - boolean: 0, - set: 0 - }, - an: { - id: 0, - array: 0, - object: 0 - } +const it = (value: any) => ({ + must: { + be: { + a: { + string: 0, + number: () => new NumberValidator(value), + boolean: 0, + set: 0 + }, + an: { + id: 0, + array: 0, + object: 0 } } - }; -}; + } +}); export default it; -- cgit v1.2.3-freya From a26de4fda5fea8a0aab1fa803bbda2821ce116fd Mon Sep 17 00:00:00 2001 From: syuilo⭐️ Date: Thu, 2 Mar 2017 17:22:18 +0900 Subject: いい感じに MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/validator2.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api') diff --git a/src/api/validator2.ts b/src/api/validator2.ts index 923f102e42..ed58f445d3 100644 --- a/src/api/validator2.ts +++ b/src/api/validator2.ts @@ -31,7 +31,7 @@ class ValidatorCore implements Validator { return [this.value, this.error]; } - validate(validator: any) { + validate(validator: CustomValidator) { if (this.error || this.value === null) return this; const result = validator(this.value); if (result === false) { -- cgit v1.2.3-freya From f6c4f13b5773da3316a483f8bd5d7ae0c7f993db Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 20:51:32 +0900 Subject: wip --- src/api/validator2.ts | 313 +++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 298 insertions(+), 15 deletions(-) (limited to 'src/api') diff --git a/src/api/validator2.ts b/src/api/validator2.ts index ed58f445d3..04dff59aac 100644 --- a/src/api/validator2.ts +++ b/src/api/validator2.ts @@ -1,17 +1,20 @@ import * as mongo from 'mongodb'; import hasDuplicates from '../common/has-duplicates'; -type CustomValidator = (value: T) => boolean | string; +type Validator = (value: T) => boolean | string; +type Modifier = (value: T) => T; -interface Validator { +interface Fuctory { get: () => [any, string]; - required: () => Validator; + required: () => Fuctory; - validate: (validator: CustomValidator) => Validator; + validate: (validator: Validator) => Fuctory; + + modify: (modifier: Modifier) => Fuctory; } -class ValidatorCore implements Validator { +class FuctoryCore implements Fuctory { value: any; error: string; @@ -20,6 +23,9 @@ class ValidatorCore implements Validator { this.error = null; } + /** + * この値が undefined または null の場合エラーにします + */ required() { if (this.error === null && this.value === null) { this.error = 'required'; @@ -27,11 +33,19 @@ class ValidatorCore implements Validator { return this; } + /** + * このインスタンスの値およびエラーを取得します + */ get(): [any, string] { return [this.value, this.error]; } - validate(validator: CustomValidator) { + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false または(エラーを表す)文字列を返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { if (this.error || this.value === null) return this; const result = validator(this.value); if (result === false) { @@ -41,9 +55,59 @@ class ValidatorCore implements Validator { } return this; } + + modify(modifier: Modifier) { + if (this.error || this.value === null) return this; + try { + this.value = modifier(this.value); + } catch (e) { + this.error = e; + } + return this; + } +} + +class BooleanFuctory extends FuctoryCore { + value: boolean; + error: string; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'boolean') { + this.error = 'must-be-a-boolean'; + } else { + this.value = value; + } + } + + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [boolean, string] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false または(エラーを表す)文字列を返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } } -class NumberValidator extends ValidatorCore { +class NumberFuctory extends FuctoryCore { value: number; error: string; @@ -58,6 +122,11 @@ class NumberValidator extends ValidatorCore { } } + /** + * 値が指定された範囲内にない場合エラーにします + * @param min 下限 + * @param max 上限 + */ range(min: number, max: number) { if (this.error || this.value === null) return this; if (this.value < min || this.value > max) { @@ -70,28 +139,242 @@ class NumberValidator extends ValidatorCore { return super.required(); } + /** + * このインスタンスの値およびエラーを取得します + */ get(): [number, string] { return super.get(); } - validate(validator: CustomValidator) { + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false または(エラーを表す)文字列を返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class StringFuctory extends FuctoryCore { + value: string; + error: string; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'string') { + this.error = 'must-be-a-string'; + } else { + this.value = value; + } + } + + /** + * 文字数が指定された範囲内にない場合エラーにします + * @param min 下限 + * @param max 上限 + */ + range(min: number, max: number) { + if (this.error || this.value === null) return this; + if (this.value.length < min || this.value.length > max) { + this.error = 'invalid-range'; + } + return this; + } + + trim() { + if (this.error || this.value === null) return this; + this.value = this.value.trim(); + return this; + } + + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [string, string] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false または(エラーを表す)文字列を返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class ArrayFuctory extends FuctoryCore { + value: any[]; + error: string; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (!Array.isArray(value)) { + this.error = 'must-be-an-array'; + } else { + this.value = value; + } + } + + /** + * 配列の値がユニークでない場合(=重複した項目がある場合)エラーにします + */ + unique() { + if (this.error || this.value === null) return this; + if (hasDuplicates(this.value)) { + this.error = 'must-be-unique'; + } + return this; + } + + /** + * 配列の長さが指定された範囲内にない場合エラーにします + * @param min 下限 + * @param max 上限 + */ + range(min: number, max: number) { + if (this.error || this.value === null) return this; + if (this.value.length < min || this.value.length > max) { + this.error = 'invalid-range'; + } + return this; + } + + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [any[], string] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false または(エラーを表す)文字列を返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class IdFuctory extends FuctoryCore { + value: mongo.ObjectID; + error: string; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { + this.error = 'must-be-an-id'; + } else { + this.value = new mongo.ObjectID(value); + } + } + + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [any[], string] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false または(エラーを表す)文字列を返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class ObjectFuctory extends FuctoryCore { + value: any; + error: string; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'object') { + this.error = 'must-be-an-object'; + } else { + this.value = value; + } + } + + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [any, string] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false または(エラーを表す)文字列を返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { return super.validate(validator); } + + modify(modifier: Modifier) { + return super.modify(modifier); + } } const it = (value: any) => ({ must: { be: { a: { - string: 0, - number: () => new NumberValidator(value), - boolean: 0, - set: 0 + string: () => new StringFuctory(value), + number: () => new NumberFuctory(value), + boolean: () => new BooleanFuctory(value) }, an: { - id: 0, - array: 0, - object: 0 + id: () => new IdFuctory(value), + array: () => new ArrayFuctory(value), + object: () => new ObjectFuctory(value) } } } -- cgit v1.2.3-freya From 789deecfe96a32adfd98ebede749380f81520402 Mon Sep 17 00:00:00 2001 From: syuilo Date: Thu, 2 Mar 2017 21:54:46 +0900 Subject: wip --- src/api/endpoints/posts/create.ts | 18 +- src/api/it.ts | 518 ++++++++++++++++++++++++++++++++++++++ src/api/validator.ts | 87 ------- src/api/validator2.ts | 385 ---------------------------- 4 files changed, 526 insertions(+), 482 deletions(-) create mode 100644 src/api/it.ts delete mode 100644 src/api/validator.ts delete mode 100644 src/api/validator2.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index 0ecce4e9ad..f707c81b1d 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -3,7 +3,7 @@ /** * Module dependencies */ -import validate from '../../validator'; +import it from '../../it'; import parse from '../../../common/text'; import Post from '../../models/post'; import { isValidText } from '../../models/post'; @@ -27,13 +27,11 @@ module.exports = (params, user, app) => new Promise(async (res, rej) => { // Get 'text' parameter - const [text, textErr] = validate(params.text, 'string', false, isValidText); + const [text, textErr] = it(params.text).must.be.a.string().validate(isValidText).get(); if (textErr) return rej('invalid text'); // Get 'media_ids' parameter - const [mediaIds, mediaIdsErr] = validate(params.media_ids, 'set', false, - x => x.length > 4 ? 'too many media' : true - ); + const [mediaIds, mediaIdsErr] = it(params.media_ids).must.be.an.array().unique().range(1, 4).get(); if (mediaIdsErr) return rej('invalid media_ids'); let files = []; @@ -42,7 +40,7 @@ module.exports = (params, user, app) => // forEach だと途中でエラーなどがあっても return できないので // 敢えて for を使っています。 for (let i = 0; i < mediaIds.length; i++) { - const [mediaId, mediaIdErr] = validate(mediaIds[i], 'id', true); + const [mediaId, mediaIdErr] = it(mediaIds[i]).must.be.an.id().required().get(); if (mediaIdErr) return rej('invalid media id'); // Fetch file @@ -65,7 +63,7 @@ module.exports = (params, user, app) => } // Get 'repost_id' parameter - const [repostId, repostIdErr] = validate(params.repost_id, 'id'); + const [repostId, repostIdErr] = it(params.repost_id).must.be.an.id().get(); if (repostIdErr) return rej('invalid repost_id'); let repost = null; @@ -107,7 +105,7 @@ module.exports = (params, user, app) => } // Get 'in_reply_to_post_id' parameter - const [inReplyToPostId, inReplyToPostIdErr] = validate(params.reply_to_id, 'id'); + const [inReplyToPostId, inReplyToPostIdErr] = it(params.reply_to_id, 'id'); if (inReplyToPostIdErr) return rej('invalid in_reply_to_post_id'); let inReplyToPost = null; @@ -128,12 +126,12 @@ module.exports = (params, user, app) => } // Get 'poll' parameter - const [_poll, pollErr] = validate(params.poll, 'object'); + const [_poll, pollErr] = it(params.poll, 'object'); if (pollErr) return rej('invalid poll'); let poll = null; if (_poll !== null) { - const [pollChoices, pollChoicesErr] = validate(params.poll, 'set', false, [ + const [pollChoices, pollChoicesErr] = it(params.poll, 'set', false, [ choices => { const shouldReject = choices.some(choice => { if (typeof choice != 'string') return true; diff --git a/src/api/it.ts b/src/api/it.ts new file mode 100644 index 0000000000..1aff99c9e7 --- /dev/null +++ b/src/api/it.ts @@ -0,0 +1,518 @@ +import * as mongo from 'mongodb'; +import hasDuplicates from '../common/has-duplicates'; + +type Validator = (value: T) => boolean | Error; +type Modifier = (value: T) => T; + +interface Factory { + get: () => [any, Error]; + + required: () => Factory; + + validate: (validator: Validator) => Factory; + + modify: (modifier: Modifier) => Factory; +} + +class FactoryCore implements Factory { + value: any; + error: Error; + + constructor() { + this.value = null; + this.error = null; + } + + /** + * このインスタンスの値が undefined または null の場合エラーにします + */ + required() { + if (this.error === null && this.value === null) { + this.error = new Error('required'); + } + return this; + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [any, Error] { + return [this.value, this.error]; + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false またはエラーを返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + if (this.error || this.value === null) return this; + const result = validator(this.value); + if (result === false) { + this.error = new Error('invalid-format'); + } else if (result instanceof Error) { + this.error = result; + } + return this; + } + + modify(modifier: Modifier) { + if (this.error || this.value === null) return this; + try { + this.value = modifier(this.value); + } catch (e) { + this.error = e; + } + return this; + } +} + +class BooleanFactory extends FactoryCore { + value: boolean; + error: Error; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'boolean') { + this.error = new Error('must-be-a-boolean'); + } else { + this.value = value; + } + } + + /** + * このインスタンスの値が undefined または null の場合エラーにします + */ + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [boolean, Error] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false またはエラーを返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class NumberFactory extends FactoryCore { + value: number; + error: Error; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (!Number.isFinite(value)) { + this.error = new Error('must-be-a-number'); + } else { + this.value = value; + } + } + + /** + * 値が指定された範囲内にない場合エラーにします + * @param min 下限 + * @param max 上限 + */ + range(min: number, max: number) { + if (this.error || this.value === null) return this; + if (this.value < min || this.value > max) { + this.error = new Error('invalid-range'); + } + return this; + } + + /** + * このインスタンスの値が undefined または null の場合エラーにします + */ + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [number, Error] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false またはエラーを返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class StringFactory extends FactoryCore { + value: string; + error: Error; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'string') { + this.error = new Error('must-be-a-string'); + } else { + this.value = value; + } + } + + /** + * 文字数が指定された範囲内にない場合エラーにします + * @param min 下限 + * @param max 上限 + */ + range(min: number, max: number) { + if (this.error || this.value === null) return this; + if (this.value.length < min || this.value.length > max) { + this.error = new Error('invalid-range'); + } + return this; + } + + trim() { + if (this.error || this.value === null) return this; + this.value = this.value.trim(); + return this; + } + + /** + * このインスタンスの値が undefined または null の場合エラーにします + */ + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [string, Error] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false またはエラーを返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class ArrayFactory extends FactoryCore { + value: any[]; + error: Error; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (!Array.isArray(value)) { + this.error = new Error('must-be-an-array'); + } else { + this.value = value; + } + } + + /** + * 配列の値がユニークでない場合(=重複した項目がある場合)エラーにします + */ + unique() { + if (this.error || this.value === null) return this; + if (hasDuplicates(this.value)) { + this.error = new Error('must-be-unique'); + } + return this; + } + + /** + * 配列の長さが指定された範囲内にない場合エラーにします + * @param min 下限 + * @param max 上限 + */ + range(min: number, max: number) { + if (this.error || this.value === null) return this; + if (this.value.length < min || this.value.length > max) { + this.error = new Error('invalid-range'); + } + return this; + } + + /** + * このインスタンスの値が undefined または null の場合エラーにします + */ + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [any[], Error] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false またはエラーを返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class IdFactory extends FactoryCore { + value: mongo.ObjectID; + error: Error; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { + this.error = new Error('must-be-an-id'); + } else { + this.value = new mongo.ObjectID(value); + } + } + + /** + * このインスタンスの値が undefined または null の場合エラーにします + */ + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [any[], Error] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false またはエラーを返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +class ObjectFactory extends FactoryCore { + value: any; + error: Error; + + constructor(value) { + super(); + if (value === undefined || value === null) { + this.value = null; + } else if (typeof value != 'object') { + this.error = new Error('must-be-an-object'); + } else { + this.value = value; + } + } + + /** + * このインスタンスの値が undefined または null の場合エラーにします + */ + required() { + return super.required(); + } + + /** + * このインスタンスの値およびエラーを取得します + */ + get(): [any, Error] { + return super.get(); + } + + /** + * このインスタンスの値に対して妥当性を検証します + * バリデータが false またはエラーを返した場合エラーにします + * @param validator バリデータ + */ + validate(validator: Validator) { + return super.validate(validator); + } + + modify(modifier: Modifier) { + return super.modify(modifier); + } +} + +type MustBe = { + must: { + be: { + a: { + string: () => StringFactory; + number: () => NumberFactory; + boolean: () => BooleanFactory; + }; + an: { + id: () => IdFactory; + array: () => ArrayFactory; + object: () => ObjectFactory; + }; + }; + }; +}; + +const mustBe = (value: any) => ({ + must: { + be: { + a: { + string: () => new StringFactory(value), + number: () => new NumberFactory(value), + boolean: () => new BooleanFactory(value) + }, + an: { + id: () => new IdFactory(value), + array: () => new ArrayFactory(value), + object: () => new ObjectFactory(value) + } + } + } +}); + +type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object'; +type Pipe = (x: T) => T | boolean | Error; + +function validate(value: any, type: 'id', isRequired?: boolean, pipe?: Pipe | Pipe[]): [mongo.ObjectID, Error]; +function validate(value: any, type: 'string', isRequired?: boolean, pipe?: Pipe | Pipe[]): [string, Error]; +function validate(value: any, type: 'number', isRequired?: boolean, pipe?: Pipe | Pipe[]): [number, Error]; +function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error]; +function validate(value: any, type: 'array', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; +function validate(value: any, type: 'set', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; +function validate(value: any, type: 'object', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any, Error]; +function validate(value: any, type: Type, isRequired?: boolean, pipe?: Pipe | Pipe[]): [any, Error] { + if (value === undefined || value === null) { + if (isRequired) { + return [null, new Error('is-required')] + } else { + return [null, null] + } + } + + switch (type) { + case 'id': + if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { + return [null, new Error('incorrect-id')]; + } + break; + + case 'string': + if (typeof value != 'string') { + return [null, new Error('must-be-a-string')]; + } + break; + + case 'number': + if (!Number.isFinite(value)) { + return [null, new Error('must-be-a-number')]; + } + break; + + case 'boolean': + if (typeof value != 'boolean') { + return [null, new Error('must-be-a-boolean')]; + } + break; + + case 'array': + if (!Array.isArray(value)) { + return [null, new Error('must-be-an-array')]; + } + break; + + case 'set': + if (!Array.isArray(value)) { + return [null, new Error('must-be-an-array')]; + } else if (hasDuplicates(value)) { + return [null, new Error('duplicated-contents')]; + } + break; + + case 'object': + if (typeof value != 'object') { + return [null, new Error('must-be-an-object')]; + } + break; + } + + if (type == 'id') value = new mongo.ObjectID(value); + + if (pipe) { + const pipes = Array.isArray(pipe) ? pipe : [pipe]; + for (let i = 0; i < pipes.length; i++) { + const result = pipes[i](value); + if (result === false) { + return [null, new Error('invalid-format')]; + } else if (result instanceof Error) { + return [null, result]; + } else if (result !== true) { + value = result; + } + } + } + + return [value, null]; +} + +function it(value: any): MustBe; +function it(value: any, type: 'id', isRequired?: boolean, pipe?: Pipe | Pipe[]): [mongo.ObjectID, Error]; +function it(value: any, type: 'string', isRequired?: boolean, pipe?: Pipe | Pipe[]): [string, Error]; +function it(value: any, type: 'number', isRequired?: boolean, pipe?: Pipe | Pipe[]): [number, Error]; +function it(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error]; +function it(value: any, type: 'array', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; +function it(value: any, type: 'set', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; +function it(value: any, type: 'object', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any, Error]; +function it(value: any, type?: any, isRequired?: boolean, pipe?: Pipe | Pipe[]): any { + if (typeof type === 'undefined') { + return mustBe(value); + } else { + return validate(value, type, isRequired, pipe); + } +} + +export default it; diff --git a/src/api/validator.ts b/src/api/validator.ts deleted file mode 100644 index 3f1678e35d..0000000000 --- a/src/api/validator.ts +++ /dev/null @@ -1,87 +0,0 @@ -import * as mongo from 'mongodb'; -import hasDuplicates from '../common/has-duplicates'; - -type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object'; - -type Validator = ((x: T) => boolean | string) | ((x: T) => boolean | string)[]; - -function validate(value: any, type: 'id', isRequired?: boolean): [mongo.ObjectID, string]; -function validate(value: any, type: 'string', isRequired?: boolean, validator?: Validator): [string, string]; -function validate(value: any, type: 'number', isRequired?: boolean, validator?: Validator): [number, string]; -function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, string]; -function validate(value: any, type: 'array', isRequired?: boolean, validator?: Validator): [any[], string]; -function validate(value: any, type: 'set', isRequired?: boolean, validator?: Validator): [any[], string]; -function validate(value: any, type: 'object', isRequired?: boolean, validator?: Validator): [any, string]; -function validate(value: any, type: Type, isRequired?: boolean, validator?: Validator): [T, string] { - if (value === undefined || value === null) { - if (isRequired) { - return [null, 'is-required'] - } else { - return [null, null] - } - } - - switch (type) { - case 'id': - if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { - return [null, 'incorrect-id']; - } - break; - - case 'string': - if (typeof value != 'string') { - return [null, 'must-be-a-string']; - } - break; - - case 'number': - if (!Number.isFinite(value)) { - return [null, 'must-be-a-number']; - } - break; - - case 'boolean': - if (typeof value != 'boolean') { - return [null, 'must-be-a-boolean']; - } - break; - - case 'array': - if (!Array.isArray(value)) { - return [null, 'must-be-an-array']; - } - break; - - case 'set': - if (!Array.isArray(value)) { - return [null, 'must-be-an-array']; - } else if (hasDuplicates(value)) { - return [null, 'duplicated-contents']; - } - break; - - case 'object': - if (typeof value != 'object') { - return [null, 'must-be-an-object']; - } - break; - } - - if (type == 'id') value = new mongo.ObjectID(value); - - if (validator) { - const validators = Array.isArray(validator) ? validator : [validator]; - for (let i = 0; i < validators.length; i++) { - const result = validators[i](value); - if (result === false) { - return [null, 'invalid-format']; - } else if (typeof result == 'string') { - return [null, result]; - } - } - } - - return [value, null]; -} - -export default validate; diff --git a/src/api/validator2.ts b/src/api/validator2.ts deleted file mode 100644 index 04dff59aac..0000000000 --- a/src/api/validator2.ts +++ /dev/null @@ -1,385 +0,0 @@ -import * as mongo from 'mongodb'; -import hasDuplicates from '../common/has-duplicates'; - -type Validator = (value: T) => boolean | string; -type Modifier = (value: T) => T; - -interface Fuctory { - get: () => [any, string]; - - required: () => Fuctory; - - validate: (validator: Validator) => Fuctory; - - modify: (modifier: Modifier) => Fuctory; -} - -class FuctoryCore implements Fuctory { - value: any; - error: string; - - constructor() { - this.value = null; - this.error = null; - } - - /** - * この値が undefined または null の場合エラーにします - */ - required() { - if (this.error === null && this.value === null) { - this.error = 'required'; - } - return this; - } - - /** - * このインスタンスの値およびエラーを取得します - */ - get(): [any, string] { - return [this.value, this.error]; - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false または(エラーを表す)文字列を返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - if (this.error || this.value === null) return this; - const result = validator(this.value); - if (result === false) { - this.error = 'invalid-format'; - } else if (typeof result == 'string') { - this.error = result; - } - return this; - } - - modify(modifier: Modifier) { - if (this.error || this.value === null) return this; - try { - this.value = modifier(this.value); - } catch (e) { - this.error = e; - } - return this; - } -} - -class BooleanFuctory extends FuctoryCore { - value: boolean; - error: string; - - constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'boolean') { - this.error = 'must-be-a-boolean'; - } else { - this.value = value; - } - } - - required() { - return super.required(); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - get(): [boolean, string] { - return super.get(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false または(エラーを表す)文字列を返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } - - modify(modifier: Modifier) { - return super.modify(modifier); - } -} - -class NumberFuctory extends FuctoryCore { - value: number; - error: string; - - constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (!Number.isFinite(value)) { - this.error = 'must-be-a-number'; - } else { - this.value = value; - } - } - - /** - * 値が指定された範囲内にない場合エラーにします - * @param min 下限 - * @param max 上限 - */ - range(min: number, max: number) { - if (this.error || this.value === null) return this; - if (this.value < min || this.value > max) { - this.error = 'invalid-range'; - } - return this; - } - - required() { - return super.required(); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - get(): [number, string] { - return super.get(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false または(エラーを表す)文字列を返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } - - modify(modifier: Modifier) { - return super.modify(modifier); - } -} - -class StringFuctory extends FuctoryCore { - value: string; - error: string; - - constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'string') { - this.error = 'must-be-a-string'; - } else { - this.value = value; - } - } - - /** - * 文字数が指定された範囲内にない場合エラーにします - * @param min 下限 - * @param max 上限 - */ - range(min: number, max: number) { - if (this.error || this.value === null) return this; - if (this.value.length < min || this.value.length > max) { - this.error = 'invalid-range'; - } - return this; - } - - trim() { - if (this.error || this.value === null) return this; - this.value = this.value.trim(); - return this; - } - - required() { - return super.required(); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - get(): [string, string] { - return super.get(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false または(エラーを表す)文字列を返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } - - modify(modifier: Modifier) { - return super.modify(modifier); - } -} - -class ArrayFuctory extends FuctoryCore { - value: any[]; - error: string; - - constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (!Array.isArray(value)) { - this.error = 'must-be-an-array'; - } else { - this.value = value; - } - } - - /** - * 配列の値がユニークでない場合(=重複した項目がある場合)エラーにします - */ - unique() { - if (this.error || this.value === null) return this; - if (hasDuplicates(this.value)) { - this.error = 'must-be-unique'; - } - return this; - } - - /** - * 配列の長さが指定された範囲内にない場合エラーにします - * @param min 下限 - * @param max 上限 - */ - range(min: number, max: number) { - if (this.error || this.value === null) return this; - if (this.value.length < min || this.value.length > max) { - this.error = 'invalid-range'; - } - return this; - } - - required() { - return super.required(); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - get(): [any[], string] { - return super.get(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false または(エラーを表す)文字列を返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } - - modify(modifier: Modifier) { - return super.modify(modifier); - } -} - -class IdFuctory extends FuctoryCore { - value: mongo.ObjectID; - error: string; - - constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { - this.error = 'must-be-an-id'; - } else { - this.value = new mongo.ObjectID(value); - } - } - - required() { - return super.required(); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - get(): [any[], string] { - return super.get(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false または(エラーを表す)文字列を返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } - - modify(modifier: Modifier) { - return super.modify(modifier); - } -} - -class ObjectFuctory extends FuctoryCore { - value: any; - error: string; - - constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'object') { - this.error = 'must-be-an-object'; - } else { - this.value = value; - } - } - - required() { - return super.required(); - } - - /** - * このインスタンスの値およびエラーを取得します - */ - get(): [any, string] { - return super.get(); - } - - /** - * このインスタンスの値に対して妥当性を検証します - * バリデータが false または(エラーを表す)文字列を返した場合エラーにします - * @param validator バリデータ - */ - validate(validator: Validator) { - return super.validate(validator); - } - - modify(modifier: Modifier) { - return super.modify(modifier); - } -} - -const it = (value: any) => ({ - must: { - be: { - a: { - string: () => new StringFuctory(value), - number: () => new NumberFuctory(value), - boolean: () => new BooleanFuctory(value) - }, - an: { - id: () => new IdFuctory(value), - array: () => new ArrayFuctory(value), - object: () => new ObjectFuctory(value) - } - } - } -}); - -export default it; - -const [n, e] = it(42).must.be.a.number().required().range(10, 70).validate(x => x != 21).get(); -- cgit v1.2.3-freya From 2fee8fbba36376651161249964e1a3eca0f3d573 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 02:10:27 +0900 Subject: wip --- src/api/endpoints/posts/create.ts | 8 +- src/api/it.ts | 203 +++++++++++--------------------------- 2 files changed, 64 insertions(+), 147 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index f707c81b1d..5ce852280a 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -27,11 +27,11 @@ 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] = it(params.text).must.be.a.string().validate(isValidText).qed(); 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] = it(params.media_ids).must.be.an.array().unique().range(1, 4).qed(); if (mediaIdsErr) return rej('invalid media_ids'); let files = []; @@ -40,7 +40,7 @@ module.exports = (params, user, app) => // forEach だと途中でエラーなどがあっても return できないので // 敢えて for を使っています。 for (let i = 0; i < mediaIds.length; i++) { - const [mediaId, mediaIdErr] = it(mediaIds[i]).must.be.an.id().required().get(); + const [mediaId, mediaIdErr] = it(mediaIds[i]).must.be.an.id().required().qed(); if (mediaIdErr) return rej('invalid media id'); // Fetch file @@ -63,7 +63,7 @@ module.exports = (params, user, app) => } // Get 'repost_id' parameter - const [repostId, repostIdErr] = it(params.repost_id).must.be.an.id().get(); + const [repostId, repostIdErr] = it(params.repost_id).must.be.an.id().qed(); if (repostIdErr) return rej('invalid repost_id'); let repost = null; diff --git a/src/api/it.ts b/src/api/it.ts index 1aff99c9e7..6e8aefdf2d 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -2,16 +2,16 @@ import * as mongo from 'mongodb'; import hasDuplicates from '../common/has-duplicates'; type Validator = (value: T) => boolean | Error; -type Modifier = (value: T) => T; interface Factory { - get: () => [any, Error]; + /** + * qedはQ.E.D.でもあり'QueryENd'の略でもある + */ + qed: () => [any, Error]; required: () => Factory; validate: (validator: Validator) => Factory; - - modify: (modifier: Modifier) => Factory; } class FactoryCore implements Factory { @@ -36,7 +36,7 @@ class FactoryCore implements Factory { /** * このインスタンスの値およびエラーを取得します */ - get(): [any, Error] { + qed(): [any, Error] { return [this.value, this.error]; } @@ -55,16 +55,6 @@ class FactoryCore implements Factory { } return this; } - - modify(modifier: Modifier) { - if (this.error || this.value === null) return this; - try { - this.value = modifier(this.value); - } catch (e) { - this.error = e; - } - return this; - } } class BooleanFactory extends FactoryCore { @@ -92,8 +82,8 @@ class BooleanFactory extends FactoryCore { /** * このインスタンスの値およびエラーを取得します */ - get(): [boolean, Error] { - return super.get(); + qed(): [boolean, Error] { + return super.qed(); } /** @@ -104,10 +94,6 @@ class BooleanFactory extends FactoryCore { validate(validator: Validator) { return super.validate(validator); } - - modify(modifier: Modifier) { - return super.modify(modifier); - } } class NumberFactory extends FactoryCore { @@ -148,8 +134,8 @@ class NumberFactory extends FactoryCore { /** * このインスタンスの値およびエラーを取得します */ - get(): [number, Error] { - return super.get(); + qed(): [number, Error] { + return super.qed(); } /** @@ -160,10 +146,6 @@ class NumberFactory extends FactoryCore { validate(validator: Validator) { return super.validate(validator); } - - modify(modifier: Modifier) { - return super.modify(modifier); - } } class StringFactory extends FactoryCore { @@ -210,8 +192,8 @@ class StringFactory extends FactoryCore { /** * このインスタンスの値およびエラーを取得します */ - get(): [string, Error] { - return super.get(); + qed(): [string, Error] { + return super.qed(); } /** @@ -222,10 +204,6 @@ class StringFactory extends FactoryCore { validate(validator: Validator) { return super.validate(validator); } - - modify(modifier: Modifier) { - return super.modify(modifier); - } } class ArrayFactory extends FactoryCore { @@ -277,8 +255,8 @@ class ArrayFactory extends FactoryCore { /** * このインスタンスの値およびエラーを取得します */ - get(): [any[], Error] { - return super.get(); + qed(): [any[], Error] { + return super.qed(); } /** @@ -289,10 +267,6 @@ class ArrayFactory extends FactoryCore { validate(validator: Validator) { return super.validate(validator); } - - modify(modifier: Modifier) { - return super.modify(modifier); - } } class IdFactory extends FactoryCore { @@ -320,8 +294,8 @@ class IdFactory extends FactoryCore { /** * このインスタンスの値およびエラーを取得します */ - get(): [any[], Error] { - return super.get(); + qed(): [any[], Error] { + return super.qed(); } /** @@ -332,10 +306,6 @@ class IdFactory extends FactoryCore { validate(validator: Validator) { return super.validate(validator); } - - modify(modifier: Modifier) { - return super.modify(modifier); - } } class ObjectFactory extends FactoryCore { @@ -363,8 +333,8 @@ class ObjectFactory extends FactoryCore { /** * このインスタンスの値およびエラーを取得します */ - get(): [any, Error] { - return super.get(); + qed(): [any, Error] { + return super.qed(); } /** @@ -375,13 +345,9 @@ class ObjectFactory extends FactoryCore { validate(validator: Validator) { return super.validate(validator); } - - modify(modifier: Modifier) { - return super.modify(modifier); - } } -type MustBe = { +type It = { must: { be: { a: { @@ -396,9 +362,17 @@ type MustBe = { }; }; }; + expect: { + string: () => StringFactory; + number: () => NumberFactory; + boolean: () => BooleanFactory; + id: () => IdFactory; + array: () => ArrayFactory; + object: () => ObjectFactory; + }; }; -const mustBe = (value: any) => ({ +const it = (value: any) => ({ must: { be: { a: { @@ -412,107 +386,50 @@ const mustBe = (value: any) => ({ object: () => new ObjectFactory(value) } } + }, + expect: { + string: () => new StringFactory(value), + number: () => new NumberFactory(value), + boolean: () => new BooleanFactory(value), + id: () => new IdFactory(value), + array: () => new ArrayFactory(value), + object: () => new ObjectFactory(value) } }); type Type = 'id' | 'string' | 'number' | 'boolean' | 'array' | 'set' | 'object'; -type Pipe = (x: T) => T | boolean | Error; - -function validate(value: any, type: 'id', isRequired?: boolean, pipe?: Pipe | Pipe[]): [mongo.ObjectID, Error]; -function validate(value: any, type: 'string', isRequired?: boolean, pipe?: Pipe | Pipe[]): [string, Error]; -function validate(value: any, type: 'number', isRequired?: boolean, pipe?: Pipe | Pipe[]): [number, Error]; -function validate(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error]; -function validate(value: any, type: 'array', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; -function validate(value: any, type: 'set', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; -function validate(value: any, type: 'object', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any, Error]; -function validate(value: any, type: Type, isRequired?: boolean, pipe?: Pipe | Pipe[]): [any, Error] { - if (value === undefined || value === null) { - if (isRequired) { - return [null, new Error('is-required')] - } else { - return [null, null] - } - } - switch (type) { - case 'id': - if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { - return [null, new Error('incorrect-id')]; - } - break; +function x(value: any): It; +function x(value: any, type: 'id', isRequired?: boolean, validator?: Validator | Validator[]): [mongo.ObjectID, Error]; +function x(value: any, type: 'string', isRequired?: boolean, validator?: Validator | Validator[]): [string, Error]; +function x(value: any, type: 'number', isRequired?: boolean, validator?: Validator | Validator[]): [number, Error]; +function x(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error]; +function x(value: any, type: 'array', isRequired?: boolean, validator?: Validator | Validator[]): [any[], Error]; +function x(value: any, type: 'set', isRequired?: boolean, validator?: Validator | Validator[]): [any[], Error]; +function x(value: any, type: 'object', isRequired?: boolean, validator?: Validator | Validator[]): [any, Error]; +function x(value: any, type?: Type, isRequired?: boolean, validator?: Validator | Validator[]): any { + if (typeof type === 'undefined') return it(value); - case 'string': - if (typeof value != 'string') { - return [null, new Error('must-be-a-string')]; - } - break; + let factory: Factory = null; - case 'number': - if (!Number.isFinite(value)) { - return [null, new Error('must-be-a-number')]; - } - break; - - case 'boolean': - if (typeof value != 'boolean') { - return [null, new Error('must-be-a-boolean')]; - } - break; - - case 'array': - if (!Array.isArray(value)) { - return [null, new Error('must-be-an-array')]; - } - break; - - case 'set': - if (!Array.isArray(value)) { - return [null, new Error('must-be-an-array')]; - } else if (hasDuplicates(value)) { - return [null, new Error('duplicated-contents')]; - } - break; - - case 'object': - if (typeof value != 'object') { - return [null, new Error('must-be-an-object')]; - } - break; + switch (type) { + case 'id': factory = it(value).expect.id(); break; + case 'string': factory = it(value).expect.string(); break; + case 'number': factory = it(value).expect.number(); break; + case 'boolean': factory = it(value).expect.boolean(); break; + case 'array': factory = it(value).expect.array(); break; + case 'set': factory = it(value).expect.array().unique(); break; + case 'object': factory = it(value).expect.object(); break; } - if (type == 'id') value = new mongo.ObjectID(value); + if (isRequired) factory = factory.required(); - if (pipe) { - const pipes = Array.isArray(pipe) ? pipe : [pipe]; - for (let i = 0; i < pipes.length; i++) { - const result = pipes[i](value); - if (result === false) { - return [null, new Error('invalid-format')]; - } else if (result instanceof Error) { - return [null, result]; - } else if (result !== true) { - value = result; - } - } + if (validator) { + (Array.isArray(validator) ? validator : [validator]) + .forEach(v => factory = factory.validate(v)); } - return [value, null]; -} - -function it(value: any): MustBe; -function it(value: any, type: 'id', isRequired?: boolean, pipe?: Pipe | Pipe[]): [mongo.ObjectID, Error]; -function it(value: any, type: 'string', isRequired?: boolean, pipe?: Pipe | Pipe[]): [string, Error]; -function it(value: any, type: 'number', isRequired?: boolean, pipe?: Pipe | Pipe[]): [number, Error]; -function it(value: any, type: 'boolean', isRequired?: boolean): [boolean, Error]; -function it(value: any, type: 'array', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; -function it(value: any, type: 'set', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any[], Error]; -function it(value: any, type: 'object', isRequired?: boolean, pipe?: Pipe | Pipe[]): [any, Error]; -function it(value: any, type?: any, isRequired?: boolean, pipe?: Pipe | Pipe[]): any { - if (typeof type === 'undefined') { - return mustBe(value); - } else { - return validate(value, type, isRequired, pipe); - } + return factory; } -export default it; +export default x; -- cgit v1.2.3-freya From c94de44f2726ba59cfb5d02751abdda5d0b9dca1 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 02:12:32 +0900 Subject: Fix bug --- src/api/endpoints/posts/create.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index 5ce852280a..686c3a67d0 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -139,12 +139,12 @@ module.exports = (params, user, app) => if (choice.trim().length > 50) return true; return false; }); - return shouldReject ? 'invalid poll choices' : true; + return shouldReject ? new Error('invalid poll choices') : true; }, // 選択肢がひとつならエラー - choices => choices.length == 1 ? 'poll choices must be ひとつ以上' : true, + choices => choices.length == 1 ? new Error('poll choices must be ひとつ以上') : true, // 選択肢が多すぎてもエラー - choices => choices.length > 10 ? 'many poll choices' : true, + choices => choices.length > 10 ? new Error('many poll choices') : true, ]); if (pollChoicesErr) return rej('invalid poll choices'); -- cgit v1.2.3-freya From d5e5419dfc14bbb8a1329c2028134b0999883258 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 02:42:17 +0900 Subject: wip --- src/api/endpoints/posts/context.ts | 27 ++++--------- src/api/it.ts | 83 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 20 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts index 673da0fab8..25ac687d34 100644 --- a/src/api/endpoints/posts/context.ts +++ b/src/api/endpoints/posts/context.ts @@ -3,7 +3,7 @@ /** * Module dependencies */ -import validate from '../../validator'; +import it from '../../it'; import Post from '../../models/post'; import serialize from '../../serializers/post'; @@ -18,37 +18,24 @@ module.exports = (params, user) => new Promise(async (res, rej) => { // Get 'post_id' parameter - const [postId, postIdErr] = validate(params.post_id, 'id', true); + const [postId, postIdErr] = it(params.post_id, 'id', true); if (postIdErr) return rej('invalid post_id'); // Get 'limit' parameter - let [limit, limitErr] = validate(params.limit, 'number'); + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); if (limitErr) return rej('invalid limit'); - if (limit !== null) { - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } + const [offset, offsetErr] = it(params.limit).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset'); // Lookup post const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { - return rej('post not found', 'POST_NOT_FOUND'); + return rej('post not found'); } const context = []; diff --git a/src/api/it.ts b/src/api/it.ts index 6e8aefdf2d..ec42547488 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -1,3 +1,8 @@ +/** + * it + * 楽しいバリデーション + */ + import * as mongo from 'mongodb'; import hasDuplicates from '../common/has-duplicates'; @@ -11,6 +16,8 @@ interface Factory { required: () => Factory; + default: (value: any) => Factory; + validate: (validator: Validator) => Factory; } @@ -33,6 +40,16 @@ class FactoryCore implements Factory { return this; } + /** + * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + */ + default(value: any) { + if (this.value === null) { + this.value = value; + } + return this; + } + /** * このインスタンスの値およびエラーを取得します */ @@ -79,6 +96,13 @@ class BooleanFactory extends FactoryCore { return super.required(); } + /** + * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + */ + default(value: boolean) { + return super.default(value); + } + /** * このインスタンスの値およびエラーを取得します */ @@ -124,6 +148,30 @@ class NumberFactory extends FactoryCore { return this; } + /** + * このインスタンスの値が指定された下限より下回っている場合エラーにします + * @param value 下限 + */ + min(value: number) { + if (this.error || this.value === null) return this; + if (this.value < value) { + this.error = new Error('invalid-range'); + } + return this; + } + + /** + * このインスタンスの値が指定された上限より上回っている場合エラーにします + * @param value 上限 + */ + max(value: number) { + if (this.error || this.value === null) return this; + if (this.value > value) { + this.error = new Error('invalid-range'); + } + return this; + } + /** * このインスタンスの値が undefined または null の場合エラーにします */ @@ -131,6 +179,13 @@ class NumberFactory extends FactoryCore { return super.required(); } + /** + * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + */ + default(value: number) { + return super.default(value); + } + /** * このインスタンスの値およびエラーを取得します */ @@ -189,6 +244,13 @@ class StringFactory extends FactoryCore { return super.required(); } + /** + * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + */ + default(value: string) { + return super.default(value); + } + /** * このインスタンスの値およびエラーを取得します */ @@ -252,6 +314,13 @@ class ArrayFactory extends FactoryCore { return super.required(); } + /** + * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + */ + default(value: any[]) { + return super.default(value); + } + /** * このインスタンスの値およびエラーを取得します */ @@ -291,6 +360,13 @@ class IdFactory extends FactoryCore { return super.required(); } + /** + * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + */ + default(value: mongo.ObjectID) { + return super.default(value); + } + /** * このインスタンスの値およびエラーを取得します */ @@ -330,6 +406,13 @@ class ObjectFactory extends FactoryCore { return super.required(); } + /** + * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + */ + default(value: any) { + return super.default(value); + } + /** * このインスタンスの値およびエラーを取得します */ -- cgit v1.2.3-freya From 21c50d42eb09313ea6f7cb932932be65c7d670e8 Mon Sep 17 00:00:00 2001 From: syuilo⭐️ Date: Fri, 3 Mar 2017 03:03:14 +0900 Subject: typo --- src/api/it.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api') diff --git a/src/api/it.ts b/src/api/it.ts index ec42547488..76a3c5e91e 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -10,7 +10,7 @@ type Validator = (value: T) => boolean | Error; interface Factory { /** - * qedはQ.E.D.でもあり'QueryENd'の略でもある + * qedはQ.E.D.でもあり'QueryEnD'の略でもある */ qed: () => [any, Error]; -- cgit v1.2.3-freya From c128d2d1a3433bc3aa334a88491dd84a4bf8afdc Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 05:33:47 +0900 Subject: better name --- src/api/it.ts | 92 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 46 insertions(+), 46 deletions(-) (limited to 'src/api') diff --git a/src/api/it.ts b/src/api/it.ts index 76a3c5e91e..5ca8d7dcae 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -8,20 +8,20 @@ import hasDuplicates from '../common/has-duplicates'; type Validator = (value: T) => boolean | Error; -interface Factory { +interface Query { /** * qedはQ.E.D.でもあり'QueryEnD'の略でもある */ qed: () => [any, Error]; - required: () => Factory; + required: () => Query; - default: (value: any) => Factory; + default: (value: any) => Query; - validate: (validator: Validator) => Factory; + validate: (validator: Validator) => Query; } -class FactoryCore implements Factory { +class QueryCore implements Query { value: any; error: Error; @@ -74,7 +74,7 @@ class FactoryCore implements Factory { } } -class BooleanFactory extends FactoryCore { +class BooleanQuery extends QueryCore { value: boolean; error: Error; @@ -120,7 +120,7 @@ class BooleanFactory extends FactoryCore { } } -class NumberFactory extends FactoryCore { +class NumberQuery extends QueryCore { value: number; error: Error; @@ -203,7 +203,7 @@ class NumberFactory extends FactoryCore { } } -class StringFactory extends FactoryCore { +class StringQuery extends QueryCore { value: string; error: Error; @@ -268,7 +268,7 @@ class StringFactory extends FactoryCore { } } -class ArrayFactory extends FactoryCore { +class ArrayQuery extends QueryCore { value: any[]; error: Error; @@ -338,7 +338,7 @@ class ArrayFactory extends FactoryCore { } } -class IdFactory extends FactoryCore { +class IdQuery extends QueryCore { value: mongo.ObjectID; error: Error; @@ -384,7 +384,7 @@ class IdFactory extends FactoryCore { } } -class ObjectFactory extends FactoryCore { +class ObjectQuery extends QueryCore { value: any; error: Error; @@ -434,24 +434,24 @@ type It = { must: { be: { a: { - string: () => StringFactory; - number: () => NumberFactory; - boolean: () => BooleanFactory; + string: () => StringQuery; + number: () => NumberQuery; + boolean: () => BooleanQuery; }; an: { - id: () => IdFactory; - array: () => ArrayFactory; - object: () => ObjectFactory; + id: () => IdQuery; + array: () => ArrayQuery; + object: () => ObjectQuery; }; }; }; expect: { - string: () => StringFactory; - number: () => NumberFactory; - boolean: () => BooleanFactory; - id: () => IdFactory; - array: () => ArrayFactory; - object: () => ObjectFactory; + string: () => StringQuery; + number: () => NumberQuery; + boolean: () => BooleanQuery; + id: () => IdQuery; + array: () => ArrayQuery; + object: () => ObjectQuery; }; }; @@ -459,24 +459,24 @@ const it = (value: any) => ({ must: { be: { a: { - string: () => new StringFactory(value), - number: () => new NumberFactory(value), - boolean: () => new BooleanFactory(value) + string: () => new StringQuery(value), + number: () => new NumberQuery(value), + boolean: () => new BooleanQuery(value) }, an: { - id: () => new IdFactory(value), - array: () => new ArrayFactory(value), - object: () => new ObjectFactory(value) + id: () => new IdQuery(value), + array: () => new ArrayQuery(value), + object: () => new ObjectQuery(value) } } }, expect: { - string: () => new StringFactory(value), - number: () => new NumberFactory(value), - boolean: () => new BooleanFactory(value), - id: () => new IdFactory(value), - array: () => new ArrayFactory(value), - object: () => new ObjectFactory(value) + string: () => new StringQuery(value), + number: () => new NumberQuery(value), + boolean: () => new BooleanQuery(value), + id: () => new IdQuery(value), + array: () => new ArrayQuery(value), + object: () => new ObjectQuery(value) } }); @@ -493,26 +493,26 @@ function x(value: any, type: 'object', isRequired?: boolean, validator?: Validat function x(value: any, type?: Type, isRequired?: boolean, validator?: Validator | Validator[]): any { if (typeof type === 'undefined') return it(value); - let factory: Factory = null; + let q: Query = null; switch (type) { - case 'id': factory = it(value).expect.id(); break; - case 'string': factory = it(value).expect.string(); break; - case 'number': factory = it(value).expect.number(); break; - case 'boolean': factory = it(value).expect.boolean(); break; - case 'array': factory = it(value).expect.array(); break; - case 'set': factory = it(value).expect.array().unique(); break; - case 'object': factory = it(value).expect.object(); break; + case 'id': q = it(value).expect.id(); break; + case 'string': q = it(value).expect.string(); break; + case 'number': q = it(value).expect.number(); break; + case 'boolean': q = it(value).expect.boolean(); break; + case 'array': q = it(value).expect.array(); break; + case 'set': q = it(value).expect.array().unique(); break; + case 'object': q = it(value).expect.object(); break; } - if (isRequired) factory = factory.required(); + if (isRequired) q = q.required(); if (validator) { (Array.isArray(validator) ? validator : [validator]) - .forEach(v => factory = factory.validate(v)); + .forEach(v => q = q.validate(v)); } - return factory; + return q; } export default x; -- cgit v1.2.3-freya From 8985d55b1bf34d81b553f77005d4bb5a856b6aef Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 05:33:59 +0900 Subject: fix --- src/api/endpoints/posts/context.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts index 25ac687d34..53fc4737e5 100644 --- a/src/api/endpoints/posts/context.ts +++ b/src/api/endpoints/posts/context.ts @@ -26,7 +26,7 @@ module.exports = (params, user) => if (limitErr) return rej('invalid limit'); // Get 'offset' parameter - const [offset, offsetErr] = it(params.limit).expect.number().min(0).default(0).qed(); + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); if (offsetErr) return rej('invalid offset'); // Lookup post -- cgit v1.2.3-freya From d6af0bb78ba2cd179161139b7ae20655e69a98c1 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 05:52:12 +0900 Subject: wip --- src/api/endpoints/posts/likes.js | 76 ---------------------------------------- src/api/endpoints/posts/likes.ts | 62 ++++++++++++++++++++++++++++++++ src/api/it.ts | 24 +++++++++++++ 3 files changed, 86 insertions(+), 76 deletions(-) delete mode 100644 src/api/endpoints/posts/likes.js create mode 100644 src/api/endpoints/posts/likes.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/likes.js b/src/api/endpoints/posts/likes.js deleted file mode 100644 index 67898218cf..0000000000 --- a/src/api/endpoints/posts/likes.js +++ /dev/null @@ -1,76 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import Like from '../../models/like'; -import serialize from '../../serializers/user'; - -/** - * Show a likes of a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -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'); - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // Get 'sort' parameter - let sort = params.sort || 'desc'; - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - // Issue query - const likes = await Like - .find({ - post_id: post._id, - deleted_at: { $exists: false } - }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(likes.map(async like => - await serialize(like.user_id, user)))); -}); diff --git a/src/api/endpoints/posts/likes.ts b/src/api/endpoints/posts/likes.ts new file mode 100644 index 0000000000..5c18679d7c --- /dev/null +++ b/src/api/endpoints/posts/likes.ts @@ -0,0 +1,62 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import Like from '../../models/like'; +import serialize from '../../serializers/user'; + +/** + * Show a likes of a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset'); + + // Get 'sort' parameter + const [sort] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + let sort = params.sort || 'desc'; + + // Lookup post + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); + + if (post === null) { + return rej('post not found'); + } + + // Issue query + const likes = await Like + .find({ + post_id: post._id, + deleted_at: { $exists: false } + }, { + limit: limit, + skip: offset, + sort: { + _id: sort == 'asc' ? 1 : -1 + } + }); + + // Serialize + res(await Promise.all(likes.map(async like => + await serialize(like.user_id, user)))); +}); diff --git a/src/api/it.ts b/src/api/it.ts index 5ca8d7dcae..2e6a7a770d 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -266,6 +266,30 @@ class StringQuery extends QueryCore { validate(validator: Validator) { return super.validate(validator); } + + /** + * このインスタンスの文字列が、与えられたパターン内の文字列のどれかと一致するか検証します + * どれとも一致しない場合エラーにします + * @param pattern 文字列の配列またはスペースで区切られた文字列 + */ + or(pattern: string | string[]) { + if (this.error || this.value === null) return this; + if (typeof pattern == 'string') pattern = pattern.split(' '); + const match = pattern.some(x => x === this.value); + if (!match) this.error = new Error('not-match-pattern'); + return this; + } + + /** + * このインスタンスの文字列が、与えられた正規表現と一致するか検証します + * 一致しない場合エラーにします + * @param pattern 正規表現 + */ + match(pattern: RegExp) { + if (this.error || this.value === null) return this; + if (!pattern.test(this.value)) this.error = new Error('not-match-pattern'); + return this; + } } class ArrayQuery extends QueryCore { -- cgit v1.2.3-freya From ffac713181978dafd1c92462338de405a3ef5360 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 06:11:11 +0900 Subject: Add usage doc --- src/api/it.ts | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'src/api') diff --git a/src/api/it.ts b/src/api/it.ts index 2e6a7a770d..3d803e71e4 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -3,6 +3,29 @@ * 楽しいバリデーション */ +/** + * Usage Examples + * + * const [val, err] = it(x).must.be.a.string().or('asc desc').default('desc').qed(); + * → xは文字列でなければならず、'asc'または'desc'でなければならない。省略された場合のデフォルトは'desc'とする。 + * + * const [val, err] = it(x).must.be.a.number().required().range(0, 100).qed(); + * → xは数値でなければならず、かつ0~100の範囲内でなければならない。この値は省略することはできない。 + * + * const [val, err] = it(x).must.be.an.array().unique().required().validate(x => x[0] != 'strawberry pasta').qed(); + * → xは配列でなければならず、かつ中身が重複していてはならない。この値を省略することはできない。そして配列の最初の要素が'strawberry pasta'という文字列であってはならない。 + * + * ~糖衣構文~ + * const [val, err] = it(x).must.be.a.string().required().qed(); + * は + * const [val, err] = it(x, 'string', true); + * と書けます + * + * ~BDD風記法~ + * must.be.a(n) の代わりに expect とも書けます: + * const [val, err] = it(x).expect.string().required().qed(); + */ + import * as mongo from 'mongodb'; import hasDuplicates from '../common/has-duplicates'; -- cgit v1.2.3-freya From 9d3dff88056dac8eb00e62fedf438b197f99c256 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 06:29:49 +0900 Subject: wip --- src/api/endpoints/posts/likes.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/posts/likes.ts b/src/api/endpoints/posts/likes.ts index 5c18679d7c..e44013c246 100644 --- a/src/api/endpoints/posts/likes.ts +++ b/src/api/endpoints/posts/likes.ts @@ -31,12 +31,12 @@ module.exports = (params, user) => if (offsetErr) return rej('invalid offset'); // Get 'sort' parameter - const [sort] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); - let sort = params.sort || 'desc'; + const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + if (sortError) return rej('invalid sort'); // Lookup post const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { -- cgit v1.2.3-freya From d3c4cd1c433d2077628d6aba0fb3d053c94ddaa1 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 06:37:09 +0900 Subject: wip --- src/api/endpoints/posts/mentions.js | 84 ------------------------------------- src/api/endpoints/posts/mentions.ts | 82 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 84 deletions(-) delete mode 100644 src/api/endpoints/posts/mentions.js create mode 100644 src/api/endpoints/posts/mentions.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/mentions.js b/src/api/endpoints/posts/mentions.js deleted file mode 100644 index 5a3d72aab8..0000000000 --- a/src/api/endpoints/posts/mentions.js +++ /dev/null @@ -1,84 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import getFriends from '../../common/get-friends'; -import serialize from '../../serializers/post'; - -/** - * Get mentions of myself - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'following' parameter - const following = params.following; - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // Construct query - const query = { - mentions: user._id - }; - - const sort = { - _id: -1 - }; - - if (following) { - const followingIds = await getFriends(user._id); - - query.user_id = { - $in: followingIds - }; - } - - if (since) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const mentions = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(mentions.map(async mention => - await serialize(mention, user) - ))); -}); diff --git a/src/api/endpoints/posts/mentions.ts b/src/api/endpoints/posts/mentions.ts new file mode 100644 index 0000000000..59802c558a --- /dev/null +++ b/src/api/endpoints/posts/mentions.ts @@ -0,0 +1,82 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import getFriends from '../../common/get-friends'; +import serialize from '../../serializers/post'; + +/** + * Get mentions of myself + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'following' parameter + const [following, followingError] = + it(params.following).expect.boolean().default(false).qed(); + if (followingError) return rej('invalid following param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // Construct query + const query = { + mentions: user._id + } as any; + + const sort = { + _id: -1 + }; + + if (following) { + const followingIds = await getFriends(user._id); + + query.user_id = { + $in: followingIds + }; + } + + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const mentions = await Post + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(mentions.map(async mention => + await serialize(mention, user) + ))); +}); -- cgit v1.2.3-freya From 6e181ee0f1ca2ecd0fdf3a78654607ef112f2a6a Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 06:48:26 +0900 Subject: wip --- src/api/endpoints/posts/context.ts | 6 +- src/api/endpoints/posts/likes.ts | 8 +-- src/api/endpoints/posts/replies.js | 72 ------------------- src/api/endpoints/posts/replies.ts | 58 +++++++++++++++ src/api/endpoints/posts/reposts.js | 84 ---------------------- src/api/endpoints/posts/reposts.ts | 78 ++++++++++++++++++++ src/api/endpoints/posts/search.js | 137 ------------------------------------ src/api/endpoints/posts/search.ts | 123 ++++++++++++++++++++++++++++++++ src/api/endpoints/posts/show.js | 44 ------------ src/api/endpoints/posts/show.ts | 37 ++++++++++ src/api/endpoints/posts/timeline.js | 77 -------------------- src/api/endpoints/posts/timeline.ts | 73 +++++++++++++++++++ 12 files changed, 376 insertions(+), 421 deletions(-) delete mode 100644 src/api/endpoints/posts/replies.js create mode 100644 src/api/endpoints/posts/replies.ts delete mode 100644 src/api/endpoints/posts/reposts.js create mode 100644 src/api/endpoints/posts/reposts.ts delete mode 100644 src/api/endpoints/posts/search.js create mode 100644 src/api/endpoints/posts/search.ts delete mode 100644 src/api/endpoints/posts/show.js create mode 100644 src/api/endpoints/posts/show.ts delete mode 100644 src/api/endpoints/posts/timeline.js create mode 100644 src/api/endpoints/posts/timeline.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/context.ts b/src/api/endpoints/posts/context.ts index 53fc4737e5..5b0a56f356 100644 --- a/src/api/endpoints/posts/context.ts +++ b/src/api/endpoints/posts/context.ts @@ -19,15 +19,15 @@ module.exports = (params, user) => { // Get 'post_id' parameter const [postId, postIdErr] = it(params.post_id, 'id', true); - if (postIdErr) return rej('invalid post_id'); + if (postIdErr) return rej('invalid post_id param'); // Get 'limit' parameter const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); - if (limitErr) return rej('invalid limit'); + if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); - if (offsetErr) return rej('invalid offset'); + if (offsetErr) return rej('invalid offset param'); // Lookup post const post = await Post.findOne({ diff --git a/src/api/endpoints/posts/likes.ts b/src/api/endpoints/posts/likes.ts index e44013c246..f299de7492 100644 --- a/src/api/endpoints/posts/likes.ts +++ b/src/api/endpoints/posts/likes.ts @@ -20,19 +20,19 @@ module.exports = (params, user) => { // Get 'post_id' parameter const [postId, postIdErr] = it(params.post_id, 'id', true); - if (postIdErr) return rej('invalid post_id'); + if (postIdErr) return rej('invalid post_id param'); // Get 'limit' parameter const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); - if (limitErr) return rej('invalid limit'); + if (limitErr) return rej('invalid limit param'); // Get 'offset' parameter const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); - if (offsetErr) return rej('invalid offset'); + if (offsetErr) return rej('invalid offset param'); // Get 'sort' parameter const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); - if (sortError) return rej('invalid sort'); + if (sortError) return rej('invalid sort param'); // Lookup post const post = await Post.findOne({ diff --git a/src/api/endpoints/posts/replies.js b/src/api/endpoints/posts/replies.js deleted file mode 100644 index cbbb5dc312..0000000000 --- a/src/api/endpoints/posts/replies.js +++ /dev/null @@ -1,72 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import serialize from '../../serializers/post'; - -/** - * Show a replies of a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -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'); - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // Get 'sort' parameter - let sort = params.sort || 'desc'; - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found', 'POST_NOT_FOUND'); - } - - // Issue query - const replies = await Post - .find({ reply_to_id: post._id }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(replies.map(async post => - await serialize(post, user)))); -}); diff --git a/src/api/endpoints/posts/replies.ts b/src/api/endpoints/posts/replies.ts new file mode 100644 index 0000000000..3f448d1632 --- /dev/null +++ b/src/api/endpoints/posts/replies.ts @@ -0,0 +1,58 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import serialize from '../../serializers/post'; + +/** + * Show a replies of a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // Get 'sort' parameter + const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + if (sortError) return rej('invalid sort param'); + + // Lookup post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + // Issue query + const replies = await Post + .find({ reply_to_id: post._id }, { + limit: limit, + skip: offset, + sort: { + _id: sort == 'asc' ? 1 : -1 + } + }); + + // Serialize + res(await Promise.all(replies.map(async post => + await serialize(post, user)))); +}); diff --git a/src/api/endpoints/posts/reposts.js b/src/api/endpoints/posts/reposts.js deleted file mode 100644 index 0ffe44cb16..0000000000 --- a/src/api/endpoints/posts/reposts.js +++ /dev/null @@ -1,84 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import serialize from '../../serializers/post'; - -/** - * Show a reposts of a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -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'); - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found', 'POST_NOT_FOUND'); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = { - repost_id: post._id - }; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const reposts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(reposts.map(async post => - await serialize(post, user)))); -}); diff --git a/src/api/endpoints/posts/reposts.ts b/src/api/endpoints/posts/reposts.ts new file mode 100644 index 0000000000..d8410b322b --- /dev/null +++ b/src/api/endpoints/posts/reposts.ts @@ -0,0 +1,78 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import serialize from '../../serializers/post'; + +/** + * Show a reposts of a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // Lookup post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + // Construct query + const sort = { + _id: -1 + }; + const query = { + repost_id: post._id + } as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const reposts = await Post + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(reposts.map(async post => + await serialize(post, user)))); +}); diff --git a/src/api/endpoints/posts/search.js b/src/api/endpoints/posts/search.js deleted file mode 100644 index bc06340fda..0000000000 --- a/src/api/endpoints/posts/search.js +++ /dev/null @@ -1,137 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -const escapeRegexp = require('escape-regexp'); -import Post from '../../models/post'; -import serialize from '../../serializers/post'; -import config from '../../../conf'; - -/** - * Search a post - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'query' parameter - let query = params.query; - if (query === undefined || query === null || query.trim() === '') { - return rej('query is required'); - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // Get 'max' parameter - let max = params.max; - if (max !== undefined && max !== null) { - max = parseInt(max, 10); - - // From 1 to 30 - if (!(1 <= max && max <= 30)) { - return rej('invalid max range'); - } - } else { - max = 10; - } - - // If Elasticsearch is available, search by it - // If not, search by MongoDB - (config.elasticsearch.enable ? byElasticsearch : byNative) - (res, rej, me, query, offset, max); -}); - -// Search by MongoDB -async function byNative(res, rej, me, query, offset, max) { - const escapedQuery = escapeRegexp(query); - - // Search posts - const posts = await Post - .find({ - text: new RegExp(escapedQuery) - }, { - sort: { - _id: -1 - }, - limit: max, - skip: offset - }); - - // Serialize - res(await Promise.all(posts.map(async post => - await serialize(post, me)))); -} - -// Search by Elasticsearch -async function byElasticsearch(res, rej, me, query, offset, max) { - const es = require('../../db/elasticsearch'); - - es.search({ - index: 'misskey', - type: 'post', - body: { - size: max, - from: offset, - query: { - simple_query_string: { - fields: ['text'], - query: query, - default_operator: 'and' - } - }, - sort: [ - { _doc: 'desc' } - ], - highlight: { - pre_tags: [''], - post_tags: [''], - encoder: 'html', - fields: { - text: {} - } - } - } - }, async (error, response) => { - if (error) { - console.error(error); - return res(500); - } - - if (response.hits.total === 0) { - return res([]); - } - - const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id)); - - // Fetch found posts - const posts = await Post - .find({ - _id: { - $in: hits - } - }, { - sort: { - _id: -1 - } - }); - - posts.map(post => { - post._highlight = response.hits.hits.filter(hit => post._id.equals(hit._id))[0].highlight.text[0]; - }); - - // Serialize - res(await Promise.all(posts.map(async post => - await serialize(post, me)))); - }); -} diff --git a/src/api/endpoints/posts/search.ts b/src/api/endpoints/posts/search.ts new file mode 100644 index 0000000000..1d02f6775d --- /dev/null +++ b/src/api/endpoints/posts/search.ts @@ -0,0 +1,123 @@ +'use strict'; + +/** + * Module dependencies + */ +import * as mongo from 'mongodb'; +import it from '../../it'; +const escapeRegexp = require('escape-regexp'); +import Post from '../../models/post'; +import serialize from '../../serializers/post'; +import config from '../../../conf'; + +/** + * Search a post + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +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 != '').qed(); + if (queryError) return rej('invalid query param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // Get 'max' parameter + const [max, maxErr] = it(params.max).expect.number().range(1, 30).default(10).qed(); + if (maxErr) return rej('invalid max param'); + + // If Elasticsearch is available, search by it + // If not, search by MongoDB + (config.elasticsearch.enable ? byElasticsearch : byNative) + (res, rej, me, query, offset, max); +}); + +// Search by MongoDB +async function byNative(res, rej, me, query, offset, max) { + const escapedQuery = escapeRegexp(query); + + // Search posts + const posts = await Post + .find({ + text: new RegExp(escapedQuery) + }, { + sort: { + _id: -1 + }, + limit: max, + skip: offset + }); + + // Serialize + res(await Promise.all(posts.map(async post => + await serialize(post, me)))); +} + +// Search by Elasticsearch +async function byElasticsearch(res, rej, me, query, offset, max) { + const es = require('../../db/elasticsearch'); + + es.search({ + index: 'misskey', + type: 'post', + body: { + size: max, + from: offset, + query: { + simple_query_string: { + fields: ['text'], + query: query, + default_operator: 'and' + } + }, + sort: [ + { _doc: 'desc' } + ], + highlight: { + pre_tags: [''], + post_tags: [''], + encoder: 'html', + fields: { + text: {} + } + } + } + }, async (error, response) => { + if (error) { + console.error(error); + return res(500); + } + + if (response.hits.total === 0) { + return res([]); + } + + const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id)); + + // Fetch found posts + const posts = await Post + .find({ + _id: { + $in: hits + } + }, { + sort: { + _id: -1 + } + }); + + posts.map(post => { + post._highlight = response.hits.hits.filter(hit => post._id.equals(hit._id))[0].highlight.text[0]; + }); + + // Serialize + res(await Promise.all(posts.map(async post => + await serialize(post, me)))); + }); +} diff --git a/src/api/endpoints/posts/show.js b/src/api/endpoints/posts/show.js deleted file mode 100644 index 4938199cdb..0000000000 --- a/src/api/endpoints/posts/show.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import serialize from '../../serializers/post'; - -/** - * Show a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -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 post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - // Serialize - res(await serialize(post, user, { - detail: true - })); -}); diff --git a/src/api/endpoints/posts/show.ts b/src/api/endpoints/posts/show.ts new file mode 100644 index 0000000000..712ef1e160 --- /dev/null +++ b/src/api/endpoints/posts/show.ts @@ -0,0 +1,37 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import serialize from '../../serializers/post'; + +/** + * Show a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + // Serialize + res(await serialize(post, user, { + detail: true + })); +}); diff --git a/src/api/endpoints/posts/timeline.js b/src/api/endpoints/posts/timeline.js deleted file mode 100644 index 48f7c26940..0000000000 --- a/src/api/endpoints/posts/timeline.js +++ /dev/null @@ -1,77 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import getFriends from '../../common/get-friends'; -import serialize from '../../serializers/post'; - -/** - * Get timeline of myself - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise} - */ -module.exports = (params, user, app) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // ID list of the user itself and other users who the user follows - const followingIds = await getFriends(user._id); - - // Construct query - const sort = { - _id: -1 - }; - const query = { - user_id: { - $in: followingIds - } - }; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const timeline = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(timeline.map(async post => - await serialize(post, user) - ))); -}); diff --git a/src/api/endpoints/posts/timeline.ts b/src/api/endpoints/posts/timeline.ts new file mode 100644 index 0000000000..5744084932 --- /dev/null +++ b/src/api/endpoints/posts/timeline.ts @@ -0,0 +1,73 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import getFriends from '../../common/get-friends'; +import serialize from '../../serializers/post'; + +/** + * Get timeline of myself + * + * @param {any} params + * @param {any} user + * @param {any} app + * @return {Promise} + */ +module.exports = (params, user, app) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // ID list of the user itself and other users who the user follows + const followingIds = await getFriends(user._id); + + // Construct query + const sort = { + _id: -1 + }; + const query = { + user_id: { + $in: followingIds + } + } as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const timeline = await Post + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(timeline.map(async post => + await serialize(post, user) + ))); +}); -- cgit v1.2.3-freya From 2e4e599c01975f959a56d0eb4b81e3c8475953f3 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 07:27:47 +0900 Subject: Refactor --- src/api/it.ts | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) (limited to 'src/api') diff --git a/src/api/it.ts b/src/api/it.ts index 3d803e71e4..f4b28531d6 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -5,22 +5,22 @@ /** * Usage Examples - * + * * const [val, err] = it(x).must.be.a.string().or('asc desc').default('desc').qed(); * → xは文字列でなければならず、'asc'または'desc'でなければならない。省略された場合のデフォルトは'desc'とする。 - * + * * const [val, err] = it(x).must.be.a.number().required().range(0, 100).qed(); * → xは数値でなければならず、かつ0~100の範囲内でなければならない。この値は省略することはできない。 - * + * * const [val, err] = it(x).must.be.an.array().unique().required().validate(x => x[0] != 'strawberry pasta').qed(); * → xは配列でなければならず、かつ中身が重複していてはならない。この値を省略することはできない。そして配列の最初の要素が'strawberry pasta'という文字列であってはならない。 - * + * * ~糖衣構文~ * const [val, err] = it(x).must.be.a.string().required().qed(); * は * const [val, err] = it(x, 'string', true); * と書けます - * + * * ~BDD風記法~ * must.be.a(n) の代わりに expect とも書けます: * const [val, err] = it(x).expect.string().required().qed(); @@ -53,6 +53,13 @@ class QueryCore implements Query { this.error = null; } + /** + * このインスタンスの値が null、またはエラーが存在しているなどして、処理をスキップするべきか否か + */ + get shouldSkip() { + return this.error !== null || this.value === null; + } + /** * このインスタンスの値が undefined または null の場合エラーにします */ @@ -86,7 +93,7 @@ class QueryCore implements Query { * @param validator バリデータ */ validate(validator: Validator) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; const result = validator(this.value); if (result === false) { this.error = new Error('invalid-format'); @@ -164,7 +171,7 @@ class NumberQuery extends QueryCore { * @param max 上限 */ range(min: number, max: number) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (this.value < min || this.value > max) { this.error = new Error('invalid-range'); } @@ -176,7 +183,7 @@ class NumberQuery extends QueryCore { * @param value 下限 */ min(value: number) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (this.value < value) { this.error = new Error('invalid-range'); } @@ -188,7 +195,7 @@ class NumberQuery extends QueryCore { * @param value 上限 */ max(value: number) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (this.value > value) { this.error = new Error('invalid-range'); } @@ -247,7 +254,7 @@ class StringQuery extends QueryCore { * @param max 上限 */ range(min: number, max: number) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (this.value.length < min || this.value.length > max) { this.error = new Error('invalid-range'); } @@ -255,7 +262,7 @@ class StringQuery extends QueryCore { } trim() { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; this.value = this.value.trim(); return this; } @@ -296,7 +303,7 @@ class StringQuery extends QueryCore { * @param pattern 文字列の配列またはスペースで区切られた文字列 */ or(pattern: string | string[]) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (typeof pattern == 'string') pattern = pattern.split(' '); const match = pattern.some(x => x === this.value); if (!match) this.error = new Error('not-match-pattern'); @@ -309,7 +316,7 @@ class StringQuery extends QueryCore { * @param pattern 正規表現 */ match(pattern: RegExp) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (!pattern.test(this.value)) this.error = new Error('not-match-pattern'); return this; } @@ -334,7 +341,7 @@ class ArrayQuery extends QueryCore { * 配列の値がユニークでない場合(=重複した項目がある場合)エラーにします */ unique() { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (hasDuplicates(this.value)) { this.error = new Error('must-be-unique'); } @@ -347,7 +354,7 @@ class ArrayQuery extends QueryCore { * @param max 上限 */ range(min: number, max: number) { - if (this.error || this.value === null) return this; + if (this.shouldSkip) return this; if (this.value.length < min || this.value.length > max) { this.error = new Error('invalid-range'); } -- cgit v1.2.3-freya From 0926d5b6da68be6c9375addbd3cec8545185dea7 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 07:47:14 +0900 Subject: wip --- src/api/endpoints/users/followers.js | 105 ---------------------- src/api/endpoints/users/followers.ts | 96 ++++++++++++++++++++ src/api/endpoints/users/following.js | 105 ---------------------- src/api/endpoints/users/following.ts | 96 ++++++++++++++++++++ src/api/endpoints/users/posts.js | 125 -------------------------- src/api/endpoints/users/posts.ts | 113 +++++++++++++++++++++++ src/api/endpoints/users/recommendation.js | 60 ------------- src/api/endpoints/users/recommendation.ts | 48 ++++++++++ src/api/endpoints/users/search.js | 117 ------------------------ src/api/endpoints/users/search.ts | 103 +++++++++++++++++++++ src/api/endpoints/users/search_by_username.js | 64 ------------- src/api/endpoints/users/search_by_username.ts | 44 +++++++++ src/api/endpoints/users/show.js | 60 ------------- src/api/endpoints/users/show.ts | 51 +++++++++++ 14 files changed, 551 insertions(+), 636 deletions(-) delete mode 100644 src/api/endpoints/users/followers.js create mode 100644 src/api/endpoints/users/followers.ts delete mode 100644 src/api/endpoints/users/following.js create mode 100644 src/api/endpoints/users/following.ts delete mode 100644 src/api/endpoints/users/posts.js create mode 100644 src/api/endpoints/users/posts.ts delete mode 100644 src/api/endpoints/users/recommendation.js create mode 100644 src/api/endpoints/users/recommendation.ts delete mode 100644 src/api/endpoints/users/search.js create mode 100644 src/api/endpoints/users/search.ts delete mode 100644 src/api/endpoints/users/search_by_username.js create mode 100644 src/api/endpoints/users/search_by_username.ts delete mode 100644 src/api/endpoints/users/show.js create mode 100644 src/api/endpoints/users/show.ts (limited to 'src/api') diff --git a/src/api/endpoints/users/followers.js b/src/api/endpoints/users/followers.js deleted file mode 100644 index 598c3b6bcf..0000000000 --- a/src/api/endpoints/users/followers.js +++ /dev/null @@ -1,105 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import Following from '../../models/following'; -import serialize from '../../serializers/user'; -import getFriends from '../../common/get-friends'; - -/** - * Get followers of a user - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - const userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Get 'iknow' parameter - const iknow = params.iknow; - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'cursor' parameter - const cursor = params.cursor || null; - - // Lookup user - const user = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - // Construct query - const query = { - followee_id: user._id, - deleted_at: { $exists: false } - }; - - // ログインしていてかつ iknow フラグがあるとき - if (me && iknow) { - // Get my friends - const myFriends = await getFriends(me._id); - - query.follower_id = { - $in: myFriends - }; - } - - // カーソルが指定されている場合 - if (cursor) { - query._id = { - $lt: new mongo.ObjectID(cursor) - }; - } - - // Get followers - const following = await Following - .find(query, { - limit: limit + 1, - sort: { _id: -1 } - }); - - // 「次のページ」があるかどうか - const inStock = following.length === limit + 1; - if (inStock) { - following.pop(); - } - - // Serialize - const users = await Promise.all(following.map(async f => - await serialize(f.follower_id, me, { detail: true }))); - - // Response - res({ - users: users, - next: inStock ? following[following.length - 1]._id : null, - }); -}); diff --git a/src/api/endpoints/users/followers.ts b/src/api/endpoints/users/followers.ts new file mode 100644 index 0000000000..011a1c70ce --- /dev/null +++ b/src/api/endpoints/users/followers.ts @@ -0,0 +1,96 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import Following from '../../models/following'; +import serialize from '../../serializers/user'; +import getFriends from '../../common/get-friends'; + +/** + * Get followers of a user + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id, 'id', true); + if (userIdErr) return rej('invalid user_id param'); + + // Get 'iknow' parameter + const [iknow, iknowErr] = it(params.iknow).expect.boolean().default(false).qed(); + if (iknowErr) return rej('invalid iknow param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'cursor' parameter + const [cursor, cursorErr] = it(params.cursor).expect.id().default(null).qed(); + if (cursorErr) return rej('invalid cursor param'); + + // Lookup user + const user = await User.findOne({ + _id: userId + }, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + // Construct query + const query = { + followee_id: user._id, + deleted_at: { $exists: false } + } as any; + + // ログインしていてかつ iknow フラグがあるとき + if (me && iknow) { + // Get my friends + const myFriends = await getFriends(me._id); + + query.follower_id = { + $in: myFriends + }; + } + + // カーソルが指定されている場合 + if (cursor) { + query._id = { + $lt: cursor + }; + } + + // Get followers + const following = await Following + .find(query, { + limit: limit + 1, + sort: { _id: -1 } + }); + + // 「次のページ」があるかどうか + const inStock = following.length === limit + 1; + if (inStock) { + following.pop(); + } + + // Serialize + const users = await Promise.all(following.map(async f => + await serialize(f.follower_id, me, { detail: true }))); + + // Response + res({ + users: users, + next: inStock ? following[following.length - 1]._id : null, + }); +}); diff --git a/src/api/endpoints/users/following.js b/src/api/endpoints/users/following.js deleted file mode 100644 index 36868d6d5c..0000000000 --- a/src/api/endpoints/users/following.js +++ /dev/null @@ -1,105 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import Following from '../../models/following'; -import serialize from '../../serializers/user'; -import getFriends from '../../common/get-friends'; - -/** - * Get following users of a user - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - const userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Get 'iknow' parameter - const iknow = params.iknow; - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'cursor' parameter - const cursor = params.cursor || null; - - // Lookup user - const user = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - // Construct query - const query = { - follower_id: user._id, - deleted_at: { $exists: false } - }; - - // ログインしていてかつ iknow フラグがあるとき - if (me && iknow) { - // Get my friends - const myFriends = await getFriends(me._id); - - query.followee_id = { - $in: myFriends - }; - } - - // カーソルが指定されている場合 - if (cursor) { - query._id = { - $lt: new mongo.ObjectID(cursor) - }; - } - - // Get followers - const following = await Following - .find(query, { - limit: limit + 1, - sort: { _id: -1 } - }); - - // 「次のページ」があるかどうか - const inStock = following.length === limit + 1; - if (inStock) { - following.pop(); - } - - // Serialize - const users = await Promise.all(following.map(async f => - await serialize(f.followee_id, me, { detail: true }))); - - // Response - res({ - users: users, - next: inStock ? following[following.length - 1]._id : null, - }); -}); diff --git a/src/api/endpoints/users/following.ts b/src/api/endpoints/users/following.ts new file mode 100644 index 0000000000..df5c058354 --- /dev/null +++ b/src/api/endpoints/users/following.ts @@ -0,0 +1,96 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import Following from '../../models/following'; +import serialize from '../../serializers/user'; +import getFriends from '../../common/get-friends'; + +/** + * Get following users of a user + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id, 'id', true); + if (userIdErr) return rej('invalid user_id param'); + + // Get 'iknow' parameter + const [iknow, iknowErr] = it(params.iknow).expect.boolean().default(false).qed(); + if (iknowErr) return rej('invalid iknow param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'cursor' parameter + const [cursor, cursorErr] = it(params.cursor).expect.id().default(null).qed(); + if (cursorErr) return rej('invalid cursor param'); + + // Lookup user + const user = await User.findOne({ + _id: userId + }, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + // Construct query + const query = { + follower_id: user._id, + deleted_at: { $exists: false } + } as any; + + // ログインしていてかつ iknow フラグがあるとき + if (me && iknow) { + // Get my friends + const myFriends = await getFriends(me._id); + + query.followee_id = { + $in: myFriends + }; + } + + // カーソルが指定されている場合 + if (cursor) { + query._id = { + $lt: cursor + }; + } + + // Get followers + const following = await Following + .find(query, { + limit: limit + 1, + sort: { _id: -1 } + }); + + // 「次のページ」があるかどうか + const inStock = following.length === limit + 1; + if (inStock) { + following.pop(); + } + + // Serialize + const users = await Promise.all(following.map(async f => + await serialize(f.followee_id, me, { detail: true }))); + + // Response + res({ + users: users, + next: inStock ? following[following.length - 1]._id : null, + }); +}); diff --git a/src/api/endpoints/users/posts.js b/src/api/endpoints/users/posts.js deleted file mode 100644 index d358c4b4dd..0000000000 --- a/src/api/endpoints/users/posts.js +++ /dev/null @@ -1,125 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../models/post'; -import User from '../../models/user'; -import serialize from '../../serializers/post'; - -/** - * Get posts of a user - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - let userId = params.user_id; - if (userId === undefined || userId === null || userId === '') { - userId = null; - } - - // Get 'username' parameter - let username = params.username; - if (username === undefined || username === null || username === '') { - username = null; - } - - if (userId === null && username === null) { - return rej('user_id or username is required'); - } - - // Get 'with_replies' parameter - let withReplies = params.with_replies; - if (withReplies == null) { - withReplies = true; - } - - // Get 'with_media' parameter - let withMedia = params.with_media; - if (withMedia == null) { - withMedia = false; - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - const q = userId != null - ? { _id: new mongo.ObjectID(userId) } - : { username_lower: username.toLowerCase() } ; - - // Lookup user - const user = await User.findOne(q, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = { - user_id: user._id - }; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - if (!withReplies) { - query.reply_to_id = null; - } - - if (withMedia) { - query.media_ids = { - $exists: true, - $ne: null - }; - } - - // Issue query - const posts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(posts.map(async (post) => - await serialize(post, me) - ))); -}); diff --git a/src/api/endpoints/users/posts.ts b/src/api/endpoints/users/posts.ts new file mode 100644 index 0000000000..526ed1ee1b --- /dev/null +++ b/src/api/endpoints/users/posts.ts @@ -0,0 +1,113 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Post from '../../models/post'; +import User from '../../models/user'; +import serialize from '../../serializers/post'; + +/** + * Get posts of a user + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id, 'id'); + if (userIdErr) return rej('invalid user_id param'); + + // Get 'username' parameter + const [username, usernameErr] = it(params.username, 'string'); + if (usernameErr) return rej('invalid username param'); + + if (userId === null && username === null) { + return rej('user_id or username is required'); + } + + // Get 'include_replies' parameter + const [includeReplies, includeRepliesErr] = it(params.include_replies).expect.boolean().default(true).qed(); + if (includeRepliesErr) return rej('invalid include_replies param'); + + // Get 'with_media' parameter + const [withMedia, withMediaErr] = it(params.with_media).expect.boolean().default(false).qed(); + if (withMediaErr) return rej('invalid with_media param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + const q = userId != null + ? { _id: userId } + : { username_lower: username.toLowerCase() } ; + + // Lookup user + const user = await User.findOne(q, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + // Construct query + const sort = { + _id: -1 + }; + const query = { + user_id: user._id + } as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + if (!includeReplies) { + query.reply_to_id = null; + } + + if (withMedia) { + query.media_ids = { + $exists: true, + $ne: null + }; + } + + // Issue query + const posts = await Post + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(posts.map(async (post) => + await serialize(post, me) + ))); +}); diff --git a/src/api/endpoints/users/recommendation.js b/src/api/endpoints/users/recommendation.js deleted file mode 100644 index 0045683a5a..0000000000 --- a/src/api/endpoints/users/recommendation.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import User from '../../models/user'; -import serialize from '../../serializers/user'; -import getFriends from '../../common/get-friends'; - -/** - * Get recommended users - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // ID list of the user itself and other users who the user follows - const followingIds = await getFriends(me._id); - - const users = await User - .find({ - _id: { - $nin: followingIds - } - }, { - limit: limit, - skip: offset, - sort: { - followers_count: -1 - } - }); - - // Serialize - res(await Promise.all(users.map(async user => - await serialize(user, me, { detail: true })))); -}); diff --git a/src/api/endpoints/users/recommendation.ts b/src/api/endpoints/users/recommendation.ts new file mode 100644 index 0000000000..c37ae4c978 --- /dev/null +++ b/src/api/endpoints/users/recommendation.ts @@ -0,0 +1,48 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import serialize from '../../serializers/user'; +import getFriends from '../../common/get-friends'; + +/** + * Get recommended users + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // ID list of the user itself and other users who the user follows + const followingIds = await getFriends(me._id); + + const users = await User + .find({ + _id: { + $nin: followingIds + } + }, { + limit: limit, + skip: offset, + sort: { + followers_count: -1 + } + }); + + // Serialize + res(await Promise.all(users.map(async user => + await serialize(user, me, { detail: true })))); +}); diff --git a/src/api/endpoints/users/search.js b/src/api/endpoints/users/search.js deleted file mode 100644 index b1f4537328..0000000000 --- a/src/api/endpoints/users/search.js +++ /dev/null @@ -1,117 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import serialize from '../../serializers/user'; -import config from '../../../conf'; -const escapeRegexp = require('escape-regexp'); - -/** - * Search a user - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'query' parameter - let query = params.query; - if (query === undefined || query === null || query.trim() === '') { - return rej('query is required'); - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // Get 'max' parameter - let max = params.max; - if (max !== undefined && max !== null) { - max = parseInt(max, 10); - - // From 1 to 30 - if (!(1 <= max && max <= 30)) { - return rej('invalid max range'); - } - } else { - max = 10; - } - - // If Elasticsearch is available, search by it - // If not, search by MongoDB - (config.elasticsearch.enable ? byElasticsearch : byNative) - (res, rej, me, query, offset, max); -}); - -// Search by MongoDB -async function byNative(res, rej, me, query, offset, max) { - const escapedQuery = escapeRegexp(query); - - // Search users - const users = await User - .find({ - $or: [{ - username_lower: new RegExp(escapedQuery.toLowerCase()) - }, { - name: new RegExp(escapedQuery) - }] - }, { - limit: max - }); - - // Serialize - res(await Promise.all(users.map(async user => - await serialize(user, me, { detail: true })))); -} - -// Search by Elasticsearch -async function byElasticsearch(res, rej, me, query, offset, max) { - const es = require('../../db/elasticsearch'); - - es.search({ - index: 'misskey', - type: 'user', - body: { - size: max, - from: offset, - query: { - simple_query_string: { - fields: ['username', 'name', 'bio'], - query: query, - default_operator: 'and' - } - } - } - }, async (error, response) => { - if (error) { - console.error(error); - return res(500); - } - - if (response.hits.total === 0) { - return res([]); - } - - const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id)); - - const users = await User - .find({ - _id: { - $in: hits - } - }); - - // Serialize - res(await Promise.all(users.map(async user => - await serialize(user, me, { detail: true })))); - }); -} diff --git a/src/api/endpoints/users/search.ts b/src/api/endpoints/users/search.ts new file mode 100644 index 0000000000..3fb08b0a35 --- /dev/null +++ b/src/api/endpoints/users/search.ts @@ -0,0 +1,103 @@ +'use strict'; + +/** + * Module dependencies + */ +import * as mongo from 'mongodb'; +import it from '../../it'; +import User from '../../models/user'; +import serialize from '../../serializers/user'; +import config from '../../../conf'; +const escapeRegexp = require('escape-regexp'); + +/** + * Search a user + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +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 != '').qed(); + if (queryError) return rej('invalid query param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // Get 'max' parameter + const [max, maxErr] = it(params.max).expect.number().range(1, 30).default(10).qed(); + if (maxErr) return rej('invalid max param'); + + // If Elasticsearch is available, search by it + // If not, search by MongoDB + (config.elasticsearch.enable ? byElasticsearch : byNative) + (res, rej, me, query, offset, max); +}); + +// Search by MongoDB +async function byNative(res, rej, me, query, offset, max) { + const escapedQuery = escapeRegexp(query); + + // Search users + const users = await User + .find({ + $or: [{ + username_lower: new RegExp(escapedQuery.toLowerCase()) + }, { + name: new RegExp(escapedQuery) + }] + }, { + limit: max + }); + + // Serialize + res(await Promise.all(users.map(async user => + await serialize(user, me, { detail: true })))); +} + +// Search by Elasticsearch +async function byElasticsearch(res, rej, me, query, offset, max) { + const es = require('../../db/elasticsearch'); + + es.search({ + index: 'misskey', + type: 'user', + body: { + size: max, + from: offset, + query: { + simple_query_string: { + fields: ['username', 'name', 'bio'], + query: query, + default_operator: 'and' + } + } + } + }, async (error, response) => { + if (error) { + console.error(error); + return res(500); + } + + if (response.hits.total === 0) { + return res([]); + } + + const hits = response.hits.hits.map(hit => new mongo.ObjectID(hit._id)); + + const users = await User + .find({ + _id: { + $in: hits + } + }); + + // Serialize + res(await Promise.all(users.map(async user => + await serialize(user, me, { detail: true })))); + }); +} diff --git a/src/api/endpoints/users/search_by_username.js b/src/api/endpoints/users/search_by_username.js deleted file mode 100644 index 7fe6f34092..0000000000 --- a/src/api/endpoints/users/search_by_username.js +++ /dev/null @@ -1,64 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import serialize from '../../serializers/user'; - -/** - * Search a user by username - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'query' parameter - let query = params.query; - if (query === undefined || query === null || query.trim() === '') { - return rej('query is required'); - } - - query = query.trim(); - - if (!/^[a-zA-Z0-9-]+$/.test(query)) { - return rej('invalid query'); - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - const users = await User - .find({ - username_lower: new RegExp(query.toLowerCase()) - }, { - limit: limit, - skip: offset - }); - - // Serialize - res(await Promise.all(users.map(async user => - await serialize(user, me, { detail: true })))); -}); diff --git a/src/api/endpoints/users/search_by_username.ts b/src/api/endpoints/users/search_by_username.ts new file mode 100644 index 0000000000..540c48e7ce --- /dev/null +++ b/src/api/endpoints/users/search_by_username.ts @@ -0,0 +1,44 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import { validateUsername } from '../../models/user'; +import serialize from '../../serializers/user'; + +/** + * Search a user by username + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => + new Promise(async (res, rej) => +{ + // Get 'query' parameter + const [query, queryError] = it(params.query).expect.string().required().trim().validate(validateUsername).qed(); + if (queryError) return rej('invalid query param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + const users = await User + .find({ + username_lower: new RegExp(query.toLowerCase()) + }, { + limit: limit, + skip: offset + }); + + // Serialize + res(await Promise.all(users.map(async user => + await serialize(user, me, { detail: true })))); +}); diff --git a/src/api/endpoints/users/show.js b/src/api/endpoints/users/show.js deleted file mode 100644 index 0eaba221cc..0000000000 --- a/src/api/endpoints/users/show.js +++ /dev/null @@ -1,60 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import serialize from '../../serializers/user'; - -/** - * Show a user - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - let userId = params.user_id; - if (userId === undefined || userId === null || userId === '') { - userId = null; - } - - // Get 'username' parameter - let username = params.username; - if (username === undefined || username === null || username === '') { - username = null; - } - - if (userId === null && username === null) { - return rej('user_id or username is required'); - } - - // Validate id - if (userId && !mongo.ObjectID.isValid(userId)) { - return rej('incorrect user_id'); - } - - const q = userId != null - ? { _id: new mongo.ObjectID(userId) } - : { username_lower: username.toLowerCase() } ; - - // Lookup user - const user = await User.findOne(q, { - fields: { - data: false - } - }); - - if (user === null) { - return rej('user not found'); - } - - // Send response - res(await serialize(user, me, { - detail: true - })); -}); diff --git a/src/api/endpoints/users/show.ts b/src/api/endpoints/users/show.ts new file mode 100644 index 0000000000..cae4ac0b7f --- /dev/null +++ b/src/api/endpoints/users/show.ts @@ -0,0 +1,51 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import serialize from '../../serializers/user'; + +/** + * Show a user + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id, 'id'); + if (userIdErr) return rej('invalid user_id param'); + + // Get 'username' parameter + const [username, usernameErr] = it(params.username, 'string'); + if (usernameErr) return rej('invalid username param'); + + if (userId === null && username === null) { + return rej('user_id or username is required'); + } + + const q = userId != null + ? { _id: userId } + : { username_lower: username.toLowerCase() } ; + + // Lookup user + const user = await User.findOne(q, { + fields: { + data: false + } + }); + + if (user === null) { + return rej('user not found'); + } + + // Send response + res(await serialize(user, me, { + detail: true + })); +}); -- cgit v1.2.3-freya From 583b64331b4d3d36f642801c847109b8634df1d9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 08:00:10 +0900 Subject: wip --- src/api/endpoints/posts/favorites/create.js | 53 --------------- src/api/endpoints/posts/favorites/create.ts | 51 ++++++++++++++ src/api/endpoints/posts/favorites/delete.js | 51 -------------- src/api/endpoints/posts/favorites/delete.ts | 49 ++++++++++++++ src/api/endpoints/posts/likes/create.js | 92 ------------------------- src/api/endpoints/posts/likes/create.ts | 85 +++++++++++++++++++++++ src/api/endpoints/posts/likes/delete.js | 84 ----------------------- src/api/endpoints/posts/likes/delete.ts | 77 +++++++++++++++++++++ src/api/endpoints/posts/polls/vote.js | 100 ---------------------------- src/api/endpoints/posts/polls/vote.ts | 88 ++++++++++++++++++++++++ src/api/endpoints/username/available.js | 41 ------------ src/api/endpoints/username/available.ts | 35 ++++++++++ 12 files changed, 385 insertions(+), 421 deletions(-) delete mode 100644 src/api/endpoints/posts/favorites/create.js create mode 100644 src/api/endpoints/posts/favorites/create.ts delete mode 100644 src/api/endpoints/posts/favorites/delete.js create mode 100644 src/api/endpoints/posts/favorites/delete.ts delete mode 100644 src/api/endpoints/posts/likes/create.js create mode 100644 src/api/endpoints/posts/likes/create.ts delete mode 100644 src/api/endpoints/posts/likes/delete.js create mode 100644 src/api/endpoints/posts/likes/delete.ts delete mode 100644 src/api/endpoints/posts/polls/vote.js create mode 100644 src/api/endpoints/posts/polls/vote.ts delete mode 100644 src/api/endpoints/username/available.js create mode 100644 src/api/endpoints/username/available.ts (limited to 'src/api') diff --git a/src/api/endpoints/posts/favorites/create.js b/src/api/endpoints/posts/favorites/create.js deleted file mode 100644 index 7ee7c0d3fb..0000000000 --- a/src/api/endpoints/posts/favorites/create.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Favorite from '../../models/favorite'; -import Post from '../../models/post'; - -/** - * Favorite a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => { - // Get 'post_id' parameter - let postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } - - // Get favoritee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - // if already favorited - const exist = await Favorite.findOne({ - post_id: post._id, - user_id: user._id - }); - - if (exist !== null) { - return rej('already favorited'); - } - - // Create favorite - await Favorite.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id - }); - - // Send response - res(); - }); diff --git a/src/api/endpoints/posts/favorites/create.ts b/src/api/endpoints/posts/favorites/create.ts new file mode 100644 index 0000000000..5be338593c --- /dev/null +++ b/src/api/endpoints/posts/favorites/create.ts @@ -0,0 +1,51 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Favorite from '../../../models/favorite'; +import Post from '../../../models/post'; + +/** + * Favorite a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get favoritee + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); + + if (post === null) { + return rej('post not found'); + } + + // if already favorited + const exist = await Favorite.findOne({ + post_id: post._id, + user_id: user._id + }); + + if (exist !== null) { + return rej('already favorited'); + } + + // Create favorite + await Favorite.insert({ + created_at: new Date(), + post_id: post._id, + user_id: user._id + }); + + // Send response + res(); + }); diff --git a/src/api/endpoints/posts/favorites/delete.js b/src/api/endpoints/posts/favorites/delete.js deleted file mode 100644 index 4b36b9bde3..0000000000 --- a/src/api/endpoints/posts/favorites/delete.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Favorite from '../../models/favorite'; -import Post from '../../models/post'; - -/** - * Unfavorite a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => { - // Get 'post_id' parameter - let postId = params.post_id; - if (postId === undefined || postId === null) { - return rej('post_id is required'); - } - - // Get favoritee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - // if already favorited - const exist = await Favorite.findOne({ - post_id: post._id, - user_id: user._id - }); - - if (exist === null) { - return rej('already not favorited'); - } - - // Delete favorite - await Favorite.deleteOne({ - _id: exist._id - }); - - // Send response - res(); - }); diff --git a/src/api/endpoints/posts/favorites/delete.ts b/src/api/endpoints/posts/favorites/delete.ts new file mode 100644 index 0000000000..4dfd761589 --- /dev/null +++ b/src/api/endpoints/posts/favorites/delete.ts @@ -0,0 +1,49 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Favorite from '../../../models/favorite'; +import Post from '../../../models/post'; + +/** + * Unfavorite a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get favoritee + const post = await Post.findOne({ + _id: new mongo.ObjectID(postId) + }); + + if (post === null) { + return rej('post not found'); + } + + // if already favorited + const exist = await Favorite.findOne({ + post_id: post._id, + user_id: user._id + }); + + if (exist === null) { + return rej('already not favorited'); + } + + // Delete favorite + await Favorite.deleteOne({ + _id: exist._id + }); + + // Send response + res(); + }); diff --git a/src/api/endpoints/posts/likes/create.js b/src/api/endpoints/posts/likes/create.js deleted file mode 100644 index 3b2c778a03..0000000000 --- a/src/api/endpoints/posts/likes/create.js +++ /dev/null @@ -1,92 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Like from '../../../models/like'; -import Post from '../../../models/post'; -import User from '../../../models/user'; -import notify from '../../../common/notify'; - -/** - * Like a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => { - // Get 'post_id' parameter - let 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 likee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - // Myself - if (post.user_id.equals(user._id)) { - return rej('-need-translate-'); - } - - // if already liked - const exist = await Like.findOne({ - post_id: post._id, - user_id: user._id, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return rej('already liked'); - } - - // Create like - await Like.insert({ - created_at: new Date(), - post_id: post._id, - user_id: user._id - }); - - // Send response - res(); - - // Increment likes count - Post.update({ _id: post._id }, { - $inc: { - likes_count: 1 - } - }); - - // Increment user likes count - User.update({ _id: user._id }, { - $inc: { - likes_count: 1 - } - }); - - // Increment user liked count - User.update({ _id: post.user_id }, { - $inc: { - liked_count: 1 - } - }); - - // Notify - notify(post.user_id, user._id, 'like', { - post_id: post._id - }); - }); diff --git a/src/api/endpoints/posts/likes/create.ts b/src/api/endpoints/posts/likes/create.ts new file mode 100644 index 0000000000..0ae417e239 --- /dev/null +++ b/src/api/endpoints/posts/likes/create.ts @@ -0,0 +1,85 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Like from '../../../models/like'; +import Post from '../../../models/post'; +import User from '../../../models/user'; +import notify from '../../../common/notify'; + +/** + * Like a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get likee + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + // Myself + if (post.user_id.equals(user._id)) { + return rej('-need-translate-'); + } + + // if already liked + const exist = await Like.findOne({ + post_id: post._id, + user_id: user._id, + deleted_at: { $exists: false } + }); + + if (exist !== null) { + return rej('already liked'); + } + + // Create like + await Like.insert({ + created_at: new Date(), + post_id: post._id, + user_id: user._id + }); + + // Send response + res(); + + // Increment likes count + Post.update({ _id: post._id }, { + $inc: { + likes_count: 1 + } + }); + + // Increment user likes count + User.update({ _id: user._id }, { + $inc: { + likes_count: 1 + } + }); + + // Increment user liked count + User.update({ _id: post.user_id }, { + $inc: { + liked_count: 1 + } + }); + + // Notify + notify(post.user_id, user._id, 'like', { + post_id: post._id + }); + }); diff --git a/src/api/endpoints/posts/likes/delete.js b/src/api/endpoints/posts/likes/delete.js deleted file mode 100644 index 1dd0f5b29a..0000000000 --- a/src/api/endpoints/posts/likes/delete.js +++ /dev/null @@ -1,84 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Like from '../../../models/like'; -import Post from '../../../models/post'; -import User from '../../../models/user'; -// import event from '../../../event'; - -/** - * Unlike a post - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => { - // Get 'post_id' parameter - let 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 likee - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - // if already liked - const exist = await Like.findOne({ - post_id: post._id, - user_id: user._id, - deleted_at: { $exists: false } - }); - - if (exist === null) { - return rej('already not liked'); - } - - // Delete like - await Like.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() - } - }); - - // Send response - res(); - - // Decrement likes count - Post.update({ _id: post._id }, { - $inc: { - likes_count: -1 - } - }); - - // Decrement user likes count - User.update({ _id: user._id }, { - $inc: { - likes_count: -1 - } - }); - - // Decrement user liked count - User.update({ _id: post.user_id }, { - $inc: { - liked_count: -1 - } - }); - }); diff --git a/src/api/endpoints/posts/likes/delete.ts b/src/api/endpoints/posts/likes/delete.ts new file mode 100644 index 0000000000..2b642c107f --- /dev/null +++ b/src/api/endpoints/posts/likes/delete.ts @@ -0,0 +1,77 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Like from '../../../models/like'; +import Post from '../../../models/post'; +import User from '../../../models/user'; +// import event from '../../../event'; + +/** + * Unlike a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get likee + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + // if already liked + const exist = await Like.findOne({ + post_id: post._id, + user_id: user._id, + deleted_at: { $exists: false } + }); + + if (exist === null) { + return rej('already not liked'); + } + + // Delete like + await Like.update({ + _id: exist._id + }, { + $set: { + deleted_at: new Date() + } + }); + + // Send response + res(); + + // Decrement likes count + Post.update({ _id: post._id }, { + $inc: { + likes_count: -1 + } + }); + + // Decrement user likes count + User.update({ _id: user._id }, { + $inc: { + likes_count: -1 + } + }); + + // Decrement user liked count + User.update({ _id: post.user_id }, { + $inc: { + liked_count: -1 + } + }); + }); diff --git a/src/api/endpoints/posts/polls/vote.js b/src/api/endpoints/posts/polls/vote.js deleted file mode 100644 index 9f9a5171a0..0000000000 --- a/src/api/endpoints/posts/polls/vote.js +++ /dev/null @@ -1,100 +0,0 @@ -'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 {any} params - * @param {any} user - * @return {Promise} - */ -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'); - } - - // if already 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/polls/vote.ts b/src/api/endpoints/posts/polls/vote.ts new file mode 100644 index 0000000000..d0caf7da95 --- /dev/null +++ b/src/api/endpoints/posts/polls/vote.ts @@ -0,0 +1,88 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Vote from '../../../models/poll-vote'; +import Post from '../../../models/post'; +import notify from '../../../common/notify'; + +/** + * Vote poll of a post + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id, 'id', true); + if (postIdErr) return rej('invalid post_id param'); + + // Get votee + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + if (post.poll == null) { + return rej('poll not found'); + } + + // Get 'choice' parameter + const [choice, choiceError] = + it(params.choice).expect.string() + .required() + .validate(c => post.poll.choices.some(x => x.id == c)) + .qed(); + if (choiceError) return rej('invalid choice param'); + + // if already 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; + + // 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/username/available.js b/src/api/endpoints/username/available.js deleted file mode 100644 index 8f4d8cf280..0000000000 --- a/src/api/endpoints/username/available.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import User from '../../models/user'; -import { validateUsername } from '../../models/user'; - -/** - * Check available username - * - * @param {any} params - * @return {Promise} - */ -module.exports = async (params) => - new Promise(async (res, rej) => -{ - // Get 'username' parameter - const username = params.username; - if (username == null || username == '') { - return rej('username-is-required'); - } - - // Validate username - if (!validateUsername(username)) { - return rej('invalid-username'); - } - - // Get exist - const exist = await User - .count({ - username_lower: username.toLowerCase() - }, { - limit: 1 - }); - - // Reply - res({ - available: exist === 0 - }); -}); diff --git a/src/api/endpoints/username/available.ts b/src/api/endpoints/username/available.ts new file mode 100644 index 0000000000..9a85644b69 --- /dev/null +++ b/src/api/endpoints/username/available.ts @@ -0,0 +1,35 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import { validateUsername } from '../../models/user'; + +/** + * Check available username + * + * @param {any} params + * @return {Promise} + */ +module.exports = async (params) => + new Promise(async (res, rej) => +{ + // Get 'username' parameter + const [username, usernameError] = it(params.username).expect.string().required().trim().validate(validateUsername).qed(); + if (usernameError) return rej('invalid username param'); + + // Get exist + const exist = await User + .count({ + username_lower: username.toLowerCase() + }, { + limit: 1 + }); + + // Reply + res({ + available: exist === 0 + }); +}); -- cgit v1.2.3-freya From 67f9c158d71c497f3f3889e10fa1587a9a038381 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 08:06:34 +0900 Subject: wip --- src/api/endpoints/drive.js | 40 ------------- src/api/endpoints/drive.ts | 40 +++++++++++++ src/api/endpoints/i.js | 25 --------- src/api/endpoints/i.ts | 25 +++++++++ src/api/endpoints/meta.js | 51 ----------------- src/api/endpoints/meta.ts | 51 +++++++++++++++++ src/api/endpoints/posts.js | 87 ----------------------------- src/api/endpoints/posts.ts | 76 +++++++++++++++++++++++++ src/api/endpoints/posts/favorites/create.ts | 2 +- src/api/endpoints/posts/favorites/delete.ts | 2 +- src/api/endpoints/users.js | 65 --------------------- src/api/endpoints/users.ts | 62 ++++++++++++++++++++ 12 files changed, 256 insertions(+), 270 deletions(-) delete mode 100644 src/api/endpoints/drive.js create mode 100644 src/api/endpoints/drive.ts delete mode 100644 src/api/endpoints/i.js create mode 100644 src/api/endpoints/i.ts delete mode 100644 src/api/endpoints/meta.js create mode 100644 src/api/endpoints/meta.ts delete mode 100644 src/api/endpoints/posts.js create mode 100644 src/api/endpoints/posts.ts delete mode 100644 src/api/endpoints/users.js create mode 100644 src/api/endpoints/users.ts (limited to 'src/api') diff --git a/src/api/endpoints/drive.js b/src/api/endpoints/drive.js deleted file mode 100644 index d73de67f44..0000000000 --- a/src/api/endpoints/drive.js +++ /dev/null @@ -1,40 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import DriveFile from '../models/drive-file'; - -/** - * Get drive information - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => { - // Calculate drive usage - const usage = ((await DriveFile - .aggregate([ - { $match: { user_id: user._id } }, - { - $project: { - datasize: true - } - }, - { - $group: { - _id: null, - usage: { $sum: '$datasize' } - } - } - ]))[0] || { - usage: 0 - }).usage; - - res({ - capacity: user.drive_capacity, - usage: usage - }); - }); diff --git a/src/api/endpoints/drive.ts b/src/api/endpoints/drive.ts new file mode 100644 index 0000000000..d73de67f44 --- /dev/null +++ b/src/api/endpoints/drive.ts @@ -0,0 +1,40 @@ +'use strict'; + +/** + * Module dependencies + */ +import DriveFile from '../models/drive-file'; + +/** + * Get drive information + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + // Calculate drive usage + const usage = ((await DriveFile + .aggregate([ + { $match: { user_id: user._id } }, + { + $project: { + datasize: true + } + }, + { + $group: { + _id: null, + usage: { $sum: '$datasize' } + } + } + ]))[0] || { + usage: 0 + }).usage; + + res({ + capacity: user.drive_capacity, + usage: usage + }); + }); diff --git a/src/api/endpoints/i.js b/src/api/endpoints/i.js deleted file mode 100644 index df8c0d0e00..0000000000 --- a/src/api/endpoints/i.js +++ /dev/null @@ -1,25 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import serialize from '../serializers/user'; - -/** - * Show myself - * - * @param {any} params - * @param {any} user - * @param {any} app - * @param {Boolean} isSecure - * @return {Promise} - */ -module.exports = (params, user, _, isSecure) => - new Promise(async (res, rej) => -{ - // Serialize - res(await serialize(user, user, { - detail: true, - includeSecrets: isSecure - })); -}); diff --git a/src/api/endpoints/i.ts b/src/api/endpoints/i.ts new file mode 100644 index 0000000000..df8c0d0e00 --- /dev/null +++ b/src/api/endpoints/i.ts @@ -0,0 +1,25 @@ +'use strict'; + +/** + * Module dependencies + */ +import serialize from '../serializers/user'; + +/** + * Show myself + * + * @param {any} params + * @param {any} user + * @param {any} app + * @param {Boolean} isSecure + * @return {Promise} + */ +module.exports = (params, user, _, isSecure) => + new Promise(async (res, rej) => +{ + // Serialize + res(await serialize(user, user, { + detail: true, + includeSecrets: isSecure + })); +}); diff --git a/src/api/endpoints/meta.js b/src/api/endpoints/meta.js deleted file mode 100644 index 421ec01d0a..0000000000 --- a/src/api/endpoints/meta.js +++ /dev/null @@ -1,51 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import prominence from 'prominence'; -import git from 'git-last-commit'; - -/** - * @swagger - * /meta: - * post: - * summary: Show the misskey's information - * responses: - * 200: - * description: Success - * schema: - * type: object - * properties: - * maintainer: - * description: maintainer's name - * type: string - * commit: - * description: latest commit's hash - * type: string - * secure: - * description: whether the server supports secure protocols - * type: boolean - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Show core info - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => { - const commit = await prominence(git).getLastCommit(); - - res({ - maintainer: config.maintainer, - commit: commit.shortHash, - secure: config.https.enable - }); - }); diff --git a/src/api/endpoints/meta.ts b/src/api/endpoints/meta.ts new file mode 100644 index 0000000000..421ec01d0a --- /dev/null +++ b/src/api/endpoints/meta.ts @@ -0,0 +1,51 @@ +'use strict'; + +/** + * Module dependencies + */ +import prominence from 'prominence'; +import git from 'git-last-commit'; + +/** + * @swagger + * /meta: + * post: + * summary: Show the misskey's information + * responses: + * 200: + * description: Success + * schema: + * type: object + * properties: + * maintainer: + * description: maintainer's name + * type: string + * commit: + * description: latest commit's hash + * type: string + * secure: + * description: whether the server supports secure protocols + * type: boolean + * + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Show core info + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => { + const commit = await prominence(git).getLastCommit(); + + res({ + maintainer: config.maintainer, + commit: commit.shortHash, + secure: config.https.enable + }); + }); diff --git a/src/api/endpoints/posts.js b/src/api/endpoints/posts.js deleted file mode 100644 index 42294a39c8..0000000000 --- a/src/api/endpoints/posts.js +++ /dev/null @@ -1,87 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import Post from '../models/post'; -import serialize from '../serializers/post'; - -/** - * Lists all posts - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => { - // Get 'include_replies' parameter - let includeReplies = params.include_replies; - if (includeReplies === true) { - includeReplies = true; - } else { - includeReplies = false; - } - - // Get 'include_reposts' parameter - let includeReposts = params.include_reposts; - if (includeReposts === true) { - includeReposts = true; - } else { - includeReposts = false; - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = {}; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - if (!includeReplies) { - query.reply_to_id = null; - } - - if (!includeReposts) { - query.repost_id = null; - } - - // Issue query - const posts = await Post - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(posts.map(async post => await serialize(post)))); - }); diff --git a/src/api/endpoints/posts.ts b/src/api/endpoints/posts.ts new file mode 100644 index 0000000000..458f7d3ded --- /dev/null +++ b/src/api/endpoints/posts.ts @@ -0,0 +1,76 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../it'; +import Post from '../models/post'; +import serialize from '../serializers/post'; + +/** + * Lists all posts + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => { + // Get 'include_replies' parameter + const [includeReplies, includeRepliesErr] = it(params.include_replies).expect.boolean().default(true).qed(); + if (includeRepliesErr) return rej('invalid include_replies param'); + + // Get 'include_reposts' parameter + const [includeReposts, includeRepostsErr] = it(params.include_reposts).expect.boolean().default(true).qed(); + if (includeRepostsErr) return rej('invalid include_reposts param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // Construct query + const sort = { + _id: -1 + }; + const query = {} as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + if (!includeReplies) { + query.reply_to_id = null; + } + + if (!includeReposts) { + query.repost_id = null; + } + + // Issue query + const posts = await Post + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(posts.map(async post => await serialize(post)))); + }); diff --git a/src/api/endpoints/posts/favorites/create.ts b/src/api/endpoints/posts/favorites/create.ts index 5be338593c..45a347ebb3 100644 --- a/src/api/endpoints/posts/favorites/create.ts +++ b/src/api/endpoints/posts/favorites/create.ts @@ -22,7 +22,7 @@ module.exports = (params, user) => // Get favoritee const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { diff --git a/src/api/endpoints/posts/favorites/delete.ts b/src/api/endpoints/posts/favorites/delete.ts index 4dfd761589..df11215903 100644 --- a/src/api/endpoints/posts/favorites/delete.ts +++ b/src/api/endpoints/posts/favorites/delete.ts @@ -22,7 +22,7 @@ module.exports = (params, user) => // Get favoritee const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) + _id: postId }); if (post === null) { diff --git a/src/api/endpoints/users.js b/src/api/endpoints/users.js deleted file mode 100644 index 63e28caa46..0000000000 --- a/src/api/endpoints/users.js +++ /dev/null @@ -1,65 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import User from '../models/user'; -import serialize from '../serializers/user'; - -/** - * Lists all users - * - * @param {any} params - * @param {any} me - * @return {Promise} - */ -module.exports = (params, me) => - new Promise(async (res, rej) => { - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = {}; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const users = await User - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(users.map(async user => - await serialize(user, me)))); - }); diff --git a/src/api/endpoints/users.ts b/src/api/endpoints/users.ts new file mode 100644 index 0000000000..74c4754fed --- /dev/null +++ b/src/api/endpoints/users.ts @@ -0,0 +1,62 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../it'; +import User from '../models/user'; +import serialize from '../serializers/user'; + +/** + * Lists all users + * + * @param {any} params + * @param {any} me + * @return {Promise} + */ +module.exports = (params, me) => + new Promise(async (res, rej) => { + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // Construct query + const sort = { + _id: -1 + }; + const query = {} as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const users = await User + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(users.map(async user => + await serialize(user, me)))); + }); -- cgit v1.2.3-freya From 2a9cba25a89b5cf2394a22696ee0fb67140076a9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 08:24:48 +0900 Subject: wip --- src/api/endpoints/messaging/history.js | 47 ------- src/api/endpoints/messaging/history.ts | 38 ++++++ src/api/endpoints/messaging/messages.js | 140 -------------------- src/api/endpoints/messaging/messages.ts | 133 +++++++++++++++++++ src/api/endpoints/messaging/messages/create.js | 169 ------------------------ src/api/endpoints/messaging/messages/create.ts | 147 +++++++++++++++++++++ src/api/endpoints/messaging/unread.js | 27 ---- src/api/endpoints/messaging/unread.ts | 27 ++++ src/api/endpoints/my/apps.js | 58 -------- src/api/endpoints/my/apps.ts | 45 +++++++ src/api/endpoints/notifications/mark_as_read.js | 53 -------- src/api/endpoints/notifications/mark_as_read.ts | 50 +++++++ src/api/it.ts | 4 +- src/api/models/messaging-message.ts | 5 + src/api/serializers/messaging-message.ts | 2 +- 15 files changed, 448 insertions(+), 497 deletions(-) delete mode 100644 src/api/endpoints/messaging/history.js create mode 100644 src/api/endpoints/messaging/history.ts delete mode 100644 src/api/endpoints/messaging/messages.js create mode 100644 src/api/endpoints/messaging/messages.ts delete mode 100644 src/api/endpoints/messaging/messages/create.js create mode 100644 src/api/endpoints/messaging/messages/create.ts delete mode 100644 src/api/endpoints/messaging/unread.js create mode 100644 src/api/endpoints/messaging/unread.ts delete mode 100644 src/api/endpoints/my/apps.js create mode 100644 src/api/endpoints/my/apps.ts delete mode 100644 src/api/endpoints/notifications/mark_as_read.js create mode 100644 src/api/endpoints/notifications/mark_as_read.ts (limited to 'src/api') diff --git a/src/api/endpoints/messaging/history.js b/src/api/endpoints/messaging/history.js deleted file mode 100644 index 60c34a6a41..0000000000 --- a/src/api/endpoints/messaging/history.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import History from '../../models/messaging-history'; -import serialize from '../../serializers/messaging-message'; - -/** - * Show messaging history - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get history - const history = await History - .find({ - user_id: user._id - }, { - limit: limit, - sort: { - updated_at: -1 - } - }); - - // Serialize - res(await Promise.all(history.map(async h => - await serialize(h.message, user)))); -}); diff --git a/src/api/endpoints/messaging/history.ts b/src/api/endpoints/messaging/history.ts new file mode 100644 index 0000000000..07ad6e0f22 --- /dev/null +++ b/src/api/endpoints/messaging/history.ts @@ -0,0 +1,38 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import History from '../../models/messaging-history'; +import serialize from '../../serializers/messaging-message'; + +/** + * Show messaging history + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get history + const history = await History + .find({ + user_id: user._id + }, { + limit: limit, + sort: { + updated_at: -1 + } + }); + + // Serialize + res(await Promise.all(history.map(async h => + await serialize(h.message, user)))); +}); diff --git a/src/api/endpoints/messaging/messages.js b/src/api/endpoints/messaging/messages.js deleted file mode 100644 index eaaf38c398..0000000000 --- a/src/api/endpoints/messaging/messages.js +++ /dev/null @@ -1,140 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Message from '../../models/messaging-message'; -import User from '../../models/user'; -import serialize from '../../serializers/messaging-message'; -import publishUserStream from '../../event'; -import { publishMessagingStream } from '../../event'; - -/** - * Get messages - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - let recipient = params.user_id; - if (recipient !== undefined && recipient !== null) { - recipient = await User.findOne({ - _id: new mongo.ObjectID(recipient) - }, { - fields: { - _id: true - } - }); - - if (recipient === null) { - return rej('user not found'); - } - } else { - return rej('user_id is required'); - } - - // Get 'mark_as_read' parameter - let markAsRead = params.mark_as_read; - if (markAsRead == null) { - markAsRead = true; - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - const query = { - $or: [{ - user_id: user._id, - recipient_id: recipient._id - }, { - user_id: recipient._id, - recipient_id: user._id - }] - }; - - const sort = { - _id: -1 - }; - - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const messages = await Message - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(messages.map(async message => - await serialize(message, user, { - populateRecipient: false - })))); - - if (messages.length === 0) { - return; - } - - // Mark as read all - if (markAsRead) { - const ids = messages - .filter(m => m.is_read == false) - .filter(m => m.recipient_id.equals(user._id)) - .map(m => m._id); - - // Update documents - await Message.update({ - _id: { $in: ids } - }, { - $set: { is_read: true } - }, { - multi: true - }); - - // Publish event - publishMessagingStream(recipient._id, user._id, 'read', ids.map(id => id.toString())); - - const count = await Message - .count({ - recipient_id: user._id, - is_read: false - }); - - if (count == 0) { - // 全ての(いままで未読だった)メッセージを(これで)読みましたよというイベントを発行 - publishUserStream(user._id, 'read_all_messaging_messages'); - } - } -}); diff --git a/src/api/endpoints/messaging/messages.ts b/src/api/endpoints/messaging/messages.ts new file mode 100644 index 0000000000..81562efbcc --- /dev/null +++ b/src/api/endpoints/messaging/messages.ts @@ -0,0 +1,133 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Message from '../../models/messaging-message'; +import User from '../../models/user'; +import serialize from '../../serializers/messaging-message'; +import publishUserStream from '../../event'; +import { publishMessagingStream } from '../../event'; + +/** + * Get messages + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().qed(); + if (recipientIdErr) return rej('invalid user_id param'); + + // Fetch recipient + const recipient = await User.findOne({ + _id: recipientId + }, { + fields: { + _id: true + } + }); + + if (recipient === null) { + return rej('user not found'); + } + + // Get 'mark_as_read' parameter + const [markAsRead, markAsReadErr] = it(params.mark_as_read).expect.boolean().default(true).qed(); + if (markAsReadErr) return rej('invalid mark_as_read param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + const query = { + $or: [{ + user_id: user._id, + recipient_id: recipient._id + }, { + user_id: recipient._id, + recipient_id: user._id + }] + } as any; + + const sort = { + _id: -1 + }; + + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const messages = await Message + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(messages.map(async message => + await serialize(message, user, { + populateRecipient: false + })))); + + if (messages.length === 0) { + return; + } + + // Mark as read all + if (markAsRead) { + const ids = messages + .filter(m => m.is_read == false) + .filter(m => m.recipient_id.equals(user._id)) + .map(m => m._id); + + // Update documents + await Message.update({ + _id: { $in: ids } + }, { + $set: { is_read: true } + }, { + multi: true + }); + + // Publish event + publishMessagingStream(recipient._id, user._id, 'read', ids.map(id => id.toString())); + + const count = await Message + .count({ + recipient_id: user._id, + is_read: false + }); + + if (count == 0) { + // 全ての(いままで未読だった)メッセージを(これで)読みましたよというイベントを発行 + publishUserStream(user._id, 'read_all_messaging_messages'); + } + } +}); diff --git a/src/api/endpoints/messaging/messages/create.js b/src/api/endpoints/messaging/messages/create.js deleted file mode 100644 index a88cc39ee8..0000000000 --- a/src/api/endpoints/messaging/messages/create.js +++ /dev/null @@ -1,169 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Message from '../../../models/messaging-message'; -import History from '../../../models/messaging-history'; -import User from '../../../models/user'; -import DriveFile from '../../../models/drive-file'; -import serialize from '../../../serializers/messaging-message'; -import publishUserStream from '../../../event'; -import { publishMessagingStream } from '../../../event'; -import config from '../../../../conf'; - -/** - * 最大文字数 - */ -const maxTextLength = 500; - -/** - * Create a message - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - let recipient = params.user_id; - if (recipient !== undefined && recipient !== null) { - if (typeof recipient != 'string') { - return rej('user_id must be a string'); - } - - // Validate id - if (!mongo.ObjectID.isValid(recipient)) { - return rej('incorrect user_id'); - } - - // Myself - if (new mongo.ObjectID(recipient).equals(user._id)) { - return rej('cannot send message to myself'); - } - - recipient = await User.findOne({ - _id: new mongo.ObjectID(recipient) - }, { - fields: { - _id: true - } - }); - - if (recipient === null) { - return rej('user not found'); - } - } else { - return rej('user_id is required'); - } - - // Get 'text' parameter - let text = params.text; - if (text !== undefined && text !== null) { - text = text.trim(); - if (text.length === 0) { - text = null; - } else if (text.length > maxTextLength) { - return rej('too long text'); - } - } else { - text = null; - } - - // Get 'file_id' parameter - let file = params.file_id; - if (file !== undefined && file !== null) { - file = await DriveFile.findOne({ - _id: new mongo.ObjectID(file), - user_id: user._id - }, { - data: false - }); - - if (file === null) { - return rej('file not found'); - } - } else { - file = null; - } - - // テキストが無いかつ添付ファイルも無かったらエラー - if (text === null && file === null) { - return rej('text or file is required'); - } - - // メッセージを作成 - const message = await Message.insert({ - created_at: new Date(), - file_id: file ? file._id : undefined, - recipient_id: recipient._id, - text: text ? text : undefined, - user_id: user._id, - is_read: false - }); - - // Serialize - const messageObj = await serialize(message); - - // Reponse - res(messageObj); - - // 自分のストリーム - publishMessagingStream(message.user_id, message.recipient_id, 'message', messageObj); - publishUserStream(message.user_id, 'messaging_message', messageObj); - - // 相手のストリーム - publishMessagingStream(message.recipient_id, message.user_id, 'message', messageObj); - publishUserStream(message.recipient_id, 'messaging_message', messageObj); - - // 5秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する - setTimeout(async () => { - const freshMessage = await Message.findOne({ _id: message._id }, { is_read: true }); - if (!freshMessage.is_read) { - publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj); - } - }, 5000); - - // Register to search database - if (message.text && config.elasticsearch.enable) { - const es = require('../../../db/elasticsearch'); - - es.index({ - index: 'misskey', - type: 'messaging_message', - id: message._id.toString(), - body: { - text: message.text - } - }); - } - - // 履歴作成(自分) - History.update({ - user_id: user._id, - partner: recipient._id - }, { - updated_at: new Date(), - user_id: user._id, - partner: recipient._id, - message: message._id - }, { - upsert: true - }); - - // 履歴作成(相手) - History.update({ - user_id: recipient._id, - partner: user._id - }, { - updated_at: new Date(), - user_id: recipient._id, - partner: user._id, - message: message._id - }, { - upsert: true - }); -}); diff --git a/src/api/endpoints/messaging/messages/create.ts b/src/api/endpoints/messaging/messages/create.ts new file mode 100644 index 0000000000..dbe7f617fa --- /dev/null +++ b/src/api/endpoints/messaging/messages/create.ts @@ -0,0 +1,147 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Message from '../../../models/messaging-message'; +import { isValidText } from '../../../models/messaging-message'; +import History from '../../../models/messaging-history'; +import User from '../../../models/user'; +import DriveFile from '../../../models/drive-file'; +import serialize from '../../../serializers/messaging-message'; +import publishUserStream from '../../../event'; +import { publishMessagingStream } from '../../../event'; +import config from '../../../../conf'; + +/** + * Create a message + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [recipientId, recipientIdErr] = it(params.user_id).expect.id().required().qed(); + if (recipientIdErr) return rej('invalid user_id param'); + + // Myself + if (recipientId.equals(user._id)) { + return rej('cannot send message to myself'); + } + + // Fetch recipient + const recipient = await User.findOne({ + _id: recipientId + }, { + fields: { + _id: true + } + }); + + if (recipient === null) { + return rej('user not found'); + } + + // Get 'text' parameter + const [text, textErr] = it(params.text).expect.string().validate(isValidText).qed(); + if (textErr) return rej('invalid text'); + + // Get 'file_id' parameter + const [fileId, fileIdErr] = it(params.file_id).expect.id().qed(); + if (fileIdErr) return rej('invalid file_id param'); + + let file = null; + if (fileId !== null) { + file = await DriveFile.findOne({ + _id: fileId, + user_id: user._id + }, { + data: false + }); + + if (file === null) { + return rej('file not found'); + } + } + + // テキストが無いかつ添付ファイルも無かったらエラー + if (text === null && file === null) { + return rej('text or file is required'); + } + + // メッセージを作成 + const message = await Message.insert({ + created_at: new Date(), + file_id: file ? file._id : undefined, + recipient_id: recipient._id, + text: text ? text : undefined, + user_id: user._id, + is_read: false + }); + + // Serialize + const messageObj = await serialize(message); + + // Reponse + res(messageObj); + + // 自分のストリーム + publishMessagingStream(message.user_id, message.recipient_id, 'message', messageObj); + publishUserStream(message.user_id, 'messaging_message', messageObj); + + // 相手のストリーム + publishMessagingStream(message.recipient_id, message.user_id, 'message', messageObj); + publishUserStream(message.recipient_id, 'messaging_message', messageObj); + + // 5秒経っても(今回作成した)メッセージが既読にならなかったら「未読のメッセージがありますよ」イベントを発行する + setTimeout(async () => { + const freshMessage = await Message.findOne({ _id: message._id }, { is_read: true }); + if (!freshMessage.is_read) { + publishUserStream(message.recipient_id, 'unread_messaging_message', messageObj); + } + }, 5000); + + // Register to search database + if (message.text && config.elasticsearch.enable) { + const es = require('../../../db/elasticsearch'); + + es.index({ + index: 'misskey', + type: 'messaging_message', + id: message._id.toString(), + body: { + text: message.text + } + }); + } + + // 履歴作成(自分) + History.update({ + user_id: user._id, + partner: recipient._id + }, { + updated_at: new Date(), + user_id: user._id, + partner: recipient._id, + message: message._id + }, { + upsert: true + }); + + // 履歴作成(相手) + History.update({ + user_id: recipient._id, + partner: user._id + }, { + updated_at: new Date(), + user_id: recipient._id, + partner: user._id, + message: message._id + }, { + upsert: true + }); +}); diff --git a/src/api/endpoints/messaging/unread.js b/src/api/endpoints/messaging/unread.js deleted file mode 100644 index faba171bdc..0000000000 --- a/src/api/endpoints/messaging/unread.js +++ /dev/null @@ -1,27 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import Message from '../../models/messaging-message'; - -/** - * Get count of unread messages - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - const count = await Message - .count({ - recipient_id: user._id, - is_read: false - }); - - res({ - count: count - }); -}); diff --git a/src/api/endpoints/messaging/unread.ts b/src/api/endpoints/messaging/unread.ts new file mode 100644 index 0000000000..faba171bdc --- /dev/null +++ b/src/api/endpoints/messaging/unread.ts @@ -0,0 +1,27 @@ +'use strict'; + +/** + * Module dependencies + */ +import Message from '../../models/messaging-message'; + +/** + * Get count of unread messages + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + const count = await Message + .count({ + recipient_id: user._id, + is_read: false + }); + + res({ + count: count + }); +}); diff --git a/src/api/endpoints/my/apps.js b/src/api/endpoints/my/apps.js deleted file mode 100644 index 1f45a1a27f..0000000000 --- a/src/api/endpoints/my/apps.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import App from '../../models/app'; -import serialize from '../../serializers/app'; - -/** - * Get my apps - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - const query = { - user_id: user._id - }; - - // Execute query - const apps = await App - .find(query, { - limit: limit, - skip: offset, - sort: { - _id: -1 - } - }); - - // Reply - res(await Promise.all(apps.map(async app => - await serialize(app)))); -}); diff --git a/src/api/endpoints/my/apps.ts b/src/api/endpoints/my/apps.ts new file mode 100644 index 0000000000..2466a47ce4 --- /dev/null +++ b/src/api/endpoints/my/apps.ts @@ -0,0 +1,45 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import App from '../../models/app'; +import serialize from '../../serializers/app'; + +/** + * Get my apps + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + const query = { + user_id: user._id + }; + + // Execute query + const apps = await App + .find(query, { + limit: limit, + skip: offset, + sort: { + _id: -1 + } + }); + + // Reply + res(await Promise.all(apps.map(async app => + await serialize(app)))); +}); diff --git a/src/api/endpoints/notifications/mark_as_read.js b/src/api/endpoints/notifications/mark_as_read.js deleted file mode 100644 index 9c8a5ee64b..0000000000 --- a/src/api/endpoints/notifications/mark_as_read.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Notification from '../../../models/notification'; -import serialize from '../../../serializers/notification'; -import event from '../../../event'; - -/** - * Mark as read a notification - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => { - const notificationId = params.notification; - - if (notificationId === undefined || notificationId === null) { - return rej('notification is required'); - } - - // Get notification - const notification = await Notification - .findOne({ - _id: new mongo.ObjectID(notificationId), - i: user._id - }); - - if (notification === null) { - return rej('notification-not-found'); - } - - // Update - notification.is_read = true; - Notification.update({ _id: notification._id }, { - $set: { - is_read: true - } - }); - - // Response - res(); - - // Serialize - const notificationObj = await serialize(notification); - - // Publish read_notification event - event(user._id, 'read_notification', notificationObj); - }); diff --git a/src/api/endpoints/notifications/mark_as_read.ts b/src/api/endpoints/notifications/mark_as_read.ts new file mode 100644 index 0000000000..6e75927cfa --- /dev/null +++ b/src/api/endpoints/notifications/mark_as_read.ts @@ -0,0 +1,50 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Notification from '../../models/notification'; +import serialize from '../../serializers/notification'; +import event from '../../event'; + +/** + * Mark as read a notification + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => { + const [notificationId, notificationIdErr] = it(params.notification_id).expect.id().required().qed(); + if (notificationIdErr) return rej('invalid notification_id param'); + + // Get notification + const notification = await Notification + .findOne({ + _id: notificationId, + i: user._id + }); + + if (notification === null) { + return rej('notification-not-found'); + } + + // Update + notification.is_read = true; + Notification.update({ _id: notification._id }, { + $set: { + is_read: true + } + }); + + // Response + res(); + + // Serialize + const notificationObj = await serialize(notification); + + // Publish read_notification event + event(user._id, 'read_notification', notificationObj); + }); diff --git a/src/api/it.ts b/src/api/it.ts index f4b28531d6..039f879957 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -424,7 +424,7 @@ class IdQuery extends QueryCore { /** * このインスタンスの値およびエラーを取得します */ - qed(): [any[], Error] { + qed(): [mongo.ObjectID, Error] { return super.qed(); } @@ -433,7 +433,7 @@ class IdQuery extends QueryCore { * バリデータが false またはエラーを返した場合エラーにします * @param validator バリデータ */ - validate(validator: Validator) { + validate(validator: Validator) { return super.validate(validator); } } diff --git a/src/api/models/messaging-message.ts b/src/api/models/messaging-message.ts index cad6823cb8..50955d7fb0 100644 --- a/src/api/models/messaging-message.ts +++ b/src/api/models/messaging-message.ts @@ -1,3 +1,8 @@ import db from '../../db/mongodb'; export default db.get('messaging_messages') as any; // fuck type definition + +export function isValidText(text: string): boolean { + return text.length <= 1000 && text.trim() != ''; +} + diff --git a/src/api/serializers/messaging-message.ts b/src/api/serializers/messaging-message.ts index 4a4c8fc5b4..da63f8b99e 100644 --- a/src/api/serializers/messaging-message.ts +++ b/src/api/serializers/messaging-message.ts @@ -19,7 +19,7 @@ import deepcopy = require('deepcopy'); */ export default ( message: any, - me: any, + me?: any, options?: { populateRecipient: boolean } -- cgit v1.2.3-freya From dc45055f2ffcea2369f12d104999746220b22c90 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 08:56:07 +0900 Subject: wip --- src/api/endpoints/i/appdata/get.js | 53 --------------- src/api/endpoints/i/appdata/get.ts | 53 +++++++++++++++ src/api/endpoints/i/appdata/set.js | 61 ----------------- src/api/endpoints/i/appdata/set.ts | 61 +++++++++++++++++ src/api/endpoints/i/authorized_apps.js | 59 ---------------- src/api/endpoints/i/authorized_apps.ts | 47 +++++++++++++ src/api/endpoints/i/favorites.js | 59 ---------------- src/api/endpoints/i/favorites.ts | 48 +++++++++++++ src/api/endpoints/i/notifications.js | 117 -------------------------------- src/api/endpoints/i/notifications.ts | 111 ++++++++++++++++++++++++++++++ src/api/endpoints/i/signin_history.js | 70 ------------------- src/api/endpoints/i/signin_history.ts | 66 ++++++++++++++++++ src/api/endpoints/i/update.js | 119 --------------------------------- src/api/endpoints/i/update.ts | 110 ++++++++++++++++++++++++++++++ src/api/endpoints/posts/create.ts | 19 +++--- src/api/it.ts | 78 ++++++++++----------- 16 files changed, 539 insertions(+), 592 deletions(-) delete mode 100644 src/api/endpoints/i/appdata/get.js create mode 100644 src/api/endpoints/i/appdata/get.ts delete mode 100644 src/api/endpoints/i/appdata/set.js create mode 100644 src/api/endpoints/i/appdata/set.ts delete mode 100644 src/api/endpoints/i/authorized_apps.js create mode 100644 src/api/endpoints/i/authorized_apps.ts delete mode 100644 src/api/endpoints/i/favorites.js create mode 100644 src/api/endpoints/i/favorites.ts delete mode 100644 src/api/endpoints/i/notifications.js create mode 100644 src/api/endpoints/i/notifications.ts delete mode 100644 src/api/endpoints/i/signin_history.js create mode 100644 src/api/endpoints/i/signin_history.ts delete mode 100644 src/api/endpoints/i/update.js create mode 100644 src/api/endpoints/i/update.ts (limited to 'src/api') diff --git a/src/api/endpoints/i/appdata/get.js b/src/api/endpoints/i/appdata/get.js deleted file mode 100644 index 7f1bdf0713..0000000000 --- a/src/api/endpoints/i/appdata/get.js +++ /dev/null @@ -1,53 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import Appdata from '../../../models/appdata'; - -/** - * Get app data - * - * @param {any} params - * @param {any} user - * @param {any} app - * @param {Boolean} isSecure - * @return {Promise} - */ -module.exports = (params, user, app, isSecure) => - new Promise(async (res, rej) => -{ - // Get 'key' parameter - let key = params.key; - if (key === undefined) { - key = null; - } - - if (isSecure) { - if (!user.data) { - return res(); - } - if (key !== null) { - const data = {}; - data[key] = user.data[key]; - res(data); - } else { - res(user.data); - } - } else { - const select = {}; - if (key !== null) { - select['data.' + key] = true; - } - const appdata = await Appdata.findOne({ - app_id: app._id, - user_id: user._id - }, select); - - if (appdata) { - res(appdata.data); - } else { - res(); - } - } -}); diff --git a/src/api/endpoints/i/appdata/get.ts b/src/api/endpoints/i/appdata/get.ts new file mode 100644 index 0000000000..7f1bdf0713 --- /dev/null +++ b/src/api/endpoints/i/appdata/get.ts @@ -0,0 +1,53 @@ +'use strict'; + +/** + * Module dependencies + */ +import Appdata from '../../../models/appdata'; + +/** + * Get app data + * + * @param {any} params + * @param {any} user + * @param {any} app + * @param {Boolean} isSecure + * @return {Promise} + */ +module.exports = (params, user, app, isSecure) => + new Promise(async (res, rej) => +{ + // Get 'key' parameter + let key = params.key; + if (key === undefined) { + key = null; + } + + if (isSecure) { + if (!user.data) { + return res(); + } + if (key !== null) { + const data = {}; + data[key] = user.data[key]; + res(data); + } else { + res(user.data); + } + } else { + const select = {}; + if (key !== null) { + select['data.' + key] = true; + } + const appdata = await Appdata.findOne({ + app_id: app._id, + user_id: user._id + }, select); + + if (appdata) { + res(appdata.data); + } else { + res(); + } + } +}); diff --git a/src/api/endpoints/i/appdata/set.js b/src/api/endpoints/i/appdata/set.js deleted file mode 100644 index 57001f4e8b..0000000000 --- a/src/api/endpoints/i/appdata/set.js +++ /dev/null @@ -1,61 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import Appdata from '../../../models/appdata'; -import User from '../../../models/user'; -import serialize from '../../../serializers/user'; -import event from '../../../event'; - -/** - * Set app data - * - * @param {any} params - * @param {any} user - * @param {any} app - * @param {Boolean} isSecure - * @return {Promise} - */ -module.exports = (params, user, app, isSecure) => - new Promise(async (res, rej) => -{ - const data = params.data; - if (data == null) { - return rej('data is required'); - } - - if (isSecure) { - const _user = await User.findOneAndUpdate(user._id, { - $set: { - data: Object.assign(user.data || {}, JSON.parse(data)) - } - }); - res(204); - - // Publish i updated event - event(user._id, 'i_updated', await serialize(_user, user, { - detail: true, - includeSecrets: true - })); - } else { - const appdata = await Appdata.findOne({ - app_id: app._id, - user_id: user._id - }); - await Appdata.update({ - app_id: app._id, - user_id: user._id - }, Object.assign({ - app_id: app._id, - user_id: user._id - }, { - $set: { - data: Object.assign((appdata || {}).data || {}, JSON.parse(data)) - } - }), { - upsert: true - }); - res(204); - } -}); diff --git a/src/api/endpoints/i/appdata/set.ts b/src/api/endpoints/i/appdata/set.ts new file mode 100644 index 0000000000..57001f4e8b --- /dev/null +++ b/src/api/endpoints/i/appdata/set.ts @@ -0,0 +1,61 @@ +'use strict'; + +/** + * Module dependencies + */ +import Appdata from '../../../models/appdata'; +import User from '../../../models/user'; +import serialize from '../../../serializers/user'; +import event from '../../../event'; + +/** + * Set app data + * + * @param {any} params + * @param {any} user + * @param {any} app + * @param {Boolean} isSecure + * @return {Promise} + */ +module.exports = (params, user, app, isSecure) => + new Promise(async (res, rej) => +{ + const data = params.data; + if (data == null) { + return rej('data is required'); + } + + if (isSecure) { + const _user = await User.findOneAndUpdate(user._id, { + $set: { + data: Object.assign(user.data || {}, JSON.parse(data)) + } + }); + res(204); + + // Publish i updated event + event(user._id, 'i_updated', await serialize(_user, user, { + detail: true, + includeSecrets: true + })); + } else { + const appdata = await Appdata.findOne({ + app_id: app._id, + user_id: user._id + }); + await Appdata.update({ + app_id: app._id, + user_id: user._id + }, Object.assign({ + app_id: app._id, + user_id: user._id + }, { + $set: { + data: Object.assign((appdata || {}).data || {}, JSON.parse(data)) + } + }), { + upsert: true + }); + res(204); + } +}); diff --git a/src/api/endpoints/i/authorized_apps.js b/src/api/endpoints/i/authorized_apps.js deleted file mode 100644 index 3c0cf75057..0000000000 --- a/src/api/endpoints/i/authorized_apps.js +++ /dev/null @@ -1,59 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import AccessToken from '../../models/access-token'; -import serialize from '../../serializers/app'; - -/** - * Get authorized apps of my account - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // Get 'sort' parameter - let sort = params.sort || 'desc'; - - // Get tokens - const tokens = await AccessToken - .find({ - user_id: user._id - }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(tokens.map(async token => - await serialize(token.app_id)))); -}); diff --git a/src/api/endpoints/i/authorized_apps.ts b/src/api/endpoints/i/authorized_apps.ts new file mode 100644 index 0000000000..fb56a107e7 --- /dev/null +++ b/src/api/endpoints/i/authorized_apps.ts @@ -0,0 +1,47 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import AccessToken from '../../models/access-token'; +import serialize from '../../serializers/app'; + +/** + * Get authorized apps of my account + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // Get 'sort' parameter + const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + if (sortError) return rej('invalid sort param'); + + // Get tokens + const tokens = await AccessToken + .find({ + user_id: user._id + }, { + limit: limit, + skip: offset, + sort: { + _id: sort == 'asc' ? 1 : -1 + } + }); + + // Serialize + res(await Promise.all(tokens.map(async token => + await serialize(token.app_id)))); +}); diff --git a/src/api/endpoints/i/favorites.js b/src/api/endpoints/i/favorites.js deleted file mode 100644 index 28e402e366..0000000000 --- a/src/api/endpoints/i/favorites.js +++ /dev/null @@ -1,59 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Favorite from '../../models/favorite'; -import serialize from '../../serializers/post'; - -/** - * Get followers of a user - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - // Get 'offset' parameter - let offset = params.offset; - if (offset !== undefined && offset !== null) { - offset = parseInt(offset, 10); - } else { - offset = 0; - } - - // Get 'sort' parameter - let sort = params.sort || 'desc'; - - // Get favorites - const favorites = await Favorites - .find({ - user_id: user._id - }, { - limit: limit, - skip: offset, - sort: { - _id: sort == 'asc' ? 1 : -1 - } - }); - - // Serialize - res(await Promise.all(favorites.map(async favorite => - await serialize(favorite.post) - ))); -}); diff --git a/src/api/endpoints/i/favorites.ts b/src/api/endpoints/i/favorites.ts new file mode 100644 index 0000000000..c04d318379 --- /dev/null +++ b/src/api/endpoints/i/favorites.ts @@ -0,0 +1,48 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Favorite from '../../models/favorite'; +import serialize from '../../serializers/post'; + +/** + * Get followers of a user + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'offset' parameter + const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed(); + if (offsetErr) return rej('invalid offset param'); + + // Get 'sort' parameter + const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); + if (sortError) return rej('invalid sort param'); + + // Get favorites + const favorites = await Favorite + .find({ + user_id: user._id + }, { + limit: limit, + skip: offset, + sort: { + _id: sort == 'asc' ? 1 : -1 + } + }); + + // Serialize + res(await Promise.all(favorites.map(async favorite => + await serialize(favorite.post) + ))); +}); diff --git a/src/api/endpoints/i/notifications.js b/src/api/endpoints/i/notifications.js deleted file mode 100644 index d5174439e2..0000000000 --- a/src/api/endpoints/i/notifications.js +++ /dev/null @@ -1,117 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Notification from '../../models/notification'; -import serialize from '../../serializers/notification'; -import getFriends from '../../common/get-friends'; - -/** - * Get notifications - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'following' parameter - const following = params.following; - - // Get 'mark_as_read' parameter - let markAsRead = params.mark_as_read; - if (markAsRead == null) { - markAsRead = true; - } - - // Get 'type' parameter - let type = params.type; - if (type !== undefined && type !== null) { - type = type.split(',').map(x => x.trim()); - } - - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - const query = { - notifiee_id: user._id - }; - - const sort = { - _id: -1 - }; - - if (following) { - // ID list of the user itself and other users who the user follows - const followingIds = await getFriends(user._id); - - query.notifier_id = { - $in: followingIds - }; - } - - if (type) { - query.type = { - $in: type - }; - } - - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const notifications = await Notification - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(notifications.map(async notification => - await serialize(notification)))); - - // Mark as read all - if (notifications.length > 0 && markAsRead) { - const ids = notifications - .filter(x => x.is_read == false) - .map(x => x._id); - - // Update documents - await Notification.update({ - _id: { $in: ids } - }, { - $set: { is_read: true } - }, { - multi: true - }); - } -}); diff --git a/src/api/endpoints/i/notifications.ts b/src/api/endpoints/i/notifications.ts new file mode 100644 index 0000000000..21537ea799 --- /dev/null +++ b/src/api/endpoints/i/notifications.ts @@ -0,0 +1,111 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Notification from '../../models/notification'; +import serialize from '../../serializers/notification'; +import getFriends from '../../common/get-friends'; + +/** + * Get notifications + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'following' parameter + const [following, followingError] = + it(params.following).expect.boolean().default(false).qed(); + if (followingError) return rej('invalid following param'); + + // Get 'mark_as_read' parameter + const [markAsRead, markAsReadErr] = it(params.mark_as_read).expect.boolean().default(true).qed(); + if (markAsReadErr) return rej('invalid mark_as_read param'); + + // Get 'type' parameter + const [type, typeErr] = it(params.type).expect.array().unique().allString().qed(); + if (typeErr) return rej('invalid type param'); + + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + const query = { + notifiee_id: user._id + } as any; + + const sort = { + _id: -1 + }; + + if (following) { + // ID list of the user itself and other users who the user follows + const followingIds = await getFriends(user._id); + + query.notifier_id = { + $in: followingIds + }; + } + + if (type) { + query.type = { + $in: type + }; + } + + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const notifications = await Notification + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(notifications.map(async notification => + await serialize(notification)))); + + // Mark as read all + if (notifications.length > 0 && markAsRead) { + const ids = notifications + .filter(x => x.is_read == false) + .map(x => x._id); + + // Update documents + await Notification.update({ + _id: { $in: ids } + }, { + $set: { is_read: true } + }, { + multi: true + }); + } +}); diff --git a/src/api/endpoints/i/signin_history.js b/src/api/endpoints/i/signin_history.js deleted file mode 100644 index ede821e3cf..0000000000 --- a/src/api/endpoints/i/signin_history.js +++ /dev/null @@ -1,70 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Signin from '../../models/signin'; -import serialize from '../../serializers/signin'; - -/** - * Get signin history of my account - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - const query = { - user_id: user._id - }; - - const sort = { - _id: -1 - }; - - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const history = await Signin - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(history.map(async record => - await serialize(record)))); -}); diff --git a/src/api/endpoints/i/signin_history.ts b/src/api/endpoints/i/signin_history.ts new file mode 100644 index 0000000000..db36438bfe --- /dev/null +++ b/src/api/endpoints/i/signin_history.ts @@ -0,0 +1,66 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import Signin from '../../models/signin'; +import serialize from '../../serializers/signin'; + +/** + * Get signin history of my account + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + const query = { + user_id: user._id + } as any; + + const sort = { + _id: -1 + }; + + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const history = await Signin + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(history.map(async record => + await serialize(record)))); +}); diff --git a/src/api/endpoints/i/update.js b/src/api/endpoints/i/update.js deleted file mode 100644 index 4abb4fcb7b..0000000000 --- a/src/api/endpoints/i/update.js +++ /dev/null @@ -1,119 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import { isValidName, isValidBirthday } from '../../models/user'; -import serialize from '../../serializers/user'; -import event from '../../event'; -import config from '../../../conf'; - -/** - * Update myself - * - * @param {any} params - * @param {any} user - * @param {any} _ - * @param {boolean} isSecure - * @return {Promise} - */ -module.exports = async (params, user, _, isSecure) => - new Promise(async (res, rej) => -{ - // Get 'name' parameter - const name = params.name; - if (name !== undefined && name !== null) { - if (typeof name != 'string') { - return rej('name must be a string'); - } - - if (!isValidName(name)) { - return rej('invalid name'); - } - - user.name = name; - } - - // Get 'description' parameter - const description = params.description; - if (description !== undefined && description !== null) { - if (description.length > 500) { - return rej('too long description'); - } - - user.description = description; - } - - // Get 'location' parameter - const location = params.location; - if (location !== undefined && location !== null) { - if (location.length > 50) { - return rej('too long location'); - } - - user.profile.location = location; - } - - // Get 'birthday' parameter - const birthday = params.birthday; - if (birthday != null) { - if (!isValidBirthday(birthday)) { - return rej('invalid birthday'); - } - - user.profile.birthday = birthday; - } else { - user.profile.birthday = null; - } - - // Get 'avatar_id' parameter - const avatar = params.avatar_id; - if (avatar !== undefined && avatar !== null) { - user.avatar_id = new mongo.ObjectID(avatar); - } - - // Get 'banner_id' parameter - const banner = params.banner_id; - if (banner !== undefined && banner !== null) { - user.banner_id = new mongo.ObjectID(banner); - } - - await User.update(user._id, { - $set: { - name: user.name, - description: user.description, - avatar_id: user.avatar_id, - banner_id: user.banner_id, - profile: user.profile - } - }); - - // Serialize - const iObj = await serialize(user, user, { - detail: true, - includeSecrets: isSecure - }); - - // Send response - res(iObj); - - // Publish i updated event - event(user._id, 'i_updated', iObj); - - // Update search index - if (config.elasticsearch.enable) { - const es = require('../../../db/elasticsearch'); - - es.index({ - index: 'misskey', - type: 'user', - id: user._id.toString(), - body: { - name: user.name, - bio: user.bio - } - }); - } -}); diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts new file mode 100644 index 0000000000..1e46315ceb --- /dev/null +++ b/src/api/endpoints/i/update.ts @@ -0,0 +1,110 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import { isValidName, isValidBirthday } from '../../models/user'; +import serialize from '../../serializers/user'; +import event from '../../event'; +import config from '../../../conf'; + +/** + * Update myself + * + * @param {any} params + * @param {any} user + * @param {any} _ + * @param {boolean} isSecure + * @return {Promise} + */ +module.exports = async (params, user, _, isSecure) => + new Promise(async (res, rej) => +{ + // Get 'name' parameter + const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed(); + if (nameErr) return rej('invalid name param'); + user.name = name; + + // Get 'description' parameter + const description = params.description; + if (description !== undefined && description !== null) { + if (description.length > 500) { + return rej('too long description'); + } + + user.description = description; + } + + // Get 'location' parameter + const location = params.location; + if (location !== undefined && location !== null) { + if (location.length > 50) { + return rej('too long location'); + } + + user.profile.location = location; + } + + // Get 'birthday' parameter + const birthday = params.birthday; + if (birthday != null) { + if (!isValidBirthday(birthday)) { + return rej('invalid birthday'); + } + + user.profile.birthday = birthday; + } else { + user.profile.birthday = null; + } + + // Get 'avatar_id' parameter + const avatar = params.avatar_id; + if (avatar !== undefined && avatar !== null) { + user.avatar_id = new mongo.ObjectID(avatar); + } + + // Get 'banner_id' parameter + const banner = params.banner_id; + if (banner !== undefined && banner !== null) { + user.banner_id = new mongo.ObjectID(banner); + } + + await User.update(user._id, { + $set: { + name: user.name, + description: user.description, + avatar_id: user.avatar_id, + banner_id: user.banner_id, + profile: user.profile + } + }); + + // Serialize + const iObj = await serialize(user, user, { + detail: true, + includeSecrets: isSecure + }); + + // Send response + res(iObj); + + // Publish i updated event + event(user._id, 'i_updated', iObj); + + // Update search index + if (config.elasticsearch.enable) { + const es = require('../../../db/elasticsearch'); + + es.index({ + index: 'misskey', + type: 'user', + id: user._id.toString(), + body: { + name: user.name, + bio: user.bio + } + }); + } +}); diff --git a/src/api/endpoints/posts/create.ts b/src/api/endpoints/posts/create.ts index 686c3a67d0..3dc121305c 100644 --- a/src/api/endpoints/posts/create.ts +++ b/src/api/endpoints/posts/create.ts @@ -131,21 +131,18 @@ module.exports = (params, user, app) => let poll = null; if (_poll !== null) { - const [pollChoices, pollChoicesErr] = it(params.poll, 'set', false, [ - choices => { - const shouldReject = choices.some(choice => { + const [pollChoices, pollChoicesErr] = + it(params.poll).expect.array() + .unique() + .allString() + .range(1, 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; - }); - return shouldReject ? new Error('invalid poll choices') : true; - }, - // 選択肢がひとつならエラー - choices => choices.length == 1 ? new Error('poll choices must be ひとつ以上') : true, - // 選択肢が多すぎてもエラー - choices => choices.length > 10 ? new Error('many poll choices') : true, - ]); + })) + .qed(); if (pollChoicesErr) return rej('invalid poll choices'); _poll.choices = pollChoices.map((choice, i) => ({ diff --git a/src/api/it.ts b/src/api/it.ts index 039f879957..b08612c709 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -48,23 +48,27 @@ class QueryCore implements Query { value: any; error: Error; - constructor() { - this.value = null; + constructor(value: any) { + this.value = value; this.error = null; } + get isEmpty() { + return this.value === undefined || this.value === null; + } + /** - * このインスタンスの値が null、またはエラーが存在しているなどして、処理をスキップするべきか否か + * このインスタンスの値が空、またはエラーが存在しているなどして、処理をスキップするべきか否か */ get shouldSkip() { - return this.error !== null || this.value === null; + return this.error !== null || this.isEmpty; } /** - * このインスタンスの値が undefined または null の場合エラーにします + * このインスタンスの値が空の場合エラーにします */ required() { - if (this.error === null && this.value === null) { + if (this.error === null && this.isEmpty) { this.error = new Error('required'); } return this; @@ -74,7 +78,7 @@ class QueryCore implements Query { * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ default(value: any) { - if (this.value === null) { + if (this.isEmpty) { this.value = value; } return this; @@ -109,13 +113,9 @@ class BooleanQuery extends QueryCore { error: Error; constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'boolean') { + super(value); + if (!this.isEmpty && typeof value != 'boolean') { this.error = new Error('must-be-a-boolean'); - } else { - this.value = value; } } @@ -155,13 +155,9 @@ class NumberQuery extends QueryCore { error: Error; constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (!Number.isFinite(value)) { + super(value); + if (!this.isEmpty && !Number.isFinite(value)) { this.error = new Error('must-be-a-number'); - } else { - this.value = value; } } @@ -238,13 +234,9 @@ class StringQuery extends QueryCore { error: Error; constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'string') { + super(value); + if (!this.isEmpty && typeof value != 'string') { this.error = new Error('must-be-a-string'); - } else { - this.value = value; } } @@ -327,13 +319,9 @@ class ArrayQuery extends QueryCore { error: Error; constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (!Array.isArray(value)) { + super(value); + if (!this.isEmpty && !Array.isArray(value)) { this.error = new Error('must-be-an-array'); - } else { - this.value = value; } } @@ -361,6 +349,18 @@ class ArrayQuery extends QueryCore { return this; } + /** + * このインスタンスの配列内の要素すべてが文字列であるか検証します + * ひとつでも文字列以外の要素が存在する場合エラーにします + */ + allString() { + if (this.shouldSkip) return this; + if (this.value.some(x => typeof x != 'string')) { + this.error = new Error('dirty-array'); + } + return this; + } + /** * このインスタンスの値が undefined または null の場合エラーにします */ @@ -397,13 +397,9 @@ class IdQuery extends QueryCore { error: Error; constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) { + super(value); + if (!this.isEmpty && (typeof value != 'string' || !mongo.ObjectID.isValid(value))) { this.error = new Error('must-be-an-id'); - } else { - this.value = new mongo.ObjectID(value); } } @@ -443,13 +439,9 @@ class ObjectQuery extends QueryCore { error: Error; constructor(value) { - super(); - if (value === undefined || value === null) { - this.value = null; - } else if (typeof value != 'object') { + super(value); + if (!this.isEmpty && typeof value != 'object') { this.error = new Error('must-be-an-object'); - } else { - this.value = value; } } -- cgit v1.2.3-freya From 665bcda0a7cbf37d5691d48838da85e2f308babc Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 09:15:38 +0900 Subject: wip --- src/api/endpoints/i/update.ts | 4 +-- src/api/it.ts | 78 ++++++++++++++++++------------------------- 2 files changed, 34 insertions(+), 48 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts index 1e46315ceb..aeda0a4527 100644 --- a/src/api/endpoints/i/update.ts +++ b/src/api/endpoints/i/update.ts @@ -23,9 +23,9 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed(); + const [name, nameErr] = it(params.name).expect.string().notNull().validate(isValidName).qed(); if (nameErr) return rej('invalid name param'); - user.name = name; + if (name) user.name = name; // Get 'description' parameter const description = params.description; diff --git a/src/api/it.ts b/src/api/it.ts index b08612c709..14f6063b24 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -53,8 +53,16 @@ class QueryCore implements Query { this.error = null; } + get isUndefined() { + return this.value === undefined; + } + + get isNull() { + return this.value === null; + } + get isEmpty() { - return this.value === undefined || this.value === null; + return this.isUndefined || this.isNull; } /** @@ -65,7 +73,7 @@ class QueryCore implements Query { } /** - * このインスタンスの値が空の場合エラーにします + * このインスタンスの値が空のときにエラーにします */ required() { if (this.error === null && this.isEmpty) { @@ -75,10 +83,30 @@ class QueryCore implements Query { } /** - * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します + * このインスタンスの値が設定されていない(=undefined)場合エラーにします + */ + notUndefined() { + if (this.error === null && this.isUndefined) { + this.error = new Error('required'); + } + return this; + } + + /** + * このインスタンスの値が null のときにエラーにします + */ + notNull() { + if (this.error === null && this.isNull) { + this.error = new Error('required'); + } + return this; + } + + /** + * このインスタンスの値が設定されていない(=undefined)ときにデフォルトで設定する値を設定します */ default(value: any) { - if (this.isEmpty) { + if (this.isUndefined) { this.value = value; } return this; @@ -119,13 +147,6 @@ class BooleanQuery extends QueryCore { } } - /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ @@ -198,13 +219,6 @@ class NumberQuery extends QueryCore { return this; } - /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ @@ -259,13 +273,6 @@ class StringQuery extends QueryCore { return this; } - /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ @@ -361,13 +368,6 @@ class ArrayQuery extends QueryCore { return this; } - /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ @@ -403,13 +403,6 @@ class IdQuery extends QueryCore { } } - /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ @@ -445,13 +438,6 @@ class ObjectQuery extends QueryCore { } } - /** - * このインスタンスの値が undefined または null の場合エラーにします - */ - required() { - return super.required(); - } - /** * このインスタンスの値が設定されていないときにデフォルトで設定する値を設定します */ -- cgit v1.2.3-freya From 42ed35bfc95147c89cb0dc3d61d8982b2646c1ca Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 09:24:17 +0900 Subject: wip --- src/api/endpoints/i/update.ts | 51 ++++++++++++++----------------------------- src/api/models/user.ts | 8 +++++++ 2 files changed, 24 insertions(+), 35 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts index aeda0a4527..e3b307b0b9 100644 --- a/src/api/endpoints/i/update.ts +++ b/src/api/endpoints/i/update.ts @@ -5,7 +5,7 @@ */ import it from '../../it'; import User from '../../models/user'; -import { isValidName, isValidBirthday } from '../../models/user'; +import { isValidName, isValidDescription, isValidLocation, isValidBirthday } from '../../models/user'; import serialize from '../../serializers/user'; import event from '../../event'; import config from '../../../conf'; @@ -28,48 +28,29 @@ module.exports = async (params, user, _, isSecure) => if (name) user.name = name; // Get 'description' parameter - const description = params.description; - if (description !== undefined && description !== null) { - if (description.length > 500) { - return rej('too long description'); - } - - user.description = description; - } + const [description, descriptionErr] = it(params.description).expect.string().validate(isValidDescription).qed(); + if (descriptionErr) return rej('invalid description param'); + if (description !== undefined) user.description = description; // Get 'location' parameter - const location = params.location; - if (location !== undefined && location !== null) { - if (location.length > 50) { - return rej('too long location'); - } - - user.profile.location = location; - } + const [location, locationErr] = it(params.location).expect.string().validate(isValidLocation).qed(); + if (locationErr) return rej('invalid location param'); + if (location !== undefined) user.location = location; // Get 'birthday' parameter - const birthday = params.birthday; - if (birthday != null) { - if (!isValidBirthday(birthday)) { - return rej('invalid birthday'); - } - - user.profile.birthday = birthday; - } else { - user.profile.birthday = null; - } + const [birthday, birthdayErr] = it(params.birthday).expect.string().validate(isValidBirthday).qed(); + if (birthdayErr) return rej('invalid birthday param'); + if (birthday !== undefined) user.birthday = birthday; // Get 'avatar_id' parameter - const avatar = params.avatar_id; - if (avatar !== undefined && avatar !== null) { - user.avatar_id = new mongo.ObjectID(avatar); - } + const [avatarId, avatarIdErr] = it(params.avatar_id).expect.id().notNull().qed(); + if (avatarIdErr) return rej('invalid avatar_id param'); + if (avatarId) user.avatar_id = avatarId; // Get 'banner_id' parameter - const banner = params.banner_id; - if (banner !== undefined && banner !== null) { - user.banner_id = new mongo.ObjectID(banner); - } + const [bannerId, bannerIdErr] = it(params.banner_id).expect.id().notNull().qed(); + if (bannerIdErr) return rej('invalid banner_id param'); + if (bannerId) user.banner_id = bannerId; await User.update(user._id, { $set: { diff --git a/src/api/models/user.ts b/src/api/models/user.ts index 5ab39d7c92..cd16459891 100644 --- a/src/api/models/user.ts +++ b/src/api/models/user.ts @@ -19,6 +19,14 @@ export function isValidName(name: string): boolean { return typeof name == 'string' && name.length < 30 && name.trim() != ''; } +export function isValidDescription(description: string): boolean { + return typeof description == 'string' && description.length < 500 && description.trim() != ''; +} + +export function isValidLocation(location: string): boolean { + return typeof location == 'string' && location.length < 50 && location.trim() != ''; +} + export function isValidBirthday(birthday: string): boolean { return typeof birthday == 'string' && /^([0-9]{4})\-([0-9]{2})-([0-9]{2})$/.test(birthday); } -- cgit v1.2.3-freya From 1e1800fd666fce3f06712c005bd63503672cb346 Mon Sep 17 00:00:00 2001 From: syuilo⭐️ Date: Fri, 3 Mar 2017 09:48:06 +0900 Subject: Update it.ts --- src/api/it.ts | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/api') diff --git a/src/api/it.ts b/src/api/it.ts index 14f6063b24..21cc872a2f 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -26,6 +26,30 @@ * const [val, err] = it(x).expect.string().required().qed(); */ +/** + * null と undefined の扱い + * + * 「値がnullまたはundefined」な状態を「値が空である」と表現しています。 + * 値が空である場合、バリデータやその他の処理メソッドは呼ばれません。 + * + * 内部的にはnullとundefinedを次のように区別しています: + * null ... 値が「無い」と明示されている + * undefined ... 値を指定していない + * + * 例えばアカウントのプロフィールを更新するAPIに次のデータを含むリクエストが来たとします: + * { name: 'Alice' } + * アカウントには本来、他にも birthday といったフィールドがありますが、 + * このリクエストではそれに触れず、ただ単に name フィールドを更新することを要求しています。 + * ここで、このリクエストにおける birthday フィールドは undefined なわけですが、 + * それはnull(=birthdayを未設定にしたい)とは違うものです。 + * undefined も null も区別しないとしたら、触れていないフィールドまでリセットされることになってしまいます。 + * ですので、undefined と null は区別しています。 + * + * 値が空であってほしくない場合は .require() を、 + * 値を必ず指定しなければならない場合(値を「無し」に指定することは許可)は .notUndefined() を、 + * 値の指定をしなくてもいいけど、する場合は「無し」は許可しない場合は .notNull() を使えます。 + */ + import * as mongo from 'mongodb'; import hasDuplicates from '../common/has-duplicates'; -- cgit v1.2.3-freya From 3ca7a442aba27f8ccf374c9f68c1082da0b5a943 Mon Sep 17 00:00:00 2001 From: syuilo⭐️ Date: Fri, 3 Mar 2017 09:56:58 +0900 Subject: Update it.ts --- src/api/it.ts | 2 ++ 1 file changed, 2 insertions(+) (limited to 'src/api') diff --git a/src/api/it.ts b/src/api/it.ts index 21cc872a2f..f540172ad3 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -15,6 +15,8 @@ * const [val, err] = it(x).must.be.an.array().unique().required().validate(x => x[0] != 'strawberry pasta').qed(); * → xは配列でなければならず、かつ中身が重複していてはならない。この値を省略することはできない。そして配列の最初の要素が'strawberry pasta'という文字列であってはならない。 * + * ・意味的に矛盾するので、required と default は併用できません。 + * * ~糖衣構文~ * const [val, err] = it(x).must.be.a.string().required().qed(); * は -- cgit v1.2.3-freya From e0c44abd4a0b034474badeadf4d03b81d8a59ed9 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 10:24:56 +0900 Subject: いい感じに MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/endpoints/i/update.ts | 8 ++--- src/api/it.ts | 71 ++++++++++++++++++++++++++++++++----------- 2 files changed, 58 insertions(+), 21 deletions(-) (limited to 'src/api') diff --git a/src/api/endpoints/i/update.ts b/src/api/endpoints/i/update.ts index e3b307b0b9..a5f1538610 100644 --- a/src/api/endpoints/i/update.ts +++ b/src/api/endpoints/i/update.ts @@ -23,22 +23,22 @@ module.exports = async (params, user, _, isSecure) => new Promise(async (res, rej) => { // Get 'name' parameter - const [name, nameErr] = it(params.name).expect.string().notNull().validate(isValidName).qed(); + const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed(); if (nameErr) return rej('invalid name param'); if (name) user.name = name; // Get 'description' parameter - const [description, descriptionErr] = it(params.description).expect.string().validate(isValidDescription).qed(); + const [description, descriptionErr] = it(params.description).expect.nullable.string().validate(isValidDescription).qed(); if (descriptionErr) return rej('invalid description param'); if (description !== undefined) user.description = description; // Get 'location' parameter - const [location, locationErr] = it(params.location).expect.string().validate(isValidLocation).qed(); + const [location, locationErr] = it(params.location).expect.nullable.string().validate(isValidLocation).qed(); if (locationErr) return rej('invalid location param'); if (location !== undefined) user.location = location; // Get 'birthday' parameter - const [birthday, birthdayErr] = it(params.birthday).expect.string().validate(isValidBirthday).qed(); + const [birthday, birthdayErr] = it(params.birthday).expect.nullable.string().validate(isValidBirthday).qed(); if (birthdayErr) return rej('invalid birthday param'); if (birthday !== undefined) user.birthday = birthday; diff --git a/src/api/it.ts b/src/api/it.ts index f540172ad3..845a56365c 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -74,9 +74,14 @@ class QueryCore implements Query { value: any; error: Error; - constructor(value: any) { - this.value = value; - this.error = null; + constructor(value: any, nullable: boolean = false) { + if (value === null && !nullable) { + this.value = undefined; + this.error = new Error('must-be-not-a-null'); + } else { + this.value = value; + this.error = null; + } } get isUndefined() { @@ -166,8 +171,8 @@ class BooleanQuery extends QueryCore { value: boolean; error: Error; - constructor(value) { - super(value); + constructor(value: any, nullable: boolean = false) { + super(value, nullable); if (!this.isEmpty && typeof value != 'boolean') { this.error = new Error('must-be-a-boolean'); } @@ -201,8 +206,8 @@ class NumberQuery extends QueryCore { value: number; error: Error; - constructor(value) { - super(value); + constructor(value: any, nullable: boolean = false) { + super(value, nullable); if (!this.isEmpty && !Number.isFinite(value)) { this.error = new Error('must-be-a-number'); } @@ -273,8 +278,8 @@ class StringQuery extends QueryCore { value: string; error: Error; - constructor(value) { - super(value); + constructor(value: any, nullable: boolean = false) { + super(value, nullable); if (!this.isEmpty && typeof value != 'string') { this.error = new Error('must-be-a-string'); } @@ -351,8 +356,8 @@ class ArrayQuery extends QueryCore { value: any[]; error: Error; - constructor(value) { - super(value); + constructor(value: any, nullable: boolean = false) { + super(value, nullable); if (!this.isEmpty && !Array.isArray(value)) { this.error = new Error('must-be-an-array'); } @@ -422,8 +427,8 @@ class IdQuery extends QueryCore { value: mongo.ObjectID; error: Error; - constructor(value) { - super(value); + constructor(value: any, nullable: boolean = false) { + super(value, nullable); if (!this.isEmpty && (typeof value != 'string' || !mongo.ObjectID.isValid(value))) { this.error = new Error('must-be-an-id'); } @@ -457,8 +462,8 @@ class ObjectQuery extends QueryCore { value: any; error: Error; - constructor(value) { - super(value); + constructor(value: any, nullable: boolean = false) { + super(value, nullable); if (!this.isEmpty && typeof value != 'object') { this.error = new Error('must-be-an-object'); } @@ -495,6 +500,14 @@ type It = { string: () => StringQuery; number: () => NumberQuery; boolean: () => BooleanQuery; + nullable: { + string: () => StringQuery; + number: () => NumberQuery; + boolean: () => BooleanQuery; + id: () => IdQuery; + array: () => ArrayQuery; + object: () => ObjectQuery; + }; }; an: { id: () => IdQuery; @@ -510,6 +523,14 @@ type It = { id: () => IdQuery; array: () => ArrayQuery; object: () => ObjectQuery; + nullable: { + string: () => StringQuery; + number: () => NumberQuery; + boolean: () => BooleanQuery; + id: () => IdQuery; + array: () => ArrayQuery; + object: () => ObjectQuery; + }; }; }; @@ -519,7 +540,15 @@ const it = (value: any) => ({ a: { string: () => new StringQuery(value), number: () => new NumberQuery(value), - boolean: () => new BooleanQuery(value) + boolean: () => new BooleanQuery(value), + nullable: { + string: () => new StringQuery(value, true), + number: () => new NumberQuery(value, true), + boolean: () => new BooleanQuery(value, true), + id: () => new IdQuery(value, true), + array: () => new ArrayQuery(value, true), + object: () => new ObjectQuery(value, true) + } }, an: { id: () => new IdQuery(value), @@ -534,7 +563,15 @@ const it = (value: any) => ({ boolean: () => new BooleanQuery(value), id: () => new IdQuery(value), array: () => new ArrayQuery(value), - object: () => new ObjectQuery(value) + object: () => new ObjectQuery(value), + nullable: { + string: () => new StringQuery(value, true), + number: () => new NumberQuery(value, true), + boolean: () => new BooleanQuery(value, true), + id: () => new IdQuery(value, true), + array: () => new ArrayQuery(value, true), + object: () => new ObjectQuery(value, true) + } } }); -- cgit v1.2.3-freya From 73ac13a274ec2eac8f909a84c23296836d833430 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 10:31:59 +0900 Subject: いい感じに MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/it.ts | 32 ++++++-------------------------- 1 file changed, 6 insertions(+), 26 deletions(-) (limited to 'src/api') diff --git a/src/api/it.ts b/src/api/it.ts index 845a56365c..df49985833 100644 --- a/src/api/it.ts +++ b/src/api/it.ts @@ -31,10 +31,10 @@ /** * null と undefined の扱い * - * 「値がnullまたはundefined」な状態を「値が空である」と表現しています。 + * 「値が null または undefined」な状態を「値が空である」と表現しています。 * 値が空である場合、バリデータやその他の処理メソッドは呼ばれません。 * - * 内部的にはnullとundefinedを次のように区別しています: + * 内部的には null と undefined を次のように区別しています: * null ... 値が「無い」と明示されている * undefined ... 値を指定していない * @@ -47,9 +47,9 @@ * undefined も null も区別しないとしたら、触れていないフィールドまでリセットされることになってしまいます。 * ですので、undefined と null は区別しています。 * - * 値が空であってほしくない場合は .require() を、 - * 値を必ず指定しなければならない場合(値を「無し」に指定することは許可)は .notUndefined() を、 - * 値の指定をしなくてもいいけど、する場合は「無し」は許可しない場合は .notNull() を使えます。 + * 明示的に null を許可しない限り、null はエラーになります。 + * null を許可する場合は nullable をプリフィックスします: + * const [val, err] = it(x).must.be.a.nullable.string().required().qed(); */ import * as mongo from 'mongodb'; @@ -104,35 +104,15 @@ class QueryCore implements Query { } /** - * このインスタンスの値が空のときにエラーにします + * このインスタンスの値が指定されていない(=undefined)ときにエラーにします */ required() { - if (this.error === null && this.isEmpty) { - this.error = new Error('required'); - } - return this; - } - - /** - * このインスタンスの値が設定されていない(=undefined)場合エラーにします - */ - notUndefined() { if (this.error === null && this.isUndefined) { this.error = new Error('required'); } return this; } - /** - * このインスタンスの値が null のときにエラーにします - */ - notNull() { - if (this.error === null && this.isNull) { - this.error = new Error('required'); - } - return this; - } - /** * このインスタンスの値が設定されていない(=undefined)ときにデフォルトで設定する値を設定します */ -- cgit v1.2.3-freya From e2461a9314026d95d5baaea864c5282eed7c5504 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 19:33:14 +0900 Subject: wip --- src/api/endpoints/drive/files.js | 82 --------------- src/api/endpoints/drive/files.ts | 74 ++++++++++++++ src/api/endpoints/drive/files/create.js | 63 ------------ src/api/endpoints/drive/files/create.ts | 57 +++++++++++ src/api/endpoints/drive/files/find.js | 49 --------- src/api/endpoints/drive/files/find.ts | 43 ++++++++ src/api/endpoints/drive/files/show.js | 44 -------- src/api/endpoints/drive/files/show.ts | 43 ++++++++ src/api/endpoints/drive/files/update.js | 101 ------------------ src/api/endpoints/drive/files/update.ts | 85 ++++++++++++++++ src/api/endpoints/drive/files/upload_from_url.js | 56 ---------- src/api/endpoints/drive/files/upload_from_url.ts | 48 +++++++++ src/api/endpoints/drive/folders.js | 79 --------------- src/api/endpoints/drive/folders.ts | 71 +++++++++++++ src/api/endpoints/drive/folders/create.js | 77 -------------- src/api/endpoints/drive/folders/create.ts | 61 +++++++++++ src/api/endpoints/drive/folders/find.js | 45 -------- src/api/endpoints/drive/folders/find.ts | 39 +++++++ src/api/endpoints/drive/folders/show.js | 41 -------- src/api/endpoints/drive/folders/show.ts | 39 +++++++ src/api/endpoints/drive/folders/update.js | 124 ----------------------- src/api/endpoints/drive/folders/update.ts | 105 +++++++++++++++++++ src/api/endpoints/drive/stream.js | 85 ---------------- src/api/endpoints/drive/stream.ts | 75 ++++++++++++++ src/api/endpoints/following/create.js | 96 ------------------ src/api/endpoints/following/create.ts | 89 ++++++++++++++++ src/api/endpoints/following/delete.js | 93 ----------------- src/api/endpoints/following/delete.ts | 86 ++++++++++++++++ 28 files changed, 915 insertions(+), 1035 deletions(-) delete mode 100644 src/api/endpoints/drive/files.js create mode 100644 src/api/endpoints/drive/files.ts delete mode 100644 src/api/endpoints/drive/files/create.js create mode 100644 src/api/endpoints/drive/files/create.ts delete mode 100644 src/api/endpoints/drive/files/find.js create mode 100644 src/api/endpoints/drive/files/find.ts delete mode 100644 src/api/endpoints/drive/files/show.js create mode 100644 src/api/endpoints/drive/files/show.ts delete mode 100644 src/api/endpoints/drive/files/update.js create mode 100644 src/api/endpoints/drive/files/update.ts delete mode 100644 src/api/endpoints/drive/files/upload_from_url.js create mode 100644 src/api/endpoints/drive/files/upload_from_url.ts delete mode 100644 src/api/endpoints/drive/folders.js create mode 100644 src/api/endpoints/drive/folders.ts delete mode 100644 src/api/endpoints/drive/folders/create.js create mode 100644 src/api/endpoints/drive/folders/create.ts delete mode 100644 src/api/endpoints/drive/folders/find.js create mode 100644 src/api/endpoints/drive/folders/find.ts delete mode 100644 src/api/endpoints/drive/folders/show.js create mode 100644 src/api/endpoints/drive/folders/show.ts delete mode 100644 src/api/endpoints/drive/folders/update.js create mode 100644 src/api/endpoints/drive/folders/update.ts delete mode 100644 src/api/endpoints/drive/stream.js create mode 100644 src/api/endpoints/drive/stream.ts delete mode 100644 src/api/endpoints/following/create.js create mode 100644 src/api/endpoints/following/create.ts delete mode 100644 src/api/endpoints/following/delete.js create mode 100644 src/api/endpoints/following/delete.ts (limited to 'src/api') diff --git a/src/api/endpoints/drive/files.js b/src/api/endpoints/drive/files.js deleted file mode 100644 index cbfe720266..0000000000 --- a/src/api/endpoints/drive/files.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFile from '../../models/drive-file'; -import serialize from '../../serializers/drive-file'; - -/** - * Get drive files - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise} - */ -module.exports = (params, user, app) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // Get 'folder_id' parameter - let folder = params.folder_id; - if (folder === undefined || folder === null) { - folder = null; - } else { - folder = new mongo.ObjectID(folder); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = { - user_id: user._id, - folder_id: folder - }; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const files = await DriveFile - .find(query, { - fields: { - data: false - }, - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(files.map(async file => - await serialize(file)))); -}); diff --git a/src/api/endpoints/drive/files.ts b/src/api/endpoints/drive/files.ts new file mode 100644 index 0000000000..c1441c554b --- /dev/null +++ b/src/api/endpoints/drive/files.ts @@ -0,0 +1,74 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import DriveFile from '../../models/drive-file'; +import serialize from '../../serializers/drive-file'; + +/** + * Get drive files + * + * @param {any} params + * @param {any} user + * @param {any} app + * @return {Promise} + */ +module.exports = (params, user, app) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + // Construct query + const sort = { + _id: -1 + }; + const query = { + user_id: user._id, + folder_id: folderId + } as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const files = await DriveFile + .find(query, { + fields: { + data: false + }, + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(files.map(async file => + await serialize(file)))); +}); diff --git a/src/api/endpoints/drive/files/create.js b/src/api/endpoints/drive/files/create.js deleted file mode 100644 index 9690b05cfd..0000000000 --- a/src/api/endpoints/drive/files/create.js +++ /dev/null @@ -1,63 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as fs from 'fs'; -import * as mongo from 'mongodb'; -import File from '../../../models/drive-file'; -import { validateFileName } from '../../../models/drive-file'; -import User from '../../../models/user'; -import serialize from '../../../serializers/drive-file'; -import create from '../../../common/add-file-to-drive'; - -/** - * Create a file - * - * @param {any} file - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (file, params, user) => - new Promise(async (res, rej) => -{ - if (file == null) { - return rej('file is required'); - } - - const buffer = fs.readFileSync(file.path); - fs.unlink(file.path, (err) => { if (err) console.log(err) }); - - // Get 'name' parameter - let name = file.originalname; - if (name !== undefined && name !== null) { - name = name.trim(); - if (name.length === 0) { - name = null; - } else if (name === 'blob') { - name = null; - } else if (!validateFileName(name)) { - return rej('invalid name'); - } - } else { - name = null; - } - - // Get 'folder_id' parameter - let folder = params.folder_id; - if (folder === undefined || folder === null) { - folder = null; - } else { - folder = new mongo.ObjectID(folder); - } - - // Create file - const driveFile = await create(user, buffer, name, null, folder); - - // Serialize - const fileObj = await serialize(driveFile); - - // Response - res(fileObj); -}); diff --git a/src/api/endpoints/drive/files/create.ts b/src/api/endpoints/drive/files/create.ts new file mode 100644 index 0000000000..7efd149813 --- /dev/null +++ b/src/api/endpoints/drive/files/create.ts @@ -0,0 +1,57 @@ +'use strict'; + +/** + * Module dependencies + */ +import * as fs from 'fs'; +import it from '../../../it'; +import { validateFileName } from '../../../models/drive-file'; +import serialize from '../../../serializers/drive-file'; +import create from '../../../common/add-file-to-drive'; + +/** + * Create a file + * + * @param {any} file + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (file, params, user) => + new Promise(async (res, rej) => +{ + if (file == null) { + return rej('file is required'); + } + + const buffer = fs.readFileSync(file.path); + fs.unlink(file.path, (err) => { if (err) console.log(err) }); + + // Get 'name' parameter + let name = file.originalname; + if (name !== undefined && name !== null) { + name = name.trim(); + if (name.length === 0) { + name = null; + } else if (name === 'blob') { + name = null; + } else if (!validateFileName(name)) { + return rej('invalid name'); + } + } else { + name = null; + } + + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + // Create file + const driveFile = await create(user, buffer, name, null, folderId); + + // Serialize + const fileObj = await serialize(driveFile); + + // Response + res(fileObj); +}); diff --git a/src/api/endpoints/drive/files/find.js b/src/api/endpoints/drive/files/find.js deleted file mode 100644 index 358767c5ed..0000000000 --- a/src/api/endpoints/drive/files/find.js +++ /dev/null @@ -1,49 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFile from '../../../models/drive-file'; -import serialize from '../../../serializers/drive-file'; - -/** - * Find a file(s) - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'name' parameter - const name = params.name; - if (name === undefined || name === null) { - return rej('name is required'); - } - - // Get 'folder_id' parameter - let folder = params.folder_id; - if (folder === undefined || folder === null) { - folder = null; - } else { - folder = new mongo.ObjectID(folder); - } - - // Issue query - const files = await DriveFile - .find({ - name: name, - user_id: user._id, - folder_id: folder - }, { - fields: { - data: false - } - }); - - // Serialize - res(await Promise.all(files.map(async file => - await serialize(file)))); -}); diff --git a/src/api/endpoints/drive/files/find.ts b/src/api/endpoints/drive/files/find.ts new file mode 100644 index 0000000000..393b8c5b98 --- /dev/null +++ b/src/api/endpoints/drive/files/find.ts @@ -0,0 +1,43 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import DriveFile from '../../../models/drive-file'; +import serialize from '../../../serializers/drive-file'; + +/** + * Find a file(s) + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'name' parameter + const [name, nameErr] = it(params.name).expect.string().required().qed(); + if (nameErr) return rej('invalid name param'); + + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + // Issue query + const files = await DriveFile + .find({ + name: name, + user_id: user._id, + folder_id: folderId + }, { + fields: { + data: false + } + }); + + // Serialize + res(await Promise.all(files.map(async file => + await serialize(file)))); +}); diff --git a/src/api/endpoints/drive/files/show.js b/src/api/endpoints/drive/files/show.js deleted file mode 100644 index 5ae98a4a7c..0000000000 --- a/src/api/endpoints/drive/files/show.js +++ /dev/null @@ -1,44 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFile from '../../../models/drive-file'; -import serialize from '../../../serializers/drive-file'; - -/** - * Show a file - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'file_id' parameter - const fileId = params.file_id; - if (fileId === undefined || fileId === null) { - return rej('file_id is required'); - } - - const file = await DriveFile - .findOne({ - _id: new mongo.ObjectID(fileId), - user_id: user._id - }, { - fields: { - data: false - } - }); - - if (file === null) { - return rej('file-not-found'); - } - - // Serialize - res(await serialize(file, { - detail: true - })); -}); diff --git a/src/api/endpoints/drive/files/show.ts b/src/api/endpoints/drive/files/show.ts new file mode 100644 index 0000000000..2024a56ca6 --- /dev/null +++ b/src/api/endpoints/drive/files/show.ts @@ -0,0 +1,43 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import DriveFile from '../../../models/drive-file'; +import serialize from '../../../serializers/drive-file'; + +/** + * Show a file + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'file_id' parameter + const [fileId, fileIdErr] = it(params.file_id).expect.id().required().qed(); + if (fileIdErr) return rej('invalid file_id param'); + + // Fetch file + const file = await DriveFile + .findOne({ + _id: fileId, + user_id: user._id + }, { + fields: { + data: false + } + }); + + if (file === null) { + return rej('file-not-found'); + } + + // Serialize + res(await serialize(file, { + detail: true + })); +}); diff --git a/src/api/endpoints/drive/files/update.js b/src/api/endpoints/drive/files/update.js deleted file mode 100644 index 8e2ff33e9d..0000000000 --- a/src/api/endpoints/drive/files/update.js +++ /dev/null @@ -1,101 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFolder from '../../../models/drive-folder'; -import DriveFile from '../../../models/drive-file'; -import { validateFileName } from '../../../models/drive-file'; -import serialize from '../../../serializers/drive-file'; -import event from '../../../event'; - -/** - * Update a file - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'file_id' parameter - const fileId = params.file_id; - if (fileId === undefined || fileId === null) { - return rej('file_id is required'); - } - - // Validate id - if (!mongo.ObjectID.isValid(fileId)) { - return rej('incorrect file_id'); - } - - const file = await DriveFile - .findOne({ - _id: new mongo.ObjectID(fileId), - user_id: user._id - }, { - fields: { - data: false - } - }); - - if (file === null) { - return rej('file-not-found'); - } - - // Get 'name' parameter - let name = params.name; - if (name) { - name = name.trim(); - if (validateFileName(name)) { - file.name = name; - } else { - return rej('invalid file name'); - } - } - - // Get 'folder_id' parameter - let folderId = params.folder_id; - if (folderId !== undefined) { - if (folderId === null) { - file.folder_id = null; - } else { - // Validate id - if (!mongo.ObjectID.isValid(folderId)) { - return rej('incorrect folder_id'); - } - - folderId = new mongo.ObjectID(folderId); - - const folder = await DriveFolder - .findOne({ - _id: folderId, - user_id: user._id - }); - - if (folder === null) { - return rej('folder-not-found'); - } - - file.folder_id = folder._id; - } - } - - DriveFile.update(file._id, { - $set: { - name: file.name, - folder_id: file.folder_id - } - }); - - // Serialize - const fileObj = await serialize(file); - - // Response - res(fileObj); - - // Publish drive_file_updated event - event(user._id, 'drive_file_updated', fileObj); -}); diff --git a/src/api/endpoints/drive/files/update.ts b/src/api/endpoints/drive/files/update.ts new file mode 100644 index 0000000000..595d501658 --- /dev/null +++ b/src/api/endpoints/drive/files/update.ts @@ -0,0 +1,85 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import DriveFolder from '../../../models/drive-folder'; +import DriveFile from '../../../models/drive-file'; +import { validateFileName } from '../../../models/drive-file'; +import serialize from '../../../serializers/drive-file'; +import event from '../../../event'; + +/** + * Update a file + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'file_id' parameter + const [fileId, fileIdErr] = it(params.file_id).expect.id().required().qed(); + if (fileIdErr) return rej('invalid file_id param'); + + // Fetch file + const file = await DriveFile + .findOne({ + _id: fileId, + user_id: user._id + }, { + fields: { + data: false + } + }); + + if (file === null) { + return rej('file-not-found'); + } + + // Get 'name' parameter + const [name, nameErr] = it(params.name).expect.string().validate(validateFileName).qed(); + if (nameErr) return rej('invalid name param'); + if (name) file.name = name; + + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + if (folderId !== undefined) { + if (folderId === null) { + file.folder_id = null; + } else { + // Fetch folder + const folder = await DriveFolder + .findOne({ + _id: folderId, + user_id: user._id + }); + + if (folder === null) { + return rej('folder-not-found'); + } + + file.folder_id = folder._id; + } + } + + DriveFile.update(file._id, { + $set: { + name: file.name, + folder_id: file.folder_id + } + }); + + // Serialize + const fileObj = await serialize(file); + + // Response + res(fileObj); + + // Publish drive_file_updated event + event(user._id, 'drive_file_updated', fileObj); +}); diff --git a/src/api/endpoints/drive/files/upload_from_url.js b/src/api/endpoints/drive/files/upload_from_url.js deleted file mode 100644 index 3619a6f107..0000000000 --- a/src/api/endpoints/drive/files/upload_from_url.js +++ /dev/null @@ -1,56 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as URL from 'url'; -const download = require('download'); -import * as mongo from 'mongodb'; -import File from '../../../models/drive-file'; -import { validateFileName } from '../../../models/drive-file'; -import User from '../../../models/user'; -import serialize from '../../../serializers/drive-file'; -import create from '../../../common/add-file-to-drive'; - -/** - * Create a file from a URL - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'url' parameter - // TODO: Validate this url - const url = params.url; - if (url == null) { - return rej('url is required'); - } - - let name = URL.parse(url).pathname.split('/').pop(); - if (!validateFileName(name)) { - name = null; - } - - // Get 'folder_id' parameter - let folder = params.folder_id; - if (folder === undefined || folder === null) { - folder = null; - } else { - folder = new mongo.ObjectID(folder); - } - - // Download file - const data = await download(url); - - // Create file - const driveFile = await create(user, data, name, null, folder); - - // Serialize - const fileObj = await serialize(driveFile); - - // Response - res(fileObj); -}); diff --git a/src/api/endpoints/drive/files/upload_from_url.ts b/src/api/endpoints/drive/files/upload_from_url.ts new file mode 100644 index 0000000000..b6f4789317 --- /dev/null +++ b/src/api/endpoints/drive/files/upload_from_url.ts @@ -0,0 +1,48 @@ +'use strict'; + +/** + * Module dependencies + */ +import * as URL from 'url'; +const download = require('download'); +import it from '../../../it'; +import { validateFileName } from '../../../models/drive-file'; +import serialize from '../../../serializers/drive-file'; +import create from '../../../common/add-file-to-drive'; + +/** + * Create a file from a URL + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'url' parameter + // TODO: Validate this url + const [url, urlErr] = it(params.url).expect.string().required().qed(); + if (urlErr) return rej('invalid url param'); + + let name = URL.parse(url).pathname.split('/').pop(); + if (!validateFileName(name)) { + name = null; + } + + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + // Download file + const data = await download(url); + + // Create file + const driveFile = await create(user, data, name, null, folderId); + + // Serialize + const fileObj = await serialize(driveFile); + + // Response + res(fileObj); +}); diff --git a/src/api/endpoints/drive/folders.js b/src/api/endpoints/drive/folders.js deleted file mode 100644 index 631d68769f..0000000000 --- a/src/api/endpoints/drive/folders.js +++ /dev/null @@ -1,79 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFolder from '../../models/drive-folder'; -import serialize from '../../serializers/drive-folder'; - -/** - * Get drive folders - * - * @param {any} params - * @param {any} user - * @param {any} app - * @return {Promise} - */ -module.exports = (params, user, app) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // Get 'folder_id' parameter - let folder = params.folder_id; - if (folder === undefined || folder === null) { - folder = null; - } else { - folder = new mongo.ObjectID(folder); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = { - user_id: user._id, - parent_id: folder - }; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - - // Issue query - const folders = await DriveFolder - .find(query, { - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(folders.map(async folder => - await serialize(folder)))); -}); diff --git a/src/api/endpoints/drive/folders.ts b/src/api/endpoints/drive/folders.ts new file mode 100644 index 0000000000..3f4a5bac0e --- /dev/null +++ b/src/api/endpoints/drive/folders.ts @@ -0,0 +1,71 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import DriveFolder from '../../models/drive-folder'; +import serialize from '../../serializers/drive-folder'; + +/** + * Get drive folders + * + * @param {any} params + * @param {any} user + * @param {any} app + * @return {Promise} + */ +module.exports = (params, user, app) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.nullable.id().default(null).qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + // Construct query + const sort = { + _id: -1 + }; + const query = { + user_id: user._id, + parent_id: folderId + } as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + + // Issue query + const folders = await DriveFolder + .find(query, { + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(folders.map(async folder => + await serialize(folder)))); +}); diff --git a/src/api/endpoints/drive/folders/create.js b/src/api/endpoints/drive/folders/create.js deleted file mode 100644 index 9ba989c212..0000000000 --- a/src/api/endpoints/drive/folders/create.js +++ /dev/null @@ -1,77 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFolder from '../../../models/drive-folder'; -import { isValidFolderName } from '../../../models/drive-folder'; -import serialize from '../../../serializers/drive-folder'; -import event from '../../../event'; - -/** - * Create drive folder - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'name' parameter - let name = params.name; - if (name !== undefined && name !== null) { - name = name.trim(); - if (name.length === 0) { - name = null; - } else if (!isValidFolderName(name)) { - return rej('invalid name'); - } - } else { - name = null; - } - - if (name == null) { - name = '無題のフォルダー'; - } - - // Get 'folder_id' parameter - let parentId = params.folder_id; - if (parentId === undefined || parentId === null) { - parentId = null; - } else { - parentId = new mongo.ObjectID(parentId); - } - - // If the parent folder is specified - let parent = null; - if (parentId !== null) { - parent = await DriveFolder - .findOne({ - _id: parentId, - user_id: user._id - }); - - if (parent === null) { - return reject('parent-not-found'); - } - } - - // Create folder - const folder = await DriveFolder.insert({ - created_at: new Date(), - name: name, - parent_id: parent !== null ? parent._id : null, - user_id: user._id - }); - - // Serialize - const folderObj = await serialize(folder); - - // Response - res(folderObj); - - // Publish drive_folder_created event - event(user._id, 'drive_folder_created', folderObj); -}); diff --git a/src/api/endpoints/drive/folders/create.ts b/src/api/endpoints/drive/folders/create.ts new file mode 100644 index 0000000000..d327572af7 --- /dev/null +++ b/src/api/endpoints/drive/folders/create.ts @@ -0,0 +1,61 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import DriveFolder from '../../../models/drive-folder'; +import { isValidFolderName } from '../../../models/drive-folder'; +import serialize from '../../../serializers/drive-folder'; +import event from '../../../event'; + +/** + * Create drive folder + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'name' parameter + const [name, nameErr] = it(params.name).expect.string().validate(isValidFolderName).default('無題のフォルダー').qed(); + if (nameErr) return rej('invalid name param'); + + // Get 'parent_id' parameter + const [parentId, parentIdErr] = it(params.parent_id).expect.nullable.id().default(null).qed(); + if (parentIdErr) return rej('invalid parent_id param'); + + // If the parent folder is specified + let parent = null; + if (parentId) { + // Fetch parent folder + parent = await DriveFolder + .findOne({ + _id: parentId, + user_id: user._id + }); + + if (parent === null) { + return rej('parent-not-found'); + } + } + + // Create folder + const folder = await DriveFolder.insert({ + created_at: new Date(), + name: name, + parent_id: parent !== null ? parent._id : null, + user_id: user._id + }); + + // Serialize + const folderObj = await serialize(folder); + + // Response + res(folderObj); + + // Publish drive_folder_created event + event(user._id, 'drive_folder_created', folderObj); +}); diff --git a/src/api/endpoints/drive/folders/find.js b/src/api/endpoints/drive/folders/find.js deleted file mode 100644 index 802d3a7909..0000000000 --- a/src/api/endpoints/drive/folders/find.js +++ /dev/null @@ -1,45 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFolder from '../../../models/drive-folder'; -import serialize from '../../../serializers/drive-folder'; - -/** - * Find a folder(s) - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'name' parameter - const name = params.name; - if (name === undefined || name === null) { - return rej('name is required'); - } - - // Get 'parent_id' parameter - let parentId = params.parent_id; - if (parentId === undefined || parentId === null) { - parentId = null; - } else { - parentId = new mongo.ObjectID(parentId); - } - - // Issue query - const folders = await DriveFolder - .find({ - name: name, - user_id: user._id, - parent_id: parentId - }); - - // Serialize - res(await Promise.all(folders.map(async folder => - await serialize(folder)))); -}); diff --git a/src/api/endpoints/drive/folders/find.ts b/src/api/endpoints/drive/folders/find.ts new file mode 100644 index 0000000000..041e9ccb2a --- /dev/null +++ b/src/api/endpoints/drive/folders/find.ts @@ -0,0 +1,39 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import DriveFolder from '../../../models/drive-folder'; +import serialize from '../../../serializers/drive-folder'; + +/** + * Find a folder(s) + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'name' parameter + const [name, nameErr] = it(params.name).expect.string().required().qed(); + if (nameErr) return rej('invalid name param'); + + // Get 'parent_id' parameter + const [parentId, parentIdErr] = it(params.parent_id).expect.id().qed(); + if (parentIdErr) return rej('invalid parent_id param'); + + // Issue query + const folders = await DriveFolder + .find({ + name: name, + user_id: user._id, + parent_id: parentId + }); + + // Serialize + res(await Promise.all(folders.map(async folder => + await serialize(folder)))); +}); diff --git a/src/api/endpoints/drive/folders/show.js b/src/api/endpoints/drive/folders/show.js deleted file mode 100644 index 986d32cf60..0000000000 --- a/src/api/endpoints/drive/folders/show.js +++ /dev/null @@ -1,41 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFolder from '../../../models/drive-folder'; -import serialize from '../../../serializers/drive-folder'; - -/** - * Show a folder - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'folder_id' parameter - const folderId = params.folder_id; - if (folderId === undefined || folderId === null) { - return rej('folder_id is required'); - } - - // Get folder - const folder = await DriveFolder - .findOne({ - _id: new mongo.ObjectID(folderId), - user_id: user._id - }); - - if (folder === null) { - return rej('folder-not-found'); - } - - // Serialize - res(await serialize(folder, { - detail: true - })); -}); diff --git a/src/api/endpoints/drive/folders/show.ts b/src/api/endpoints/drive/folders/show.ts new file mode 100644 index 0000000000..3b3ed41719 --- /dev/null +++ b/src/api/endpoints/drive/folders/show.ts @@ -0,0 +1,39 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import DriveFolder from '../../../models/drive-folder'; +import serialize from '../../../serializers/drive-folder'; + +/** + * Show a folder + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + // Get folder + const folder = await DriveFolder + .findOne({ + _id: folderId, + user_id: user._id + }); + + if (folder === null) { + return rej('folder-not-found'); + } + + // Serialize + res(await serialize(folder, { + detail: true + })); +}); diff --git a/src/api/endpoints/drive/folders/update.js b/src/api/endpoints/drive/folders/update.js deleted file mode 100644 index 713e17b43e..0000000000 --- a/src/api/endpoints/drive/folders/update.js +++ /dev/null @@ -1,124 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFolder from '../../../models/drive-folder'; -import { isValidFolderName } from '../../../models/drive-folder'; -import serialize from '../../../serializers/drive-file'; -import event from '../../../event'; - -/** - * Update a folder - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'folder_id' parameter - const folderId = params.folder_id; - if (folderId === undefined || folderId === null) { - return rej('folder_id is required'); - } - - // Validate id - if (!mongo.ObjectID.isValid(folderId)) { - return rej('incorrect folder_id'); - } - - // Fetch folder - const folder = await DriveFolder - .findOne({ - _id: new mongo.ObjectID(folderId), - user_id: user._id - }); - - if (folder === null) { - return rej('folder-not-found'); - } - - // Get 'name' parameter - let name = params.name; - if (name) { - name = name.trim(); - if (isValidFolderName(name)) { - folder.name = name; - } else { - return rej('invalid folder name'); - } - } - - // Get 'parent_id' parameter - let parentId = params.parent_id; - if (parentId !== undefined) { - if (parentId === null) { - folder.parent_id = null; - } else { - // Validate id - if (!mongo.ObjectID.isValid(parentId)) { - return rej('incorrect parent_id'); - } - - parentId = new mongo.ObjectID(parentId); - - // Get parent folder - const parent = await DriveFolder - .findOne({ - _id: parentId, - user_id: user._id - }); - - if (parent === null) { - return rej('parent-folder-not-found'); - } - - // Check if the circular reference will occur - async function checkCircle(folderId) { - // Fetch folder - const folder2 = await DriveFolder.findOne({ - _id: folderId - }, { - _id: true, - parent_id: true - }); - - if (folder2._id.equals(folder._id)) { - return true; - } else if (folder2.parent_id) { - return await checkCircle(folder2.parent_id); - } else { - return false; - } - } - - if (parent.parent_id !== null) { - if (await checkCircle(parent.parent_id)) { - return rej('detected-circular-definition'); - } - } - - folder.parent_id = parent._id; - } - } - - // Update - DriveFolder.update(folder._id, { - $set: { - name: folder.name, - parent_id: folder.parent_id - } - }); - - // Serialize - const folderObj = await serialize(folder); - - // Response - res(folderObj); - - // Publish drive_folder_updated event - event(user._id, 'drive_folder_updated', folderObj); -}); diff --git a/src/api/endpoints/drive/folders/update.ts b/src/api/endpoints/drive/folders/update.ts new file mode 100644 index 0000000000..81d414354c --- /dev/null +++ b/src/api/endpoints/drive/folders/update.ts @@ -0,0 +1,105 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import DriveFolder from '../../../models/drive-folder'; +import { isValidFolderName } from '../../../models/drive-folder'; +import serialize from '../../../serializers/drive-file'; +import event from '../../../event'; + +/** + * Update a folder + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'folder_id' parameter + const [folderId, folderIdErr] = it(params.folder_id).expect.id().required().qed(); + if (folderIdErr) return rej('invalid folder_id param'); + + // Fetch folder + const folder = await DriveFolder + .findOne({ + _id: folderId, + user_id: user._id + }); + + if (folder === null) { + return rej('folder-not-found'); + } + + // Get 'name' parameter + const [name, nameErr] = it(params.name).expect.string().validate(isValidFolderName).qed(); + if (nameErr) return rej('invalid name param'); + if (name) folder.name = name; + + // Get 'parent_id' parameter + const [parentId, parentIdErr] = it(params.parent_id).expect.nullable.id().qed(); + if (parentIdErr) return rej('invalid parent_id param'); + if (parentId !== undefined) { + if (parentId === null) { + folder.parent_id = null; + } else { + // Get parent folder + const parent = await DriveFolder + .findOne({ + _id: parentId, + user_id: user._id + }); + + if (parent === null) { + return rej('parent-folder-not-found'); + } + + // Check if the circular reference will occur + async function checkCircle(folderId) { + // Fetch folder + const folder2 = await DriveFolder.findOne({ + _id: folderId + }, { + _id: true, + parent_id: true + }); + + if (folder2._id.equals(folder._id)) { + return true; + } else if (folder2.parent_id) { + return await checkCircle(folder2.parent_id); + } else { + return false; + } + } + + if (parent.parent_id !== null) { + if (await checkCircle(parent.parent_id)) { + return rej('detected-circular-definition'); + } + } + + folder.parent_id = parent._id; + } + } + + // Update + DriveFolder.update(folder._id, { + $set: { + name: folder.name, + parent_id: folder.parent_id + } + }); + + // Serialize + const folderObj = await serialize(folder); + + // Response + res(folderObj); + + // Publish drive_folder_updated event + event(user._id, 'drive_folder_updated', folderObj); +}); diff --git a/src/api/endpoints/drive/stream.js b/src/api/endpoints/drive/stream.js deleted file mode 100644 index cd39261de8..0000000000 --- a/src/api/endpoints/drive/stream.js +++ /dev/null @@ -1,85 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import DriveFile from '../../models/drive-file'; -import serialize from '../../serializers/drive-file'; - -/** - * Get drive stream - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'limit' parameter - let limit = params.limit; - if (limit !== undefined && limit !== null) { - limit = parseInt(limit, 10); - - // From 1 to 100 - if (!(1 <= limit && limit <= 100)) { - return rej('invalid limit range'); - } - } else { - limit = 10; - } - - const since = params.since_id || null; - const max = params.max_id || null; - - // Check if both of since_id and max_id is specified - if (since !== null && max !== null) { - return rej('cannot set since_id and max_id'); - } - - // Get 'type' parameter - let type = params.type; - if (type === undefined || type === null) { - type = null; - } else if (!/^[a-zA-Z\/\-\*]+$/.test(type)) { - return rej('invalid type format'); - } else { - type = new RegExp(`^${type.replace(/\*/g, '.+?')}$`); - } - - // Construct query - const sort = { - _id: -1 - }; - const query = { - user_id: user._id - }; - if (since !== null) { - sort._id = 1; - query._id = { - $gt: new mongo.ObjectID(since) - }; - } else if (max !== null) { - query._id = { - $lt: new mongo.ObjectID(max) - }; - } - if (type !== null) { - query.type = type; - } - - // Issue query - const files = await DriveFile - .find(query, { - fields: { - data: false - }, - limit: limit, - sort: sort - }); - - // Serialize - res(await Promise.all(files.map(async file => - await serialize(file)))); -}); diff --git a/src/api/endpoints/drive/stream.ts b/src/api/endpoints/drive/stream.ts new file mode 100644 index 0000000000..6ede044f58 --- /dev/null +++ b/src/api/endpoints/drive/stream.ts @@ -0,0 +1,75 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import DriveFile from '../../models/drive-file'; +import serialize from '../../serializers/drive-file'; + +/** + * Get drive stream + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'limit' parameter + const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); + if (limitErr) return rej('invalid limit param'); + + // Get 'since_id' parameter + const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed(); + if (sinceIdErr) return rej('invalid since_id param'); + + // Get 'max_id' parameter + const [maxId, maxIdErr] = it(params.max_id).expect.id().qed(); + if (maxIdErr) return rej('invalid max_id param'); + + // Check if both of since_id and max_id is specified + if (sinceId !== null && maxId !== null) { + return rej('cannot set since_id and max_id'); + } + + // Get 'type' parameter + const [type, typeErr] = it(params.type).expect.string().match(/^[a-zA-Z\/\-\*]+$/).qed(); + if (typeErr) return rej('invalid type param'); + + // Construct query + const sort = { + _id: -1 + }; + const query = { + user_id: user._id + } as any; + if (sinceId) { + sort._id = 1; + query._id = { + $gt: sinceId + }; + } else if (maxId) { + query._id = { + $lt: maxId + }; + } + if (type !== null) { + query.type = new RegExp(`^${type.replace(/\*/g, '.+?')}$`); + } + + // Issue query + const files = await DriveFile + .find(query, { + fields: { + data: false + }, + limit: limit, + sort: sort + }); + + // Serialize + res(await Promise.all(files.map(async file => + await serialize(file)))); +}); diff --git a/src/api/endpoints/following/create.js b/src/api/endpoints/following/create.js deleted file mode 100644 index 46ff77ddf1..0000000000 --- a/src/api/endpoints/following/create.js +++ /dev/null @@ -1,96 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import Following from '../../models/following'; -import notify from '../../common/notify'; -import event from '../../event'; -import serializeUser from '../../serializers/user'; - -/** - * Follow a user - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - const follower = user; - - // Get 'user_id' parameter - let userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Validate id - if (!mongo.ObjectID.isValid(userId)) { - return rej('incorrect user_id'); - } - - // 自分自身 - if (user._id.equals(userId)) { - return rej('followee is yourself'); - } - - // Get followee - const followee = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - data: false, - profile: false - } - }); - - if (followee === null) { - return rej('user not found'); - } - - // Check if already following - const exist = await Following.findOne({ - follower_id: follower._id, - followee_id: followee._id, - deleted_at: { $exists: false } - }); - - if (exist !== null) { - return rej('already following'); - } - - // Create following - await Following.insert({ - created_at: new Date(), - follower_id: follower._id, - followee_id: followee._id - }); - - // Send response - res(); - - // Increment following count - User.update(follower._id, { - $inc: { - following_count: 1 - } - }); - - // Increment followers count - User.update({ _id: followee._id }, { - $inc: { - followers_count: 1 - } - }); - - // Publish follow event - event(follower._id, 'follow', await serializeUser(followee, follower)); - event(followee._id, 'followed', await serializeUser(follower, followee)); - - // Notify - notify(followee._id, follower._id, 'follow'); -}); diff --git a/src/api/endpoints/following/create.ts b/src/api/endpoints/following/create.ts new file mode 100644 index 0000000000..0edc122b94 --- /dev/null +++ b/src/api/endpoints/following/create.ts @@ -0,0 +1,89 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import Following from '../../models/following'; +import notify from '../../common/notify'; +import event from '../../event'; +import serializeUser from '../../serializers/user'; + +/** + * Follow a user + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + const follower = user; + + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id, 'id', true); + if (userIdErr) return rej('invalid user_id param'); + + // 自分自身 + if (user._id.equals(userId)) { + return rej('followee is yourself'); + } + + // Get followee + const followee = await User.findOne({ + _id: userId + }, { + fields: { + data: false, + profile: false + } + }); + + if (followee === null) { + return rej('user not found'); + } + + // Check if already following + const exist = await Following.findOne({ + follower_id: follower._id, + followee_id: followee._id, + deleted_at: { $exists: false } + }); + + if (exist !== null) { + return rej('already following'); + } + + // Create following + await Following.insert({ + created_at: new Date(), + follower_id: follower._id, + followee_id: followee._id + }); + + // Send response + res(); + + // Increment following count + User.update(follower._id, { + $inc: { + following_count: 1 + } + }); + + // Increment followers count + User.update({ _id: followee._id }, { + $inc: { + followers_count: 1 + } + }); + + // Publish follow event + event(follower._id, 'follow', await serializeUser(followee, follower)); + event(followee._id, 'followed', await serializeUser(follower, followee)); + + // Notify + notify(followee._id, follower._id, 'follow'); +}); diff --git a/src/api/endpoints/following/delete.js b/src/api/endpoints/following/delete.js deleted file mode 100644 index 1085013d03..0000000000 --- a/src/api/endpoints/following/delete.js +++ /dev/null @@ -1,93 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../models/user'; -import Following from '../../models/following'; -import event from '../../event'; -import serializeUser from '../../serializers/user'; - -/** - * Unfollow a user - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - const follower = user; - - // Get 'user_id' parameter - let userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Validate id - if (!mongo.ObjectID.isValid(userId)) { - return rej('incorrect user_id'); - } - - // Check if the followee is yourself - if (user._id.equals(userId)) { - return rej('followee is yourself'); - } - - // Get followee - const followee = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - data: false, - profile: false - } - }); - - if (followee === null) { - return rej('user not found'); - } - - // Check not following - const exist = await Following.findOne({ - follower_id: follower._id, - followee_id: followee._id, - deleted_at: { $exists: false } - }); - - if (exist === null) { - return rej('already not following'); - } - - // Delete following - await Following.update({ - _id: exist._id - }, { - $set: { - deleted_at: new Date() - } - }); - - // Send response - res(); - - // Decrement following count - User.update({ _id: follower._id }, { - $inc: { - following_count: -1 - } - }); - - // Decrement followers count - User.update({ _id: followee._id }, { - $inc: { - followers_count: -1 - } - }); - - // Publish follow event - event(follower._id, 'unfollow', await serializeUser(followee, follower)); -}); diff --git a/src/api/endpoints/following/delete.ts b/src/api/endpoints/following/delete.ts new file mode 100644 index 0000000000..7f0e908068 --- /dev/null +++ b/src/api/endpoints/following/delete.ts @@ -0,0 +1,86 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import User from '../../models/user'; +import Following from '../../models/following'; +import event from '../../event'; +import serializeUser from '../../serializers/user'; + +/** + * Unfollow a user + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + const follower = user; + + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id, 'id', true); + if (userIdErr) return rej('invalid user_id param'); + + // Check if the followee is yourself + if (user._id.equals(userId)) { + return rej('followee is yourself'); + } + + // Get followee + const followee = await User.findOne({ + _id: userId + }, { + fields: { + data: false, + profile: false + } + }); + + if (followee === null) { + return rej('user not found'); + } + + // Check not following + const exist = await Following.findOne({ + follower_id: follower._id, + followee_id: followee._id, + deleted_at: { $exists: false } + }); + + if (exist === null) { + return rej('already not following'); + } + + // Delete following + await Following.update({ + _id: exist._id + }, { + $set: { + deleted_at: new Date() + } + }); + + // Send response + res(); + + // Decrement following count + User.update({ _id: follower._id }, { + $inc: { + following_count: -1 + } + }); + + // Decrement followers count + User.update({ _id: followee._id }, { + $inc: { + followers_count: -1 + } + }); + + // Publish follow event + event(follower._id, 'unfollow', await serializeUser(followee, follower)); +}); -- cgit v1.2.3-freya From d1557bcae8abc45ea655d2fe0cdb6732a0207aa0 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 19:39:41 +0900 Subject: wip --- src/api/endpoints/auth/accept.js | 98 ------------------------ src/api/endpoints/auth/accept.ts | 97 ++++++++++++++++++++++++ src/api/endpoints/auth/session/generate.js | 81 -------------------- src/api/endpoints/auth/session/generate.ts | 80 ++++++++++++++++++++ src/api/endpoints/auth/session/show.js | 76 ------------------- src/api/endpoints/auth/session/show.ts | 75 +++++++++++++++++++ src/api/endpoints/auth/session/userkey.js | 115 ----------------------------- src/api/endpoints/auth/session/userkey.ts | 112 ++++++++++++++++++++++++++++ 8 files changed, 364 insertions(+), 370 deletions(-) delete mode 100644 src/api/endpoints/auth/accept.js create mode 100644 src/api/endpoints/auth/accept.ts delete mode 100644 src/api/endpoints/auth/session/generate.js create mode 100644 src/api/endpoints/auth/session/generate.ts delete mode 100644 src/api/endpoints/auth/session/show.js create mode 100644 src/api/endpoints/auth/session/show.ts delete mode 100644 src/api/endpoints/auth/session/userkey.js create mode 100644 src/api/endpoints/auth/session/userkey.ts (limited to 'src/api') diff --git a/src/api/endpoints/auth/accept.js b/src/api/endpoints/auth/accept.js deleted file mode 100644 index 1c0b100948..0000000000 --- a/src/api/endpoints/auth/accept.js +++ /dev/null @@ -1,98 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import rndstr from 'rndstr'; -const crypto = require('crypto'); -import App from '../../models/app'; -import AuthSess from '../../models/auth-session'; -import AccessToken from '../../models/access-token'; - -/** - * @swagger - * /auth/accept: - * post: - * summary: Accept a session - * parameters: - * - $ref: "#/parameters/NativeToken" - * - - * name: token - * description: Session Token - * in: formData - * required: true - * type: string - * responses: - * 204: - * description: OK - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Accept - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'token' parameter - const sesstoken = params.token; - if (sesstoken == null) { - return rej('token is required'); - } - - // Fetch token - const session = await AuthSess - .findOne({ token: sesstoken }); - - if (session === null) { - return rej('session not found'); - } - - // Generate access token - const token = rndstr('a-zA-Z0-9', 32); - - // Fetch exist access token - const exist = await AccessToken.findOne({ - app_id: session.app_id, - user_id: user._id, - }); - - if (exist === null) { - // Lookup app - const app = await App.findOne({ - _id: session.app_id - }); - - // Generate Hash - const sha256 = crypto.createHash('sha256'); - sha256.update(token + app.secret); - const hash = sha256.digest('hex'); - - // Insert access token doc - await AccessToken.insert({ - created_at: new Date(), - app_id: session.app_id, - user_id: user._id, - token: token, - hash: hash - }); - } - - // Update session - await AuthSess.update(session._id, { - $set: { - user_id: user._id - } - }); - - // Response - res(); -}); diff --git a/src/api/endpoints/auth/accept.ts b/src/api/endpoints/auth/accept.ts new file mode 100644 index 0000000000..2c104ef1c6 --- /dev/null +++ b/src/api/endpoints/auth/accept.ts @@ -0,0 +1,97 @@ +'use strict'; + +/** + * Module dependencies + */ +import rndstr from 'rndstr'; +const crypto = require('crypto'); +import it from '../../it'; +import App from '../../models/app'; +import AuthSess from '../../models/auth-session'; +import AccessToken from '../../models/access-token'; + +/** + * @swagger + * /auth/accept: + * post: + * summary: Accept a session + * parameters: + * - $ref: "#/parameters/NativeToken" + * - + * name: token + * description: Session Token + * in: formData + * required: true + * type: string + * responses: + * 204: + * description: OK + * + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Accept + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'token' parameter + const [token, tokenErr] = it(params.token).expect.string().required().qed(); + if (tokenErr) return rej('invalid token param'); + + // Fetch token + const session = await AuthSess + .findOne({ token: token }); + + if (session === null) { + return rej('session not found'); + } + + // Generate access token + const accessToken = rndstr('a-zA-Z0-9', 32); + + // Fetch exist access token + const exist = await AccessToken.findOne({ + app_id: session.app_id, + user_id: user._id, + }); + + if (exist === null) { + // Lookup app + const app = await App.findOne({ + _id: session.app_id + }); + + // Generate Hash + const sha256 = crypto.createHash('sha256'); + sha256.update(accessToken + app.secret); + const hash = sha256.digest('hex'); + + // Insert access token doc + await AccessToken.insert({ + created_at: new Date(), + app_id: session.app_id, + user_id: user._id, + token: accessToken, + hash: hash + }); + } + + // Update session + await AuthSess.update(session._id, { + $set: { + user_id: user._id + } + }); + + // Response + res(); +}); diff --git a/src/api/endpoints/auth/session/generate.js b/src/api/endpoints/auth/session/generate.js deleted file mode 100644 index cf75b83e2d..0000000000 --- a/src/api/endpoints/auth/session/generate.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as uuid from 'uuid'; -import App from '../../../models/app'; -import AuthSess from '../../../models/auth-session'; -import config from '../../../../conf'; - -/** - * @swagger - * /auth/session/generate: - * post: - * summary: Generate a session - * parameters: - * - - * name: app_secret - * description: App Secret - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: OK - * schema: - * type: object - * properties: - * token: - * type: string - * description: Session Token - * url: - * type: string - * description: Authentication form's URL - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Generate a session - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => -{ - // Get 'app_secret' parameter - const appSecret = params.app_secret; - if (appSecret == null) { - return rej('app_secret is required'); - } - - // Lookup app - const app = await App.findOne({ - secret: appSecret - }); - - if (app == null) { - return rej('app not found'); - } - - // Generate token - const token = uuid.v4(); - - // Create session token document - const doc = await AuthSess.insert({ - created_at: new Date(), - app_id: app._id, - token: token - }); - - // Response - res({ - token: doc.token, - url: `${config.auth_url}/${doc.token}` - }); -}); diff --git a/src/api/endpoints/auth/session/generate.ts b/src/api/endpoints/auth/session/generate.ts new file mode 100644 index 0000000000..6e730123c1 --- /dev/null +++ b/src/api/endpoints/auth/session/generate.ts @@ -0,0 +1,80 @@ +'use strict'; + +/** + * Module dependencies + */ +import * as uuid from 'uuid'; +import it from '../../../it'; +import App from '../../../models/app'; +import AuthSess from '../../../models/auth-session'; +import config from '../../../../conf'; + +/** + * @swagger + * /auth/session/generate: + * post: + * summary: Generate a session + * parameters: + * - + * name: app_secret + * description: App Secret + * in: formData + * required: true + * type: string + * + * responses: + * 200: + * description: OK + * schema: + * type: object + * properties: + * token: + * type: string + * description: Session Token + * url: + * type: string + * description: Authentication form's URL + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Generate a session + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'app_secret' parameter + const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().qed(); + if (appSecretErr) return rej('invalid app_secret param'); + + // Lookup app + const app = await App.findOne({ + secret: appSecret + }); + + if (app == null) { + return rej('app not found'); + } + + // Generate token + const token = uuid.v4(); + + // Create session token document + const doc = await AuthSess.insert({ + created_at: new Date(), + app_id: app._id, + token: token + }); + + // Response + res({ + token: doc.token, + url: `${config.auth_url}/${doc.token}` + }); +}); diff --git a/src/api/endpoints/auth/session/show.js b/src/api/endpoints/auth/session/show.js deleted file mode 100644 index 425c980d9d..0000000000 --- a/src/api/endpoints/auth/session/show.js +++ /dev/null @@ -1,76 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import AuthSess from '../../../models/auth-session'; -import serialize from '../../../serializers/auth-session'; - -/** - * @swagger - * /auth/session/show: - * post: - * summary: Show a session information - * parameters: - * - - * name: token - * description: Session Token - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: OK - * schema: - * type: object - * properties: - * created_at: - * type: string - * format: date-time - * description: Date and time of the session creation - * app_id: - * type: string - * description: Application ID - * token: - * type: string - * description: Session Token - * user_id: - * type: string - * description: ID of user who create the session - * app: - * $ref: "#/definitions/Application" - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Show a session - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = (params, user) => - new Promise(async (res, rej) => -{ - // Get 'token' parameter - const token = params.token; - if (token == null) { - return rej('token is required'); - } - - // Lookup session - const session = await AuthSess.findOne({ - token: token - }); - - if (session == null) { - return rej('session not found'); - } - - // Response - res(await serialize(session, user)); -}); diff --git a/src/api/endpoints/auth/session/show.ts b/src/api/endpoints/auth/session/show.ts new file mode 100644 index 0000000000..55641929d8 --- /dev/null +++ b/src/api/endpoints/auth/session/show.ts @@ -0,0 +1,75 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import AuthSess from '../../../models/auth-session'; +import serialize from '../../../serializers/auth-session'; + +/** + * @swagger + * /auth/session/show: + * post: + * summary: Show a session information + * parameters: + * - + * name: token + * description: Session Token + * in: formData + * required: true + * type: string + * + * responses: + * 200: + * description: OK + * schema: + * type: object + * properties: + * created_at: + * type: string + * format: date-time + * description: Date and time of the session creation + * app_id: + * type: string + * description: Application ID + * token: + * type: string + * description: Session Token + * user_id: + * type: string + * description: ID of user who create the session + * app: + * $ref: "#/definitions/Application" + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Show a session + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = (params, user) => + new Promise(async (res, rej) => +{ + // Get 'token' parameter + const [token, tokenErr] = it(params.token).expect.string().required().qed(); + if (tokenErr) return rej('invalid token param'); + + // Lookup session + const session = await AuthSess.findOne({ + token: token + }); + + if (session == null) { + return rej('session not found'); + } + + // Response + res(await serialize(session, user)); +}); diff --git a/src/api/endpoints/auth/session/userkey.js b/src/api/endpoints/auth/session/userkey.js deleted file mode 100644 index 2c34304a5e..0000000000 --- a/src/api/endpoints/auth/session/userkey.js +++ /dev/null @@ -1,115 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import App from '../../../models/app'; -import AuthSess from '../../../models/auth-session'; -import AccessToken from '../../../models/access-token'; -import serialize from '../../../serializers/user'; - -/** - * @swagger - * /auth/session/userkey: - * post: - * summary: Get an access token(userkey) - * parameters: - * - - * name: app_secret - * description: App Secret - * in: formData - * required: true - * type: string - * - - * name: token - * description: Session Token - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: OK - * schema: - * type: object - * properties: - * userkey: - * type: string - * description: Access Token - * user: - * $ref: "#/definitions/User" - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Generate a session - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => { - // Get 'app_secret' parameter - const appSecret = params.app_secret; - if (appSecret == null) { - return rej('app_secret is required'); - } - - // Lookup app - const app = await App.findOne({ - secret: appSecret - }); - - if (app == null) { - return rej('app not found'); - } - - // Get 'token' parameter - const token = params.token; - if (token == null) { - return rej('token is required'); - } - - // Fetch token - const session = await AuthSess - .findOne({ - token: token, - app_id: app._id - }); - - if (session === null) { - return rej('session not found'); - } - - if (session.user_id == null) { - return rej('this session is not allowed yet'); - } - - // Lookup access token - const accessToken = await AccessToken.findOne({ - app_id: app._id, - user_id: session.user_id - }); - - // Delete session - - /* https://github.com/Automattic/monk/issues/178 - AuthSess.deleteOne({ - _id: session._id - }); - */ - AuthSess.remove({ - _id: session._id - }); - - // Response - res({ - access_token: accessToken.token, - user: await serialize(session.user_id, null, { - detail: true - }) - }); - }); diff --git a/src/api/endpoints/auth/session/userkey.ts b/src/api/endpoints/auth/session/userkey.ts new file mode 100644 index 0000000000..fdb8c26d4e --- /dev/null +++ b/src/api/endpoints/auth/session/userkey.ts @@ -0,0 +1,112 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import App from '../../../models/app'; +import AuthSess from '../../../models/auth-session'; +import AccessToken from '../../../models/access-token'; +import serialize from '../../../serializers/user'; + +/** + * @swagger + * /auth/session/userkey: + * post: + * summary: Get an access token(userkey) + * parameters: + * - + * name: app_secret + * description: App Secret + * in: formData + * required: true + * type: string + * - + * name: token + * description: Session Token + * in: formData + * required: true + * type: string + * + * responses: + * 200: + * description: OK + * schema: + * type: object + * properties: + * userkey: + * type: string + * description: Access Token + * user: + * $ref: "#/definitions/User" + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Generate a session + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => { + // Get 'app_secret' parameter + const [appSecret, appSecretErr] = it(params.app_secret).expect.string().required().qed(); + if (appSecretErr) return rej('invalid app_secret param'); + + // Lookup app + const app = await App.findOne({ + secret: appSecret + }); + + if (app == null) { + return rej('app not found'); + } + + // Get 'token' parameter + const [token, tokenErr] = it(params.token).expect.string().required().qed(); + if (tokenErr) return rej('invalid token param'); + + // Fetch token + const session = await AuthSess + .findOne({ + token: token, + app_id: app._id + }); + + if (session === null) { + return rej('session not found'); + } + + if (session.user_id == null) { + return rej('this session is not allowed yet'); + } + + // Lookup access token + const accessToken = await AccessToken.findOne({ + app_id: app._id, + user_id: session.user_id + }); + + // Delete session + + /* https://github.com/Automattic/monk/issues/178 + AuthSess.deleteOne({ + _id: session._id + }); + */ + AuthSess.remove({ + _id: session._id + }); + + // Response + res({ + access_token: accessToken.token, + user: await serialize(session.user_id, null, { + detail: true + }) + }); + }); -- cgit v1.2.3-freya From f11bdf36b9c75a3e10850e46ef045cf9675ab7f7 Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 19:48:00 +0900 Subject: wip --- src/api/endpoints/app/create.js | 128 ----------------------------- src/api/endpoints/app/create.ts | 114 +++++++++++++++++++++++++ src/api/endpoints/app/name_id/available.js | 69 ---------------- src/api/endpoints/app/name_id/available.ts | 64 +++++++++++++++ src/api/endpoints/app/show.js | 81 ------------------ src/api/endpoints/app/show.ts | 77 +++++++++++++++++ src/api/models/app.ts | 4 + src/api/serializers/app.ts | 4 +- 8 files changed, 261 insertions(+), 280 deletions(-) delete mode 100644 src/api/endpoints/app/create.js create mode 100644 src/api/endpoints/app/create.ts delete mode 100644 src/api/endpoints/app/name_id/available.js create mode 100644 src/api/endpoints/app/name_id/available.ts delete mode 100644 src/api/endpoints/app/show.js create mode 100644 src/api/endpoints/app/show.ts (limited to 'src/api') diff --git a/src/api/endpoints/app/create.js b/src/api/endpoints/app/create.js deleted file mode 100644 index 8b85da7ffa..0000000000 --- a/src/api/endpoints/app/create.js +++ /dev/null @@ -1,128 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import rndstr from 'rndstr'; -import App from '../../models/app'; -import serialize from '../../serializers/app'; - -/** - * @swagger - * /app/create: - * post: - * summary: Create an application - * parameters: - * - $ref: "#/parameters/AccessToken" - * - - * name: name_id - * description: Application unique name - * in: formData - * required: true - * type: string - * - - * name: name - * description: Application name - * in: formData - * required: true - * type: string - * - - * name: description - * description: Application description - * in: formData - * required: true - * type: string - * - - * name: permission - * description: Permissions that application has - * in: formData - * required: true - * type: array - * items: - * type: string - * collectionFormat: csv - * - - * name: callback_url - * description: URL called back after authentication - * in: formData - * required: false - * type: string - * - * responses: - * 200: - * description: Created application's information - * schema: - * $ref: "#/definitions/Application" - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Create an app - * - * @param {any} params - * @param {any} user - * @return {Promise} - */ -module.exports = async (params, user) => - new Promise(async (res, rej) => -{ - // Get 'name_id' parameter - const nameId = params.name_id; - if (nameId == null) { - return rej('name_id is required'); - } else if (typeof nameId != 'string') { - return rej('name_id must be a string'); - } - - // Validate name_id - if (!/^[a-zA-Z0-9\-]{3,30}$/.test(nameId)) { - return rej('invalid name_id'); - } - - // Get 'name' parameter - const name = params.name; - if (name == null || name == '') { - return rej('name is required'); - } - - // Get 'description' parameter - const description = params.description; - if (description == null || description == '') { - return rej('description is required'); - } - - // Get 'permission' parameter - const permission = params.permission; - if (permission == null || permission == '') { - return rej('permission is required'); - } - - // Get 'callback_url' parameter - let callback = params.callback_url; - if (callback === '') { - callback = null; - } - - // Generate secret - const secret = rndstr('a-zA-Z0-9', 32); - - // Create account - const app = await App.insert({ - created_at: new Date(), - user_id: user._id, - name: name, - name_id: nameId, - name_id_lower: nameId.toLowerCase(), - description: description, - permission: permission.split(','), - callback_url: callback, - secret: secret - }); - - // Response - res(await serialize(app)); -}); diff --git a/src/api/endpoints/app/create.ts b/src/api/endpoints/app/create.ts new file mode 100644 index 0000000000..adbb205f62 --- /dev/null +++ b/src/api/endpoints/app/create.ts @@ -0,0 +1,114 @@ +'use strict'; + +/** + * Module dependencies + */ +import rndstr from 'rndstr'; +import it from '../../it'; +import App from '../../models/app'; +import { isValidNameId } from '../../models/app'; +import serialize from '../../serializers/app'; + +/** + * @swagger + * /app/create: + * post: + * summary: Create an application + * parameters: + * - $ref: "#/parameters/AccessToken" + * - + * name: name_id + * description: Application unique name + * in: formData + * required: true + * type: string + * - + * name: name + * description: Application name + * in: formData + * required: true + * type: string + * - + * name: description + * description: Application description + * in: formData + * required: true + * type: string + * - + * name: permission + * description: Permissions that application has + * in: formData + * required: true + * type: array + * items: + * type: string + * collectionFormat: csv + * - + * name: callback_url + * description: URL called back after authentication + * in: formData + * required: false + * type: string + * + * responses: + * 200: + * description: Created application's information + * schema: + * $ref: "#/definitions/Application" + * + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Create an app + * + * @param {any} params + * @param {any} user + * @return {Promise} + */ +module.exports = async (params, user) => + new Promise(async (res, rej) => +{ + // Get 'name_id' parameter + const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).qed(); + if (nameIdErr) return rej('invalid name_id param'); + + // Get 'name' parameter + const [name, nameErr] = it(params.name).expect.string().required().qed(); + if (nameErr) return rej('invalid name param'); + + // Get 'description' parameter + const [description, descriptionErr] = it(params.description).expect.string().required().qed(); + if (descriptionErr) return rej('invalid description param'); + + // Get 'permission' parameter + const [permission, permissionErr] = it(params.permission).expect.array().unique().allString().required().qed(); + if (permissionErr) return rej('invalid permission param'); + + // Get 'callback_url' parameter + // TODO: Check it is valid url + const [callbackUrl, callbackUrlErr] = it(params.callback_url).expect.nullable.string().default(null).qed(); + if (callbackUrlErr) return rej('invalid callback_url param'); + + // Generate secret + const secret = rndstr('a-zA-Z0-9', 32); + + // Create account + const app = await App.insert({ + created_at: new Date(), + user_id: user._id, + name: name, + name_id: nameId, + name_id_lower: nameId.toLowerCase(), + description: description, + permission: permission, + callback_url: callbackUrl, + secret: secret + }); + + // Response + res(await serialize(app)); +}); diff --git a/src/api/endpoints/app/name_id/available.js b/src/api/endpoints/app/name_id/available.js deleted file mode 100644 index 159d4fff4e..0000000000 --- a/src/api/endpoints/app/name_id/available.js +++ /dev/null @@ -1,69 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import App from '../../../models/app'; - -/** - * @swagger - * /app/name_id/available: - * post: - * summary: Check available name_id on creation an application - * parameters: - * - - * name: name_id - * description: Application unique name - * in: formData - * required: true - * type: string - * - * responses: - * 200: - * description: Success - * schema: - * type: object - * properties: - * available: - * description: Whether name_id is available - * type: boolean - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Check available name_id of app - * - * @param {any} params - * @return {Promise} - */ -module.exports = async (params) => - new Promise(async (res, rej) => -{ - // Get 'name_id' parameter - const nameId = params.name_id; - if (nameId == null || nameId == '') { - return rej('name_id is required'); - } - - // Validate name_id - if (!/^[a-zA-Z0-9\-]{3,30}$/.test(nameId)) { - return rej('invalid name_id'); - } - - // Get exist - const exist = await App - .count({ - name_id_lower: nameId.toLowerCase() - }, { - limit: 1 - }); - - // Reply - res({ - available: exist === 0 - }); -}); diff --git a/src/api/endpoints/app/name_id/available.ts b/src/api/endpoints/app/name_id/available.ts new file mode 100644 index 0000000000..6af18ae83c --- /dev/null +++ b/src/api/endpoints/app/name_id/available.ts @@ -0,0 +1,64 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import App from '../../../models/app'; +import { isValidNameId } from '../../../models/app'; + +/** + * @swagger + * /app/name_id/available: + * post: + * summary: Check available name_id on creation an application + * parameters: + * - + * name: name_id + * description: Application unique name + * in: formData + * required: true + * type: string + * + * responses: + * 200: + * description: Success + * schema: + * type: object + * properties: + * available: + * description: Whether name_id is available + * type: boolean + * + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Check available name_id of app + * + * @param {any} params + * @return {Promise} + */ +module.exports = async (params) => + new Promise(async (res, rej) => +{ + // Get 'name_id' parameter + const [nameId, nameIdErr] = it(params.name_id).expect.string().required().validate(isValidNameId).qed(); + if (nameIdErr) return rej('invalid name_id param'); + + // Get exist + const exist = await App + .count({ + name_id_lower: nameId.toLowerCase() + }, { + limit: 1 + }); + + // Reply + res({ + available: exist === 0 + }); +}); diff --git a/src/api/endpoints/app/show.js b/src/api/endpoints/app/show.js deleted file mode 100644 index ab5f6f4562..0000000000 --- a/src/api/endpoints/app/show.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import App from '../../models/app'; -import serialize from '../../serializers/app'; - -/** - * @swagger - * /app/show: - * post: - * summary: Show an application's information - * description: Require app_id or name_id - * parameters: - * - - * name: app_id - * description: Application ID - * in: formData - * type: string - * - - * name: name_id - * description: Application unique name - * in: formData - * type: string - * - * responses: - * 200: - * description: Success - * schema: - * $ref: "#/definitions/Application" - * - * default: - * description: Failed - * schema: - * $ref: "#/definitions/Error" - */ - -/** - * Show an app - * - * @param {any} params - * @param {any} user - * @param {any} _ - * @param {any} isSecure - * @return {Promise} - */ -module.exports = (params, user, _, isSecure) => - new Promise(async (res, rej) => -{ - // Get 'app_id' parameter - let appId = params.app_id; - if (appId == null || appId == '') { - appId = null; - } - - // Get 'name_id' parameter - let nameId = params.name_id; - if (nameId == null || nameId == '') { - nameId = null; - } - - if (appId === null && nameId === null) { - return rej('app_id or name_id is required'); - } - - // Lookup app - const app = appId !== null - ? await App.findOne({ _id: new mongo.ObjectID(appId) }) - : await App.findOne({ name_id_lower: nameId.toLowerCase() }); - - if (app === null) { - return rej('app not found'); - } - - // Send response - res(await serialize(app, user, { - includeSecret: isSecure && app.user_id.equals(user._id) - })); -}); diff --git a/src/api/endpoints/app/show.ts b/src/api/endpoints/app/show.ts new file mode 100644 index 0000000000..cfb03bb9e5 --- /dev/null +++ b/src/api/endpoints/app/show.ts @@ -0,0 +1,77 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../it'; +import App from '../../models/app'; +import serialize from '../../serializers/app'; + +/** + * @swagger + * /app/show: + * post: + * summary: Show an application's information + * description: Require app_id or name_id + * parameters: + * - + * name: app_id + * description: Application ID + * in: formData + * type: string + * - + * name: name_id + * description: Application unique name + * in: formData + * type: string + * + * responses: + * 200: + * description: Success + * schema: + * $ref: "#/definitions/Application" + * + * default: + * description: Failed + * schema: + * $ref: "#/definitions/Error" + */ + +/** + * Show an app + * + * @param {any} params + * @param {any} user + * @param {any} _ + * @param {any} isSecure + * @return {Promise} + */ +module.exports = (params, user, _, isSecure) => + new Promise(async (res, rej) => +{ + // Get 'app_id' parameter + const [appId, appIdErr] = it(params.app_id, 'id'); + if (appIdErr) return rej('invalid app_id param'); + + // Get 'name_id' parameter + const [nameId, nameIdErr] = it(params.name_id, 'string'); + if (nameIdErr) return rej('invalid name_id param'); + + if (appId === null && nameId === null) { + return rej('app_id or name_id is required'); + } + + // Lookup app + const app = appId !== null + ? await App.findOne({ _id: appId }) + : await App.findOne({ name_id_lower: nameId.toLowerCase() }); + + if (app === null) { + return rej('app not found'); + } + + // Send response + res(await serialize(app, user, { + includeSecret: isSecure && app.user_id.equals(user._id) + })); +}); diff --git a/src/api/models/app.ts b/src/api/models/app.ts index a947d88e4c..bf5dc80c2c 100644 --- a/src/api/models/app.ts +++ b/src/api/models/app.ts @@ -7,3 +7,7 @@ const collection = db.get('apps'); (collection as any).index('secret'); // fuck type definition export default collection as any; // fuck type definition + +export function isValidNameId(nameId: string): boolean { + return typeof nameId == 'string' && /^[a-zA-Z0-9\-]{3,30}$/.test(nameId); +} diff --git a/src/api/serializers/app.ts b/src/api/serializers/app.ts index 9a02c5637a..fdeef338d9 100644 --- a/src/api/serializers/app.ts +++ b/src/api/serializers/app.ts @@ -21,8 +21,8 @@ export default ( app: any, me?: any, options?: { - includeSecret: boolean, - includeProfileImageIds: boolean + includeSecret?: boolean, + includeProfileImageIds?: boolean } ) => new Promise(async (resolve, reject) => { const opts = options || { -- cgit v1.2.3-freya From 970843acd429e62c884c780601fae317a6a0fabb Mon Sep 17 00:00:00 2001 From: syuilo Date: Fri, 3 Mar 2017 19:52:36 +0900 Subject: done --- src/api/endpoints/aggregation/posts/like.js | 82 ---------------- src/api/endpoints/aggregation/posts/like.ts | 80 ++++++++++++++++ src/api/endpoints/aggregation/posts/likes.js | 75 --------------- src/api/endpoints/aggregation/posts/likes.ts | 73 ++++++++++++++ src/api/endpoints/aggregation/posts/reply.js | 81 ---------------- src/api/endpoints/aggregation/posts/reply.ts | 79 +++++++++++++++ src/api/endpoints/aggregation/posts/repost.js | 81 ---------------- src/api/endpoints/aggregation/posts/repost.ts | 79 +++++++++++++++ src/api/endpoints/aggregation/users/followers.js | 80 ---------------- src/api/endpoints/aggregation/users/followers.ts | 78 +++++++++++++++ src/api/endpoints/aggregation/users/following.js | 79 --------------- src/api/endpoints/aggregation/users/following.ts | 77 +++++++++++++++ src/api/endpoints/aggregation/users/like.js | 86 ----------------- src/api/endpoints/aggregation/users/like.ts | 84 ++++++++++++++++ src/api/endpoints/aggregation/users/post.js | 116 ----------------------- src/api/endpoints/aggregation/users/post.ts | 114 ++++++++++++++++++++++ 16 files changed, 664 insertions(+), 680 deletions(-) delete mode 100644 src/api/endpoints/aggregation/posts/like.js create mode 100644 src/api/endpoints/aggregation/posts/like.ts delete mode 100644 src/api/endpoints/aggregation/posts/likes.js create mode 100644 src/api/endpoints/aggregation/posts/likes.ts delete mode 100644 src/api/endpoints/aggregation/posts/reply.js create mode 100644 src/api/endpoints/aggregation/posts/reply.ts delete mode 100644 src/api/endpoints/aggregation/posts/repost.js create mode 100644 src/api/endpoints/aggregation/posts/repost.ts delete mode 100644 src/api/endpoints/aggregation/users/followers.js create mode 100644 src/api/endpoints/aggregation/users/followers.ts delete mode 100644 src/api/endpoints/aggregation/users/following.js create mode 100644 src/api/endpoints/aggregation/users/following.ts delete mode 100644 src/api/endpoints/aggregation/users/like.js create mode 100644 src/api/endpoints/aggregation/users/like.ts delete mode 100644 src/api/endpoints/aggregation/users/post.js create mode 100644 src/api/endpoints/aggregation/users/post.ts (limited to 'src/api') diff --git a/src/api/endpoints/aggregation/posts/like.js b/src/api/endpoints/aggregation/posts/like.js deleted file mode 100644 index 02724aceb6..0000000000 --- a/src/api/endpoints/aggregation/posts/like.js +++ /dev/null @@ -1,82 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../../models/post'; -import Like from '../../../models/like'; - -/** - * Aggregate like of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - 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'); - } - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Like - .aggregate([ - { $match: { post_id: post._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data) - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }) - }; - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/like.ts b/src/api/endpoints/aggregation/posts/like.ts new file mode 100644 index 0000000000..38ed7e6e16 --- /dev/null +++ b/src/api/endpoints/aggregation/posts/like.ts @@ -0,0 +1,80 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Post from '../../../models/post'; +import Like from '../../../models/like'; + +/** + * Aggregate like of a post + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + if (postIdErr) return rej('invalid post_id param'); + + // Lookup post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + const datas = await Like + .aggregate([ + { $match: { post_id: post._id } }, + { $project: { + created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST + }}, + { $project: { + date: { + year: { $year: '$created_at' }, + month: { $month: '$created_at' }, + day: { $dayOfMonth: '$created_at' } + } + }}, + { $group: { + _id: '$date', + count: { $sum: 1 } + }} + ]); + + datas.forEach(data => { + data.date = data._id; + delete data._id; + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + + const data = datas.filter(d => + d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() + )[0]; + + if (data) { + graph.push(data) + } else { + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: 0 + }) + }; + } + + res(graph); +}); diff --git a/src/api/endpoints/aggregation/posts/likes.js b/src/api/endpoints/aggregation/posts/likes.js deleted file mode 100644 index 1049c70687..0000000000 --- a/src/api/endpoints/aggregation/posts/likes.js +++ /dev/null @@ -1,75 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../../models/post'; -import Like from '../../../models/like'; - -/** - * Aggregate likes of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - 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'); - } - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); - - const likes = await Like - .find({ - post_id: post._id, - $or: [ - { deleted_at: { $exists: false } }, - { deleted_at: { $gt: startTime } } - ] - }, { - _id: false, - post_id: false - }, { - sort: { created_at: -1 } - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - //day = day.getTime(); - - const count = likes.filter(l => - l.created_at < day && (l.deleted_at == null || l.deleted_at > day) - ).length; - - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/likes.ts b/src/api/endpoints/aggregation/posts/likes.ts new file mode 100644 index 0000000000..55fe077f64 --- /dev/null +++ b/src/api/endpoints/aggregation/posts/likes.ts @@ -0,0 +1,73 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Post from '../../../models/post'; +import Like from '../../../models/like'; + +/** + * Aggregate likes of a post + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + if (postIdErr) return rej('invalid post_id param'); + + // Lookup post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); + + const likes = await Like + .find({ + post_id: post._id, + $or: [ + { deleted_at: { $exists: false } }, + { deleted_at: { $gt: startTime } } + ] + }, { + _id: false, + post_id: false + }, { + sort: { created_at: -1 } + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + day = new Date(day.setMilliseconds(999)); + day = new Date(day.setSeconds(59)); + day = new Date(day.setMinutes(59)); + day = new Date(day.setHours(23)); + //day = day.getTime(); + + const count = likes.filter(l => + l.created_at < day && (l.deleted_at == null || l.deleted_at > day) + ).length; + + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: count + }); + } + + res(graph); +}); diff --git a/src/api/endpoints/aggregation/posts/reply.js b/src/api/endpoints/aggregation/posts/reply.js deleted file mode 100644 index 9d051c6593..0000000000 --- a/src/api/endpoints/aggregation/posts/reply.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../../models/post'; - -/** - * Aggregate reply of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - 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'); - } - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Post - .aggregate([ - { $match: { reply_to: post._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data) - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }) - }; - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/reply.ts b/src/api/endpoints/aggregation/posts/reply.ts new file mode 100644 index 0000000000..1f936bbc2f --- /dev/null +++ b/src/api/endpoints/aggregation/posts/reply.ts @@ -0,0 +1,79 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Post from '../../../models/post'; + +/** + * Aggregate reply of a post + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + if (postIdErr) return rej('invalid post_id param'); + + // Lookup post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + const datas = await Post + .aggregate([ + { $match: { reply_to: post._id } }, + { $project: { + created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST + }}, + { $project: { + date: { + year: { $year: '$created_at' }, + month: { $month: '$created_at' }, + day: { $dayOfMonth: '$created_at' } + } + }}, + { $group: { + _id: '$date', + count: { $sum: 1 } + }} + ]); + + datas.forEach(data => { + data.date = data._id; + delete data._id; + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + + const data = datas.filter(d => + d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() + )[0]; + + if (data) { + graph.push(data) + } else { + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: 0 + }) + }; + } + + res(graph); +}); diff --git a/src/api/endpoints/aggregation/posts/repost.js b/src/api/endpoints/aggregation/posts/repost.js deleted file mode 100644 index 01899ecea6..0000000000 --- a/src/api/endpoints/aggregation/posts/repost.js +++ /dev/null @@ -1,81 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import Post from '../../../models/post'; - -/** - * Aggregate repost of a post - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - 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'); - } - - // Lookup post - const post = await Post.findOne({ - _id: new mongo.ObjectID(postId) - }); - - if (post === null) { - return rej('post not found'); - } - - const datas = await Post - .aggregate([ - { $match: { repost_id: post._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data) - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }) - }; - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/posts/repost.ts b/src/api/endpoints/aggregation/posts/repost.ts new file mode 100644 index 0000000000..e4a1bf7c7b --- /dev/null +++ b/src/api/endpoints/aggregation/posts/repost.ts @@ -0,0 +1,79 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import Post from '../../../models/post'; + +/** + * Aggregate repost of a post + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'post_id' parameter + const [postId, postIdErr] = it(params.post_id).expect.id().required().qed(); + if (postIdErr) return rej('invalid post_id param'); + + // Lookup post + const post = await Post.findOne({ + _id: postId + }); + + if (post === null) { + return rej('post not found'); + } + + const datas = await Post + .aggregate([ + { $match: { repost_id: post._id } }, + { $project: { + created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST + }}, + { $project: { + date: { + year: { $year: '$created_at' }, + month: { $month: '$created_at' }, + day: { $dayOfMonth: '$created_at' } + } + }}, + { $group: { + _id: '$date', + count: { $sum: 1 } + }} + ]); + + datas.forEach(data => { + data.date = data._id; + delete data._id; + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + + const data = datas.filter(d => + d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() + )[0]; + + if (data) { + graph.push(data) + } else { + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: 0 + }) + }; + } + + res(graph); +}); diff --git a/src/api/endpoints/aggregation/users/followers.js b/src/api/endpoints/aggregation/users/followers.js deleted file mode 100644 index 3b8d1d6041..0000000000 --- a/src/api/endpoints/aggregation/users/followers.js +++ /dev/null @@ -1,80 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../../models/user'; -import Following from '../../../models/following'; - -/** - * Aggregate followers of a user - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - const userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Lookup user - const user = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); - - const following = await Following - .find({ - followee_id: user._id, - $or: [ - { deleted_at: { $exists: false } }, - { deleted_at: { $gt: startTime } } - ] - }, { - _id: false, - follower_id: false, - followee_id: false - }, { - sort: { created_at: -1 } - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - // day = day.getTime(); - - const count = following.filter(f => - f.created_at < day && (f.deleted_at == null || f.deleted_at > day) - ).length; - - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/followers.ts b/src/api/endpoints/aggregation/users/followers.ts new file mode 100644 index 0000000000..9336a102f1 --- /dev/null +++ b/src/api/endpoints/aggregation/users/followers.ts @@ -0,0 +1,78 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import User from '../../../models/user'; +import Following from '../../../models/following'; + +/** + * Aggregate followers of a user + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + if (userIdErr) return rej('invalid user_id param'); + + // Lookup user + const user = await User.findOne({ + _id: userId + }, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); + + const following = await Following + .find({ + followee_id: user._id, + $or: [ + { deleted_at: { $exists: false } }, + { deleted_at: { $gt: startTime } } + ] + }, { + _id: false, + follower_id: false, + followee_id: false + }, { + sort: { created_at: -1 } + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + day = new Date(day.setMilliseconds(999)); + day = new Date(day.setSeconds(59)); + day = new Date(day.setMinutes(59)); + day = new Date(day.setHours(23)); + // day = day.getTime(); + + const count = following.filter(f => + f.created_at < day && (f.deleted_at == null || f.deleted_at > day) + ).length; + + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: count + }); + } + + res(graph); +}); diff --git a/src/api/endpoints/aggregation/users/following.js b/src/api/endpoints/aggregation/users/following.js deleted file mode 100644 index 0b04ff9543..0000000000 --- a/src/api/endpoints/aggregation/users/following.js +++ /dev/null @@ -1,79 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../../models/user'; -import Following from '../../../models/following'; - -/** - * Aggregate following of a user - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - const userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Lookup user - const user = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); - - const following = await Following - .find({ - follower_id: user._id, - $or: [ - { deleted_at: { $exists: false } }, - { deleted_at: { $gt: startTime } } - ] - }, { - _id: false, - follower_id: false, - followee_id: false - }, { - sort: { created_at: -1 } - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - day = new Date(day.setMilliseconds(999)); - day = new Date(day.setSeconds(59)); - day = new Date(day.setMinutes(59)); - day = new Date(day.setHours(23)); - - const count = following.filter(f => - f.created_at < day && (f.deleted_at == null || f.deleted_at > day) - ).length; - - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: count - }); - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/following.ts b/src/api/endpoints/aggregation/users/following.ts new file mode 100644 index 0000000000..d468229158 --- /dev/null +++ b/src/api/endpoints/aggregation/users/following.ts @@ -0,0 +1,77 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import User from '../../../models/user'; +import Following from '../../../models/following'; + +/** + * Aggregate following of a user + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + if (userIdErr) return rej('invalid user_id param'); + + // Lookup user + const user = await User.findOne({ + _id: userId + }, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + const startTime = new Date(new Date().setMonth(new Date().getMonth() - 1)); + + const following = await Following + .find({ + follower_id: user._id, + $or: [ + { deleted_at: { $exists: false } }, + { deleted_at: { $gt: startTime } } + ] + }, { + _id: false, + follower_id: false, + followee_id: false + }, { + sort: { created_at: -1 } + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + day = new Date(day.setMilliseconds(999)); + day = new Date(day.setSeconds(59)); + day = new Date(day.setMinutes(59)); + day = new Date(day.setHours(23)); + + const count = following.filter(f => + f.created_at < day && (f.deleted_at == null || f.deleted_at > day) + ).length; + + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: count + }); + } + + res(graph); +}); diff --git a/src/api/endpoints/aggregation/users/like.js b/src/api/endpoints/aggregation/users/like.js deleted file mode 100644 index 0b20dd09a9..0000000000 --- a/src/api/endpoints/aggregation/users/like.js +++ /dev/null @@ -1,86 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../../models/user'; -import Like from '../../../models/like'; - -/** - * Aggregate like of a user - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - const userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Lookup user - const user = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const datas = await Like - .aggregate([ - { $match: { user_id: user._id } }, - { $project: { - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - } - }}, - { $group: { - _id: '$date', - count: { $sum: 1 } - }} - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data) - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - count: 0 - }) - }; - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/like.ts b/src/api/endpoints/aggregation/users/like.ts new file mode 100644 index 0000000000..4a932354ad --- /dev/null +++ b/src/api/endpoints/aggregation/users/like.ts @@ -0,0 +1,84 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import User from '../../../models/user'; +import Like from '../../../models/like'; + +/** + * Aggregate like of a user + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + if (userIdErr) return rej('invalid user_id param'); + + // Lookup user + const user = await User.findOne({ + _id: userId + }, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + const datas = await Like + .aggregate([ + { $match: { user_id: user._id } }, + { $project: { + created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST + }}, + { $project: { + date: { + year: { $year: '$created_at' }, + month: { $month: '$created_at' }, + day: { $dayOfMonth: '$created_at' } + } + }}, + { $group: { + _id: '$date', + count: { $sum: 1 } + }} + ]); + + datas.forEach(data => { + data.date = data._id; + delete data._id; + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + + const data = datas.filter(d => + d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() + )[0]; + + if (data) { + graph.push(data) + } else { + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + count: 0 + }) + }; + } + + res(graph); +}); diff --git a/src/api/endpoints/aggregation/users/post.js b/src/api/endpoints/aggregation/users/post.js deleted file mode 100644 index 01082801e4..0000000000 --- a/src/api/endpoints/aggregation/users/post.js +++ /dev/null @@ -1,116 +0,0 @@ -'use strict'; - -/** - * Module dependencies - */ -import * as mongo from 'mongodb'; -import User from '../../../models/user'; -import Post from '../../../models/post'; - -/** - * Aggregate post of a user - * - * @param {any} params - * @return {Promise} - */ -module.exports = (params) => - new Promise(async (res, rej) => -{ - // Get 'user_id' parameter - const userId = params.user_id; - if (userId === undefined || userId === null) { - return rej('user_id is required'); - } - - // Lookup user - const user = await User.findOne({ - _id: new mongo.ObjectID(userId) - }, { - fields: { - _id: true - } - }); - - if (user === null) { - return rej('user not found'); - } - - const datas = await Post - .aggregate([ - { $match: { user_id: user._id } }, - { $project: { - repost_id: '$repost_id', - reply_to_id: '$reply_to_id', - created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST - }}, - { $project: { - date: { - year: { $year: '$created_at' }, - month: { $month: '$created_at' }, - day: { $dayOfMonth: '$created_at' } - }, - type: { - $cond: { - if: { $ne: ['$repost_id', null] }, - then: 'repost', - else: { - $cond: { - if: { $ne: ['$reply_to_id', null] }, - then: 'reply', - else: 'post' - } - } - } - }} - }, - { $group: { _id: { - date: '$date', - type: '$type' - }, count: { $sum: 1 } } }, - { $group: { - _id: '$_id.date', - data: { $addToSet: { - type: '$_id.type', - count: '$count' - }} - } } - ]); - - datas.forEach(data => { - data.date = data._id; - delete data._id; - - data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; - data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; - data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; - - delete data.data; - }); - - const graph = []; - - for (let i = 0; i < 30; i++) { - let day = new Date(new Date().setDate(new Date().getDate() - i)); - - const data = datas.filter(d => - d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() - )[0]; - - if (data) { - graph.push(data) - } else { - graph.push({ - date: { - year: day.getFullYear(), - month: day.getMonth() + 1, // In JavaScript, month is zero-based. - day: day.getDate() - }, - posts: 0, - reposts: 0, - replies: 0 - }) - }; - } - - res(graph); -}); diff --git a/src/api/endpoints/aggregation/users/post.ts b/src/api/endpoints/aggregation/users/post.ts new file mode 100644 index 0000000000..b62dd6ec98 --- /dev/null +++ b/src/api/endpoints/aggregation/users/post.ts @@ -0,0 +1,114 @@ +'use strict'; + +/** + * Module dependencies + */ +import it from '../../../it'; +import User from '../../../models/user'; +import Post from '../../../models/post'; + +/** + * Aggregate post of a user + * + * @param {any} params + * @return {Promise} + */ +module.exports = (params) => + new Promise(async (res, rej) => +{ + // Get 'user_id' parameter + const [userId, userIdErr] = it(params.user_id).expect.id().required().qed(); + if (userIdErr) return rej('invalid user_id param'); + + // Lookup user + const user = await User.findOne({ + _id: userId + }, { + fields: { + _id: true + } + }); + + if (user === null) { + return rej('user not found'); + } + + const datas = await Post + .aggregate([ + { $match: { user_id: user._id } }, + { $project: { + repost_id: '$repost_id', + reply_to_id: '$reply_to_id', + created_at: { $add: ['$created_at', 9 * 60 * 60 * 1000] } // Convert into JST + }}, + { $project: { + date: { + year: { $year: '$created_at' }, + month: { $month: '$created_at' }, + day: { $dayOfMonth: '$created_at' } + }, + type: { + $cond: { + if: { $ne: ['$repost_id', null] }, + then: 'repost', + else: { + $cond: { + if: { $ne: ['$reply_to_id', null] }, + then: 'reply', + else: 'post' + } + } + } + }} + }, + { $group: { _id: { + date: '$date', + type: '$type' + }, count: { $sum: 1 } } }, + { $group: { + _id: '$_id.date', + data: { $addToSet: { + type: '$_id.type', + count: '$count' + }} + } } + ]); + + datas.forEach(data => { + data.date = data._id; + delete data._id; + + data.posts = (data.data.filter(x => x.type == 'post')[0] || { count: 0 }).count; + data.reposts = (data.data.filter(x => x.type == 'repost')[0] || { count: 0 }).count; + data.replies = (data.data.filter(x => x.type == 'reply')[0] || { count: 0 }).count; + + delete data.data; + }); + + const graph = []; + + for (let i = 0; i < 30; i++) { + let day = new Date(new Date().setDate(new Date().getDate() - i)); + + const data = datas.filter(d => + d.date.year == day.getFullYear() && d.date.month == day.getMonth() + 1 && d.date.day == day.getDate() + )[0]; + + if (data) { + graph.push(data) + } else { + graph.push({ + date: { + year: day.getFullYear(), + month: day.getMonth() + 1, // In JavaScript, month is zero-based. + day: day.getDate() + }, + posts: 0, + reposts: 0, + replies: 0 + }) + }; + } + + res(graph); +}); -- cgit v1.2.3-freya