This commit is contained in:
syuilo 2017-03-03 06:48:26 +09:00
parent d3c4cd1c43
commit 6e181ee0f1
7 changed files with 63 additions and 108 deletions

View file

@ -19,15 +19,15 @@ module.exports = (params, user) =>
{ {
// Get 'post_id' parameter // Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id', true); 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 // Get 'limit' parameter
const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); 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 // Get 'offset' parameter
const [offset, offsetErr] = it(params.offset).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'); if (offsetErr) return rej('invalid offset param');
// Lookup post // Lookup post
const post = await Post.findOne({ const post = await Post.findOne({

View file

@ -20,19 +20,19 @@ module.exports = (params, user) =>
{ {
// Get 'post_id' parameter // Get 'post_id' parameter
const [postId, postIdErr] = it(params.post_id, 'id', true); 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 // Get 'limit' parameter
const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed(); 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 // Get 'offset' parameter
const [offset, offsetErr] = it(params.offset).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'); if (offsetErr) return rej('invalid offset param');
// Get 'sort' parameter // Get 'sort' parameter
const [sort, sortError] = it(params.sort).expect.string().or('desc asc').default('desc').qed(); 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 // Lookup post
const post = await Post.findOne({ const post = await Post.findOne({

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 serialize from '../../serializers/post'; import serialize from '../../serializers/post';
@ -18,42 +18,28 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'post_id' parameter // Get 'post_id' parameter
const postId = params.post_id; const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// 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');
// Lookup post // Lookup post
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _id: postId
}); });
if (post === null) { if (post === null) {
return rej('post not found', 'POST_NOT_FOUND'); return rej('post not found');
} }
// Issue query // Issue query

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 serialize from '../../serializers/post'; import serialize from '../../serializers/post';
@ -18,39 +18,33 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'post_id' parameter // Get 'post_id' parameter
const postId = params.post_id; const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// 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');
} }
// Lookup post // Lookup post
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _id: postId
}); });
if (post === null) { if (post === null) {
return rej('post not found', 'POST_NOT_FOUND'); return rej('post not found');
} }
// Construct query // Construct query
@ -59,15 +53,15 @@ module.exports = (params, user) =>
}; };
const query = { const query = {
repost_id: post._id repost_id: post._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
}; };
} }

View file

@ -4,6 +4,7 @@
* Module dependencies * Module dependencies
*/ */
import * as mongo from 'mongodb'; import * as mongo from 'mongodb';
import it from '../../it';
const escapeRegexp = require('escape-regexp'); const escapeRegexp = require('escape-regexp');
import Post from '../../models/post'; import Post from '../../models/post';
import serialize from '../../serializers/post'; import serialize from '../../serializers/post';
@ -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,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 serialize from '../../serializers/post'; import serialize from '../../serializers/post';
@ -18,19 +18,12 @@ module.exports = (params, user) =>
new Promise(async (res, rej) => new Promise(async (res, rej) =>
{ {
// Get 'post_id' parameter // Get 'post_id' parameter
const postId = params.post_id; const [postId, postIdErr] = it(params.post_id, 'id', true);
if (postId === undefined || postId === null) { if (postIdErr) return rej('invalid post_id param');
return rej('post_id is required');
}
// Validate id
if (!mongo.ObjectID.isValid(postId)) {
return rej('incorrect post_id');
}
// Get post // Get post
const post = await Post.findOne({ const post = await Post.findOne({
_id: new mongo.ObjectID(postId) _id: postId
}); });
if (post === null) { if (post === null) {

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 getFriends from '../../common/get-friends'; import getFriends from '../../common/get-friends';
import serialize from '../../serializers/post'; import serialize from '../../serializers/post';
@ -20,23 +20,19 @@ module.exports = (params, user, app) =>
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');
} }
@ -51,15 +47,15 @@ module.exports = (params, user, app) =>
user_id: { user_id: {
$in: followingIds $in: followingIds
} }
}; } 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
}; };
} }