From 34cac3092f76535cd9a969f55dba0ea4befd4700 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 26 Jul 2021 23:06:14 +0300 Subject: [PATCH] Moved utils for sending bot commands to separated file. --- Telegram/CMakeLists.txt | 2 + .../SourceFiles/chat_helpers/bot_command.cpp | 49 +++++++++++++++++++ .../SourceFiles/chat_helpers/bot_command.h | 31 ++++++++++++ Telegram/SourceFiles/facades.cpp | 2 +- .../SourceFiles/history/history_widget.cpp | 27 +++++----- Telegram/SourceFiles/history/history_widget.h | 7 +-- .../history/view/history_view_list_widget.cpp | 30 ------------ .../history/view/history_view_list_widget.h | 9 ---- .../view/history_view_replies_section.cpp | 5 +- .../view/history_view_scheduled_section.cpp | 5 +- Telegram/SourceFiles/mainwidget.cpp | 8 +-- Telegram/SourceFiles/mainwidget.h | 7 +-- Telegram/SourceFiles/window/section_widget.h | 1 + 13 files changed, 111 insertions(+), 72 deletions(-) create mode 100644 Telegram/SourceFiles/chat_helpers/bot_command.cpp create mode 100644 Telegram/SourceFiles/chat_helpers/bot_command.h diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index bcce3dc7c7..a034160700 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -265,6 +265,8 @@ PRIVATE calls/calls_video_bubble.h calls/calls_video_incoming.cpp calls/calls_video_incoming.h + chat_helpers/bot_command.cpp + chat_helpers/bot_command.h chat_helpers/bot_keyboard.cpp chat_helpers/bot_keyboard.h chat_helpers/emoji_keywords.cpp diff --git a/Telegram/SourceFiles/chat_helpers/bot_command.cpp b/Telegram/SourceFiles/chat_helpers/bot_command.cpp new file mode 100644 index 0000000000..64be1e4b87 --- /dev/null +++ b/Telegram/SourceFiles/chat_helpers/bot_command.cpp @@ -0,0 +1,49 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#include "chat_helpers/bot_command.h" + +#include "data/data_channel.h" +#include "data/data_chat.h" +#include "data/data_peer.h" +#include "data/data_user.h" +#include "data/data_session.h" +#include "history/history_item.h" + +namespace Bot { + +QString WrapCommandInChat( + not_null peer, + const QString &command, + const FullMsgId &context) { + auto result = command; + if (const auto item = peer->owner().message(context)) { + if (const auto user = item->fromOriginal()->asUser()) { + return WrapCommandInChat(peer, command, user); + } + } + return result; +} + +QString WrapCommandInChat( + not_null peer, + const QString &command, + not_null bot) { + if (!bot->isBot() || bot->username.isEmpty()) { + return command; + } + const auto botStatus = peer->isChat() + ? peer->asChat()->botStatus + : peer->isMegagroup() + ? peer->asChannel()->mgInfo->botStatus + : -1; + return ((command.indexOf('@') < 2) && (botStatus == 0 || botStatus == 2)) + ? command + '@' + bot->username + : command; +} + +} // namespace Bot diff --git a/Telegram/SourceFiles/chat_helpers/bot_command.h b/Telegram/SourceFiles/chat_helpers/bot_command.h new file mode 100644 index 0000000000..79f79f9bb9 --- /dev/null +++ b/Telegram/SourceFiles/chat_helpers/bot_command.h @@ -0,0 +1,31 @@ +/* +This file is part of Telegram Desktop, +the official desktop application for the Telegram messaging service. + +For license and copyright information please follow this link: +https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL +*/ +#pragma once + +class PeerData; +class UserData; + +namespace Bot { + +struct SendCommandRequest { + not_null peer; + QString command; + FullMsgId context; + int replyTo = 0; +}; + +[[nodiscard]] QString WrapCommandInChat( + not_null peer, + const QString &command, + const FullMsgId &context); +[[nodiscard]] QString WrapCommandInChat( + not_null peer, + const QString &command, + not_null bot); + +} // namespace Bot diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index 4bfb64cfc6..0994844ac5 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -63,7 +63,7 @@ void sendBotCommand( UserData *bot, const QString &cmd, MsgId replyTo) { if (const auto m = CheckMainWidget(&peer->session())) { - m->sendBotCommand(peer, bot, cmd, replyTo); + m->sendBotCommand({ peer, /*bot,*/ cmd, FullMsgId(), replyTo }); } } diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index 96dadd4ab6..be88ff850b 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -3601,34 +3601,33 @@ void HistoryWidget::mouseReleaseEvent(QMouseEvent *e) { } } -void HistoryWidget::sendBotCommand( - not_null peer, - UserData *bot, - const QString &cmd, - MsgId replyTo) { // replyTo != 0 from ReplyKeyboardMarkup, == 0 from cmd links - if (_peer != peer.get()) { +void HistoryWidget::sendBotCommand(Bot::SendCommandRequest request) { +// replyTo != 0 from ReplyKeyboardMarkup, == 0 from command links + if (_peer != request.peer.get()) { return; } else if (showSlowmodeError()) { return; } - bool lastKeyboardUsed = (_keyboard->forMsgId() == FullMsgId(_channel, _history->lastKeyboardId)) && (_keyboard->forMsgId() == FullMsgId(_channel, replyTo)); + const auto lastKeyboardUsed = (_keyboard->forMsgId() + == FullMsgId(_channel, _history->lastKeyboardId)) + && (_keyboard->forMsgId() == FullMsgId(_channel, request.replyTo)); // 'bot' may be nullptr in case of sending from FieldAutocomplete. - const auto toSend = (replyTo || !bot) - ? cmd - : HistoryView::WrapBotCommandInChat(_peer, cmd, bot); + const auto toSend = (request.replyTo/* || !bot*/) + ? request.command + : Bot::WrapCommandInChat(_peer, request.command, request.context); auto message = ApiWrap::MessageToSend(_history); message.textWithTags = { toSend, TextWithTags::Tags() }; - message.action.replyTo = replyTo + message.action.replyTo = request.replyTo ? ((!_peer->isUser()/* && (botStatus == 0 || botStatus == 2)*/) - ? replyTo + ? request.replyTo : replyToId()) : 0; session().api().sendMessage(std::move(message)); - if (replyTo) { - if (_replyToId == replyTo) { + if (request.replyTo) { + if (_replyToId == request.replyTo) { cancelReply(); saveCloudDraft(); } diff --git a/Telegram/SourceFiles/history/history_widget.h b/Telegram/SourceFiles/history/history_widget.h index 88469d6c7e..2d8b0c3c47 100644 --- a/Telegram/SourceFiles/history/history_widget.h +++ b/Telegram/SourceFiles/history/history_widget.h @@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "history/history_drag_area.h" #include "ui/widgets/tooltip.h" #include "mainwidget.h" +#include "chat_helpers/bot_command.h" #include "chat_helpers/field_autocomplete.h" #include "window/section_widget.h" #include "ui/widgets/input_fields.h" @@ -212,11 +213,7 @@ public: void escape(); - void sendBotCommand( - not_null peer, - UserData *bot, - const QString &cmd, - MsgId replyTo); + void sendBotCommand(Bot::SendCommandRequest request); void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo); bool insertBotCommand(const QString &cmd); diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp index 15f1637fe6..c6786e75ad 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.cpp +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.cpp @@ -3029,34 +3029,4 @@ void ConfirmSendNowSelectedItems(not_null widget) { clearSelection); } -QString WrapBotCommandInChat( - not_null peer, - const QString &command, - const FullMsgId &context) { - auto result = command; - if (const auto item = peer->owner().message(context)) { - if (const auto user = item->fromOriginal()->asUser()) { - return WrapBotCommandInChat(peer, command, user); - } - } - return result; -} - -QString WrapBotCommandInChat( - not_null peer, - const QString &command, - not_null bot) { - if (!bot->isBot() || bot->username.isEmpty()) { - return command; - } - const auto botStatus = peer->isChat() - ? peer->asChat()->botStatus - : peer->isMegagroup() - ? peer->asChannel()->mgInfo->botStatus - : -1; - return ((command.indexOf('@') < 2) && (botStatus == 0 || botStatus == 2)) - ? command + '@' + bot->username - : command; -} - } // namespace HistoryView diff --git a/Telegram/SourceFiles/history/view/history_view_list_widget.h b/Telegram/SourceFiles/history/view/history_view_list_widget.h index a4a5dff626..5d09109856 100644 --- a/Telegram/SourceFiles/history/view/history_view_list_widget.h +++ b/Telegram/SourceFiles/history/view/history_view_list_widget.h @@ -599,13 +599,4 @@ void ConfirmDeleteSelectedItems(not_null widget); void ConfirmForwardSelectedItems(not_null widget); void ConfirmSendNowSelectedItems(not_null widget); -[[nodiscard]] QString WrapBotCommandInChat( - not_null peer, - const QString &command, - const FullMsgId &context); -[[nodiscard]] QString WrapBotCommandInChat( - not_null peer, - const QString &command, - not_null bot); - } // namespace HistoryView diff --git a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp index 96dfdba2aa..2d29151e0e 100644 --- a/Telegram/SourceFiles/history/view/history_view_replies_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_replies_section.cpp @@ -1766,7 +1766,10 @@ bool RepliesWidget::listIsGoodForAroundPosition( void RepliesWidget::listSendBotCommand( const QString &command, const FullMsgId &context) { - const auto text = WrapBotCommandInChat(_history->peer, command, context); + const auto text = Bot::WrapCommandInChat( + _history->peer, + command, + context); auto message = ApiWrap::MessageToSend(_history); message.textWithTags = { text }; message.action.replyTo = replyToId(); diff --git a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp index 9cc1f62f41..cc3b7d4645 100644 --- a/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp +++ b/Telegram/SourceFiles/history/view/history_view_scheduled_section.cpp @@ -1181,7 +1181,10 @@ void ScheduledWidget::listSendBotCommand( const QString &command, const FullMsgId &context) { const auto callback = [=](Api::SendOptions options) { - const auto text = WrapBotCommandInChat(_history->peer, command, context); + const auto text = Bot::WrapCommandInChat( + _history->peer, + command, + context); auto message = ApiWrap::MessageToSend(_history); message.textWithTags = { text }; message.action.options = options; diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index d50f7ec91f..acc296483a 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -827,12 +827,8 @@ crl::time MainWidget::highlightStartTime(not_null item) cons return _history->highlightStartTime(item); } -void MainWidget::sendBotCommand( - not_null peer, - UserData *bot, - const QString &cmd, - MsgId replyTo) { - _history->sendBotCommand(peer, bot, cmd, replyTo); +void MainWidget::sendBotCommand(Bot::SendCommandRequest request) { + _history->sendBotCommand(request); } void MainWidget::hideSingleUseKeyboard(PeerData *peer, MsgId replyTo) { diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index 23aa9e8507..5a281bdc8a 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "base/timer.h" #include "base/weak_ptr.h" +#include "chat_helpers/bot_command.h" #include "ui/rp_widget.h" #include "ui/effects/animations.h" #include "media/player/media_player_float.h" @@ -178,11 +179,7 @@ public: // While HistoryInner is not HistoryView::ListWidget. crl::time highlightStartTime(not_null item) const; - void sendBotCommand( - not_null peer, - UserData *bot, - const QString &cmd, - MsgId replyTo); + void sendBotCommand(Bot::SendCommandRequest request); void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo); bool insertBotCommand(const QString &cmd); diff --git a/Telegram/SourceFiles/window/section_widget.h b/Telegram/SourceFiles/window/section_widget.h index e599bb0285..ef7c7720b3 100644 --- a/Telegram/SourceFiles/window/section_widget.h +++ b/Telegram/SourceFiles/window/section_widget.h @@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #pragma once #include "ui/rp_widget.h" +#include "chat_helpers/bot_command.h" #include "dialogs/dialogs_key.h" #include "media/player/media_player_float.h" // FloatSectionDelegate #include "base/object_ptr.h"