paricafe/packages/backend/src/server/api/endpoints/pages/show.ts
Shun Sakai c2370a1be6
chore: 著作権とライセンスについての情報を各ファイルに追加する (#11348)
* chore: Add the SPDX information to each file

Add copyright and licensing information as defined in version 3.0 of
the REUSE Specification.

* tweak format

---------

Co-authored-by: syuilo <Syuilotan@yahoo.co.jp>
2023-07-27 14:31:52 +09:00

85 lines
2 KiB
TypeScript

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { IsNull } from 'typeorm';
import { Inject, Injectable } from '@nestjs/common';
import type { UsersRepository, PagesRepository } from '@/models/index.js';
import type { Page } from '@/models/entities/Page.js';
import { Endpoint } from '@/server/api/endpoint-base.js';
import { PageEntityService } from '@/core/entities/PageEntityService.js';
import { DI } from '@/di-symbols.js';
import { ApiError } from '../../error.js';
export const meta = {
tags: ['pages'],
requireCredential: false,
res: {
type: 'object',
optional: false, nullable: false,
ref: 'Page',
},
errors: {
noSuchPage: {
message: 'No such page.',
code: 'NO_SUCH_PAGE',
id: '222120c0-3ead-4528-811b-b96f233388d7',
},
},
} as const;
export const paramDef = {
type: 'object',
properties: {
pageId: { type: 'string', format: 'misskey:id' },
name: { type: 'string' },
username: { type: 'string' },
},
anyOf: [
{ required: ['pageId'] },
{ required: ['name', 'username'] },
],
} as const;
// eslint-disable-next-line import/no-default-export
@Injectable()
export default class extends Endpoint<typeof meta, typeof paramDef> {
constructor(
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.pagesRepository)
private pagesRepository: PagesRepository,
private pageEntityService: PageEntityService,
) {
super(meta, paramDef, async (ps, me) => {
let page: Page | null = null;
if (ps.pageId) {
page = await this.pagesRepository.findOneBy({ id: ps.pageId });
} else if (ps.name && ps.username) {
const author = await this.usersRepository.findOneBy({
host: IsNull(),
usernameLower: ps.username.toLowerCase(),
});
if (author) {
page = await this.pagesRepository.findOneBy({
name: ps.name,
userId: author.id,
});
}
}
if (page == null) {
throw new ApiError(meta.errors.noSuchPage);
}
return await this.pageEntityService.pack(page, me);
});
}
}