snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
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
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
This commit is contained in:
@@ -0,0 +1,206 @@
|
||||
import type { TFunction } from 'i18next';
|
||||
|
||||
import { getMessagePreview } from './getMessagePreview';
|
||||
import { normalizeMessagePreview } from './normalizeMessagePreview';
|
||||
import { createFakeSubscription, createFakeMessage } from '../../../../tests/mocks/data';
|
||||
|
||||
jest.mock('./normalizeMessagePreview');
|
||||
|
||||
const mockNormalizeMessagePreview = normalizeMessagePreview as jest.MockedFunction<typeof normalizeMessagePreview>;
|
||||
|
||||
describe('getMessagePreview', () => {
|
||||
const mockT = jest.fn((key: string) => key) as unknown as TFunction;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
mockNormalizeMessagePreview.mockReturnValue('normalized message');
|
||||
});
|
||||
|
||||
describe('when room has a draft', () => {
|
||||
it('should return "Draft: content" taking precedence over the last message', () => {
|
||||
const room = createFakeSubscription({ draft: 'unfinished text' });
|
||||
const message = createFakeMessage();
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('Draft: unfinished text');
|
||||
expect(mockT).toHaveBeenCalledWith('Draft');
|
||||
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should escape HTML in the draft content', () => {
|
||||
const room = createFakeSubscription({ draft: '<img src=x onerror=alert(1)>' });
|
||||
|
||||
const result = getMessagePreview(room, undefined, mockT);
|
||||
|
||||
expect(result).toBe('Draft: <img src=x onerror=alert(1)>');
|
||||
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should fall back to a thread-composer draft when there is no main draft', () => {
|
||||
const room = createFakeSubscription({ draft: undefined, threadDrafts: { 'thread-id': 'unfinished thread reply' } });
|
||||
const message = createFakeMessage();
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('Draft: unfinished thread reply');
|
||||
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should prefer the main draft over thread-composer drafts', () => {
|
||||
const room = createFakeSubscription({ draft: 'main draft', threadDrafts: { 'thread-id': 'thread draft' } });
|
||||
|
||||
const result = getMessagePreview(room, undefined, mockT);
|
||||
|
||||
expect(result).toBe('Draft: main draft');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when lastMessage is undefined', () => {
|
||||
it('should return translation for No_messages_yet', () => {
|
||||
const room = createFakeSubscription();
|
||||
const result = getMessagePreview(room, undefined, mockT);
|
||||
|
||||
expect(result).toBe('No_messages_yet');
|
||||
expect(mockT).toHaveBeenCalledWith('No_messages_yet');
|
||||
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message is a video conference message', () => {
|
||||
it('should return translation for Call_started', () => {
|
||||
const room = createFakeSubscription();
|
||||
const message = createFakeMessage({ t: 'videoconf' });
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('Call_started');
|
||||
expect(mockT).toHaveBeenCalledWith('Call_started');
|
||||
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message is E2EE', () => {
|
||||
it('should return encrypted message preview when e2e is not done', () => {
|
||||
const room = createFakeSubscription();
|
||||
const message = createFakeMessage({ t: 'e2e', e2e: 'pending' });
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('Encrypted_message_preview_unavailable');
|
||||
expect(mockT).toHaveBeenCalledWith('Encrypted_message_preview_unavailable');
|
||||
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should proceed to normalization when e2e is done', () => {
|
||||
const room = createFakeSubscription({ t: 'c' });
|
||||
const message = createFakeMessage({ t: 'e2e', e2e: 'done', u: { _id: 'user-id', username: 'testuser', name: 'Test User' } });
|
||||
mockNormalizeMessagePreview.mockReturnValue('decrypted message');
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('Test User: decrypted message');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message has no user', () => {
|
||||
it('should return normalized message directly', () => {
|
||||
const room = createFakeSubscription();
|
||||
const message = createFakeMessage({ u: undefined });
|
||||
mockNormalizeMessagePreview.mockReturnValue('normalized message');
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('normalized message');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message is from current user', () => {
|
||||
it('should return "You: normalized message" when usernames match', () => {
|
||||
const room = createFakeSubscription({ u: { _id: 'user-id', username: 'testuser', name: 'Test User' } });
|
||||
const message = createFakeMessage({ u: { _id: 'user-id', username: 'testuser', name: 'Test User' } });
|
||||
mockNormalizeMessagePreview.mockReturnValue('my message');
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('You: my message');
|
||||
expect(mockT).toHaveBeenCalledWith('You');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when room is a direct message room', () => {
|
||||
it('should return normalized message without sender name for single DM', () => {
|
||||
const room = createFakeSubscription({
|
||||
t: 'd',
|
||||
uids: ['user1', 'user2'],
|
||||
});
|
||||
const message = createFakeMessage({
|
||||
u: { _id: 'other-user-id', username: 'otheruser', name: 'Other User' },
|
||||
});
|
||||
mockNormalizeMessagePreview.mockReturnValue('direct message');
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('direct message');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
|
||||
it('should return sender name and message for multiple DM', () => {
|
||||
const room = createFakeSubscription({
|
||||
t: 'd',
|
||||
uids: ['user1', 'user2', 'user3'],
|
||||
});
|
||||
const message = createFakeMessage({
|
||||
u: { _id: 'other-user-id', username: 'otheruser', name: 'Other User' },
|
||||
});
|
||||
mockNormalizeMessagePreview.mockReturnValue('group message');
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('Other User: group message');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when room is a group or channel', () => {
|
||||
it('should return sender name and message when user has name', () => {
|
||||
const room = createFakeSubscription({ t: 'c' });
|
||||
const message = createFakeMessage({
|
||||
u: { _id: 'other-user-id', username: 'otheruser', name: 'Other User' },
|
||||
});
|
||||
mockNormalizeMessagePreview.mockReturnValue('channel message');
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('Other User: channel message');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
|
||||
it('should return username and message when user has no name', () => {
|
||||
const room = createFakeSubscription({ t: 'p' });
|
||||
const message = createFakeMessage({
|
||||
u: { _id: 'other-user-id', username: 'otheruser' },
|
||||
});
|
||||
mockNormalizeMessagePreview.mockReturnValue('private message');
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('otheruser: private message');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message has no preview', () => {
|
||||
it('should return empty string', () => {
|
||||
const room = createFakeSubscription();
|
||||
const message = createFakeMessage();
|
||||
mockNormalizeMessagePreview.mockReturnValue(undefined);
|
||||
|
||||
const result = getMessagePreview(room, message, mockT);
|
||||
|
||||
expect(result).toBe('');
|
||||
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,41 @@
|
||||
import type { IMessage } from '@rocket.chat/core-typings';
|
||||
import { isDirectMessageRoom, isE2EEMessage, isMultipleDirectMessageRoom, isVideoConfMessage } from '@rocket.chat/core-typings';
|
||||
import { escapeHTML } from '@rocket.chat/tools';
|
||||
import type { SubscriptionWithRoom } from '@rocket.chat/ui-contexts';
|
||||
import type { TFunction } from 'i18next';
|
||||
|
||||
import { normalizeMessagePreview } from './normalizeMessagePreview';
|
||||
import { getSubscriptionDraft } from '../getSubscriptionDraft';
|
||||
|
||||
export const getMessagePreview = (room: SubscriptionWithRoom, lastMessage: IMessage | undefined, t: TFunction): string | undefined => {
|
||||
const draft = getSubscriptionDraft(room);
|
||||
if (draft) {
|
||||
return `${t('Draft')}: ${escapeHTML(draft)}`;
|
||||
}
|
||||
|
||||
if (!lastMessage) {
|
||||
return t('No_messages_yet');
|
||||
}
|
||||
if (isVideoConfMessage(lastMessage)) {
|
||||
return t('Call_started');
|
||||
}
|
||||
if (isE2EEMessage(lastMessage) && lastMessage.e2e !== 'done') {
|
||||
return t('Encrypted_message_preview_unavailable');
|
||||
}
|
||||
|
||||
const normalizedMessage = normalizeMessagePreview(lastMessage, t);
|
||||
if (!normalizedMessage) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!lastMessage.u) {
|
||||
return normalizedMessage;
|
||||
}
|
||||
if (lastMessage.u?.username === room.u?.username) {
|
||||
return `${t('You')}: ${normalizedMessage}`;
|
||||
}
|
||||
if (isDirectMessageRoom(room) && !isMultipleDirectMessageRoom(room)) {
|
||||
return normalizedMessage;
|
||||
}
|
||||
return `${escapeHTML(lastMessage.u.name || lastMessage.u.username)}: ${normalizedMessage}`;
|
||||
};
|
||||
@@ -0,0 +1,222 @@
|
||||
import type { TFunction } from 'i18next';
|
||||
|
||||
import { normalizeMessagePreview } from './normalizeMessagePreview';
|
||||
import { createFakeMessage, createFakeMessageWithAttachment } from '../../../../tests/mocks/data';
|
||||
|
||||
describe('normalizeMessagePreview', () => {
|
||||
const mockT = jest.fn((key: string) => key) as unknown as TFunction;
|
||||
|
||||
beforeEach(() => {
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('when message has msg property', () => {
|
||||
it('should return escaped HTML filtered markdown with emoji conversion', () => {
|
||||
const message = createFakeMessage({ msg: 'Hello :smile: world' });
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Hello 😄 world');
|
||||
});
|
||||
|
||||
it('should filter markdown from message', () => {
|
||||
const message = createFakeMessage({ msg: '*bold* text' });
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('bold text');
|
||||
});
|
||||
|
||||
it('should escape HTML in message', () => {
|
||||
const message = createFakeMessage({ msg: '<script>alert("xss")</script>' });
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('<script>alert("xss")</script>');
|
||||
});
|
||||
|
||||
it('should convert emoji shortnames to unicode', () => {
|
||||
const message = createFakeMessage({ msg: 'Hello :smile: :heart:' });
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Hello 😄 ❤️');
|
||||
});
|
||||
|
||||
it('should return undefined when message is empty', () => {
|
||||
const message = createFakeMessage({ msg: '' });
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message has attachments', () => {
|
||||
it('should return attachment description when available', () => {
|
||||
const message = createFakeMessageWithAttachment({
|
||||
msg: '',
|
||||
attachments: [
|
||||
{
|
||||
title: 'Attachment title',
|
||||
description: 'Attachment description',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Attachment description');
|
||||
});
|
||||
|
||||
it('should return attachment title when description is not available', () => {
|
||||
const message = createFakeMessageWithAttachment({
|
||||
msg: '',
|
||||
attachments: [
|
||||
{
|
||||
title: 'Attachment title',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Attachment title');
|
||||
});
|
||||
|
||||
it('should return translation when attachment has no title or description', () => {
|
||||
const message = createFakeMessageWithAttachment({
|
||||
msg: '',
|
||||
attachments: [
|
||||
{
|
||||
type: 'file',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Sent_an_attachment');
|
||||
expect(mockT).toHaveBeenCalledWith('Sent_an_attachment');
|
||||
});
|
||||
|
||||
it('should find first attachment with title', () => {
|
||||
const message = createFakeMessageWithAttachment({
|
||||
msg: '',
|
||||
attachments: [
|
||||
{
|
||||
type: 'file',
|
||||
},
|
||||
{
|
||||
title: 'Second attachment title',
|
||||
},
|
||||
{
|
||||
description: 'Third attachment description',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Second attachment title');
|
||||
});
|
||||
|
||||
it('should find first attachment description', () => {
|
||||
const message = createFakeMessageWithAttachment({
|
||||
msg: '',
|
||||
attachments: [
|
||||
{
|
||||
type: 'file',
|
||||
},
|
||||
{
|
||||
description: 'Second attachment description',
|
||||
},
|
||||
{
|
||||
title: 'Third attachment title',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Second attachment description');
|
||||
});
|
||||
|
||||
it('should escape HTML in attachment description', () => {
|
||||
const message = createFakeMessageWithAttachment({
|
||||
msg: '',
|
||||
attachments: [
|
||||
{
|
||||
description: '<script>alert("xss")</script>',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('<script>alert("xss")</script>');
|
||||
});
|
||||
|
||||
it('should escape HTML in attachment title', () => {
|
||||
const message = createFakeMessageWithAttachment({
|
||||
msg: '',
|
||||
attachments: [
|
||||
{
|
||||
title: '<img src="x" onerror="alert(1)">',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('<img src="x" onerror="alert(1)">');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message has both msg and attachments', () => {
|
||||
it('should prioritize msg over attachments', () => {
|
||||
const message = createFakeMessage({
|
||||
msg: 'Message text',
|
||||
attachments: [
|
||||
{
|
||||
title: 'Attachment title',
|
||||
description: 'Attachment description',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Message text');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message has no msg and no attachments', () => {
|
||||
it('should return undefined', () => {
|
||||
const message = createFakeMessage({ msg: undefined });
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message has empty attachments array', () => {
|
||||
it('should return "Sent_an_attachment"', () => {
|
||||
const message = createFakeMessage({
|
||||
msg: undefined,
|
||||
attachments: [],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Sent_an_attachment');
|
||||
expect(mockT).toHaveBeenCalledWith('Sent_an_attachment');
|
||||
});
|
||||
});
|
||||
|
||||
describe('when message has attachments but none have title or description', () => {
|
||||
it('should return translation for Sent_an_attachment', () => {
|
||||
const message = createFakeMessage({
|
||||
msg: undefined,
|
||||
attachments: [
|
||||
{
|
||||
type: 'file',
|
||||
},
|
||||
{
|
||||
type: 'file',
|
||||
},
|
||||
],
|
||||
});
|
||||
const result = normalizeMessagePreview(message, mockT);
|
||||
|
||||
expect(result).toBe('Sent_an_attachment');
|
||||
expect(mockT).toHaveBeenCalledWith('Sent_an_attachment');
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,26 @@
|
||||
import type { IMessage } from '@rocket.chat/core-typings';
|
||||
import { escapeHTML } from '@rocket.chat/tools';
|
||||
import type { TFunction } from 'i18next';
|
||||
|
||||
import { shortnameToUnicode } from '../../../../app/emoji-native/lib/shortnameToUnicode';
|
||||
import { filterMarkdown } from '../../../../app/markdown/lib/markdown';
|
||||
|
||||
export const normalizeMessagePreview = (message: IMessage, t: TFunction): string | undefined => {
|
||||
if (message.msg) {
|
||||
return escapeHTML(filterMarkdown(shortnameToUnicode(message.msg)));
|
||||
}
|
||||
|
||||
if (message.attachments) {
|
||||
const attachment = message.attachments.find((attachment) => attachment.title || attachment.description);
|
||||
|
||||
if (attachment?.description) {
|
||||
return escapeHTML(attachment.description);
|
||||
}
|
||||
|
||||
if (attachment?.title) {
|
||||
return escapeHTML(attachment.title);
|
||||
}
|
||||
|
||||
return t('Sent_an_attachment');
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user