Improve some API definitions

This commit is contained in:
syuilo 2018-10-24 18:32:19 +09:00
parent 44e9be5a1c
commit 6dcc3800e0
No known key found for this signature in database
GPG key ID: BDC4C49D06AB9D69
3 changed files with 78 additions and 32 deletions
src/server/api/endpoints/drive/folders

View file

@ -2,8 +2,11 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder';
import { publishDriveStream } from '../../../../../stream';
import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';
export const meta = {
stability: 'stable',
desc: {
'ja-JP': 'ドライブのフォルダを作成します。',
'en-US': 'Create a folder of drive.'
@ -11,25 +14,37 @@ export const meta = {
requireCredential: true,
kind: 'drive-write'
kind: 'drive-write',
params: {
name: $.str.optional.pipe(isValidFolderName).note({
default: 'Untitled',
desc: {
'ja-JP': 'フォルダ名',
'en-US': 'Folder name'
}
}),
parentId: $.type(ID).optional.nullable.note({
desc: {
'ja-JP': '親フォルダID',
'en-US': 'Parent folder ID'
}
})
}
};
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'name' parameter
const [name = '無題のフォルダー', nameErr] = $.str.optional.pipe(isValidFolderName).get(params.name);
if (nameErr) return rej('invalid name param');
// Get 'parentId' parameter
const [parentId = null, parentIdErr] = $.type(ID).optional.nullable.get(params.parentId);
if (parentIdErr) return rej('invalid parentId param');
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
// If the parent folder is specified
let parent = null;
if (parentId) {
if (ps.parentId) {
// Fetch parent folder
parent = await DriveFolder
.findOne({
_id: parentId,
_id: ps.parentId,
userId: user._id
});
@ -41,7 +56,7 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
// Create folder
const folder = await DriveFolder.insert({
createdAt: new Date(),
name: name,
name: ps.name,
parentId: parent !== null ? parent._id : null,
userId: user._id
});

View file

@ -1,26 +1,38 @@
import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
import DriveFolder, { pack } from '../../../../../models/drive-folder';
import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';
export const meta = {
stability: 'stable',
desc: {
'ja-JP': '指定したドライブのフォルダの情報を取得します。'
'ja-JP': '指定したドライブのフォルダの情報を取得します。',
'en-US': 'Get specified folder of drive.'
},
requireCredential: true,
kind: 'drive-read'
kind: 'drive-read',
params: {
folderId: $.type(ID).note({
desc: {
'ja-JP': '対象のフォルダID',
'en-US': 'Target folder ID'
}
})
}
};
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'folderId' parameter
const [folderId, folderIdErr] = $.type(ID).get(params.folderId);
if (folderIdErr) return rej('invalid folderId param');
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
// Get folder
const folder = await DriveFolder
.findOne({
_id: folderId,
_id: ps.folderId,
userId: user._id
});

View file

@ -2,8 +2,11 @@ import $ from 'cafy'; import ID from '../../../../../misc/cafy-id';
import DriveFolder, { isValidFolderName, pack } from '../../../../../models/drive-folder';
import { publishDriveStream } from '../../../../../stream';
import { ILocalUser } from '../../../../../models/user';
import getParams from '../../../get-params';
export const meta = {
stability: 'stable',
desc: {
'ja-JP': '指定したドライブのフォルダの情報を更新します。',
'en-US': 'Update specified folder of drive.'
@ -11,18 +14,40 @@ export const meta = {
requireCredential: true,
kind: 'drive-write'
kind: 'drive-write',
params: {
folderId: $.type(ID).note({
desc: {
'ja-JP': '対象のフォルダID',
'en-US': 'Target folder ID'
}
}),
name: $.str.optional.pipe(isValidFolderName).note({
desc: {
'ja-JP': 'フォルダ名',
'en-US': 'Folder name'
}
}),
parentId: $.type(ID).optional.nullable.note({
desc: {
'ja-JP': '親フォルダID',
'en-US': 'Parent folder ID'
}
})
}
};
export default (params: any, user: ILocalUser) => new Promise(async (res, rej) => {
// Get 'folderId' parameter
const [folderId, folderIdErr] = $.type(ID).get(params.folderId);
if (folderIdErr) return rej('invalid folderId param');
const [ps, psErr] = getParams(meta, params);
if (psErr) return rej(psErr);
// Fetch folder
const folder = await DriveFolder
.findOne({
_id: folderId,
_id: ps.folderId,
userId: user._id
});
@ -30,22 +55,16 @@ export default (params: any, user: ILocalUser) => new Promise(async (res, rej) =
return rej('folder-not-found');
}
// Get 'name' parameter
const [name, nameErr] = $.str.optional.pipe(isValidFolderName).get(params.name);
if (nameErr) return rej('invalid name param');
if (name) folder.name = name;
if (ps.name) folder.name = ps.name;
// Get 'parentId' parameter
const [parentId, parentIdErr] = $.type(ID).optional.nullable.get(params.parentId);
if (parentIdErr) return rej('invalid parentId param');
if (parentId !== undefined) {
if (parentId === null) {
if (ps.parentId !== undefined) {
if (ps.parentId === null) {
folder.parentId = null;
} else {
// Get parent folder
const parent = await DriveFolder
.findOne({
_id: parentId,
_id: ps.parentId,
userId: user._id
});