2019-04-28 19:11:57 -05:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
|
|
|
import { Page } from '../entities/page';
|
2019-06-27 04:04:09 -05:00
|
|
|
import { SchemaType } from '../../misc/schema';
|
2019-05-17 05:56:47 -05:00
|
|
|
import { Users, DriveFiles, PageLikes } from '..';
|
2019-04-28 19:11:57 -05:00
|
|
|
import { awaitAll } from '../../prelude/await-all';
|
|
|
|
import { DriveFile } from '../entities/drive-file';
|
2019-05-17 05:56:47 -05:00
|
|
|
import { User } from '../entities/user';
|
|
|
|
import { ensure } from '../../prelude/ensure';
|
2019-04-28 19:11:57 -05:00
|
|
|
|
|
|
|
export type PackedPage = SchemaType<typeof packedPageSchema>;
|
|
|
|
|
|
|
|
@EntityRepository(Page)
|
|
|
|
export class PageRepository extends Repository<Page> {
|
|
|
|
public async pack(
|
2019-05-17 05:56:47 -05:00
|
|
|
src: Page['id'] | Page,
|
|
|
|
me?: User['id'] | User | null | undefined,
|
2019-04-28 19:11:57 -05:00
|
|
|
): Promise<PackedPage> {
|
2019-05-17 05:56:47 -05:00
|
|
|
const meId = me ? typeof me === 'string' ? me : me.id : null;
|
|
|
|
const page = typeof src === 'object' ? src : await this.findOne(src).then(ensure);
|
|
|
|
|
2019-04-28 19:11:57 -05:00
|
|
|
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,
|
2019-05-17 05:56:47 -05:00
|
|
|
userId: page.userId
|
2019-04-28 19:11:57 -05:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
if (x.children) {
|
|
|
|
collectFile(x.children);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-05-17 05:56:47 -05:00
|
|
|
collectFile(page.content);
|
2019-04-29 22:15:41 -05: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 05:56:47 -05:00
|
|
|
migrate(page.content);
|
2019-04-29 22:15:41 -05:00
|
|
|
if (migrated) {
|
2019-05-17 05:56:47 -05:00
|
|
|
this.update(page.id, {
|
|
|
|
content: page.content
|
2019-04-29 22:15:41 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-04-28 19:11:57 -05:00
|
|
|
return await awaitAll({
|
2019-05-17 05:56:47 -05:00
|
|
|
id: page.id,
|
|
|
|
createdAt: page.createdAt.toISOString(),
|
|
|
|
updatedAt: page.updatedAt.toISOString(),
|
|
|
|
userId: page.userId,
|
|
|
|
user: Users.pack(page.user || page.userId),
|
|
|
|
content: page.content,
|
|
|
|
variables: page.variables,
|
|
|
|
title: page.title,
|
|
|
|
name: page.name,
|
|
|
|
summary: page.summary,
|
2019-07-06 16:56:13 -05:00
|
|
|
hideTitleWhenPinned: page.hideTitleWhenPinned,
|
2019-05-17 05:56:47 -05:00
|
|
|
alignCenter: page.alignCenter,
|
|
|
|
font: page.font,
|
|
|
|
eyeCatchingImageId: page.eyeCatchingImageId,
|
|
|
|
eyeCatchingImage: page.eyeCatchingImageId ? await DriveFiles.pack(page.eyeCatchingImageId) : null,
|
|
|
|
attachedFiles: DriveFiles.packMany(await Promise.all(attachedFiles)),
|
|
|
|
likedCount: page.likedCount,
|
|
|
|
isLiked: meId ? await PageLikes.findOne({ pageId: page.id, userId: meId }).then(x => x != null) : undefined,
|
2019-04-28 19:11:57 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public packMany(
|
|
|
|
pages: Page[],
|
|
|
|
) {
|
|
|
|
return Promise.all(pages.map(x => this.pack(x)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const packedPageSchema = {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-28 19:11:57 -05:00
|
|
|
properties: {
|
2019-04-30 14:44:46 -05:00
|
|
|
id: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
updatedAt: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
title: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
},
|
|
|
|
name: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
},
|
|
|
|
summary: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
},
|
|
|
|
content: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
},
|
|
|
|
variables: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'array' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
},
|
|
|
|
userId: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
2019-06-27 04:04:09 -05:00
|
|
|
type: 'object' as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
ref: 'User',
|
2019-06-27 04:04:09 -05:00
|
|
|
optional: false as const, nullable: false as const,
|
2019-04-30 14:44:46 -05:00
|
|
|
},
|
2019-04-28 19:11:57 -05:00
|
|
|
}
|
|
|
|
};
|