2022-09-18 03:27:08 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-21 05:33:11 +09:00
|
|
|
import type { GalleryLikesRepository, GalleryPostsRepository } from '@/models/index.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import type { Packed } from '@/misc/schema.js';
|
|
|
|
import type { } from '@/models/entities/Blocking.js';
|
|
|
|
import type { User } from '@/models/entities/User.js';
|
|
|
|
import type { GalleryPost } from '@/models/entities/GalleryPost.js';
|
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
import { DriveFileEntityService } from './DriveFileEntityService.js';
|
2022-12-04 15:03:09 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-18 03:27:08 +09:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class GalleryPostEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.galleryPostsRepository)
|
|
|
|
private galleryPostsRepository: GalleryPostsRepository,
|
|
|
|
|
|
|
|
@Inject(DI.galleryLikesRepository)
|
|
|
|
private galleryLikesRepository: GalleryLikesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
|
|
|
private driveFileEntityService: DriveFileEntityService,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public async pack(
|
|
|
|
src: GalleryPost['id'] | GalleryPost,
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
): Promise<Packed<'GalleryPost'>> {
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
const post = typeof src === 'object' ? src : await this.galleryPostsRepository.findOneByOrFail({ id: src });
|
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: post.id,
|
|
|
|
createdAt: post.createdAt.toISOString(),
|
|
|
|
updatedAt: post.updatedAt.toISOString(),
|
|
|
|
userId: post.userId,
|
|
|
|
user: this.userEntityService.pack(post.user ?? post.userId, me),
|
|
|
|
title: post.title,
|
|
|
|
description: post.description,
|
|
|
|
fileIds: post.fileIds,
|
2023-03-03 10:06:59 +09:00
|
|
|
// TODO: packMany causes N+1 queries
|
|
|
|
files: this.driveFileEntityService.packManyByIds(post.fileIds),
|
2022-09-18 03:27:08 +09:00
|
|
|
tags: post.tags.length > 0 ? post.tags : undefined,
|
|
|
|
isSensitive: post.isSensitive,
|
|
|
|
likedCount: post.likedCount,
|
|
|
|
isLiked: meId ? await this.galleryLikesRepository.findOneBy({ postId: post.id, userId: meId }).then(x => x != null) : undefined,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 15:03:09 +09:00
|
|
|
@bindThis
|
2022-09-18 03:27:08 +09:00
|
|
|
public packMany(
|
|
|
|
posts: GalleryPost[],
|
|
|
|
me?: { id: User['id'] } | null | undefined,
|
|
|
|
) {
|
|
|
|
return Promise.all(posts.map(x => this.pack(x, me)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|