2016-12-29 00:49:51 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Module dependencies
|
|
|
|
*/
|
|
|
|
import * as mongo from 'mongodb';
|
2017-01-02 23:03:19 +02:00
|
|
|
import deepcopy = require('deepcopy');
|
2016-12-29 00:49:51 +02:00
|
|
|
import App from '../models/app';
|
|
|
|
import User from '../models/user';
|
2017-01-06 05:09:57 +02:00
|
|
|
import AccessToken from '../models/access-token';
|
2016-12-29 00:49:51 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serialize an app
|
|
|
|
*
|
|
|
|
* @param {Object} app
|
|
|
|
* @param {Object} me?
|
|
|
|
* @param {Object} options?
|
|
|
|
* @return {Promise<Object>}
|
|
|
|
*/
|
|
|
|
export default (
|
|
|
|
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 User.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) {
|
|
|
|
// 既に連携しているか
|
2017-01-06 05:09:57 +02:00
|
|
|
const exist = await AccessToken.count({
|
2016-12-29 00:49:51 +02:00
|
|
|
app_id: _app.id,
|
|
|
|
user_id: me,
|
|
|
|
}, {
|
|
|
|
limit: 1
|
|
|
|
});
|
|
|
|
|
|
|
|
_app.is_authorized = exist === 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
resolve(_app);
|
|
|
|
});
|