From dc2d7984a1f689214fff2ea8845cf8365cd4b217 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 6 Dec 2022 13:29:47 +0100 Subject: [PATCH] Custom Emoji: Fix size in message media caption (#2179) --- src/api/gramjs/apiBuilders/messages.ts | 17 +++++++++++------ src/api/gramjs/methods/messages.ts | 12 ++++++++---- .../helpers/getEmojiOnlyCountForMessage.ts | 13 +++++++++++++ 3 files changed, 32 insertions(+), 10 deletions(-) create mode 100644 src/global/helpers/getEmojiOnlyCountForMessage.ts diff --git a/src/api/gramjs/apiBuilders/messages.ts b/src/api/gramjs/apiBuilders/messages.ts index 0fecbef87..964ee54b4 100644 --- a/src/api/gramjs/apiBuilders/messages.ts +++ b/src/api/gramjs/apiBuilders/messages.ts @@ -57,7 +57,7 @@ import { buildPeer } from '../gramjsBuilders'; import { addPhotoToLocalDb, resolveMessageApiChatId, serializeBytes } from '../helpers'; import { buildApiPeerId, getApiChatIdFromMtpPeer, isPeerUser } from './peers'; import { buildApiCallDiscardReason } from './calls'; -import parseEmojiOnlyString from '../../../util/parseEmojiOnlyString'; +import { getEmojiOnlyCountForMessage } from '../../../global/helpers/getEmojiOnlyCountForMessage'; const LOCAL_MEDIA_UPLOADING_TEMP_ID = 'temp'; const INPUT_WAVEFORM_LENGTH = 63; @@ -177,7 +177,7 @@ export function buildApiMessageWithChatId(chatId: string, mtpMessage: UniversalM const shouldHideKeyboardButtons = mtpMessage.replyMarkup instanceof GramJs.ReplyKeyboardHide; const isProtected = mtpMessage.noforwards || isInvoiceMedia; const isForwardingAllowed = !mtpMessage.noforwards; - const emojiOnlyCount = content.text && !groupedId && parseEmojiOnlyString(content.text.text); + const emojiOnlyCount = getEmojiOnlyCountForMessage(content, groupedId); return { id: mtpMessage.id, @@ -1219,9 +1219,8 @@ export function buildLocalMessage( const localId = getNextLocalMessageId(); const media = attachment && buildUploadingMedia(attachment); const isChannel = chat.type === 'chatTypeChannel'; - const emojiOnlyCount = text && !groupedId && parseEmojiOnlyString(text); - return { + const message = { id: localId, chatId: chat.id, content: { @@ -1240,7 +1239,6 @@ export function buildLocalMessage( date: scheduledAt || Math.round(Date.now() / 1000) + serverTimeOffset, isOutgoing: !isChannel, senderId: sendAs?.id || currentUserId, - ...(emojiOnlyCount && { emojiOnlyCount }), ...(replyingTo && { replyToMessageId: replyingTo }), ...(replyingToTopId && { replyToTopMessageId: replyingToTopId }), ...(groupedId && { @@ -1250,6 +1248,13 @@ export function buildLocalMessage( ...(scheduledAt && { isScheduled: true }), isForwardingAllowed: true, }; + + const emojiOnlyCount = getEmojiOnlyCountForMessage(message.content, message.groupedId); + + return { + ...message, + ...(emojiOnlyCount && { emojiOnlyCount }), + }; } export function buildLocalForwardedMessage( @@ -1281,7 +1286,7 @@ export function buildLocalForwardedMessage( text: content.text.text, entities: content.text.entities.filter((entity) => entity.type !== ApiMessageEntityTypes.CustomEmoji), } : content.text; - const emojiOnlyCount = content.text && !groupedId && parseEmojiOnlyString(content.text.text); + const emojiOnlyCount = getEmojiOnlyCountForMessage(content, groupedId); const updatedContent = { ...content, diff --git a/src/api/gramjs/methods/messages.ts b/src/api/gramjs/methods/messages.ts index f067271c1..14266f2bc 100644 --- a/src/api/gramjs/methods/messages.ts +++ b/src/api/gramjs/methods/messages.ts @@ -63,7 +63,7 @@ import { } from '../helpers'; import { interpolateArray } from '../../../util/waveform'; import { requestChatUpdate } from './chats'; -import parseEmojiOnlyString from '../../../util/parseEmojiOnlyString'; +import { getEmojiOnlyCountForMessage } from '../../../global/helpers/getEmojiOnlyCountForMessage'; const FAST_SEND_TIMEOUT = 1000; const INPUT_WAVEFORM_LENGTH = 63; @@ -492,8 +492,7 @@ export async function editMessage({ serverTimeOffset: number; }) { const isScheduled = message.date * 1000 > Date.now() + serverTimeOffset * 1000; - const emojiOnlyCount = text && !message.groupedId ? parseEmojiOnlyString(text) : undefined; - const messageUpdate: Partial = { + let messageUpdate: Partial = { content: { ...message.content, ...(text && { @@ -503,7 +502,12 @@ export async function editMessage({ }, }), }, - emojiOnlyCount: emojiOnlyCount || undefined, + }; + + const emojiOnlyCount = getEmojiOnlyCountForMessage(messageUpdate.content!, messageUpdate.groupedId); + messageUpdate = { + ...messageUpdate, + emojiOnlyCount, }; onUpdate({ diff --git a/src/global/helpers/getEmojiOnlyCountForMessage.ts b/src/global/helpers/getEmojiOnlyCountForMessage.ts new file mode 100644 index 000000000..093121950 --- /dev/null +++ b/src/global/helpers/getEmojiOnlyCountForMessage.ts @@ -0,0 +1,13 @@ +import type { ApiMessage } from '../../api/types'; +import { ApiMessageEntityTypes } from '../../api/types'; +import parseEmojiOnlyString from '../../util/parseEmojiOnlyString'; + +export function getEmojiOnlyCountForMessage(content: ApiMessage['content'], groupedId?: string): number | undefined { + if (!content.text) return undefined; + return ( + !groupedId + && Object.keys(content).length === 1 // Only text is present + && !content.text.entities?.some((entity) => entity.type !== ApiMessageEntityTypes.CustomEmoji) + && parseEmojiOnlyString(content.text.text) + ) || undefined; +}