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 2s
93 lines
3.2 KiB
TypeScript
93 lines
3.2 KiB
TypeScript
import { Base64 } from '@rocket.chat/base64';
|
|
import type { IMessage, MessageAttachment } from '@rocket.chat/core-typings';
|
|
import {
|
|
isFileImageAttachment,
|
|
isFileAttachment,
|
|
isFileAudioAttachment,
|
|
isFileVideoAttachment,
|
|
isQuoteAttachment,
|
|
} from '@rocket.chat/core-typings';
|
|
import type { Options } from '@rocket.chat/message-parser';
|
|
import { useMemo } from 'react';
|
|
|
|
import type { MessageWithMdEnforced } from '../../../lib/parseMessageTextToAstMarkdown';
|
|
import { parseMessageTextToAstMarkdown } from '../../../lib/parseMessageTextToAstMarkdown';
|
|
import { useAutoLinkDomains } from '../../../views/room/MessageList/hooks/useAutoLinkDomains';
|
|
import { useMessageListAutoTranslate, useMessageListKatex, useMessageListShowColors } from '../list/MessageListContext';
|
|
|
|
const normalizeAttachments = (attachments: MessageAttachment[], name?: string, type?: string): MessageAttachment[] => {
|
|
if (name) {
|
|
name = String.fromCharCode(...new TextEncoder().encode(name));
|
|
}
|
|
|
|
return attachments.map((attachment) => {
|
|
if (isQuoteAttachment(attachment) && attachment.attachments) {
|
|
attachment.attachments = normalizeAttachments(attachment.attachments);
|
|
return attachment;
|
|
}
|
|
|
|
if (!attachment.encryption) {
|
|
return attachment;
|
|
}
|
|
|
|
const key = Base64.encode(
|
|
JSON.stringify({
|
|
...attachment.encryption,
|
|
name,
|
|
type,
|
|
}),
|
|
);
|
|
|
|
if (isFileAttachment(attachment)) {
|
|
if (attachment.title_link && !attachment.title_link.startsWith('/file-decrypt/')) {
|
|
attachment.title_link = `/file-decrypt${attachment.title_link}?key=${encodeURIComponent(key)}`;
|
|
}
|
|
if (isFileImageAttachment(attachment) && !attachment.image_url.startsWith('/file-decrypt/')) {
|
|
attachment.image_url = `/file-decrypt${attachment.image_url}?key=${encodeURIComponent(key)}`;
|
|
}
|
|
if (isFileAudioAttachment(attachment) && !attachment.audio_url.startsWith('/file-decrypt/')) {
|
|
attachment.audio_url = `/file-decrypt${attachment.audio_url}?key=${encodeURIComponent(key)}`;
|
|
}
|
|
if (isFileVideoAttachment(attachment) && !attachment.video_url.startsWith('/file-decrypt/')) {
|
|
attachment.video_url = `/file-decrypt${attachment.video_url}?key=${encodeURIComponent(key)}`;
|
|
}
|
|
}
|
|
|
|
return attachment;
|
|
});
|
|
};
|
|
|
|
export const useNormalizedMessage = <TMessage extends IMessage>(message: TMessage): MessageWithMdEnforced => {
|
|
const katex = useMessageListKatex();
|
|
const katexEnabled = !!katex;
|
|
const customDomains = useAutoLinkDomains();
|
|
const autoTranslateOptions = useMessageListAutoTranslate();
|
|
const showColors = useMessageListShowColors();
|
|
|
|
return useMemo(() => {
|
|
const parseOptions: Options = {
|
|
colors: showColors,
|
|
emoticons: true,
|
|
customDomains,
|
|
...(katexEnabled && {
|
|
katex: {
|
|
dollarSyntax: katex.dollarSyntaxEnabled,
|
|
parenthesisSyntax: katex.parenthesisSyntaxEnabled,
|
|
},
|
|
}),
|
|
};
|
|
|
|
const normalizedMessage = parseMessageTextToAstMarkdown(message, parseOptions, autoTranslateOptions);
|
|
|
|
if (normalizedMessage.attachments) {
|
|
normalizedMessage.attachments = normalizeAttachments(
|
|
normalizedMessage.attachments,
|
|
normalizedMessage.file?.name,
|
|
normalizedMessage.file?.type,
|
|
);
|
|
}
|
|
|
|
return normalizedMessage;
|
|
}, [showColors, customDomains, katexEnabled, katex?.dollarSyntaxEnabled, katex?.parenthesisSyntaxEnabled, message, autoTranslateOptions]);
|
|
};
|