2023-07-27 07:31:52 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Inject, Injectable } from '@nestjs/common';
|
2023-07-15 02:57:58 +02:00
|
|
|
import { MoreThan } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Endpoint } from '@/server/api/endpoint-base.js';
|
2023-09-15 07:28:29 +02:00
|
|
|
import type { RegistrationTicketsRepository } from '@/models/_.js';
|
2023-07-15 02:57:58 +02:00
|
|
|
import { RoleService } from '@/core/RoleService.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { DI } from '@/di-symbols.js';
|
2018-08-17 12:17:23 +02:00
|
|
|
|
|
|
|
export const meta = {
|
2023-01-13 06:22:53 +01:00
|
|
|
tags: ['meta'],
|
2019-02-23 03:20:58 +01:00
|
|
|
|
2022-01-18 14:27:10 +01:00
|
|
|
requireCredential: true,
|
2023-01-15 12:52:53 +01:00
|
|
|
requireRolePolicy: 'canInvite',
|
2018-08-17 12:17:23 +02:00
|
|
|
|
2021-03-06 14:34:11 +01:00
|
|
|
res: {
|
2022-01-18 14:27:10 +01:00
|
|
|
type: 'object',
|
|
|
|
optional: false, nullable: false,
|
2021-03-06 14:34:11 +01:00
|
|
|
properties: {
|
2023-07-15 02:57:58 +02:00
|
|
|
remaining: {
|
|
|
|
type: 'integer',
|
|
|
|
optional: false, nullable: true,
|
2021-12-09 15:58:30 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-01-18 14:27:10 +01:00
|
|
|
} as const;
|
2018-08-17 12:17:23 +02:00
|
|
|
|
2022-02-20 05:15:40 +01:00
|
|
|
export const paramDef = {
|
2022-02-19 06:05:32 +01:00
|
|
|
type: 'object',
|
|
|
|
properties: {},
|
|
|
|
required: [],
|
|
|
|
} as const;
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Injectable()
|
2023-08-17 14:20:58 +02:00
|
|
|
export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-disable-line import/no-default-export
|
2022-09-17 20:27:08 +02:00
|
|
|
constructor(
|
|
|
|
@Inject(DI.registrationTicketsRepository)
|
2022-12-25 00:30:13 +01:00
|
|
|
private registrationTicketsRepository: RegistrationTicketsRepository,
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-07-15 02:57:58 +02:00
|
|
|
private roleService: RoleService,
|
2022-09-17 20:27:08 +02:00
|
|
|
) {
|
2023-01-13 06:22:53 +01:00
|
|
|
super(meta, paramDef, async (ps, me) => {
|
2023-07-15 02:57:58 +02:00
|
|
|
const policies = await this.roleService.getUserPolicies(me.id);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-07-15 02:57:58 +02:00
|
|
|
const count = policies.inviteLimit ? await this.registrationTicketsRepository.countBy({
|
|
|
|
createdAt: MoreThan(new Date(Date.now() - (policies.inviteExpirationTime * 60 * 1000))),
|
|
|
|
createdById: me.id,
|
|
|
|
}) : null;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
return {
|
2023-07-15 02:57:58 +02:00
|
|
|
remaining: count !== null ? Math.max(0, policies.inviteLimit - count) : null,
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|