2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { GalleryLikesRepository, GalleryPostsRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { } from '@/models/Blocking.js';
|
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiGalleryPost } from '@/models/GalleryPost.js';
|
2023-03-10 06:22:37 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-16 03:45:22 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2022-09-17 20:27:08 +02: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,
|
2023-10-16 03:45:22 +02:00
|
|
|
private idService: IdService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2022-09-17 20:27:08 +02:00
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiGalleryPost['id'] | MiGalleryPost,
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2024-05-31 08:32:42 +02:00
|
|
|
hint?: {
|
|
|
|
packedUser?: Packed<'UserLite'>
|
|
|
|
},
|
2022-09-17 20:27:08 +02: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,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(post.id).date.toISOString(),
|
2022-09-17 20:27:08 +02:00
|
|
|
updatedAt: post.updatedAt.toISOString(),
|
|
|
|
userId: post.userId,
|
2024-05-31 08:32:42 +02:00
|
|
|
user: hint?.packedUser ?? this.userEntityService.pack(post.user ?? post.userId, me),
|
2022-09-17 20:27:08 +02:00
|
|
|
title: post.title,
|
|
|
|
description: post.description,
|
|
|
|
fileIds: post.fileIds,
|
2023-03-04 08:48:50 +01:00
|
|
|
// TODO: packMany causes N+1 queries
|
|
|
|
files: this.driveFileEntityService.packManyByIds(post.fileIds),
|
2022-09-17 20:27:08 +02:00
|
|
|
tags: post.tags.length > 0 ? post.tags : undefined,
|
|
|
|
isSensitive: post.isSensitive,
|
|
|
|
likedCount: post.likedCount,
|
2024-02-08 08:04:41 +01:00
|
|
|
isLiked: meId ? await this.galleryLikesRepository.exists({ where: { postId: post.id, userId: meId } }) : undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2024-05-31 08:32:42 +02:00
|
|
|
public async packMany(
|
2023-08-16 10:51:28 +02:00
|
|
|
posts: MiGalleryPost[],
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
2024-05-31 08:32:42 +02:00
|
|
|
const _users = posts.map(({ user, userId }) => user ?? userId);
|
|
|
|
const _userMap = await this.userEntityService.packMany(_users, me)
|
|
|
|
.then(users => new Map(users.map(u => [u.id, u])));
|
|
|
|
return Promise.all(posts.map(post => this.pack(post, me, { packedUser: _userMap.get(post.userId) })));
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|