wip
This commit is contained in:
		
							parent
							
								
									583b64331b
								
							
						
					
					
						commit
						67f9c158d7
					
				
					 8 changed files with 93 additions and 107 deletions
				
			
		| 
						 | 
				
			
			@ -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<any>}
 | 
			
		||||
 */
 | 
			
		||||
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))));
 | 
			
		||||
	});
 | 
			
		||||
							
								
								
									
										76
									
								
								src/api/endpoints/posts.ts
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										76
									
								
								src/api/endpoints/posts.ts
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -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<any>}
 | 
			
		||||
 */
 | 
			
		||||
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))));
 | 
			
		||||
	});
 | 
			
		||||
| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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) {
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -3,6 +3,7 @@
 | 
			
		|||
/**
 | 
			
		||||
 * Module dependencies
 | 
			
		||||
 */
 | 
			
		||||
import it from '../it';
 | 
			
		||||
import User from '../models/user';
 | 
			
		||||
import serialize from '../serializers/user';
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -16,23 +17,19 @@ import serialize from '../serializers/user';
 | 
			
		|||
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);
 | 
			
		||||
		const [limit, limitErr] = it(params.limit).expect.number().range(1, 100).default(10).qed();
 | 
			
		||||
		if (limitErr) return rej('invalid limit param');
 | 
			
		||||
 | 
			
		||||
			// From 1 to 100
 | 
			
		||||
			if (!(1 <= limit && limit <= 100)) {
 | 
			
		||||
				return rej('invalid limit range');
 | 
			
		||||
			}
 | 
			
		||||
		} else {
 | 
			
		||||
			limit = 10;
 | 
			
		||||
		}
 | 
			
		||||
		// Get 'since_id' parameter
 | 
			
		||||
		const [sinceId, sinceIdErr] = it(params.since_id).expect.id().qed();
 | 
			
		||||
		if (sinceIdErr) return rej('invalid since_id param');
 | 
			
		||||
 | 
			
		||||
		const since = params.since_id || null;
 | 
			
		||||
		const max = params.max_id || null;
 | 
			
		||||
		// 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 (since !== null && max !== null) {
 | 
			
		||||
		if (sinceId !== null && maxId !== null) {
 | 
			
		||||
			return rej('cannot set since_id and max_id');
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -40,15 +37,15 @@ module.exports = (params, me) =>
 | 
			
		|||
		const sort = {
 | 
			
		||||
			_id: -1
 | 
			
		||||
		};
 | 
			
		||||
		const query = {};
 | 
			
		||||
		if (since !== null) {
 | 
			
		||||
		const query = {} as any;
 | 
			
		||||
		if (sinceId) {
 | 
			
		||||
			sort._id = 1;
 | 
			
		||||
			query._id = {
 | 
			
		||||
				$gt: new mongo.ObjectID(since)
 | 
			
		||||
				$gt: sinceId
 | 
			
		||||
			};
 | 
			
		||||
		} else if (max !== null) {
 | 
			
		||||
		} else if (maxId) {
 | 
			
		||||
			query._id = {
 | 
			
		||||
				$lt: new mongo.ObjectID(max)
 | 
			
		||||
				$lt: maxId
 | 
			
		||||
			};
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue