2019-04-29 09:11:57 +09:00
|
|
|
import $ from 'cafy';
|
|
|
|
import define from '../../define';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { Pages, Users } from '../../../../models';
|
2021-03-23 17:43:07 +09:00
|
|
|
import { ID } from '@/misc/cafy-id';
|
2019-04-29 09:11:57 +09:00
|
|
|
import { Page } from '../../../../models/entities/page';
|
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['pages'],
|
|
|
|
|
2020-02-15 21:33:32 +09:00
|
|
|
requireCredential: false as const,
|
2019-04-29 09:11:57 +09:00
|
|
|
|
|
|
|
params: {
|
|
|
|
pageId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
name: {
|
|
|
|
validator: $.optional.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
username: {
|
|
|
|
validator: $.optional.str,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2019-06-27 18:04:09 +09:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-29 09:11:57 +09:00
|
|
|
ref: 'Page',
|
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchPage: {
|
|
|
|
message: 'No such page.',
|
|
|
|
code: 'NO_SUCH_PAGE',
|
|
|
|
id: '222120c0-3ead-4528-811b-b96f233388d7'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export default define(meta, async (ps, user) => {
|
|
|
|
let page: Page | undefined;
|
|
|
|
|
|
|
|
if (ps.pageId) {
|
|
|
|
page = await Pages.findOne(ps.pageId);
|
|
|
|
} else if (ps.name && ps.username) {
|
|
|
|
const author = await Users.findOne({
|
|
|
|
host: null,
|
|
|
|
usernameLower: ps.username.toLowerCase()
|
|
|
|
});
|
|
|
|
if (author) {
|
|
|
|
page = await Pages.findOne({
|
|
|
|
name: ps.name,
|
|
|
|
userId: author.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchPage);
|
|
|
|
}
|
|
|
|
|
2019-05-17 19:56:47 +09:00
|
|
|
return await Pages.pack(page, user);
|
2019-04-29 09:11:57 +09:00
|
|
|
});
|