2019-04-29 09:11:57 +09:00
|
|
|
import $ from 'cafy';
|
|
|
|
import * as ms from 'ms';
|
2021-08-19 18:33:41 +09:00
|
|
|
import define from '../../define.js';
|
|
|
|
import { ApiError } from '../../error.js';
|
|
|
|
import { Pages, DriveFiles } from '@/models/index.js';
|
|
|
|
import { ID } from '@/misc/cafy-id.js';
|
2019-08-28 08:00:05 +09:00
|
|
|
import { Not } from 'typeorm';
|
2019-04-29 09:11:57 +09:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['pages'],
|
|
|
|
|
2020-02-15 21:33:32 +09:00
|
|
|
requireCredential: true as const,
|
2019-04-29 09:11:57 +09:00
|
|
|
|
|
|
|
kind: 'write:pages',
|
|
|
|
|
|
|
|
limit: {
|
|
|
|
duration: ms('1hour'),
|
|
|
|
max: 300
|
|
|
|
},
|
|
|
|
|
|
|
|
params: {
|
|
|
|
pageId: {
|
|
|
|
validator: $.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
title: {
|
|
|
|
validator: $.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
name: {
|
2019-09-02 06:02:35 +09:00
|
|
|
validator: $.str.min(1),
|
2019-04-29 09:11:57 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
summary: {
|
|
|
|
validator: $.optional.nullable.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
content: {
|
|
|
|
validator: $.arr($.obj())
|
|
|
|
},
|
|
|
|
|
|
|
|
variables: {
|
|
|
|
validator: $.arr($.obj())
|
|
|
|
},
|
|
|
|
|
2020-04-13 03:23:23 +09:00
|
|
|
script: {
|
|
|
|
validator: $.str,
|
|
|
|
},
|
|
|
|
|
2019-04-29 09:11:57 +09:00
|
|
|
eyeCatchingImageId: {
|
|
|
|
validator: $.optional.nullable.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
font: {
|
|
|
|
validator: $.optional.str.or(['serif', 'sans-serif']),
|
|
|
|
},
|
|
|
|
|
|
|
|
alignCenter: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
2019-07-07 06:56:13 +09:00
|
|
|
|
|
|
|
hideTitleWhenPinned: {
|
|
|
|
validator: $.optional.bool,
|
|
|
|
},
|
2019-04-29 09:11:57 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchPage: {
|
|
|
|
message: 'No such page.',
|
|
|
|
code: 'NO_SUCH_PAGE',
|
|
|
|
id: '21149b9e-3616-4778-9592-c4ce89f5a864'
|
|
|
|
},
|
|
|
|
|
|
|
|
accessDenied: {
|
|
|
|
message: 'Access denied.',
|
|
|
|
code: 'ACCESS_DENIED',
|
|
|
|
id: '3c15cd52-3b4b-4274-967d-6456fc4f792b'
|
|
|
|
},
|
|
|
|
|
|
|
|
noSuchFile: {
|
|
|
|
message: 'No such file.',
|
|
|
|
code: 'NO_SUCH_FILE',
|
|
|
|
id: 'cfc23c7c-3887-490e-af30-0ed576703c82'
|
|
|
|
},
|
2019-08-28 08:00:05 +09:00
|
|
|
nameAlreadyExists: {
|
|
|
|
message: 'Specified name already exists.',
|
|
|
|
code: 'NAME_ALREADY_EXISTS',
|
|
|
|
id: '2298a392-d4a1-44c5-9ebb-ac1aeaa5a9ab'
|
|
|
|
}
|
2019-04-29 09:11:57 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
const page = await Pages.findOne(ps.pageId);
|
|
|
|
if (page == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchPage);
|
|
|
|
}
|
|
|
|
if (page.userId !== user.id) {
|
|
|
|
throw new ApiError(meta.errors.accessDenied);
|
|
|
|
}
|
|
|
|
|
|
|
|
let eyeCatchingImage = null;
|
|
|
|
if (ps.eyeCatchingImageId != null) {
|
|
|
|
eyeCatchingImage = await DriveFiles.findOne({
|
|
|
|
id: ps.eyeCatchingImageId,
|
|
|
|
userId: user.id
|
|
|
|
});
|
|
|
|
|
|
|
|
if (eyeCatchingImage == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 08:00:05 +09:00
|
|
|
await Pages.find({
|
|
|
|
id: Not(ps.pageId),
|
|
|
|
userId: user.id,
|
|
|
|
name: ps.name
|
|
|
|
}).then(result => {
|
|
|
|
if (result.length > 0) {
|
|
|
|
throw new ApiError(meta.errors.nameAlreadyExists);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-29 09:11:57 +09:00
|
|
|
await Pages.update(page.id, {
|
|
|
|
updatedAt: new Date(),
|
|
|
|
title: ps.title,
|
|
|
|
name: ps.name === undefined ? page.name : ps.name,
|
|
|
|
summary: ps.name === undefined ? page.summary : ps.summary,
|
|
|
|
content: ps.content,
|
|
|
|
variables: ps.variables,
|
2020-04-13 03:23:23 +09:00
|
|
|
script: ps.script,
|
2019-04-29 09:11:57 +09:00
|
|
|
alignCenter: ps.alignCenter === undefined ? page.alignCenter : ps.alignCenter,
|
2019-07-07 06:56:13 +09:00
|
|
|
hideTitleWhenPinned: ps.hideTitleWhenPinned === undefined ? page.hideTitleWhenPinned : ps.hideTitleWhenPinned,
|
2019-04-29 09:11:57 +09:00
|
|
|
font: ps.font === undefined ? page.font : ps.font,
|
|
|
|
eyeCatchingImageId: ps.eyeCatchingImageId === null
|
|
|
|
? null
|
|
|
|
: ps.eyeCatchingImageId === undefined
|
|
|
|
? page.eyeCatchingImageId
|
|
|
|
: eyeCatchingImage!.id,
|
|
|
|
});
|
|
|
|
});
|