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/endpoints') 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