diff --git a/src/components/common/MessageSummary.tsx b/src/components/common/MessageSummary.tsx index 80ae5167e..f630bb19e 100644 --- a/src/components/common/MessageSummary.tsx +++ b/src/components/common/MessageSummary.tsx @@ -10,6 +10,7 @@ import { getMessageSummaryDescription, getMessageSummaryEmoji, getMessageSummaryText, + extractMessageText, TRUNCATED_SUMMARY_LENGTH, } from '../../global/helpers'; import renderText from './helpers/renderText'; @@ -25,6 +26,7 @@ interface OwnProps { observeIntersectionForLoading?: ObserveFn; observeIntersectionForPlaying?: ObserveFn; withTranslucentThumbs?: boolean; + inChatList?: boolean; } function MessageSummary({ @@ -35,12 +37,13 @@ function MessageSummary({ truncateLength = TRUNCATED_SUMMARY_LENGTH, observeIntersectionForLoading, observeIntersectionForPlaying, - withTranslucentThumbs, + withTranslucentThumbs = false, + inChatList = false, }: OwnProps) { - const { text, entities } = message.content.text || {}; - + const { text, entities } = extractMessageText(message, inChatList) || {}; const hasSpoilers = entities?.some((e) => e.type === ApiMessageEntityTypes.Spoiler); const hasCustomEmoji = entities?.some((e) => e.type === ApiMessageEntityTypes.CustomEmoji); + if (!text || (!hasSpoilers && !hasCustomEmoji)) { const trimmedText = trimText(getMessageSummaryText(lang, message, noEmoji), truncateLength); @@ -65,6 +68,7 @@ function MessageSummary({ observeIntersectionForPlaying={observeIntersectionForPlaying} withTranslucentThumbs={withTranslucentThumbs} truncateLength={truncateLength} + inChatList={inChatList} /> ); } diff --git a/src/components/common/MessageText.tsx b/src/components/common/MessageText.tsx index 6ac680ee5..ce5db0bfe 100644 --- a/src/components/common/MessageText.tsx +++ b/src/components/common/MessageText.tsx @@ -7,7 +7,7 @@ import type { ObserveFn } from '../../hooks/useIntersectionObserver'; import { ApiMessageEntityTypes } from '../../api/types'; import trimText from '../../util/trimText'; -import { getMessageText, stripCustomEmoji } from '../../global/helpers'; +import { extractMessageText, getMessageText, stripCustomEmoji } from '../../global/helpers'; import { renderTextWithEntities } from './helpers/renderTextWithEntities'; import useSyncEffect from '../../hooks/useSyncEffect'; @@ -24,6 +24,7 @@ interface OwnProps { observeIntersectionForPlaying?: ObserveFn; withTranslucentThumbs?: boolean; shouldRenderAsHtml?: boolean; + inChatList?: boolean; } const MIN_CUSTOM_EMOJIS_FOR_SHARED_CANVAS = 3; @@ -41,6 +42,7 @@ function MessageText({ observeIntersectionForPlaying, withTranslucentThumbs, shouldRenderAsHtml, + inChatList, }: OwnProps) { // eslint-disable-next-line no-null/no-null const sharedCanvasRef = useRef(null); @@ -49,10 +51,8 @@ function MessageText({ const textCacheBusterRef = useRef(0); - const formattedText = translatedText || message.content.text || undefined; - + const formattedText = translatedText || extractMessageText(message, inChatList); const adaptedFormattedText = isForAnimation && formattedText ? stripCustomEmoji(formattedText) : formattedText; - const { text, entities } = adaptedFormattedText || {}; useSyncEffect(() => { diff --git a/src/components/left/main/hooks/useChatListEntry.tsx b/src/components/left/main/hooks/useChatListEntry.tsx index d8f113c58..ff9fe102f 100644 --- a/src/components/left/main/hooks/useChatListEntry.tsx +++ b/src/components/left/main/hooks/useChatListEntry.tsx @@ -214,6 +214,7 @@ function renderSummary( message={message} noEmoji={Boolean(blobUrl)} observeIntersectionForLoading={observeIntersection} + inChatList /> ); diff --git a/src/global/helpers/messages.ts b/src/global/helpers/messages.ts index 4862aa8b9..6d7ee5ba0 100644 --- a/src/global/helpers/messages.ts +++ b/src/global/helpers/messages.ts @@ -295,3 +295,28 @@ export function mergeIdRanges(ranges: number[][], idsUpdate: number[]): number[] return newOutlyingLists; } + +export function extractMessageText(message: ApiMessage, inChatList = false) { + const contentText = message.content.text; + if (!contentText) return undefined; + + const { text } = contentText; + let { entities } = contentText; + + if (text && inChatList && message.chatId === SERVICE_NOTIFICATIONS_USER_ID) { + const authCode = text.match(/^\D*([\d-]{5,7})\D/)?.[1]; + if (authCode) { + entities = [ + ...entities || [], + { + type: ApiMessageEntityTypes.Spoiler, + offset: text.indexOf(authCode), + length: authCode.length, + }, + ]; + entities.sort((a, b) => (a.offset > b.offset ? 1 : -1)); + } + } + + return { text, entities }; +}