2021-04-24 13:38:24 +00:00
|
|
|
import { EntityRepository, Repository } from 'typeorm';
|
2021-08-19 13:04:15 +00:00
|
|
|
import { GalleryPost } from '@/models/entities/gallery-post';
|
2021-09-11 16:12:23 +00:00
|
|
|
import { SchemaType } from '@/misc/schema';
|
2021-08-19 12:55:45 +00:00
|
|
|
import { Users, DriveFiles, GalleryLikes } from '../index';
|
2021-08-19 13:04:15 +00:00
|
|
|
import { awaitAll } from '@/prelude/await-all';
|
|
|
|
import { User } from '@/models/entities/user';
|
2021-04-24 13:38:24 +00:00
|
|
|
|
|
|
|
export type PackedGalleryPost = SchemaType<typeof packedGalleryPostSchema>;
|
|
|
|
|
|
|
|
@EntityRepository(GalleryPost)
|
|
|
|
export class GalleryPostRepository extends Repository<GalleryPost> {
|
|
|
|
public async pack(
|
|
|
|
src: GalleryPost['id'] | GalleryPost,
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
): Promise<PackedGalleryPost> {
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
const post = typeof src === 'object' ? src : await this.findOneOrFail(src);
|
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: post.id,
|
|
|
|
createdAt: post.createdAt.toISOString(),
|
|
|
|
updatedAt: post.updatedAt.toISOString(),
|
|
|
|
userId: post.userId,
|
|
|
|
user: Users.pack(post.user || post.userId, me),
|
|
|
|
title: post.title,
|
|
|
|
description: post.description,
|
|
|
|
fileIds: post.fileIds,
|
|
|
|
files: DriveFiles.packMany(post.fileIds),
|
|
|
|
tags: post.tags.length > 0 ? post.tags : undefined,
|
|
|
|
isSensitive: post.isSensitive,
|
|
|
|
likedCount: post.likedCount,
|
|
|
|
isLiked: meId ? await GalleryLikes.findOne({ postId: post.id, userId: meId }).then(x => x != null) : undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public packMany(
|
|
|
|
posts: GalleryPost[],
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
) {
|
|
|
|
return Promise.all(posts.map(x => this.pack(x, me)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const packedGalleryPostSchema = {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
properties: {
|
|
|
|
id: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'id',
|
|
|
|
example: 'xxxxxxxxxx',
|
|
|
|
},
|
|
|
|
createdAt: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
updatedAt: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'date-time',
|
|
|
|
},
|
|
|
|
title: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: true as const,
|
|
|
|
},
|
|
|
|
userId: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'id',
|
|
|
|
},
|
|
|
|
user: {
|
|
|
|
type: 'object' as const,
|
2021-09-11 16:12:23 +00:00
|
|
|
ref: 'User' as const,
|
2021-04-24 13:38:24 +00:00
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
},
|
|
|
|
fileIds: {
|
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
|
|
|
items: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
format: 'id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
files: {
|
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
|
|
|
items: {
|
|
|
|
type: 'object' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
2021-09-11 16:12:23 +00:00
|
|
|
ref: 'DriveFile' as const,
|
2021-04-24 13:38:24 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
tags: {
|
|
|
|
type: 'array' as const,
|
|
|
|
optional: true as const, nullable: false as const,
|
|
|
|
items: {
|
|
|
|
type: 'string' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
isSensitive: {
|
|
|
|
type: 'boolean' as const,
|
|
|
|
optional: false as const, nullable: false as const,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|