Refactoring, Clean up and bug fixes

This commit is contained in:
syuilo
2018-11-02 03:32:24 +09:00
parent b4b9e76c8d
commit 931bdc6aac
108 changed files with 1722 additions and 1539 deletions

View File

@ -1,36 +1,44 @@
import $ from 'cafy'; import ID from '../../../../misc/cafy-id';
import $ from 'cafy'; import ID, { transform } from '../../../../misc/cafy-id';
import DriveFile, { packMany } from '../../../../models/drive-file';
import { ILocalUser } from '../../../../models/user';
import getParams from '../../get-params';
export const meta = {
requireCredential: true,
kind: 'drive-read'
kind: 'drive-read',
params: {
limit: {
validator: $.num.optional.range(1, 100),
default: 10
},
sinceId: {
validator: $.type(ID).optional,
transform: transform,
},
untilId: {
validator: $.type(ID).optional,
transform: transform,
},
type: {
validator: $.str.optional.match(/^[a-zA-Z\/\-\*]+$/)
}
}
};
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'limit' parameter
const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
if (limitErr) return rej('invalid limit param');
// Get 'sinceId' parameter
const [sinceId, sinceIdErr] = $.type(ID).optional.get(params.sinceId);
if (sinceIdErr) return rej('invalid sinceId param');
// Get 'untilId' parameter
const [untilId, untilIdErr] = $.type(ID).optional.get(params.untilId);
if (untilIdErr) return rej('invalid untilId param');
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
// Check if both of sinceId and untilId is specified
if (sinceId && untilId) {
if (ps.sinceId && ps.untilId) {
return rej('cannot set sinceId and untilId');
}
// Get 'type' parameter
const [type, typeErr] = $.str.optional.match(/^[a-zA-Z\/\-\*]+$/).get(params.type);
if (typeErr) return rej('invalid type param');
// Construct query
const sort = {
_id: -1
};
@ -40,28 +48,26 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
'metadata.deletedAt': { $exists: false }
} as any;
if (sinceId) {
if (ps.sinceId) {
sort._id = 1;
query._id = {
$gt: sinceId
$gt: ps.sinceId
};
} else if (untilId) {
} else if (ps.untilId) {
query._id = {
$lt: untilId
$lt: ps.untilId
};
}
if (type) {
query.contentType = new RegExp(`^${type.replace(/\*/g, '.+?')}$`);
if (ps.type) {
query.contentType = new RegExp(`^${ps.type.replace(/\*/g, '.+?')}$`);
}
// Issue query
const files = await DriveFile
.find(query, {
limit: limit,
limit: ps.limit,
sort: sort
});
// Serialize
res(await packMany(files));
});