From 2321f385505ec97462ca585fef4b204732af4f1e Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 12 Aug 2016 19:28:10 +0300 Subject: [PATCH] Added same_peer flag support for keyboard switch inline button. --- Telegram/SourceFiles/facades.cpp | 15 ++++++++++----- Telegram/SourceFiles/facades.h | 2 +- Telegram/SourceFiles/history.cpp | 17 ++++++++++++----- Telegram/SourceFiles/history.h | 1 + Telegram/SourceFiles/historywidget.cpp | 13 +++++++++++-- Telegram/SourceFiles/historywidget.h | 2 +- Telegram/SourceFiles/mainwidget.cpp | 4 ++-- Telegram/SourceFiles/mainwidget.h | 2 +- 8 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Telegram/SourceFiles/facades.cpp b/Telegram/SourceFiles/facades.cpp index 936ea552d9..a3cf6c41b6 100644 --- a/Telegram/SourceFiles/facades.cpp +++ b/Telegram/SourceFiles/facades.cpp @@ -91,6 +91,7 @@ void activateBotCommand(const HistoryItem *msg, int row, int col) { Ui::showLayer(box); } break; + case HistoryMessageReplyMarkup::Button::SwitchInlineSame: case HistoryMessageReplyMarkup::Button::SwitchInline: { if (auto m = App::main()) { auto getMessageBot = [msg]() -> UserData* { @@ -104,8 +105,12 @@ void activateBotCommand(const HistoryItem *msg, int row, int col) { return nullptr; }; if (auto bot = getMessageBot()) { - auto tryFastSwitch = [bot, &button]() -> bool { - if (bot->botInfo && bot->botInfo->inlineReturnPeerId) { + auto tryFastSwitch = [bot, &button, msgId = msg->id]() -> bool { + auto samePeer = (button->type == HistoryMessageReplyMarkup::Button::SwitchInlineSame); + if (samePeer) { + Notify::switchInlineBotButtonReceived(QString::fromUtf8(button->data), bot, msgId); + return true; + } else if (bot->botInfo && bot->botInfo->inlineReturnPeerId) { if (Notify::switchInlineBotButtonReceived(QString::fromUtf8(button->data))) { return true; } @@ -332,9 +337,9 @@ void inlineKeyboardMoved(const HistoryItem *item, int oldKeyboardTop, int newKey } } -bool switchInlineBotButtonReceived(const QString &query) { - if (MainWidget *m = App::main()) { - return m->notify_switchInlineBotButtonReceived(query); +bool switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo) { + if (auto m = App::main()) { + return m->notify_switchInlineBotButtonReceived(query, samePeerBot, samePeerReplyTo); } return false; } diff --git a/Telegram/SourceFiles/facades.h b/Telegram/SourceFiles/facades.h index 364dc28da5..ff9b0f950f 100644 --- a/Telegram/SourceFiles/facades.h +++ b/Telegram/SourceFiles/facades.h @@ -127,7 +127,7 @@ void botCommandsChanged(UserData *user); void inlineBotRequesting(bool requesting); void replyMarkupUpdated(const HistoryItem *item); void inlineKeyboardMoved(const HistoryItem *item, int oldKeyboardTop, int newKeyboardTop); -bool switchInlineBotButtonReceived(const QString &query); +bool switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot = nullptr, MsgId samePeerReplyTo = 0); void migrateUpdated(PeerData *peer); diff --git a/Telegram/SourceFiles/history.cpp b/Telegram/SourceFiles/history.cpp index 6d4076dd12..f8344fbe66 100644 --- a/Telegram/SourceFiles/history.cpp +++ b/Telegram/SourceFiles/history.cpp @@ -689,10 +689,10 @@ void checkForSwitchInlineButton(HistoryItem *item) { } // namespace HistoryItem *Histories::addNewMessage(const MTPMessage &msg, NewMessageType type) { - PeerId peer = peerFromMessage(msg); + auto peer = peerFromMessage(msg); if (!peer) return nullptr; - HistoryItem *result = App::history(peer)->addNewMessage(msg, type); + auto result = App::history(peer)->addNewMessage(msg, type); if (result && type == NewMessageUnread) { checkForSwitchInlineButton(result); } @@ -2552,9 +2552,14 @@ void HistoryMessageReplyMarkup::createFromButtonRows(const QVectorasUser() : nullptr) { +bool HistoryWidget::notify_switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo) { + if (samePeerBot) { + if (_history) { + TextWithTags textWithTags = { '@' + samePeerBot->username + ' ' + query, TextWithTags::Tags() }; + MessageCursor cursor = { textWithTags.text.size(), textWithTags.text.size(), QFIXED_MAX }; + auto replyTo = _history->peer->isUser() ? 0 : samePeerReplyTo; + _history->setLocalDraft(std_::make_unique(textWithTags, replyTo, cursor, false)); + applyDraft(); + return true; + } + } else if (auto bot = _peer ? _peer->asUser() : nullptr) { PeerId toPeerId = bot->botInfo ? bot->botInfo->inlineReturnPeerId : 0; if (!toPeerId) { return false; diff --git a/Telegram/SourceFiles/historywidget.h b/Telegram/SourceFiles/historywidget.h index ee97d4115b..73910cee00 100644 --- a/Telegram/SourceFiles/historywidget.h +++ b/Telegram/SourceFiles/historywidget.h @@ -720,7 +720,7 @@ public: void notify_inlineBotRequesting(bool requesting); void notify_replyMarkupUpdated(const HistoryItem *item); void notify_inlineKeyboardMoved(const HistoryItem *item, int oldKeyboardTop, int newKeyboardTop); - bool notify_switchInlineBotButtonReceived(const QString &query); + bool notify_switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo); void notify_userIsBotChanged(UserData *user); void notify_migrateUpdated(PeerData *peer); void notify_clipStopperHidden(ClipStopperType type); diff --git a/Telegram/SourceFiles/mainwidget.cpp b/Telegram/SourceFiles/mainwidget.cpp index e49d9f1ede..5bda53b1d9 100644 --- a/Telegram/SourceFiles/mainwidget.cpp +++ b/Telegram/SourceFiles/mainwidget.cpp @@ -444,8 +444,8 @@ void MainWidget::notify_inlineKeyboardMoved(const HistoryItem *item, int oldKeyb _history->notify_inlineKeyboardMoved(item, oldKeyboardTop, newKeyboardTop); } -bool MainWidget::notify_switchInlineBotButtonReceived(const QString &query) { - return _history->notify_switchInlineBotButtonReceived(query); +bool MainWidget::notify_switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo) { + return _history->notify_switchInlineBotButtonReceived(query, samePeerBot, samePeerReplyTo); } void MainWidget::notify_userIsBotChanged(UserData *bot) { diff --git a/Telegram/SourceFiles/mainwidget.h b/Telegram/SourceFiles/mainwidget.h index 0ee2899c7c..e0645a95be 100644 --- a/Telegram/SourceFiles/mainwidget.h +++ b/Telegram/SourceFiles/mainwidget.h @@ -391,7 +391,7 @@ public: void notify_inlineBotRequesting(bool requesting); void notify_replyMarkupUpdated(const HistoryItem *item); void notify_inlineKeyboardMoved(const HistoryItem *item, int oldKeyboardTop, int newKeyboardTop); - bool notify_switchInlineBotButtonReceived(const QString &query); + bool notify_switchInlineBotButtonReceived(const QString &query, UserData *samePeerBot, MsgId samePeerReplyTo); void notify_userIsBotChanged(UserData *bot); void notify_userIsContactChanged(UserData *user, bool fromThisApp); void notify_migrateUpdated(PeerData *peer);