2022-03-26 15:34:00 +09:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { Page } from '@/models/entities/page.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import { awaitAll } from '@/prelude/await-all.js';
|
|
|
|
import { DriveFile } from '@/models/entities/drive-file.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
2022-04-17 12:59:41 +09:00
|
|
|
import { Users, DriveFiles, PageLikes } from '../index.js';
|
2019-04-29 09:11:57 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
export const PageRepository = db.getRepository(Page).extend({
|
|
|
|
async pack(
|
2019-05-17 19:56:47 +09:00
|
|
|
src: Page['id'] | Page,
|
2021-03-24 11:05:37 +09:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2021-09-22 22:35:55 +09:00
|
|
|
): Promise<Packed<'Page'>> {
|
2021-03-24 11:05:37 +09:00
|
|
|
const meId = me ? me.id : null;
|
2022-03-26 15:34:00 +09:00
|
|
|
const page = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2019-05-17 19:56:47 +09:00
|
|
|
|
2022-04-17 12:59:41 +09:00
|
|
|
const attachedFiles: Promise<DriveFile | null>[] = [];
|
2019-04-29 09:11:57 +09:00
|
|
|
const collectFile = (xs: any[]) => {
|
|
|
|
for (const x of xs) {
|
|
|
|
if (x.type === 'image') {
|
2022-03-26 15:34:00 +09:00
|
|
|
attachedFiles.push(DriveFiles.findOneBy({
|
2019-04-29 09:11:57 +09:00
|
|
|
id: x.fileId,
|
2021-12-09 23:58:30 +09:00
|
|
|
userId: page.userId,
|
2019-04-29 09:11:57 +09:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (x.children) {
|
|
|
|
collectFile(x.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-05-17 19:56:47 +09:00
|
|
|
collectFile(page.content);
|
2019-04-30 12:15:41 +09:00
|
|
|
|
|
|
|
// 後方互換性のため
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-05-17 19:56:47 +09:00
|
|
|
migrate(page.content);
|
2019-04-30 12:15:41 +09:00
|
|
|
if (migrated) {
|
2019-05-17 19:56:47 +09:00
|
|
|
this.update(page.id, {
|
2021-12-09 23:58:30 +09:00
|
|
|
content: page.content,
|
2019-04-30 12:15:41 +09:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-29 09:11:57 +09:00
|
|
|
return await awaitAll({
|
2019-05-17 19:56:47 +09:00
|
|
|
id: page.id,
|
|
|
|
createdAt: page.createdAt.toISOString(),
|
|
|
|
updatedAt: page.updatedAt.toISOString(),
|
|
|
|
userId: page.userId,
|
2020-02-01 11:35:49 +09:00
|
|
|
user: Users.pack(page.user || page.userId, me), // { detail: true } すると無限ループするので注意
|
2019-05-17 19:56:47 +09:00
|
|
|
content: page.content,
|
|
|
|
variables: page.variables,
|
|
|
|
title: page.title,
|
|
|
|
name: page.name,
|
|
|
|
summary: page.summary,
|
2019-07-07 06:56:13 +09:00
|
|
|
hideTitleWhenPinned: page.hideTitleWhenPinned,
|
2019-05-17 19:56:47 +09:00
|
|
|
alignCenter: page.alignCenter,
|
|
|
|
font: page.font,
|
2020-04-13 03:23:23 +09:00
|
|
|
script: page.script,
|
2019-05-17 19:56:47 +09:00
|
|
|
eyeCatchingImageId: page.eyeCatchingImageId,
|
|
|
|
eyeCatchingImage: page.eyeCatchingImageId ? await DriveFiles.pack(page.eyeCatchingImageId) : null,
|
2022-04-17 12:59:41 +09:00
|
|
|
attachedFiles: DriveFiles.packMany((await Promise.all(attachedFiles)).filter((x): x is DriveFile => x != null)),
|
2019-05-17 19:56:47 +09:00
|
|
|
likedCount: page.likedCount,
|
2022-03-26 15:34:00 +09:00
|
|
|
isLiked: meId ? await PageLikes.findOneBy({ pageId: page.id, userId: meId }).then(x => x != null) : undefined,
|
2019-04-29 09:11:57 +09:00
|
|
|
});
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2019-04-29 09:11:57 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
packMany(
|
2019-04-29 09:11:57 +09:00
|
|
|
pages: Page[],
|
2021-03-24 11:05:37 +09:00
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2019-04-29 09:11:57 +09:00
|
|
|
) {
|
2020-11-17 14:59:15 +09:00
|
|
|
return Promise.all(pages.map(x => this.pack(x, me)));
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
|
|
|
});
|