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