Fix incorrect text in notification about reaction (#2853 )

This commit is contained in:
Alexander Zinchuk 2023-03-30 18:25:52 -05:00
parent 2e9dcb8060
commit 31e8a934e5
4 changed files with 53 additions and 20 deletions

View File

@ -855,7 +855,7 @@ function copyTextForMessages(global: GlobalState, chatId: string, messageIds: nu
const resultText = messages.reduce((acc, message) => { const resultText = messages.reduce((acc, message) => {
const sender = selectSender(global, message); const sender = selectSender(global, message);
acc.push(`> ${sender ? getSenderTitle(lang, sender) : ''}:`); acc.push(`> ${sender ? getSenderTitle(lang, sender) : ''}:`);
acc.push(`${getMessageSummaryText(lang, message, false, 0, undefined, true)}\n`); acc.push(`${getMessageSummaryText(lang, message, false, 0, true)}\n`);
return acc; return acc;
}, [] as string[]); }, [] as string[]);

View File

@ -7,7 +7,6 @@ import type { LangFn } from '../../hooks/useLang';
import trimText from '../../util/trimText'; import trimText from '../../util/trimText';
import { getMessageText, getMessageTranscription } from './messages'; import { getMessageText, getMessageTranscription } from './messages';
import { getMessageRecentReaction } from './reactions';
const SPOILER_CHARS = ['⠺', '⠵', '⠞', '⠟']; const SPOILER_CHARS = ['⠺', '⠵', '⠞', '⠟'];
export const TRUNCATED_SUMMARY_LENGTH = 80; export const TRUNCATED_SUMMARY_LENGTH = 80;
@ -17,13 +16,12 @@ export function getMessageSummaryText(
message: ApiMessage, message: ApiMessage,
noEmoji = false, noEmoji = false,
truncateLength = TRUNCATED_SUMMARY_LENGTH, truncateLength = TRUNCATED_SUMMARY_LENGTH,
noReactions = true,
isExtended = false, isExtended = false,
) { ) {
const emoji = !noEmoji && getMessageSummaryEmoji(message, noReactions); const emoji = !noEmoji && getMessageSummaryEmoji(message);
const emojiWithSpace = emoji ? `${emoji} ` : ''; const emojiWithSpace = emoji ? `${emoji} ` : '';
const text = trimText(getMessageTextWithSpoilers(message), truncateLength); const text = trimText(getMessageTextWithSpoilers(message), truncateLength);
const description = getMessageSummaryDescription(lang, message, text, noReactions, isExtended); const description = getMessageSummaryDescription(lang, message, text, isExtended);
return `${emojiWithSpace}${description}`; return `${emojiWithSpace}${description}`;
} }
@ -58,7 +56,7 @@ export function getMessageTextWithSpoilers(message: ApiMessage) {
return transcription ? `${transcription}\n${text}` : text; return transcription ? `${transcription}\n${text}` : text;
} }
export function getMessageSummaryEmoji(message: ApiMessage, noReactions = true) { export function getMessageSummaryEmoji(message: ApiMessage) {
const { const {
photo, photo,
video, video,
@ -97,11 +95,6 @@ export function getMessageSummaryEmoji(message: ApiMessage, noReactions = true)
return '📊'; return '📊';
} }
const reaction = !noReactions && getMessageRecentReaction(message);
if (reaction) {
return reaction.reaction;
}
return undefined; return undefined;
} }
@ -109,7 +102,6 @@ export function getMessageSummaryDescription(
lang: LangFn, lang: LangFn,
message: ApiMessage, message: ApiMessage,
truncatedText?: string | TeactNode, truncatedText?: string | TeactNode,
noReactions = true,
isExtended = false, isExtended = false,
) { ) {
const { const {
@ -189,11 +181,6 @@ export function getMessageSummaryDescription(
summary = `🎮 ${game.title}`; summary = `🎮 ${game.title}`;
} }
const reaction = !noReactions && getMessageRecentReaction(message);
if (summary && reaction) {
summary = `to your "${summary}"`;
}
return summary || CONTENT_NOT_SUPPORTED; return summary || CONTENT_NOT_SUPPORTED;
} }

View File

@ -12,7 +12,7 @@ export function renderMessageSummaryHtml(
const text = renderMessageText( const text = renderMessageText(
message, undefined, undefined, undefined, undefined, undefined, true, message, undefined, undefined, undefined, undefined, undefined, true,
)?.join(''); )?.join('');
const description = getMessageSummaryDescription(lang, message, text, true, true); const description = getMessageSummaryDescription(lang, message, text, true);
return `${emojiWithSpace}${description}`; return `${emojiWithSpace}${description}`;
} }

View File

@ -32,6 +32,7 @@ import { IS_SERVICE_WORKER_SUPPORTED, IS_TOUCH_ENV } from './windowEnvironment';
import { translate } from './langProvider'; import { translate } from './langProvider';
import * as mediaLoader from './mediaLoader'; import * as mediaLoader from './mediaLoader';
import { debounce } from './schedulers'; import { debounce } from './schedulers';
import { buildCollectionByKey } from './iteratees';
function getDeviceToken(subscription: PushSubscription) { function getDeviceToken(subscription: PushSubscription) {
const data = subscription.toJSON(); const data = subscription.toJSON();
@ -191,6 +192,28 @@ async function loadNotificationSettings() {
return selectNotifySettings(global); return selectNotifySettings(global);
} }
// Load custom emoji from the api if it's not cached already
async function loadCustomEmoji(id: string) {
let global = getGlobal();
if (global.customEmojis.byId[id]) return;
const customEmoji = await callApi('fetchCustomEmoji', {
documentId: [id],
});
if (!customEmoji) return;
global = getGlobal();
global = {
...global,
customEmojis: {
...global.customEmojis,
byId: {
...global.customEmojis.byId,
...buildCollectionByKey(customEmoji, 'id'),
},
},
};
setGlobal(global);
}
export async function subscribe() { export async function subscribe() {
if (!checkIfPushSupported()) { if (!checkIfPushSupported()) {
// Ask for notification permissions only if service worker notifications are not supported // Ask for notification permissions only if service worker notifications are not supported
@ -267,7 +290,8 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage, reaction?: A
let { let {
senderId, senderId,
} = message; } = message;
if (reaction) senderId = reaction.userId; const hasReaction = Boolean(reaction);
if (hasReaction) senderId = reaction.userId;
const { isScreenLocked } = global.passcode; const { isScreenLocked } = global.passcode;
const messageSender = senderId ? selectUser(global, senderId) : undefined; const messageSender = senderId ? selectUser(global, senderId) : undefined;
@ -311,7 +335,12 @@ function getNotificationContent(chat: ApiChat, message: ApiMessage, reaction?: A
} else { } else {
// TODO[forums] Support ApiChat // TODO[forums] Support ApiChat
const senderName = getMessageSenderName(translate, chat.id, messageSender); const senderName = getMessageSenderName(translate, chat.id, messageSender);
const summary = getMessageSummaryText(translate, message, false, 60, false); let summary = getMessageSummaryText(translate, message, hasReaction, 60);
if (hasReaction) {
const emoji = getReactionEmoji(reaction);
summary = translate('PushReactText', [emoji, summary]);
}
body = senderName ? `${senderName}: ${summary}` : summary; body = senderName ? `${senderName}: ${summary}` : summary;
} }
@ -339,6 +368,18 @@ async function getAvatar(chat: ApiChat | ApiUser) {
return mediaData; return mediaData;
} }
function getReactionEmoji(reaction: ApiUserReaction) {
let emoji;
if ('emoticon' in reaction.reaction) {
emoji = reaction.reaction.emoticon;
}
if ('documentId' in reaction.reaction) {
// eslint-disable-next-line eslint-multitab-tt/no-immediate-global
emoji = getGlobal().customEmojis.byId[reaction.reaction.documentId]?.emoji;
}
return emoji || '❤️';
}
export async function notifyAboutCall({ export async function notifyAboutCall({
call, user, call, user,
}: { }: {
@ -396,6 +437,11 @@ export async function notifyAboutMessage({
// Do not notify about reactions on messages that are not outgoing // Do not notify about reactions on messages that are not outgoing
if (isReaction && !activeReaction) return; if (isReaction && !activeReaction) return;
// If this is a custom emoji reaction we need to make sure it is loaded
if (isReaction && activeReaction && 'documentId' in activeReaction.reaction) {
await loadCustomEmoji(activeReaction.reaction.documentId);
}
const icon = await getAvatar(chat); const icon = await getAvatar(chat);
const { const {