2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-04-17 06:14:29 +02:00
|
|
|
import { IsNull } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { UsersRepository, PagesRepository } from '@/models/_.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiPage } from '@/models/Page.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
|
|
|
import { PageEntityService } from '@/core/entities/PageEntityService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-04-17 06:14:29 +02:00
|
|
|
import { ApiError } from '../../error.js';
|
2019-04-29 02:11:57 +02:00
|
|
|
|
|
|
|
export const meta = {
|
|
|
|
tags: ['pages'],
|
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: false,
|
2019-04-29 02:11:57 +02:00
|
|
|
|
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
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',
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2019-04-29 02:11:57 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
2023-02-26 03:28:05 +01:00
|
|
|
properties: {
|
|
|
|
pageId: { type: 'string', format: 'misskey:id' },
|
|
|
|
name: { type: 'string' },
|
|
|
|
username: { type: 'string' },
|
|
|
|
},
|
2022-04-03 06:57:26 +02:00
|
|
|
anyOf: [
|
2023-02-26 03:28:05 +01:00
|
|
|
{ required: ['pageId'] },
|
|
|
|
{ required: ['name', 'username'] },
|
2022-04-03 06:57:26 +02:00
|
|
|
],
|
2022-02-19 06:05:32 +01:00
|
|
|
} as const;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
2023-08-17 14:20:58 +02:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
2019-04-29 02:11:57 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.pagesRepository)
|
|
|
|
private pagesRepository: PagesRepository,
|
2019-04-29 02:11:57 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
private pageEntityService: PageEntityService,
|
|
|
|
) {
|
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-08-16 10:51:28 +02:00
|
|
|
let page: MiPage | null = null;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
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,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-04-29 02:11:57 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (page == null) {
|
|
|
|
throw new ApiError(meta.errors.noSuchPage);
|
|
|
|
}
|
|
|
|
|
|
|
|
return await this.pageEntityService.pack(page, me);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|