Files
upstream-rocketchat/apps/meteor/server/api/v1/emoji-custom.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

319 lines
8.6 KiB
TypeScript

import { Media } from '@rocket.chat/core-services';
import type { IEmojiCustom, RocketChatRecordDeleted } from '@rocket.chat/core-typings';
import { EmojiCustom } from '@rocket.chat/models';
import { ajv, isEmojiCustomList, validateUnauthorizedErrorResponse, validateBadRequestErrorResponse } from '@rocket.chat/rest-typings';
import { escapeRegExp } from '@rocket.chat/tools';
import { Meteor } from 'meteor/meteor';
import type { WithId } from 'mongodb';
import type { EmojiData } from '../../lib/media/emoji-custom/lib/insertOrUpdateEmoji';
import { insertOrUpdateEmoji } from '../../lib/media/emoji-custom/lib/insertOrUpdateEmoji';
import { uploadEmojiCustomWithBuffer } from '../../lib/media/emoji-custom/lib/uploadEmojiCustom';
import { deleteEmojiCustom } from '../../meteor-methods/media/deleteEmojiCustom';
import { settings } from '../../settings';
import type { ExtractRoutesFromAPI } from '../ApiClass';
import { API } from '../api';
import { findEmojisCustom } from '../lib/emoji-custom';
import { getPaginationItems } from '../lib/getPaginationItems';
import { getUploadFormData } from '../lib/getUploadFormData';
const emojiDeleteBodySchema = ajv.compile({
type: 'object',
properties: { emojiId: { type: 'string', minLength: 1 } },
required: ['emojiId'],
additionalProperties: false,
});
const emojiCustomAllResponseSchema = ajv.compile<{ emojis: IEmojiCustom[]; count: number; offset: number; total: number }>({
type: 'object',
properties: {
emojis: {
type: 'array',
items: { $ref: '#/components/schemas/IEmojiCustom' },
},
count: { type: 'number' },
offset: { type: 'number' },
total: { type: 'number' },
success: { type: 'boolean', enum: [true] },
},
required: ['emojis', 'count', 'offset', 'total', 'success'],
additionalProperties: false,
});
const emojiCustomDeleteResponseSchema = ajv.compile<void>({
type: 'object',
properties: {
success: { type: 'boolean', enum: [true] },
},
required: ['success'],
additionalProperties: false,
});
function validateDateParam(paramName: string, paramValue: string | undefined): Date | undefined {
if (!paramValue) {
return undefined;
}
const date = new Date(paramValue);
if (isNaN(date.getTime())) {
throw new Meteor.Error('error-roomId-param-invalid', `The "${paramName}" query parameter must be a valid date.`);
}
return date;
}
const emojiCustomListResponseSchema = ajv.compile<{
emojis: { update: IEmojiCustom[]; remove: WithId<RocketChatRecordDeleted<IEmojiCustom>>[] };
}>({
type: 'object',
properties: {
emojis: {
type: 'object',
properties: {
update: { type: 'array', items: { type: 'object' } },
remove: { type: 'array', items: { type: 'object' } },
},
required: ['update', 'remove'],
},
success: { type: 'boolean', enum: [true] },
},
required: ['emojis', 'success'],
additionalProperties: false,
});
const emojiCustomCreateEndpoints = API.v1
.get(
'emoji-custom.list',
{
authRequired: true,
query: isEmojiCustomList,
response: {
200: emojiCustomListResponseSchema,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { query } = await this.parseJsonQuery();
const { updatedSince, _updatedAt, _id } = this.queryParams;
const updatedSinceDate = validateDateParam('updatedSince', updatedSince);
const _updatedAtDate = validateDateParam('_updatedAt', _updatedAt);
if (updatedSinceDate) {
const [update, remove] = await Promise.all([
EmojiCustom.find({
...query,
...(_id ? { _id } : {}),
...(_updatedAtDate ? { _updatedAt: { $gt: _updatedAtDate } } : {}),
_updatedAt: { $gt: updatedSinceDate },
}).toArray(),
EmojiCustom.trashFindDeletedAfter(updatedSinceDate).toArray(),
]);
return API.v1.success({
emojis: {
update,
remove,
},
});
}
return API.v1.success({
emojis: {
update: await EmojiCustom.find({
...query,
...(_id ? { _id } : {}),
...(_updatedAtDate ? { _updatedAt: { $gt: _updatedAtDate } } : {}),
}).toArray(),
remove: [],
},
});
},
)
.get(
'emoji-custom.all',
{
authRequired: true,
response: {
200: emojiCustomAllResponseSchema,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { offset, count } = await getPaginationItems(this.queryParams);
const { sort, query } = await this.parseJsonQuery();
const { name } = this.queryParams;
return API.v1.success(
await findEmojisCustom({
query: name
? {
name: {
$regex: escapeRegExp(name),
$options: 'i',
},
}
: query,
pagination: {
offset,
count,
sort,
},
}),
);
},
)
.post(
'emoji-custom.create',
{
authRequired: true,
response: {
200: ajv.compile<void>({
type: 'object',
properties: {
success: {
type: 'boolean',
enum: [true],
},
},
required: ['success'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const emoji = await getUploadFormData(
{
request: this.request,
},
{
field: 'emoji',
sizeLimit: settings.get('FileUpload_MaxFileSize'),
},
);
const { fields, fileBuffer, mimetype } = emoji;
const isUploadable = await Media.isImage(fileBuffer);
if (!isUploadable) {
throw new Meteor.Error('emoji-is-not-image', "Emoji file provided cannot be uploaded since it's not an image");
}
const [, extension] = mimetype.split('/');
fields.extension = extension;
const emojiData = await insertOrUpdateEmoji(this.userId, {
...fields,
newFile: true,
aliases: fields.aliases || '',
name: fields.name,
extension: fields.extension,
});
await uploadEmojiCustomWithBuffer(this.userId, fileBuffer, mimetype, emojiData);
return API.v1.success();
},
)
.post(
'emoji-custom.update',
{
authRequired: true,
response: {
200: ajv.compile({
type: 'object',
properties: { success: { type: 'boolean', enum: [true] } },
required: ['success'],
additionalProperties: false,
}),
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const emoji = await getUploadFormData(
{
request: this.request,
},
{ field: 'emoji', sizeLimit: settings.get('FileUpload_MaxFileSize'), fileOptional: true },
);
const { fields, fileBuffer, mimetype } = emoji;
if (!fields._id) {
throw new Meteor.Error('The required "_id" query param is missing.');
}
const emojiToUpdate = await EmojiCustom.findOneById<Pick<IEmojiCustom, 'name' | 'extension'>>(fields._id, {
projection: { name: 1, extension: 1 },
});
if (!emojiToUpdate) {
throw new Meteor.Error('Emoji not found.');
}
const emojiData: EmojiData = {
previousName: emojiToUpdate.name,
previousExtension: emojiToUpdate.extension,
aliases: fields.aliases || '',
name: fields.name,
extension: fields.extension,
_id: fields._id,
newFile: false,
};
const isNewFile = fileBuffer?.length && !!mimetype;
if (isNewFile) {
emojiData.newFile = isNewFile;
const isUploadable = await Media.isImage(fileBuffer);
if (!isUploadable) {
throw new Meteor.Error('emoji-is-not-image', "Emoji file provided cannot be uploaded since it's not an image");
}
const [, extension] = mimetype.split('/');
emojiData.extension = extension;
} else {
emojiData.extension = emojiToUpdate.extension;
}
const updatedEmojiData = await insertOrUpdateEmoji(this.userId, emojiData);
if (isNewFile) {
await uploadEmojiCustomWithBuffer(this.userId, fileBuffer, mimetype, updatedEmojiData);
}
return API.v1.success();
},
)
.post(
'emoji-custom.delete',
{
authRequired: true,
body: emojiDeleteBodySchema,
response: {
200: emojiCustomDeleteResponseSchema,
400: validateBadRequestErrorResponse,
401: validateUnauthorizedErrorResponse,
},
},
async function action() {
const { emojiId } = this.bodyParams;
if (!emojiId) {
return API.v1.failure('The "emojiId" params is required!');
}
await deleteEmojiCustom(this.userId, emojiId);
return API.v1.success();
},
);
type EmojiCustomCreateEndpoints = ExtractRoutesFromAPI<typeof emojiCustomCreateEndpoints>;
export type EmojiCustomEndpoints = EmojiCustomCreateEndpoints;
declare module '@rocket.chat/rest-typings' {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-empty-interface
interface Endpoints extends EmojiCustomCreateEndpoints {}
}