1
0
mirror of https://github.com/misskey-dev/misskey synced 2024-12-03 17:28:33 +09:00
This commit is contained in:
syuilo 2017-03-03 08:56:07 +09:00
parent 2a9cba25a8
commit dc45055f2f
9 changed files with 104 additions and 157 deletions

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import AccessToken from '../../models/access-token'; import AccessToken from '../../models/access-token';
import serialize from '../../serializers/app'; import serialize from '../../serializers/app';
@ -18,28 +18,16 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
// Get 'offset' parameter // Get 'offset' parameter
let offset = params.offset; const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed();
if (offset !== undefined && offset !== null) { if (offsetErr) return rej('invalid offset param');
offset = parseInt(offset, 10);
} else {
offset = 0;
}
// Get 'sort' parameter // Get 'sort' parameter
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 param');
// Get tokens // Get tokens
const tokens = await AccessToken const tokens = await AccessToken

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import Favorite from '../../models/favorite'; import Favorite from '../../models/favorite';
import serialize from '../../serializers/post'; import serialize from '../../serializers/post';
@ -11,37 +11,26 @@ import serialize from '../../serializers/post';
* Get followers of a user * Get followers of a user
* *
* @param {any} params * @param {any} params
* @param {any} user
* @return {Promise<any>} * @return {Promise<any>}
*/ */
module.exports = (params) => module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100
if (!(1 <= limit && limit <= 100)) {
return rej('invalid limit range');
}
} else {
limit = 10;
}
// Get 'offset' parameter // Get 'offset' parameter
let offset = params.offset; const [offset, offsetErr] = it(params.offset).expect.number().min(0).default(0).qed();
if (offset !== undefined && offset !== null) { if (offsetErr) return rej('invalid offset param');
offset = parseInt(offset, 10);
} else {
offset = 0;
}
// Get 'sort' parameter // Get 'sort' parameter
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 param');
// Get favorites // Get favorites
const favorites = await Favorites const favorites = await Favorite
.find({ .find({
user_id: user._id user_id: user._id
}, { }, {

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import Notification from '../../models/notification'; import Notification from '../../models/notification';
import serialize from '../../serializers/notification'; import serialize from '../../serializers/notification';
import getFriends from '../../common/get-friends'; import getFriends from '../../common/get-friends';
@ -19,44 +19,38 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'following' parameter // Get 'following' parameter
const following = params.following; const [following, followingError] =
it(params.following).expect.boolean().default(false).qed();
if (followingError) return rej('invalid following param');
// Get 'mark_as_read' parameter // Get 'mark_as_read' parameter
let markAsRead = params.mark_as_read; const [markAsRead, markAsReadErr] = it(params.mark_as_read).expect.boolean().default(true).qed();
if (markAsRead == null) { if (markAsReadErr) return rej('invalid mark_as_read param');
markAsRead = true;
}
// Get 'type' parameter // Get 'type' parameter
let type = params.type; const [type, typeErr] = it(params.type).expect.array().unique().allString().qed();
if (type !== undefined && type !== null) { if (typeErr) return rej('invalid type param');
type = type.split(',').map(x => x.trim());
}
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100 // Get 'since_id' parameter
if (!(1 <= limit && limit <= 100)) { const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
return rej('invalid limit range'); if (sinceIdErr) return rej('invalid since_id param');
}
} else {
limit = 10;
}
const since = params.since_id || null; // Get 'max_id' parameter
const max = params.max_id || null; 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 // Check if both of since_id and max_id is specified
if (since !== null && max !== null) { if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
const query = { const query = {
notifiee_id: user._id notifiee_id: user._id
}; } as any;
const sort = { const sort = {
_id: -1 _id: -1
@ -77,14 +71,14 @@ module.exports = (params, user) =>
}; };
} }
if (since !== null) { if (sinceId) {
sort._id = 1; sort._id = 1;
query._id = { query._id = {
$gt: new mongo.ObjectID(since) $gt: sinceId
}; };
} else if (max !== null) { } else if (maxId) {
query._id = { query._id = {
$lt: new mongo.ObjectID(max) $lt: maxId
}; };
} }

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import Signin from '../../models/signin'; import Signin from '../../models/signin';
import serialize from '../../serializers/signin'; import serialize from '../../serializers/signin';
@ -18,42 +18,38 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'limit' parameter // Get 'limit' parameter
let limit = params.limit; const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
if (limit !== undefined && limit !== null) { if (limitErr) return rej('invalid limit param');
limit = parseInt(limit, 10);
// From 1 to 100 // Get 'since_id' parameter
if (!(1 <= limit && limit <= 100)) { const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
return rej('invalid limit range'); if (sinceIdErr) return rej('invalid since_id param');
}
} else {
limit = 10;
}
const since = params.since_id || null; // Get 'max_id' parameter
const max = params.max_id || null; 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 // Check if both of since_id and max_id is specified
if (since !== null && max !== null) { if (sinceId !== null && maxId !== null) {
return rej('cannot set since_id and max_id'); return rej('cannot set since_id and max_id');
} }
const query = { const query = {
user_id: user._id user_id: user._id
}; } as any;
const sort = { const sort = {
_id: -1 _id: -1
}; };
if (since !== null) { if (sinceId) {
sort._id = 1; sort._id = 1;
query._id = { query._id = {
$gt: new mongo.ObjectID(since) $gt: sinceId
}; };
} else if (max !== null) { } else if (maxId) {
query._id = { query._id = {
$lt: new mongo.ObjectID(max) $lt: maxId
}; };
} }

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import { isValidName, isValidBirthday } from '../../models/user'; import { isValidName, isValidBirthday } from '../../models/user';
import serialize from '../../serializers/user'; import serialize from '../../serializers/user';
@ -23,18 +23,9 @@ module.exports = async (params, user, _, isSecure) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'name' parameter // Get 'name' parameter
const name = params.name; const [name, nameErr] = it(params.name).expect.string().validate(isValidName).qed();
if (name !== undefined && name !== null) { if (nameErr) return rej('invalid name param');
if (typeof name != 'string') { user.name = name;
return rej('name must be a string');
}
if (!isValidName(name)) {
return rej('invalid name');
}
user.name = name;
}
// Get 'description' parameter // Get 'description' parameter
const description = params.description; const description = params.description;

View File

@ -131,21 +131,18 @@ module.exports = (params, user, app) =>
let poll = null; let poll = null;
if (_poll !== null) { if (_poll !== null) {
const [pollChoices, pollChoicesErr] = it(params.poll, 'set', false, [ const [pollChoices, pollChoicesErr] =
choices => { it(params.poll).expect.array()
const shouldReject = choices.some(choice => { .unique()
.allString()
.range(1, 10)
.validate(choices => !choices.some(choice => {
if (typeof choice != 'string') return true; if (typeof choice != 'string') return true;
if (choice.trim().length == 0) return true; if (choice.trim().length == 0) return true;
if (choice.trim().length > 50) return true; if (choice.trim().length > 50) return true;
return false; return false;
}); }))
return shouldReject ? new Error('invalid poll choices') : true; .qed();
},
// 選択肢がひとつならエラー
choices => choices.length == 1 ? new Error('poll choices must be ひとつ以上') : true,
// 選択肢が多すぎてもエラー
choices => choices.length > 10 ? new Error('many poll choices') : true,
]);
if (pollChoicesErr) return rej('invalid poll choices'); if (pollChoicesErr) return rej('invalid poll choices');
_poll.choices = pollChoices.map((choice, i) => ({ _poll.choices = pollChoices.map((choice, i) => ({

View File

@ -48,23 +48,27 @@ class QueryCore implements Query {
value: any; value: any;
error: Error; error: Error;
constructor() { constructor(value: any) {
this.value = null; this.value = value;
this.error = null; this.error = null;
} }
/** get isEmpty() {
* null return this.value === undefined || this.value === null;
*/
get shouldSkip() {
return this.error !== null || this.value === null;
} }
/** /**
* undefined  null *
*/
get shouldSkip() {
return this.error !== null || this.isEmpty;
}
/**
*
*/ */
required() { required() {
if (this.error === null && this.value === null) { if (this.error === null && this.isEmpty) {
this.error = new Error('required'); this.error = new Error('required');
} }
return this; return this;
@ -74,7 +78,7 @@ class QueryCore implements Query {
* *
*/ */
default(value: any) { default(value: any) {
if (this.value === null) { if (this.isEmpty) {
this.value = value; this.value = value;
} }
return this; return this;
@ -109,13 +113,9 @@ class BooleanQuery extends QueryCore {
error: Error; error: Error;
constructor(value) { constructor(value) {
super(); super(value);
if (value === undefined || value === null) { if (!this.isEmpty && typeof value != 'boolean') {
this.value = null;
} else if (typeof value != 'boolean') {
this.error = new Error('must-be-a-boolean'); this.error = new Error('must-be-a-boolean');
} else {
this.value = value;
} }
} }
@ -155,13 +155,9 @@ class NumberQuery extends QueryCore {
error: Error; error: Error;
constructor(value) { constructor(value) {
super(); super(value);
if (value === undefined || value === null) { if (!this.isEmpty && !Number.isFinite(value)) {
this.value = null;
} else if (!Number.isFinite(value)) {
this.error = new Error('must-be-a-number'); this.error = new Error('must-be-a-number');
} else {
this.value = value;
} }
} }
@ -238,13 +234,9 @@ class StringQuery extends QueryCore {
error: Error; error: Error;
constructor(value) { constructor(value) {
super(); super(value);
if (value === undefined || value === null) { if (!this.isEmpty && typeof value != 'string') {
this.value = null;
} else if (typeof value != 'string') {
this.error = new Error('must-be-a-string'); this.error = new Error('must-be-a-string');
} else {
this.value = value;
} }
} }
@ -327,13 +319,9 @@ class ArrayQuery extends QueryCore {
error: Error; error: Error;
constructor(value) { constructor(value) {
super(); super(value);
if (value === undefined || value === null) { if (!this.isEmpty && !Array.isArray(value)) {
this.value = null;
} else if (!Array.isArray(value)) {
this.error = new Error('must-be-an-array'); this.error = new Error('must-be-an-array');
} else {
this.value = value;
} }
} }
@ -361,6 +349,18 @@ class ArrayQuery extends QueryCore {
return this; 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 * undefined  null
*/ */
@ -397,13 +397,9 @@ class IdQuery extends QueryCore {
error: Error; error: Error;
constructor(value) { constructor(value) {
super(); super(value);
if (value === undefined || value === null) { if (!this.isEmpty && (typeof value != 'string' || !mongo.ObjectID.isValid(value))) {
this.value = null;
} else if (typeof value != 'string' || !mongo.ObjectID.isValid(value)) {
this.error = new Error('must-be-an-id'); 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; error: Error;
constructor(value) { constructor(value) {
super(); super(value);
if (value === undefined || value === null) { if (!this.isEmpty && typeof value != 'object') {
this.value = null;
} else if (typeof value != 'object') {
this.error = new Error('must-be-an-object'); this.error = new Error('must-be-an-object');
} else {
this.value = value;
} }
} }