Custom Emoji: Fix size in message media caption (#2179)

This commit is contained in:
Alexander Zinchuk 2022-12-06 13:29:47 +01:00
parent af49c11dd4
commit dc2d7984a1
3 changed files with 32 additions and 10 deletions

View File

@ -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,

View File

@ -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<ApiMessage> = {
let messageUpdate: Partial<ApiMessage> = {
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({

View File

@ -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;
}