2018-02-01 17:06:01 -06:00
|
|
|
import * as mongo from 'mongodb';
|
2019-02-01 04:56:16 -06:00
|
|
|
import * as deepcopy from 'deepcopy';
|
2018-02-01 17:06:01 -06:00
|
|
|
import AccessToken from './access-token';
|
2018-03-29 06:32:18 -05:00
|
|
|
import db from '../db/mongodb';
|
2018-10-15 21:38:09 -05:00
|
|
|
import isObjectId from '../misc/is-objectid';
|
2018-04-01 23:15:53 -05:00
|
|
|
import config from '../config';
|
2019-03-06 08:23:50 -06:00
|
|
|
import { dbLogger } from '../db/logger';
|
2017-01-16 18:12:33 -06:00
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
const App = db.get<IApp>('apps');
|
|
|
|
App.createIndex('secret');
|
|
|
|
export default App;
|
2016-12-28 16:49:51 -06:00
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
export type IApp = {
|
|
|
|
_id: mongo.ObjectID;
|
2018-03-29 00:48:47 -05:00
|
|
|
createdAt: Date;
|
2018-08-12 22:30:32 -05:00
|
|
|
userId: mongo.ObjectID | null;
|
2018-02-03 23:52:33 -06:00
|
|
|
secret: string;
|
2018-03-29 00:48:47 -05:00
|
|
|
name: string;
|
|
|
|
description: string;
|
2018-04-11 03:40:01 -05:00
|
|
|
permission: string[];
|
2018-03-29 00:48:47 -05:00
|
|
|
callbackUrl: string;
|
2018-02-01 17:06:01 -06:00
|
|
|
};
|
2017-03-03 04:48:00 -06:00
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
/**
|
|
|
|
* Pack an app for API response
|
|
|
|
*/
|
|
|
|
export const pack = (
|
|
|
|
app: any,
|
|
|
|
me?: any,
|
|
|
|
options?: {
|
2018-10-31 19:19:22 -05:00
|
|
|
detail?: boolean,
|
2018-02-01 17:06:01 -06:00
|
|
|
includeSecret?: boolean,
|
|
|
|
includeProfileImageIds?: boolean
|
|
|
|
}
|
|
|
|
) => new Promise<any>(async (resolve, reject) => {
|
2018-10-31 19:19:22 -05:00
|
|
|
const opts = Object.assign({
|
|
|
|
detail: false,
|
2018-02-01 17:06:01 -06:00
|
|
|
includeSecret: false,
|
|
|
|
includeProfileImageIds: false
|
2018-10-31 19:19:22 -05:00
|
|
|
}, options);
|
2018-02-01 17:06:01 -06:00
|
|
|
|
|
|
|
let _app: any;
|
|
|
|
|
2018-10-31 19:19:22 -05:00
|
|
|
const fields = opts.detail ? {} : {
|
|
|
|
name: true
|
|
|
|
};
|
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
// Populate the app if 'app' is ID
|
2018-10-15 21:38:09 -05:00
|
|
|
if (isObjectId(app)) {
|
2018-02-01 17:06:01 -06:00
|
|
|
_app = await App.findOne({
|
|
|
|
_id: app
|
|
|
|
});
|
|
|
|
} else if (typeof app === 'string') {
|
|
|
|
_app = await App.findOne({
|
|
|
|
_id: new mongo.ObjectID(app)
|
2018-10-31 19:19:22 -05:00
|
|
|
}, { fields });
|
2018-02-01 17:06:01 -06:00
|
|
|
} else {
|
|
|
|
_app = deepcopy(app);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Me
|
2018-10-15 21:38:09 -05:00
|
|
|
if (me && !isObjectId(me)) {
|
2018-02-01 17:06:01 -06:00
|
|
|
if (typeof me === 'string') {
|
|
|
|
me = new mongo.ObjectID(me);
|
|
|
|
} else {
|
|
|
|
me = me._id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-06 08:23:50 -06:00
|
|
|
// (データベースの欠損などで)アプリがデータベース上に見つからなかったとき
|
|
|
|
if (_app == null) {
|
|
|
|
dbLogger.warn(`[DAMAGED DB] (missing) pkg: app :: ${app}`);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-02-01 17:06:01 -06:00
|
|
|
// Rename _id to id
|
|
|
|
_app.id = _app._id;
|
|
|
|
delete _app._id;
|
|
|
|
|
|
|
|
// Visible by only owner
|
|
|
|
if (!opts.includeSecret) {
|
|
|
|
delete _app.secret;
|
|
|
|
}
|
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
_app.iconUrl = _app.icon != null
|
2019-02-23 21:53:22 -06:00
|
|
|
? `${config.driveUrl}/${_app.icon}`
|
|
|
|
: `${config.driveUrl}/app-default.jpg`;
|
2018-02-01 17:06:01 -06:00
|
|
|
|
|
|
|
if (me) {
|
|
|
|
// 既に連携しているか
|
|
|
|
const exist = await AccessToken.count({
|
2018-03-29 00:48:47 -05:00
|
|
|
appId: _app.id,
|
|
|
|
userId: me,
|
2018-02-01 17:06:01 -06:00
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
2018-03-29 00:48:47 -05:00
|
|
|
_app.isAuthorized = exist === 1;
|
2018-02-01 17:06:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_app);
|
|
|
|
});
|