Pages (#4811)
* wip * wip * wip * Update page-editor.vue * wip * wip * wip * wip * wip * wip * wip * Update page-editor.variable.core.vue * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Update aiscript.ts * wip * Update package.json * wip * wip * wip * wip * wip * Update page.vue * wip * wip * wip * wip * more info * wip fn * wip * wip * wip
This commit is contained in:
61
src/models/repositories/page.ts
Normal file
61
src/models/repositories/page.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
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<typeof packedPageSchema>;
|
||||
|
||||
@EntityRepository(Page)
|
||||
export class PageRepository extends Repository<Page> {
|
||||
public async pack(
|
||||
src: Page,
|
||||
): Promise<PackedPage> {
|
||||
const attachedFiles: Promise<DriveFile | undefined>[] = [];
|
||||
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: {
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user