From 90f8fe7e538bb7e52d2558152a0390e693f39b11 Mon Sep 17 00:00:00 2001 From: Akihiko Odaki Date: Thu, 29 Mar 2018 01:20:40 +0900 Subject: Introduce processor --- src/api/models/app.ts | 97 --------------------------------------------------- 1 file changed, 97 deletions(-) delete mode 100644 src/api/models/app.ts (limited to 'src/api/models/app.ts') diff --git a/src/api/models/app.ts b/src/api/models/app.ts deleted file mode 100644 index 34e9867db7..0000000000 --- a/src/api/models/app.ts +++ /dev/null @@ -1,97 +0,0 @@ -import * as mongo from 'mongodb'; -import deepcopy = require('deepcopy'); -import AccessToken from './access-token'; -import db from '../../db/mongodb'; -import config from '../../conf'; - -const App = db.get('apps'); -App.createIndex('name_id'); -App.createIndex('name_id_lower'); -App.createIndex('secret'); -export default App; - -export type IApp = { - _id: mongo.ObjectID; - created_at: Date; - user_id: mongo.ObjectID; - secret: string; -}; - -export function isValidNameId(nameId: string): boolean { - return typeof nameId == 'string' && /^[a-zA-Z0-9\-]{3,30}$/.test(nameId); -} - -/** - * Pack an app for API response - * - * @param {any} app - * @param {any} me? - * @param {any} options? - * @return {Promise} - */ -export const pack = ( - app: any, - me?: any, - options?: { - includeSecret?: boolean, - includeProfileImageIds?: boolean - } -) => new Promise(async (resolve, reject) => { - const opts = options || { - includeSecret: false, - includeProfileImageIds: false - }; - - let _app: any; - - // Populate the app if 'app' is ID - if (mongo.ObjectID.prototype.isPrototypeOf(app)) { - _app = await App.findOne({ - _id: app - }); - } else if (typeof app === 'string') { - _app = await App.findOne({ - _id: new mongo.ObjectID(app) - }); - } else { - _app = deepcopy(app); - } - - // Me - if (me && !mongo.ObjectID.prototype.isPrototypeOf(me)) { - if (typeof me === 'string') { - me = new mongo.ObjectID(me); - } else { - me = me._id; - } - } - - // Rename _id to id - _app.id = _app._id; - delete _app._id; - - delete _app.name_id_lower; - - // Visible by only owner - if (!opts.includeSecret) { - delete _app.secret; - } - - _app.icon_url = _app.icon != null - ? `${config.drive_url}/${_app.icon}` - : `${config.drive_url}/app-default.jpg`; - - if (me) { - // 既に連携しているか - const exist = await AccessToken.count({ - app_id: _app.id, - user_id: me, - }, { - limit: 1 - }); - - _app.is_authorized = exist === 1; - } - - resolve(_app); -}); -- cgit v1.2.3-freya