2019-04-29 00:11:57 +00:00
|
|
|
import $ from 'cafy';
|
2021-11-12 10:47:04 +00:00
|
|
|
import ms from 'ms';
|
2021-08-19 12:55:45 +00:00
|
|
|
import define from '../../define';
|
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import { Pages, DriveFiles } from '@/models/index';
|
|
|
|
import { genId } from '@/misc/gen-id';
|
|
|
|
import { Page } from '@/models/entities/page';
|
|
|
|
import { ApiError } from '../../error';
|
2019-04-29 00:11:57 +00:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['pages'],
|
|
|
|
|
2020-02-15 12:33:32 +00:00
|
|
|
requireCredential: true as const,
|
2019-04-29 00:11:57 +00:00
|
|
|
|
|
|
|
kind: 'write:pages',
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
2021-12-09 14:58:30 +00:00
|
|
|
max: 300,
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
params: {
|
|
|
|
title: {
|
|
|
|
validator: $.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
name: {
|
2019-09-01 21:02:35 +00:00
|
|
|
validator: $.str.min(1),
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
summary: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
content: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.arr($.obj()),
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
variables: {
|
2021-12-09 14:58:30 +00:00
|
|
|
validator: $.arr($.obj()),
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
|
|
|
|
2020-04-12 18:23:23 +00:00
|
|
|
script: {
|
|
|
|
validator: $.str,
|
|
|
|
},
|
|
|
|
|
2019-04-29 00:11:57 +00:00
|
|
|
eyeCatchingImageId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
font: {
|
|
|
|
validator: $.optional.str.or(['serif', 'sans-serif']),
|
2021-12-09 14:58:30 +00:00
|
|
|
default: 'sans-serif',
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
alignCenter: {
|
|
|
|
validator: $.optional.bool,
|
2021-12-09 14:58:30 +00:00
|
|
|
default: false,
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
2019-07-06 21:56:13 +00:00
|
|
|
|
|
|
|
hideTitleWhenPinned: {
|
|
|
|
validator: $.optional.bool,
|
2021-12-09 14:58:30 +00:00
|
|
|
default: false,
|
2019-07-06 21:56:13 +00:00
|
|
|
},
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2019-06-27 09:04:09 +00:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-29 00:11:57 +00:00
|
|
|
ref: 'Page',
|
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: 'b7b97489-0f66-4b12-a5ff-b21bd63f6e1c',
|
2019-04-29 00:11:57 +00:00
|
|
|
},
|
2019-08-27 23:00:05 +00:00
|
|
|
nameAlreadyExists: {
|
|
|
|
message: 'Specified name already exists.',
|
|
|
|
code: 'NAME_ALREADY_EXISTS',
|
2021-12-09 14:58:30 +00:00
|
|
|
id: '4650348e-301c-499a-83c9-6aa988c66bc1',
|
|
|
|
},
|
|
|
|
},
|
2019-04-29 00:11:57 +00:00
|
|
|
};
|
|
|
|
|
2022-01-02 17:12:50 +00:00
|
|
|
// eslint-disable-next-line import/no-default-export
|
2019-04-29 00:11:57 +00:00
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
let eyeCatchingImage = null;
|
|
|
|
if (ps.eyeCatchingImageId != null) {
|
|
|
|
eyeCatchingImage = await DriveFiles.findOne({
|
|
|
|
id: ps.eyeCatchingImageId,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2019-04-29 00:11:57 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (eyeCatchingImage == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 23:00:05 +00:00
|
|
|
await Pages.find({
|
|
|
|
userId: user.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
name: ps.name,
|
2019-08-27 23:00:05 +00:00
|
|
|
}).then(result => {
|
|
|
|
if (result.length > 0) {
|
|
|
|
throw new ApiError(meta.errors.nameAlreadyExists);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-29 00:11:57 +00:00
|
|
|
const page = await Pages.save(new Page({
|
|
|
|
id: genId(),
|
|
|
|
createdAt: new Date(),
|
|
|
|
updatedAt: new Date(),
|
|
|
|
title: ps.title,
|
|
|
|
name: ps.name,
|
|
|
|
summary: ps.summary,
|
|
|
|
content: ps.content,
|
|
|
|
variables: ps.variables,
|
2020-04-12 18:23:23 +00:00
|
|
|
script: ps.script,
|
2019-04-29 00:11:57 +00:00
|
|
|
eyeCatchingImageId: eyeCatchingImage ? eyeCatchingImage.id : null,
|
|
|
|
userId: user.id,
|
|
|
|
visibility: 'public',
|
|
|
|
alignCenter: ps.alignCenter,
|
2019-07-06 21:56:13 +00:00
|
|
|
hideTitleWhenPinned: ps.hideTitleWhenPinned,
|
2021-12-09 14:58:30 +00:00
|
|
|
font: ps.font,
|
2019-04-29 00:11:57 +00:00
|
|
|
}));
|
|
|
|
|
|
|
|
return await Pages.pack(page);
|
|
|
|
});
|