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
|
|
|
|
*/
|
|
|
|
|
2023-07-15 02:57:58 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { RegistrationTicketsRepository } from '@/models/_.js';
|
2023-07-15 02:57:58 +02:00
|
|
|
import { awaitAll } from '@/misc/prelude/await-all.js';
|
|
|
|
import type { Packed } from '@/misc/json-schema.js';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser } from '@/models/User.js';
|
|
|
|
import type { MiRegistrationTicket } from '@/models/RegistrationTicket.js';
|
2023-07-15 02:57:58 +02:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-10-16 03:45:22 +02:00
|
|
|
import { IdService } from '@/core/IdService.js';
|
2024-05-31 08:32:42 +02:00
|
|
|
import { isNotNull } from '@/misc/is-not-null.js';
|
2023-07-15 02:57:58 +02:00
|
|
|
import { UserEntityService } from './UserEntityService.js';
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class InviteCodeEntityService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.registrationTicketsRepository)
|
|
|
|
private registrationTicketsRepository: RegistrationTicketsRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2023-10-16 03:45:22 +02:00
|
|
|
private idService: IdService,
|
2023-07-15 02:57:58 +02:00
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
|
|
|
public async pack(
|
2023-08-16 10:51:28 +02:00
|
|
|
src: MiRegistrationTicket['id'] | MiRegistrationTicket,
|
|
|
|
me?: { id: MiUser['id'] } | null | undefined,
|
2024-05-31 08:32:42 +02:00
|
|
|
hints?: {
|
|
|
|
packedCreatedBy?: Packed<'UserLite'>,
|
|
|
|
packedUsedBy?: Packed<'UserLite'>,
|
|
|
|
},
|
2023-07-15 02:57:58 +02:00
|
|
|
): Promise<Packed<'InviteCode'>> {
|
|
|
|
const target = typeof src === 'object' ? src : await this.registrationTicketsRepository.findOneOrFail({
|
|
|
|
where: {
|
|
|
|
id: src,
|
|
|
|
},
|
|
|
|
relations: ['createdBy', 'usedBy'],
|
|
|
|
});
|
|
|
|
|
|
|
|
return await awaitAll({
|
|
|
|
id: target.id,
|
|
|
|
code: target.code,
|
|
|
|
expiresAt: target.expiresAt ? target.expiresAt.toISOString() : null,
|
2023-10-16 03:45:22 +02:00
|
|
|
createdAt: this.idService.parse(target.id).date.toISOString(),
|
2024-05-31 08:32:42 +02:00
|
|
|
createdBy: target.createdBy ? hints?.packedCreatedBy ?? await this.userEntityService.pack(target.createdBy, me) : null,
|
|
|
|
usedBy: target.usedBy ? hints?.packedUsedBy ?? await this.userEntityService.pack(target.usedBy, me) : null,
|
2023-07-15 02:57:58 +02:00
|
|
|
usedAt: target.usedAt ? target.usedAt.toISOString() : null,
|
|
|
|
used: !!target.usedAt,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@bindThis
|
2024-05-31 08:32:42 +02:00
|
|
|
public async packMany(
|
|
|
|
tickets: MiRegistrationTicket[],
|
2023-08-16 10:51:28 +02:00
|
|
|
me: { id: MiUser['id'] },
|
2023-07-15 02:57:58 +02:00
|
|
|
) {
|
2024-05-31 08:32:42 +02:00
|
|
|
const _createdBys = tickets.map(({ createdBy, createdById }) => createdBy ?? createdById).filter(isNotNull);
|
|
|
|
const _usedBys = tickets.map(({ usedBy, usedById }) => usedBy ?? usedById).filter(isNotNull);
|
|
|
|
const _userMap = await this.userEntityService.packMany([..._createdBys, ..._usedBys], me)
|
|
|
|
.then(users => new Map(users.map(u => [u.id, u])));
|
|
|
|
return Promise.all(
|
|
|
|
tickets.map(ticket => {
|
|
|
|
const packedCreatedBy = ticket.createdById != null ? _userMap.get(ticket.createdById) : undefined;
|
|
|
|
const packedUsedBy = ticket.usedById != null ? _userMap.get(ticket.usedById) : undefined;
|
|
|
|
return this.pack(ticket, me, { packedCreatedBy, packedUsedBy });
|
|
|
|
}),
|
|
|
|
);
|
2023-07-15 02:57:58 +02:00
|
|
|
}
|
|
|
|
}
|