2019-04-29 02:11:57 +02:00
|
|
|
import $ from 'cafy';
|
2021-08-19 14:55:45 +02:00
|
|
|
import define from '../../define';
|
|
|
|
import { ApiError } from '../../error';
|
|
|
|
import { Pages, Users } from '@/models/index';
|
|
|
|
import { ID } from '@/misc/cafy-id';
|
|
|
|
import { Page } from '@/models/entities/page';
|
2019-04-29 02:11:57 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['pages'],
|
|
|
|
|
2020-02-15 13:33:32 +01:00
|
|
|
requireCredential: false as const,
|
2019-04-29 02:11:57 +02:00
|
|
|
|
|
|
|
params: {
|
|
|
|
pageId: {
|
|
|
|
validator: $.optional.type(ID),
|
|
|
|
},
|
|
|
|
|
|
|
|
name: {
|
|
|
|
validator: $.optional.str,
|
|
|
|
},
|
|
|
|
|
|
|
|
username: {
|
|
|
|
validator: $.optional.str,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
res: {
|
2019-06-27 11:04:09 +02:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-29 02:11:57 +02:00
|
|
|
ref: 'Page',
|
|
|
|
},
|
|
|
|
|
|
|
|
errors: {
|
|
|
|
noSuchPage: {
|
|
|
|
message: 'No such page.',
|
|
|
|
code: 'NO_SUCH_PAGE',
|
2021-12-09 15:58:30 +01:00
|
|
|
id: '222120c0-3ead-4528-811b-b96f233388d7',
|
|
|
|
},
|
|
|
|
},
|
2019-04-29 02:11:57 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
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,
|
2021-12-09 15:58:30 +01:00
|
|
|
usernameLower: ps.username.toLowerCase(),
|
2019-04-29 02:11:57 +02:00
|
|
|
});
|
|
|
|
if (author) {
|
|
|
|
page = await Pages.findOne({
|
|
|
|
name: ps.name,
|
2021-12-09 15:58:30 +01:00
|
|
|
userId: author.id,
|
2019-04-29 02:11:57 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (page == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchPage);
|
|
|
|
}
|
|
|
|
|
2019-05-17 12:56:47 +02:00
|
|
|
return await Pages.pack(page, user);
|
2019-04-29 02:11:57 +02:00
|
|
|
});
|