Refactoring, Clean up and bug fixes
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
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 = {
|
||||
desc: {
|
||||
@ -10,68 +11,75 @@ 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,
|
||||
},
|
||||
|
||||
folderId: {
|
||||
validator: $.type(ID).optional.nullable,
|
||||
default: null as any,
|
||||
transform: transform,
|
||||
},
|
||||
|
||||
type: {
|
||||
validator: $.str.optional.match(/^[a-zA-Z\/\-\*]+$/)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default async (params: any, user: ILocalUser) => {
|
||||
// Get 'limit' parameter
|
||||
const [limit = 10, limitErr] = $.num.optional.range(1, 100).get(params.limit);
|
||||
if (limitErr) throw 'invalid limit param';
|
||||
|
||||
// Get 'sinceId' parameter
|
||||
const [sinceId, sinceIdErr] = $.type(ID).optional.get(params.sinceId);
|
||||
if (sinceIdErr) throw 'invalid sinceId param';
|
||||
|
||||
// Get 'untilId' parameter
|
||||
const [untilId, untilIdErr] = $.type(ID).optional.get(params.untilId);
|
||||
if (untilIdErr) throw 'invalid untilId param';
|
||||
const [ps, psErr] = getParams(meta, params);
|
||||
if (psErr) throw psErr;
|
||||
|
||||
// Check if both of sinceId and untilId is specified
|
||||
if (sinceId && untilId) {
|
||||
if (ps.sinceId && ps.untilId) {
|
||||
throw 'cannot set sinceId and untilId';
|
||||
}
|
||||
|
||||
// Get 'folderId' parameter
|
||||
const [folderId = null, folderIdErr] = $.type(ID).optional.nullable.get(params.folderId);
|
||||
if (folderIdErr) throw 'invalid folderId param';
|
||||
|
||||
// Get 'type' parameter
|
||||
const [type, typeErr] = $.str.optional.match(/^[a-zA-Z\/\-\*]+$/).get(params.type);
|
||||
if (typeErr) throw 'invalid type param';
|
||||
|
||||
// Construct query
|
||||
const sort = {
|
||||
_id: -1
|
||||
};
|
||||
|
||||
const query = {
|
||||
'metadata.userId': user._id,
|
||||
'metadata.folderId': folderId,
|
||||
'metadata.folderId': ps.folderId,
|
||||
'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
|
||||
return await packMany(files);
|
||||
};
|
||||
|
Reference in New Issue
Block a user