This commit is contained in:
syuilo 2017-03-03 07:47:14 +09:00
parent 2e4e599c01
commit 0926d5b6da
7 changed files with 78 additions and 163 deletions

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 Following from '../../models/following'; import Following from '../../models/following';
import serialize from '../../serializers/user'; import serialize from '../../serializers/user';
@ -20,33 +20,24 @@ module.exports = (params, me) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'user_id' parameter // Get 'user_id' parameter
const userId = params.user_id; const [userId, userIdErr] = it(params.user_id, 'id', true);
if (userId === undefined || userId === null) { if (userIdErr) return rej('invalid user_id param');
return rej('user_id is required');
}
// Get 'iknow' parameter // Get 'iknow' parameter
const iknow = params.iknow; const [iknow, iknowErr] = it(params.iknow).expect.boolean().default(false).qed();
if (iknowErr) return rej('invalid iknow param');
// 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 'cursor' parameter // Get 'cursor' parameter
const cursor = params.cursor || null; const [cursor, cursorErr] = it(params.cursor).expect.id().default(null).qed();
if (cursorErr) return rej('invalid cursor param');
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: userId
}, { }, {
fields: { fields: {
_id: true _id: true
@ -61,7 +52,7 @@ module.exports = (params, me) =>
const query = { const query = {
followee_id: user._id, followee_id: user._id,
deleted_at: { $exists: false } deleted_at: { $exists: false }
}; } as any;
// ログインしていてかつ iknow フラグがあるとき // ログインしていてかつ iknow フラグがあるとき
if (me && iknow) { if (me && iknow) {
@ -76,7 +67,7 @@ module.exports = (params, me) =>
// カーソルが指定されている場合 // カーソルが指定されている場合
if (cursor) { if (cursor) {
query._id = { query._id = {
$lt: new mongo.ObjectID(cursor) $lt: cursor
}; };
} }

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 Following from '../../models/following'; import Following from '../../models/following';
import serialize from '../../serializers/user'; import serialize from '../../serializers/user';
@ -20,33 +20,24 @@ module.exports = (params, me) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'user_id' parameter // Get 'user_id' parameter
const userId = params.user_id; const [userId, userIdErr] = it(params.user_id, 'id', true);
if (userId === undefined || userId === null) { if (userIdErr) return rej('invalid user_id param');
return rej('user_id is required');
}
// Get 'iknow' parameter // Get 'iknow' parameter
const iknow = params.iknow; const [iknow, iknowErr] = it(params.iknow).expect.boolean().default(false).qed();
if (iknowErr) return rej('invalid iknow param');
// 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 'cursor' parameter // Get 'cursor' parameter
const cursor = params.cursor || null; const [cursor, cursorErr] = it(params.cursor).expect.id().default(null).qed();
if (cursorErr) return rej('invalid cursor param');
// Lookup user // Lookup user
const user = await User.findOne({ const user = await User.findOne({
_id: new mongo.ObjectID(userId) _id: userId
}, { }, {
fields: { fields: {
_id: true _id: true
@ -61,7 +52,7 @@ module.exports = (params, me) =>
const query = { const query = {
follower_id: user._id, follower_id: user._id,
deleted_at: { $exists: false } deleted_at: { $exists: false }
}; } as any;
// ログインしていてかつ iknow フラグがあるとき // ログインしていてかつ iknow フラグがあるとき
if (me && iknow) { if (me && iknow) {
@ -76,7 +67,7 @@ module.exports = (params, me) =>
// カーソルが指定されている場合 // カーソルが指定されている場合
if (cursor) { if (cursor) {
query._id = { query._id = {
$lt: new mongo.ObjectID(cursor) $lt: cursor
}; };
} }

View File

@ -3,7 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import Post from '../../models/post'; import Post from '../../models/post';
import User from '../../models/user'; import User from '../../models/user';
import serialize from '../../serializers/post'; import serialize from '../../serializers/post';
@ -19,56 +19,44 @@ module.exports = (params, me) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'user_id' parameter // Get 'user_id' parameter
let userId = params.user_id; const [userId, userIdErr] = it(params.user_id, 'id');
if (userId === undefined || userId === null || userId === '') { if (userIdErr) return rej('invalid user_id param');
userId = null;
}
// Get 'username' parameter // Get 'username' parameter
let username = params.username; const [username, usernameErr] = it(params.username, 'string');
if (username === undefined || username === null || username === '') { if (usernameErr) return rej('invalid username param');
username = null;
}
if (userId === null && username === null) { if (userId === null && username === null) {
return rej('user_id or username is required'); return rej('user_id or username is required');
} }
// Get 'with_replies' parameter // Get 'include_replies' parameter
let withReplies = params.with_replies; const [includeReplies, includeRepliesErr] = it(params.include_replies).expect.boolean().default(true).qed();
if (withReplies == null) { if (includeRepliesErr) return rej('invalid include_replies param');
withReplies = true;
}
// Get 'with_media' parameter // Get 'with_media' parameter
let withMedia = params.with_media; const [withMedia, withMediaErr] = it(params.with_media).expect.boolean().default(false).qed();
if (withMedia == null) { if (withMediaErr) return rej('invalid with_media param');
withMedia = false;
}
// 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 q = userId != null const q = userId != null
? { _id: new mongo.ObjectID(userId) } ? { _id: userId }
: { username_lower: username.toLowerCase() } ; : { username_lower: username.toLowerCase() } ;
// Lookup user // Lookup user
@ -88,19 +76,19 @@ module.exports = (params, me) =>
}; };
const query = { const query = {
user_id: user._id user_id: user._id
}; } as any;
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
}; };
} }
if (!withReplies) { if (!includeReplies) {
query.reply_to_id = null; query.reply_to_id = null;
} }

View File

@ -3,6 +3,7 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import serialize from '../../serializers/user'; import serialize from '../../serializers/user';
import getFriends from '../../common/get-friends'; import getFriends from '../../common/get-friends';
@ -18,25 +19,12 @@ module.exports = (params, me) =>
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;
}
// ID list of the user itself and other users who the user follows // ID list of the user itself and other users who the user follows
const followingIds = await getFriends(me._id); const followingIds = await getFriends(me._id);

View File

@ -4,6 +4,7 @@
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import serialize from '../../serializers/user'; import serialize from '../../serializers/user';
import config from '../../../conf'; import config from '../../../conf';
@ -20,31 +21,16 @@ module.exports = (params, me) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'query' parameter // Get 'query' parameter
let query = params.query; const [query, queryError] = it(params.query).expect.string().required().trim().validate(x => x != '').qed();
if (query === undefined || query === null || query.trim() === '') { if (queryError) return rej('invalid query param');
return rej('query is required');
}
// 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 'max' parameter // Get 'max' parameter
let max = params.max; const [max, maxErr] = it(params.max).expect.number().range(1, 30).default(10).qed();
if (max !== undefined && max !== null) { if (maxErr) return rej('invalid max param');
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 Elasticsearch is available, search by it
// If not, search by MongoDB // If not, search by MongoDB

View File

@ -3,8 +3,9 @@
/** /**
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import it from '../../it';
import User from '../../models/user'; import User from '../../models/user';
import { validateUsername } from '../../models/user';
import serialize from '../../serializers/user'; import serialize from '../../serializers/user';
/** /**
@ -18,37 +19,16 @@ module.exports = (params, me) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'query' parameter // Get 'query' parameter
let query = params.query; const [query, queryError] = it(params.query).expect.string().required().trim().validate(validateUsername).qed();
if (query === undefined || query === null || query.trim() === '') { if (queryError) return rej('invalid query param');
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 // 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 { // Get 'limit' parameter
offset = 0; 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 const users = await User
.find({ .find({

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 serialize from '../../serializers/user'; import serialize from '../../serializers/user';
@ -18,28 +18,19 @@ module.exports = (params, me) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'user_id' parameter // Get 'user_id' parameter
let userId = params.user_id; const [userId, userIdErr] = it(params.user_id, 'id');
if (userId === undefined || userId === null || userId === '') { if (userIdErr) return rej('invalid user_id param');
userId = null;
}
// Get 'username' parameter // Get 'username' parameter
let username = params.username; const [username, usernameErr] = it(params.username, 'string');
if (username === undefined || username === null || username === '') { if (usernameErr) return rej('invalid username param');
username = null;
}
if (userId === null && username === null) { if (userId === null && username === null) {
return rej('user_id or username is required'); 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 const q = userId != null
? { _id: new mongo.ObjectID(userId) } ? { _id: userId }
: { username_lower: username.toLowerCase() } ; : { username_lower: username.toLowerCase() } ;
// Lookup user // Lookup user