misskey/src/api/serializers/drive-folder.ts

53 lines
1.0 KiB
TypeScript
Raw Normal View History

2016-12-28 23:49:51 +01:00
'use strict';
/**
* Module dependencies
*/
import * as mongo from 'mongodb';
import DriveFolder from '../models/drive-folder';
2017-01-02 22:03:19 +01:00
import deepcopy = require('deepcopy');
2016-12-28 23:49:51 +01:00
/**
* Serialize a drive folder
*
* @param {Object} folder
* @param {Object} options?
* @return {Promise<Object>}
*/
const self = (
folder: any,
options?: {
2017-02-16 08:55:01 +01:00
detail: boolean
2016-12-28 23:49:51 +01:00
}
) => new Promise<Object>(async (resolve, reject) => {
2017-02-16 08:55:01 +01:00
const opts = Object.assign({
detail: false
}, options);
2016-12-28 23:49:51 +01:00
let _folder: any;
// Populate the folder if 'folder' is ID
if (mongo.ObjectID.prototype.isPrototypeOf(folder)) {
_folder = await DriveFolder.findOne({_id: folder});
} else if (typeof folder === 'string') {
_folder = await DriveFolder.findOne({_id: new mongo.ObjectID(folder)});
} else {
_folder = deepcopy(folder);
}
// Rename _id to id
_folder.id = _folder._id;
delete _folder._id;
2017-02-16 08:55:01 +01:00
if (opts.detail && _folder.parent_id) {
2016-12-28 23:49:51 +01:00
// Populate parent folder
_folder.parent = await self(_folder.parent_id, {
2017-02-16 08:55:01 +01:00
detail: true
2016-12-28 23:49:51 +01:00
});
}
resolve(_folder);
});
export default self;