2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { DriveFilesRepository, PagesRepository, PageLikesRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { } from '@/models/Blocking.js';
|
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiPage } from '@/models/Page.js';
|
|
|
|
import type { MiDriveFile } from '@/models/DriveFile.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-16 03:45:22 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2024-02-23 06:12:57 +01:00
|
|
|
import { isNotNull } from '@/misc/is-not-null.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
import { DriveFileEntityService } from './DriveFileEntityService.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class PageEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.pagesRepository)
|
|
|
|
private pagesRepository: PagesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.pageLikesRepository)
|
|
|
|
private pageLikesRepository: PageLikesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.driveFilesRepository)
|
|
|
|
private driveFilesRepository: DriveFilesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
2023-10-16 03:45:22 +02:00
|
|
|
private idService: IdService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiPage['id'] | MiPage,
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
): Promise<Packed<'Page'>> {
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
const page = typeof src === 'object' ? src : await this.pagesRepository.findOneByOrFail({ id: src });
|
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
const attachedFiles: Promise<MiDriveFile | null>[] = [];
|
2022-09-17 20:27:08 +02:00
|
|
|
const collectFile = (xs: any[]) => {
|
|
|
|
for (const x of xs) {
|
|
|
|
if (x.type === 'image') {
|
|
|
|
attachedFiles.push(this.driveFilesRepository.findOneBy({
|
|
|
|
id: x.fileId,
|
|
|
|
userId: page.userId,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (x.children) {
|
|
|
|
collectFile(x.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
collectFile(page.content);
|
|
|
|
|
|
|
|
// 後方互換性のため
|
|
|
|
let migrated = false;
|
|
|
|
const migrate = (xs: any[]) => {
|
|
|
|
for (const x of xs) {
|
|
|
|
if (x.type === 'input') {
|
|
|
|
if (x.inputType === 'text') {
|
|
|
|
x.type = 'textInput';
|
|
|
|
}
|
|
|
|
if (x.inputType === 'number') {
|
|
|
|
x.type = 'numberInput';
|
|
|
|
if (x.default) x.default = parseInt(x.default, 10);
|
|
|
|
}
|
|
|
|
migrated = true;
|
|
|
|
}
|
|
|
|
if (x.children) {
|
|
|
|
migrate(x.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
migrate(page.content);
|
|
|
|
if (migrated) {
|
|
|
|
this.pagesRepository.update(page.id, {
|
|
|
|
content: page.content,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: page.id,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(page.id).date.toISOString(),
|
2022-09-17 20:27:08 +02:00
|
|
|
updatedAt: page.updatedAt.toISOString(),
|
|
|
|
userId: page.userId,
|
2024-01-31 07:45:35 +01:00
|
|
|
user: this.userEntityService.pack(page.user ?? page.userId, me), // { schema: 'UserDetailed' } すると無限ループするので注意
|
2022-09-17 20:27:08 +02:00
|
|
|
content: page.content,
|
|
|
|
variables: page.variables,
|
|
|
|
title: page.title,
|
|
|
|
name: page.name,
|
|
|
|
summary: page.summary,
|
|
|
|
hideTitleWhenPinned: page.hideTitleWhenPinned,
|
|
|
|
alignCenter: page.alignCenter,
|
|
|
|
font: page.font,
|
|
|
|
script: page.script,
|
|
|
|
eyeCatchingImageId: page.eyeCatchingImageId,
|
|
|
|
eyeCatchingImage: page.eyeCatchingImageId ? await this.driveFileEntityService.pack(page.eyeCatchingImageId) : null,
|
2024-02-23 06:12:57 +01:00
|
|
|
attachedFiles: this.driveFileEntityService.packMany((await Promise.all(attachedFiles)).filter(isNotNull)),
|
2022-09-17 20:27:08 +02:00
|
|
|
likedCount: page.likedCount,
|
2024-02-08 08:04:41 +01:00
|
|
|
isLiked: meId ? await this.pageLikesRepository.exists({ where: { pageId: page.id, userId: meId } }) : undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public packMany(
|
2023-08-16 10:51:28 +02:00
|
|
|
pages: MiPage[],
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
return Promise.all(pages.map(x => this.pack(x, me)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|