Files
upstream-rocketchat/apps/meteor/server/api/v1/custom-sounds.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
Code scanning - action / CodeQL-Build (push) Failing after 2s
Auto-close duplicate issues / auto-close-duplicates (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

340 lines
9.4 KiB
TypeScript

import type { ICustomSound } from '@rocket.chat/core-typings';
import { CustomSounds } from '@rocket.chat/models';
import type { PaginatedResult } from '@rocket.chat/rest-typings';
import {
isCustomSoundsGetOneProps,
isCustomSoundsListProps,
isCustomSoundsCreateProps,
isCustomSoundsDeleteProps,
isCustomSoundsUpdateProps,
ajv,
validateBadRequestErrorResponse,
validateNotFoundErrorResponse,
validateForbiddenErrorResponse,
validateUnauthorizedErrorResponse,
validateInternalErrorResponse,
} from '@rocket.chat/rest-typings';
import { escapeRegExp } from '@rocket.chat/tools';
import { Meteor } from 'meteor/meteor';
import { getExtension, getMimeTypeFromFileName } from '../../../app/utils/lib/mimeTypes';
import { MAX_CUSTOM_SOUND_SIZE_BYTES, CUSTOM_SOUND_ALLOWED_MIME_TYPES } from '../../../lib/constants';
import { SystemLogger } from '../../lib/logger/system';
import { deleteCustomSound } from '../../lib/media/custom-sounds/lib/deleteCustomSound';
import { insertOrUpdateSound } from '../../lib/media/custom-sounds/lib/insertOrUpdateSound';
import { uploadCustomSound } from '../../lib/media/custom-sounds/lib/uploadCustomSound';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';
import { getPaginationItems } from '../lib/getPaginationItems';
import { getUploadFormData } from '../lib/getUploadFormData';
const createCustomSoundsResponse = ajv.compile<{ sound: Pick<ICustomSound, '_id'>; success: boolean }>({
additionalProperties: false,
type: 'object',
properties: {
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
sound: {
type: 'object',
properties: {
_id: {
type: 'string',
description: 'The ID of the sound.',
},
},
required: ['_id'],
},
},
required: ['success', 'sound'],
});
const updateCustomSoundsResponse = ajv.compile<{ success: boolean }>({
additionalProperties: false,
type: 'object',
properties: {
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
},
required: ['success'],
});
const deleteCustomSoundsResponse = ajv.compile<void>({
additionalProperties: false,
type: 'object',
properties: {
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
},
required: ['success'],
});
const customSoundsEndpoints = API.v1
.get(
'custom-sounds.list',
{
response: {
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
200: ajv.compile<
PaginatedResult<{
sounds: ICustomSound[];
}>
>({
additionalProperties: false,
type: 'object',
properties: {
count: {
type: 'number',
description: 'The number of sounds returned in this response.',
},
offset: {
type: 'number',
description: 'The number of sounds that were skipped in this response.',
},
total: {
type: 'number',
description: 'The total number of sounds that match the query.',
},
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
sounds: {
type: 'array',
items: {
$ref: '#/components/schemas/ICustomSound',
},
},
},
required: ['count', 'offset', 'total', 'sounds', 'success'],
}),
},
query: isCustomSoundsListProps,
authRequired: true,
},
async function action() {
const { offset, count } = await getPaginationItems(this.queryParams);
const { sort, query } = await this.parseJsonQuery();
const { name } = this.queryParams;
const filter = {
...query,
...(name ? { name: { $regex: escapeRegExp(name), $options: 'i' } } : {}),
};
const { cursor, totalCount } = CustomSounds.findPaginated(filter, {
sort: sort || { name: 1 },
skip: offset,
limit: count,
});
const [sounds, total] = await Promise.all([cursor.toArray(), totalCount]);
return API.v1.success({
sounds,
count: sounds.length,
offset,
total,
});
},
)
.get(
'custom-sounds.getOne',
{
response: {
200: ajv.compile<{ sound: ICustomSound; success: boolean }>({
additionalProperties: false,
type: 'object',
properties: {
sound: {
$ref: '#/components/schemas/ICustomSound',
},
success: {
type: 'boolean',
description: 'Indicates if the request was successful.',
},
},
required: ['sound', 'success'],
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
404: validateNotFoundErrorResponse,
},
query: isCustomSoundsGetOneProps,
authRequired: true,
},
async function action() {
const { _id } = this.queryParams;
const sound = await CustomSounds.findOneById(_id);
if (!sound) {
return API.v1.notFound('Custom Sound not found.');
}
return API.v1.success({ sound });
},
)
.post(
'custom-sounds.create',
{
response: {
200: createCustomSoundsResponse,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
},
authRequired: true,
permissionsRequired: ['manage-sounds'],
},
async function action() {
const { fields, fileBuffer, filename } = await getUploadFormData(
{
request: this.request,
},
{
field: 'sound',
sizeLimit: MAX_CUSTOM_SOUND_SIZE_BYTES,
validate: isCustomSoundsCreateProps,
},
);
const computedMimeType = getMimeTypeFromFileName(filename);
if (!CUSTOM_SOUND_ALLOWED_MIME_TYPES.includes(computedMimeType)) {
return API.v1.failure('MIME type not allowed');
}
// We extract its extension from the name or use getExtension() as fallback.
const soundExtension =
(filename.includes('.') ? filename.split('.').pop()?.toLowerCase() : undefined) || getExtension(computedMimeType);
try {
const _id = await insertOrUpdateSound({
name: fields.name,
extension: soundExtension,
});
await uploadCustomSound(fileBuffer, computedMimeType, { _id, name: fields.name, extension: soundExtension });
return API.v1.success({ sound: { _id } });
} catch (error) {
SystemLogger.error({ error });
return API.v1.failure(error instanceof Error ? error.message : 'Unknown error');
}
},
)
.post(
'custom-sounds.update',
{
response: {
200: updateCustomSoundsResponse,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
},
authRequired: true,
permissionsRequired: ['manage-sounds'],
},
async function action() {
const { fields, fileBuffer, filename } = await getUploadFormData(
{
request: this.request,
},
{
field: 'sound',
fileOptional: true,
sizeLimit: MAX_CUSTOM_SOUND_SIZE_BYTES,
validate: isCustomSoundsUpdateProps,
},
);
let computedMimeType = '';
if (fileBuffer) {
computedMimeType = getMimeTypeFromFileName(filename);
if (!CUSTOM_SOUND_ALLOWED_MIME_TYPES.includes(computedMimeType)) {
return API.v1.failure('MIME type not allowed');
}
}
const soundToUpdate = await CustomSounds.findOneById<Pick<ICustomSound, '_id' | 'name' | 'extension'>>(fields._id, {
projection: { _id: 1, name: 1, extension: 1 },
});
if (!soundToUpdate) {
return API.v1.failure('Custom Sound not found.');
}
// If new file, we'll extract its extension or use getExtension() as fallback. If there's no new file, we will keep the current extension.
const nextExtension = fileBuffer
? (filename.includes('.') ? filename.split('.').pop()?.toLowerCase() : undefined) || getExtension(computedMimeType)
: soundToUpdate.extension;
try {
await insertOrUpdateSound({
_id: fields._id,
name: fields.name,
extension: nextExtension,
previousName: soundToUpdate.name,
previousExtension: soundToUpdate.extension,
});
if (fileBuffer) {
await uploadCustomSound(fileBuffer, computedMimeType, {
_id: fields._id,
name: fields.name,
previousExtension: soundToUpdate.extension,
extension: nextExtension,
});
}
return API.v1.success({});
} catch (error) {
SystemLogger.error({ error });
return API.v1.failure(error instanceof Error ? error.message : 'Unknown error');
}
},
)
.post(
'custom-sounds.delete',
{
response: {
200: deleteCustomSoundsResponse,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
403: validateForbiddenErrorResponse,
404: validateNotFoundErrorResponse,
500: validateInternalErrorResponse,
},
authRequired: true,
body: isCustomSoundsDeleteProps,
permissionsRequired: ['manage-sounds'],
},
async function action() {
const { _id } = this.bodyParams;
try {
await deleteCustomSound(_id);
return API.v1.success();
} catch (error: unknown) {
this.logger.error({ error });
if (error instanceof Meteor.Error && error.error === 'Custom_Sound_Error_Invalid_Sound') {
return API.v1.failure(error.error);
}
return API.v1.internalError();
}
},
);
export type CustomSoundEndpoints = ExtractRoutesFromAPI<typeof customSoundsEndpoints>;
declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends CustomSoundEndpoints {}
}