import { EntityRepository, Repository } from 'typeorm'; import { Page } from '../entities/page'; import { SchemaType, types, bool } from '../../misc/schema'; import { Users, DriveFiles } from '..'; import { awaitAll } from '../../prelude/await-all'; import { DriveFile } from '../entities/drive-file'; export type PackedPage = SchemaType; @EntityRepository(Page) export class PageRepository extends Repository { public async pack( src: Page, ): Promise { const attachedFiles: Promise[] = []; const collectFile = (xs: any[]) => { for (const x of xs) { if (x.type === 'image') { attachedFiles.push(DriveFiles.findOne({ id: x.fileId, userId: src.userId })); } if (x.children) { collectFile(x.children); } } }; collectFile(src.content); return await awaitAll({ id: src.id, createdAt: src.createdAt.toISOString(), updatedAt: src.updatedAt.toISOString(), userId: src.userId, user: Users.pack(src.user || src.userId), content: src.content, variables: src.variables, title: src.title, name: src.name, summary: src.summary, alignCenter: src.alignCenter, font: src.font, eyeCatchingImageId: src.eyeCatchingImageId, eyeCatchingImage: src.eyeCatchingImageId ? await DriveFiles.pack(src.eyeCatchingImageId) : null, attachedFiles: DriveFiles.packMany(await Promise.all(attachedFiles)) }); } public packMany( pages: Page[], ) { return Promise.all(pages.map(x => this.pack(x))); } } export const packedPageSchema = { type: types.object, optional: bool.false, nullable: bool.false, properties: { } };