2018-02-01 15:06:01 -08:00
|
|
|
import * as mongo from 'mongodb';
|
|
|
|
import deepcopy = require('deepcopy');
|
|
|
|
import AccessToken from './access-token';
|
2018-03-29 04:32:18 -07:00
|
|
|
import db from '../db/mongodb';
|
2018-04-01 21:15:53 -07:00
|
|
|
import config from '../config';
|
2017-01-16 16:12:33 -08:00
|
|
|
|
2018-02-01 15:06:01 -08:00
|
|
|
const App = db.get<IApp>('apps');
|
2018-03-28 22:48:47 -07:00
|
|
|
App.createIndex('nameId');
|
|
|
|
App.createIndex('nameIdLower');
|
2018-02-01 15:06:01 -08:00
|
|
|
App.createIndex('secret');
|
|
|
|
export default App;
|
2016-12-28 14:49:51 -08:00
|
|
|
|
2018-02-01 15:06:01 -08:00
|
|
|
export type IApp = {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-28 22:48:47 -07:00
|
|
|
createdAt: Date;
|
|
|
|
userId: mongo.ObjectID;
|
2018-02-03 21:52:33 -08:00
|
|
|
secret: string;
|
2018-03-28 22:48:47 -07:00
|
|
|
name: string;
|
|
|
|
nameId: string;
|
|
|
|
nameIdLower: string;
|
|
|
|
description: string;
|
|
|
|
permission: string;
|
|
|
|
callbackUrl: string;
|
2018-02-01 15:06:01 -08:00
|
|
|
};
|
2017-03-03 02:48:00 -08:00
|
|
|
|
|
|
|
export function isValidNameId(nameId: string): boolean {
|
|
|
|
return typeof nameId == 'string' && /^[a-zA-Z0-9\-]{3,30}$/.test(nameId);
|
|
|
|
}
|
2018-02-01 15:06:01 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pack an app for API response
|
|
|
|
*
|
|
|
|
* @param {any} app
|
|
|
|
* @param {any} me?
|
|
|
|
* @param {any} options?
|
|
|
|
* @return {Promise<any>}
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
app: any,
|
|
|
|
me?: any,
|
|
|
|
options?: {
|
|
|
|
includeSecret?: boolean,
|
|
|
|
includeProfileImageIds?: boolean
|
|
|
|
}
|
|
|
|
) => new Promise<any>(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;
|
|
|
|
|
2018-03-28 22:48:47 -07:00
|
|
|
delete _app.nameIdLower;
|
2018-02-01 15:06:01 -08:00
|
|
|
|
|
|
|
// Visible by only owner
|
|
|
|
if (!opts.includeSecret) {
|
|
|
|
delete _app.secret;
|
|
|
|
}
|
|
|
|
|
2018-03-28 22:48:47 -07:00
|
|
|
_app.iconUrl = _app.icon != null
|
2018-02-01 15:06:01 -08:00
|
|
|
? `${config.drive_url}/${_app.icon}`
|
|
|
|
: `${config.drive_url}/app-default.jpg`;
|
|
|
|
|
|
|
|
if (me) {
|
|
|
|
// 既に連携しているか
|
|
|
|
const exist = await AccessToken.count({
|
2018-03-28 22:48:47 -07:00
|
|
|
appId: _app.id,
|
|
|
|
userId: me,
|
2018-02-01 15:06:01 -08:00
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
2018-03-28 22:48:47 -07:00
|
|
|
_app.isAuthorized = exist === 1;
|
2018-02-01 15:06:01 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_app);
|
|
|
|
});
|