From 2dcf951f4cd41c88481aa38c05df62e8379943c7 Mon Sep 17 00:00:00 2001 From: Alexander Zinchuk Date: Tue, 28 Sep 2021 15:42:35 +0300 Subject: [PATCH] Support forced replies to bots (#1471) --- src/api/gramjs/apiBuilders/messages.ts | 1 + src/api/gramjs/updater.ts | 5 +++++ src/api/types/messages.ts | 1 + src/api/types/updates.ts | 1 + src/components/middle/composer/Composer.scss | 8 ++++---- src/modules/actions/apiUpdaters/messages.ts | 10 ++++++++-- 6 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/api/gramjs/apiBuilders/messages.ts b/src/api/gramjs/apiBuilders/messages.ts index 6dd1c8e9..f084b415 100644 --- a/src/api/gramjs/apiBuilders/messages.ts +++ b/src/api/gramjs/apiBuilders/messages.ts @@ -171,6 +171,7 @@ export function buildApiMessageWithChatId(chatId: number, mtpMessage: UniversalM ...(isEdited && { isEdited }), ...(isMediaUnread && { isMediaUnread }), ...(mtpMessage.mentioned && isMediaUnread && { hasUnreadMention: true }), + ...(mtpMessage.mentioned && { isMentioned: true }), ...(groupedId && { groupedId, isInAlbum, diff --git a/src/api/gramjs/updater.ts b/src/api/gramjs/updater.ts index 697ecf84..6650a2ad 100644 --- a/src/api/gramjs/updater.ts +++ b/src/api/gramjs/updater.ts @@ -85,6 +85,7 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) { || update instanceof GramJs.UpdateServiceNotification ) { let message: ApiMessage | undefined; + let shouldForceReply: boolean | undefined; if (update instanceof GramJs.UpdateShortChatMessage) { message = buildApiMessageFromShortChat(update); @@ -113,6 +114,9 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) { } message = buildApiMessage(update.message)!; + shouldForceReply = 'replyMarkup' in update.message + && update.message?.replyMarkup instanceof GramJs.ReplyKeyboardForceReply + && (!update.message.replyMarkup.selective || message.isMentioned); } // eslint-disable-next-line no-underscore-dangle @@ -161,6 +165,7 @@ export function updater(update: Update, originRequest?: GramJs.AnyRequest) { id: message.id, chatId: message.chatId, message, + shouldForceReply, }); } diff --git a/src/api/types/messages.ts b/src/api/types/messages.ts index c34f5d6c..74d6520a 100644 --- a/src/api/types/messages.ts +++ b/src/api/types/messages.ts @@ -245,6 +245,7 @@ export interface ApiMessage { previousLocalId?: number; views?: number; isEdited?: boolean; + isMentioned?: boolean; isMediaUnread?: boolean; groupedId?: string; isInAlbum?: boolean; diff --git a/src/api/types/updates.ts b/src/api/types/updates.ts index f6bd3e39..8f5ad756 100644 --- a/src/api/types/updates.ts +++ b/src/api/types/updates.ts @@ -158,6 +158,7 @@ export type ApiUpdateNewMessage = { chatId: number; id: number; message: Partial; + shouldForceReply?: boolean; }; export type ApiUpdateMessage = { diff --git a/src/components/middle/composer/Composer.scss b/src/components/middle/composer/Composer.scss index 19bb012a..3bb14487 100644 --- a/src/components/middle/composer/Composer.scss +++ b/src/components/middle/composer/Composer.scss @@ -334,15 +334,15 @@ font-size: var(--composer-text-size, 1rem); top: calc((3.25rem - var(--composer-text-size, 1rem) * 1.375) / 2); bottom: auto; + white-space: nowrap; + overflow: hidden; + text-overflow: ellipsis; + max-width: 100%; } .forced-placeholder { z-index: var(--z-below); left: 0; - white-space: nowrap; - overflow: hidden; - max-width: 100%; - text-overflow: ellipsis; } &[dir=rtl] .placeholder-text { diff --git a/src/modules/actions/apiUpdaters/messages.ts b/src/modules/actions/apiUpdaters/messages.ts index 8566021e..ac7b4b8e 100644 --- a/src/modules/actions/apiUpdaters/messages.ts +++ b/src/modules/actions/apiUpdaters/messages.ts @@ -46,7 +46,9 @@ const ANIMATION_DELAY = 350; addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { switch (update['@type']) { case 'newMessage': { - const { chatId, id, message } = update; + const { + chatId, id, message, shouldForceReply, + } = update; global = updateWithLocalMedia(global, chatId, id, message); global = updateListedAndViewportIds(global, actions, message as ApiMessage); @@ -86,7 +88,11 @@ addReducer('apiUpdate', (global, actions, update: ApiUpdate) => { // @perf Wait until scroll animation finishes or simply rely on delivery status update (which is itself delayed) if (!isMessageLocal(message as ApiMessage)) { setTimeout(() => { - setGlobal(updateChatLastMessage(getGlobal(), chatId, newMessage)); + let delayedGlobal = getGlobal(); + if (shouldForceReply) { + delayedGlobal = replaceThreadParam(delayedGlobal, chatId, MAIN_THREAD_ID, 'replyingToId', id); + } + setGlobal(updateChatLastMessage(delayedGlobal, chatId, newMessage)); }, ANIMATION_DELAY); } } else {