mizzkey/packages/backend/src/server/api/endpoints/pages/update.ts

143 lines
2.9 KiB
TypeScript
Raw Normal View History

import $ from 'cafy';
2021-11-12 11:47:04 +01:00
import ms from 'ms';
import define from '../../define';
import { ApiError } from '../../error';
import { Pages, DriveFiles } from '@/models/index';
import { ID } from '@/misc/cafy-id';
import { Not } from 'typeorm';
export const meta = {
tags: ['pages'],
2020-02-15 13:33:32 +01:00
requireCredential: true as const,
kind: 'write:pages',
limit: {
duration: ms('1hour'),
2021-12-09 15:58:30 +01:00
max: 300,
},
params: {
pageId: {
validator: $.type(ID),
},
title: {
validator: $.str,
},
name: {
validator: $.str.min(1),
},
summary: {
validator: $.optional.nullable.str,
},
content: {
2021-12-09 15:58:30 +01:00
validator: $.arr($.obj()),
},
variables: {
2021-12-09 15:58:30 +01:00
validator: $.arr($.obj()),
},
2020-04-12 20:23:23 +02:00
script: {
validator: $.str,
},
eyeCatchingImageId: {
validator: $.optional.nullable.type(ID),
},
font: {
validator: $.optional.str.or(['serif', 'sans-serif']),
},
alignCenter: {
validator: $.optional.bool,
},
hideTitleWhenPinned: {
validator: $.optional.bool,
},
},
errors: {
noSuchPage: {
message: 'No such page.',
code: 'NO_SUCH_PAGE',
2021-12-09 15:58:30 +01:00
id: '21149b9e-3616-4778-9592-c4ce89f5a864',
},
accessDenied: {
message: 'Access denied.',
code: 'ACCESS_DENIED',
2021-12-09 15:58:30 +01:00
id: '3c15cd52-3b4b-4274-967d-6456fc4f792b',
},
noSuchFile: {
message: 'No such file.',
code: 'NO_SUCH_FILE',
2021-12-09 15:58:30 +01:00
id: 'cfc23c7c-3887-490e-af30-0ed576703c82',
},
nameAlreadyExists: {
message: 'Specified name already exists.',
code: 'NAME_ALREADY_EXISTS',
2021-12-09 15:58:30 +01:00
id: '2298a392-d4a1-44c5-9ebb-ac1aeaa5a9ab',
},
},
};
2022-01-02 18:12:50 +01:00
// eslint-disable-next-line import/no-default-export
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,
2021-12-09 15:58:30 +01:00
userId: user.id,
});
if (eyeCatchingImage == null) {
throw new ApiError(meta.errors.noSuchFile);
}
}
await Pages.find({
id: Not(ps.pageId),
userId: user.id,
2021-12-09 15:58:30 +01:00
name: ps.name,
}).then(result => {
if (result.length > 0) {
throw new ApiError(meta.errors.nameAlreadyExists);
}
});
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-12 20:23:23 +02:00
script: ps.script,
alignCenter: ps.alignCenter === undefined ? page.alignCenter : ps.alignCenter,
hideTitleWhenPinned: ps.hideTitleWhenPinned === undefined ? page.hideTitleWhenPinned : ps.hideTitleWhenPinned,
font: ps.font === undefined ? page.font : ps.font,
eyeCatchingImageId: ps.eyeCatchingImageId === null
? null
: ps.eyeCatchingImageId === undefined
? page.eyeCatchingImageId
: eyeCatchingImage!.id,
});
});