2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2023-01-05 05:59:48 +01:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { FlashsRepository, FlashLikesRepository } from '@/models/_.js';
|
2023-01-05 05:59:48 +01: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 { MiFlash } from '@/models/Flash.js';
|
2023-01-05 05:59:48 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-16 03:45:22 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2023-01-05 05:59:48 +01: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 03:45:22 +02:00
|
|
|
private idService: IdService,
|
2023-01-05 05:59:48 +01:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiFlash['id'] | MiFlash,
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2023-01-05 05:59:48 +01:00
|
|
|
): Promise<Packed<'Flash'>> {
|
|
|
|
const meId = me ? me.id : null;
|
|
|
|
const flash = typeof src === 'object' ? src : await this.flashsRepository.findOneByOrFail({ id: src });
|
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: flash.id,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(flash.id).date.toISOString(),
|
2023-01-05 05:59:48 +01:00
|
|
|
updatedAt: flash.updatedAt.toISOString(),
|
|
|
|
userId: flash.userId,
|
|
|
|
user: this.userEntityService.pack(flash.user ?? flash.userId, me), // { detail: true } すると無限ループするので注意
|
|
|
|
title: flash.title,
|
|
|
|
summary: flash.summary,
|
|
|
|
script: flash.script,
|
|
|
|
likedCount: flash.likedCount,
|
2023-07-11 07:58:58 +02:00
|
|
|
isLiked: meId ? await this.flashLikesRepository.exist({ where: { flashId: flash.id, userId: meId } }) : undefined,
|
2023-01-05 05:59:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public packMany(
|
2023-08-16 10:51:28 +02:00
|
|
|
flashs: MiFlash[],
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2023-01-05 05:59:48 +01:00
|
|
|
) {
|
|
|
|
return Promise.all(flashs.map(x => this.pack(x, me)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|