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