2023-07-27 05:31:52 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { GalleryLikesRepository, GalleryPostsRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { } from '@/models/entities/Blocking.js';
|
2023-08-16 08:51:28 +00:00
|
|
|
import type { MiUser } from '@/models/entities/User.js';
|
|
|
|
import type { MiGalleryPost } from '@/models/entities/GalleryPost.js';
|
2023-03-10 05:22:37 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
import { DriveFileEntityService } from './DriveFileEntityService.js';
|
|
|
|
|
|
|
|
@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 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public async pack(
|
2023-08-16 08:51:28 +00:00
|
|
|
src: MiGalleryPost['id'] | MiGalleryPost,
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
): 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-04 07:48:50 +00:00
|
|
|
// TODO: packMany causes N+1 queries
|
|
|
|
files: this.driveFileEntityService.packManyByIds(post.fileIds),
|
2022-09-17 18:27:08 +00:00
|
|
|
tags: post.tags.length > 0 ? post.tags : undefined,
|
|
|
|
isSensitive: post.isSensitive,
|
|
|
|
likedCount: post.likedCount,
|
2023-07-11 05:58:58 +00:00
|
|
|
isLiked: meId ? await this.galleryLikesRepository.exist({ where: { postId: post.id, userId: meId } }) : undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-17 18:27:08 +00:00
|
|
|
public packMany(
|
2023-08-16 08:51:28 +00:00
|
|
|
posts: MiGalleryPost[],
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2022-09-17 18:27:08 +00:00
|
|
|
) {
|
|
|
|
return Promise.all(posts.map(x => this.pack(x, me)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|