iceshrimp/src/models/repositories/page.ts

145 lines
3.9 KiB
TypeScript
Raw Normal View History

import { EntityRepository, Repository } from 'typeorm';
import { Page } from '../entities/page';
2021-03-23 17:43:07 +09:00
import { SchemaType } from '@/misc/schema';
import { Users, DriveFiles, PageLikes } from '..';
import { awaitAll } from '../../prelude/await-all';
import { DriveFile } from '../entities/drive-file';
import { User } from '../entities/user';
export type PackedPage = SchemaType<typeof packedPageSchema>;
@EntityRepository(Page)
export class PageRepository extends Repository<Page> {
public async pack(
src: Page['id'] | Page,
me?: User['id'] | User | null | undefined,
): Promise<PackedPage> {
const meId = me ? typeof me === 'string' ? me : me.id : null;
2021-02-13 15:33:38 +09:00
const page = typeof src === 'object' ? src : await this.findOneOrFail(src);
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: page.userId
}));
}
if (x.children) {
collectFile(x.children);
}
}
};
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);
}
}
};
migrate(page.content);
2019-04-30 12:15:41 +09:00
if (migrated) {
this.update(page.id, {
content: page.content
2019-04-30 12:15:41 +09:00
});
}
return await awaitAll({
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 } すると無限ループするので注意
content: page.content,
variables: page.variables,
title: page.title,
name: page.name,
summary: page.summary,
hideTitleWhenPinned: page.hideTitleWhenPinned,
alignCenter: page.alignCenter,
font: page.font,
2020-04-13 03:23:23 +09:00
script: page.script,
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,
});
}
public packMany(
pages: Page[],
me?: User['id'] | User | null | undefined,
) {
return Promise.all(pages.map(x => this.pack(x, me)));
}
}
export const packedPageSchema = {
2019-06-27 18:04:09 +09:00
type: 'object' as const,
optional: false as const, nullable: false as const,
properties: {
2019-05-01 04:44:46 +09:00
id: {
2019-06-27 18:04:09 +09:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
format: 'id',
example: 'xxxxxxxxxx',
},
createdAt: {
2019-06-27 18:04:09 +09:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
format: 'date-time',
},
updatedAt: {
2019-06-27 18:04:09 +09:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
format: 'date-time',
},
title: {
2019-06-27 18:04:09 +09:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
},
name: {
2019-06-27 18:04:09 +09:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
},
summary: {
2019-06-27 18:04:09 +09:00
type: 'string' as const,
optional: false as const, nullable: true as const,
2019-05-01 04:44:46 +09:00
},
content: {
2019-06-27 18:04:09 +09:00
type: 'array' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
},
variables: {
2019-06-27 18:04:09 +09:00
type: 'array' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
},
userId: {
2019-06-27 18:04:09 +09:00
type: 'string' as const,
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
format: 'id',
},
user: {
2019-06-27 18:04:09 +09:00
type: 'object' as const,
2019-05-01 04:44:46 +09:00
ref: 'User',
2019-06-27 18:04:09 +09:00
optional: false as const, nullable: false as const,
2019-05-01 04:44:46 +09:00
},
}
};