2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2023-01-05 13:59:48 +09:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2024-10-05 14:37:52 +09:00
|
|
|
import type { FlashLikesRepository, FlashsRepository } from '@/models/_.js';
|
2023-03-10 14:22:37 +09:00
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-20 11:33:36 +09:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
|
import type { MiFlash } from '@/models/Flash.js';
|
2023-01-05 13:59:48 +09:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-16 10:45:22 +09:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-01-05 13:59:48 +09:00
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class FlashEntityService {
|
|
|
|
|
constructor(
|
|
|
|
|
@Inject(DI.flashsRepository)
|
|
|
|
|
private flashsRepository: FlashsRepository,
|
|
|
|
|
@Inject(DI.flashLikesRepository)
|
|
|
|
|
private flashLikesRepository: FlashLikesRepository,
|
|
|
|
|
private userEntityService: UserEntityService,
|
2023-10-16 10:45:22 +09:00
|
|
|
private idService: IdService,
|
2023-01-05 13:59:48 +09:00
|
|
|
) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
|
public async pack(
|
2023-08-16 17:51:28 +09:00
|
|
|
src: MiFlash['id'] | MiFlash,
|
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2024-05-31 15:32:42 +09:00
|
|
|
hint?: {
|
2024-10-05 14:37:52 +09:00
|
|
|
packedUser?: Packed<'UserLite'>,
|
|
|
|
|
likedFlashIds?: MiFlash['id'][],
|
2024-05-31 15:32:42 +09:00
|
|
|
},
|
2023-01-05 13:59:48 +09:00
|
|
|
): Promise<Packed<'Flash'>> {
|
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
|
const flash = typeof src === 'object' ? src : await this.flashsRepository.findOneByOrFail({ id: src });
|
|
|
|
|
|
2024-10-05 14:37:52 +09:00
|
|
|
// { schema: 'UserDetailed' } すると無限ループするので注意
|
|
|
|
|
const user = hint?.packedUser ?? await this.userEntityService.pack(flash.user ?? flash.userId, me);
|
|
|
|
|
|
2024-10-08 18:59:10 +09:00
|
|
|
let isLiked = undefined;
|
2024-10-05 14:37:52 +09:00
|
|
|
if (meId) {
|
|
|
|
|
isLiked = hint?.likedFlashIds
|
|
|
|
|
? hint.likedFlashIds.includes(flash.id)
|
|
|
|
|
: await this.flashLikesRepository.exists({ where: { flashId: flash.id, userId: meId } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
2023-01-05 13:59:48 +09:00
|
|
|
id: flash.id,
|
2023-10-16 10:45:22 +09:00
|
|
|
createdAt: this.idService.parse(flash.id).date.toISOString(),
|
2023-01-05 13:59:48 +09:00
|
|
|
updatedAt: flash.updatedAt.toISOString(),
|
|
|
|
|
userId: flash.userId,
|
2024-10-05 14:37:52 +09:00
|
|
|
user: user,
|
2023-01-05 13:59:48 +09:00
|
|
|
title: flash.title,
|
|
|
|
|
summary: flash.summary,
|
|
|
|
|
script: flash.script,
|
2024-08-10 09:34:49 +09:00
|
|
|
visibility: flash.visibility,
|
2023-01-05 13:59:48 +09:00
|
|
|
likedCount: flash.likedCount,
|
2024-10-05 14:37:52 +09:00
|
|
|
isLiked: isLiked,
|
|
|
|
|
};
|
2023-01-05 13:59:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@bindThis
|
2024-05-31 15:32:42 +09:00
|
|
|
public async packMany(
|
|
|
|
|
flashes: MiFlash[],
|
2023-08-16 17:51:28 +09:00
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2023-01-05 13:59:48 +09:00
|
|
|
) {
|
2024-05-31 15:32:42 +09:00
|
|
|
const _users = flashes.map(({ user, userId }) => user ?? userId);
|
|
|
|
|
const _userMap = await this.userEntityService.packMany(_users, me)
|
|
|
|
|
.then(users => new Map(users.map(u => [u.id, u])));
|
2024-10-05 14:37:52 +09:00
|
|
|
const _likedFlashIds = me
|
|
|
|
|
? await this.flashLikesRepository.createQueryBuilder('flashLike')
|
|
|
|
|
.select('flashLike.flashId')
|
|
|
|
|
.where('flashLike.userId = :userId', { userId: me.id })
|
|
|
|
|
.getRawMany<{ flashLike_flashId: string }>()
|
|
|
|
|
.then(likes => [...new Set(likes.map(like => like.flashLike_flashId))])
|
|
|
|
|
: [];
|
|
|
|
|
return Promise.all(
|
|
|
|
|
flashes.map(flash => this.pack(flash, me, {
|
|
|
|
|
packedUser: _userMap.get(flash.userId),
|
|
|
|
|
likedFlashIds: _likedFlashIds,
|
|
|
|
|
})),
|
|
|
|
|
);
|
2023-01-05 13:59:48 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|