2022-03-26 15:34:00 +09:00
|
|
|
import { db } from '@/db/postgre.js';
|
2022-02-27 11:07:39 +09:00
|
|
|
import { GalleryPost } from '@/models/entities/gallery-post.js';
|
|
|
|
import { Packed } from '@/misc/schema.js';
|
|
|
|
import { Users, DriveFiles, GalleryLikes } from '../index.js';
|
|
|
|
import { awaitAll } from '@/prelude/await-all.js';
|
|
|
|
import { User } from '@/models/entities/user.js';
|
2021-04-24 22:38:24 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
export const GalleryPostRepository = db.getRepository(GalleryPost).extend({
|
|
|
|
async pack(
|
2021-04-24 22:38:24 +09:00
|
|
|
src: GalleryPost['id'] | GalleryPost,
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
2021-09-22 22:35:55 +09:00
|
|
|
): Promise<Packed<'GalleryPost'>> {
|
2021-04-24 22:38:24 +09:00
|
|
|
const meId = me ? me.id : null;
|
2022-03-26 15:34:00 +09:00
|
|
|
const post = typeof src === 'object' ? src : await this.findOneByOrFail({ id: src });
|
2021-04-24 22:38:24 +09:00
|
|
|
|
|
|
|
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,
|
2022-03-26 15:34:00 +09:00
|
|
|
isLiked: meId ? await GalleryLikes.findOneBy({ postId: post.id, userId: meId }).then(x => x != null) : undefined,
|
2021-04-24 22:38:24 +09:00
|
|
|
});
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
2021-04-24 22:38:24 +09:00
|
|
|
|
2022-03-26 15:34:00 +09:00
|
|
|
packMany(
|
2021-04-24 22:38:24 +09:00
|
|
|
posts: GalleryPost[],
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
) {
|
|
|
|
return Promise.all(posts.map(x => this.pack(x, me)));
|
2022-03-26 15:34:00 +09:00
|
|
|
},
|
|
|
|
});
|