diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-02-09 00:11:16 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-02-09 00:11:16 +0900 |
| commit | bc1698e755cbfa33257360f5bdff781e8ecef517 (patch) | |
| tree | 195b3b9654fcd13ccc708a508ccb55f2d2fee91a /src/api/endpoints | |
| parent | Refactor: Clean up (diff) | |
| download | sharkey-bc1698e755cbfa33257360f5bdff781e8ecef517.tar.gz sharkey-bc1698e755cbfa33257360f5bdff781e8ecef517.tar.bz2 sharkey-bc1698e755cbfa33257360f5bdff781e8ecef517.zip | |
nanka iroiro
Diffstat (limited to 'src/api/endpoints')
| -rw-r--r-- | src/api/endpoints/i/authorized_apps.js | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/api/endpoints/i/authorized_apps.js b/src/api/endpoints/i/authorized_apps.js new file mode 100644 index 0000000000..17f971af2f --- /dev/null +++ b/src/api/endpoints/i/authorized_apps.js @@ -0,0 +1,60 @@ +'use strict'; + +/** + * Module dependencies + */ +import * as mongo from 'mongodb'; +import AccessToken from '../../models/access-token'; +import App from '../../models/app'; +import serialize from '../../serializers/app'; + +/** + * Get authorized apps of my account + * + * @param {Object} params + * @param {Object} user + * @return {Promise<object>} + */ +module.exports = (params, user) => + 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; + } + + // Get 'offset' parameter + let offset = params.offset; + if (offset !== undefined && offset !== null) { + offset = parseInt(offset, 10); + } else { + offset = 0; + } + + // Get 'sort' parameter + let sort = params.sort || 'desc'; + + // Get tokens + const tokens = await AccessToken + .find({ + user_id: user._id + }, { + limit: limit, + skip: offset, + sort: { + _id: sort == 'asc' ? 1 : -1 + } + }); + + // Serialize + res(await Promise.all(tokens.map(async token => + await serialize(token.app_id)))); +}); |