Files
upstream-rocketchat/apps/meteor/server/api/v1/invites.ts
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 1s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

302 lines
7.0 KiB
TypeScript

import type { IInvite } from '@rocket.chat/core-typings';
import {
ajv,
isFindOrCreateInviteParams,
isUseInviteTokenProps,
isValidateInviteTokenProps,
isSendInvitationEmailParams,
validateBadRequestErrorResponse,
validateUnauthorizedErrorResponse,
} from '@rocket.chat/rest-typings';
import { findOrCreateInvite } from '../../lib/rooms/invites/findOrCreateInvite';
import { listInvites } from '../../lib/rooms/invites/listInvites';
import { removeInvite } from '../../lib/rooms/invites/removeInvite';
import { sendInvitationEmail } from '../../lib/rooms/invites/sendInvitationEmail';
import { useInviteToken } from '../../lib/rooms/invites/useInviteToken';
import { validateInviteToken } from '../../lib/rooms/invites/validateInviteToken';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';
const removeInviteResponseSchema = ajv.compile({
type: 'boolean',
enum: [true],
});
const useInviteTokenResponseSchema = ajv.compile({
type: 'object',
properties: {
room: {
type: 'object',
properties: {
rid: { type: 'string' },
prid: { type: 'string', nullable: true },
fname: { type: 'string', nullable: true },
name: { type: 'string', nullable: true },
t: { type: 'string' },
},
required: ['rid', 't'],
additionalProperties: false,
},
success: { type: 'boolean', enum: [true] },
},
required: ['room', 'success'],
additionalProperties: false,
});
const validateInviteTokenResponseSchema = ajv.compile<{ valid: boolean }>({
type: 'object',
properties: {
valid: { type: 'boolean' },
success: { type: 'boolean', enum: [true] },
},
required: ['valid', 'success'],
additionalProperties: false,
});
const sendInvitationEmailResponseSchema = ajv.compile<{ success: boolean }>({
type: 'object',
properties: {
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
additionalProperties: false,
});
const invites = API.v1
.get(
'listInvites',
{
authRequired: true,
response: {
200: ajv.compile<IInvite[]>({
type: 'array',
items: {
$ref: '#/components/schemas/IInvite',
},
}),
401: ajv.compile({
additionalProperties: false,
type: 'object',
properties: {
error: {
type: 'string',
},
status: {
type: 'string',
nullable: true,
},
message: {
type: 'string',
nullable: true,
},
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
},
required: ['success', 'error'],
}),
},
},
async function () {
const result = await listInvites(this.userId);
return API.v1.success(result);
},
)
.post(
'findOrCreateInvite',
{
authRequired: true,
body: isFindOrCreateInviteParams,
response: {
200: ajv.compile({
additionalProperties: false,
type: 'object',
properties: {
_id: {
type: 'string',
},
rid: {
type: 'string',
},
userId: {
type: 'string',
},
createdAt: {
type: 'string',
},
_updatedAt: {
type: 'string',
},
expires: {
type: 'string',
nullable: true,
},
url: {
type: 'string',
},
maxUses: {
type: 'number',
},
days: {
type: 'number',
},
uses: {
type: 'number',
},
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
},
required: ['_id', 'rid', 'createdAt', 'maxUses', 'uses', 'userId', '_updatedAt', 'days', 'success'],
}),
400: ajv.compile({
additionalProperties: false,
type: 'object',
properties: {
error: {
type: 'string',
},
stack: {
type: 'string',
nullable: true,
},
errorType: {
type: 'string',
},
details: {
type: 'object',
nullable: true,
properties: {
rid: {
type: 'string',
},
method: {
type: 'string',
},
},
},
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
},
required: ['success', 'errorType', 'error'],
}),
401: ajv.compile({
additionalProperties: false,
type: 'object',
properties: {
error: {
type: 'string',
},
status: {
type: 'string',
nullable: true,
},
message: {
type: 'string',
nullable: true,
},
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
},
required: ['success', 'error'],
}),
},
},
async function () {
const { rid, days, maxUses } = this.bodyParams;
return API.v1.success((await findOrCreateInvite(this.userId, { rid, days, maxUses })) as IInvite);
},
)
.delete(
'removeInvite/:_id',
{
authRequired: true,
response: {
200: removeInviteResponseSchema,
400: validateBadRequestErrorResponse,
401: ajv.compile({
type: 'object',
properties: { error: { type: 'string' }, success: { type: 'boolean', enum: [false] } },
required: ['success', 'error'],
additionalProperties: false,
}),
},
},
async function action() {
const { _id } = this.urlParams;
return API.v1.success(await removeInvite(this.userId, { _id }));
},
)
.post(
'useInviteToken',
{
authRequired: true,
body: isUseInviteTokenProps,
response: {
200: useInviteTokenResponseSchema,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { token } = this.bodyParams;
// eslint-disable-next-line react-hooks/rules-of-hooks
return API.v1.success(await useInviteToken(this.userId, token));
},
)
.post(
'validateInviteToken',
{
authRequired: false,
body: isValidateInviteTokenProps,
response: {
200: validateInviteTokenResponseSchema,
},
},
async function action() {
const { token } = this.bodyParams;
try {
return API.v1.success({ valid: Boolean(await validateInviteToken(token)) });
} catch (_) {
return API.v1.success({ valid: false });
}
},
)
.post(
'sendInvitationEmail',
{
authRequired: true,
body: isSendInvitationEmailParams,
response: {
200: sendInvitationEmailResponseSchema,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { emails } = this.bodyParams;
try {
return API.v1.success({ success: Boolean(await sendInvitationEmail(this.userId, emails)) });
} catch (e: unknown) {
return API.v1.failure({ error: e instanceof Error ? e.message : String(e) });
}
},
);
type InvitesEndpoints = ExtractRoutesFromAPI<typeof invites>;
declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends InvitesEndpoints {}
}