Added same_peer flag support for keyboard switch inline button.

This commit is contained in:
John Preston 2016-08-12 19:28:10 +03:00
parent f4d5960953
commit 2321f38550
8 changed files with 39 additions and 17 deletions

View File

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

View File

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

View File

@ -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 QVector<MTPKeyboardBu
buttonRow.push_back({ Button::Url, qs(buttonData.vtext), qba(buttonData.vurl), 0 });
} break;
case mtpc_keyboardButtonSwitchInline: {
const auto &buttonData(button.c_keyboardButtonSwitchInline());
buttonRow.push_back({ Button::SwitchInline, qs(buttonData.vtext), qba(buttonData.vquery), 0 });
flags |= MTPDreplyKeyboardMarkup_ClientFlag::f_has_switch_inline_button;
auto &buttonData = button.c_keyboardButtonSwitchInline();
auto buttonType = buttonData.is_same_peer() ? Button::SwitchInlineSame : Button::SwitchInline;
buttonRow.push_back({ buttonType, qs(buttonData.vtext), qba(buttonData.vquery), 0 });
if (buttonType == Button::SwitchInline) {
// Optimization flag.
// Fast check on all new messages if there is a switch button to auto-click it.
flags |= MTPDreplyKeyboardMarkup_ClientFlag::f_has_switch_inline_button;
}
} break;
}
}
@ -6593,6 +6598,7 @@ void HistoryMessage::KeyboardStyle::paintButtonIcon(Painter &p, const QRect &rec
case Button::Url: sprite = st::msgBotKbUrlIcon; break;
// case Button::RequestPhone: sprite = st::msgBotKbRequestPhoneIcon; break;
// case Button::RequestLocation: sprite = st::msgBotKbRequestLocationIcon; break;
case Button::SwitchInlineSame:
case Button::SwitchInline: sprite = st::msgBotKbSwitchPmIcon; break;
}
if (!sprite.isEmpty()) {
@ -6612,6 +6618,7 @@ int HistoryMessage::KeyboardStyle::minButtonWidth(HistoryMessageReplyMarkup::But
case Button::Url: iconWidth = st::msgBotKbUrlIcon.pxWidth(); break;
//case Button::RequestPhone: iconWidth = st::msgBotKbRequestPhoneIcon.pxWidth(); break;
//case Button::RequestLocation: iconWidth = st::msgBotKbRequestLocationIcon.pxWidth(); break;
case Button::SwitchInlineSame:
case Button::SwitchInline: iconWidth = st::msgBotKbSwitchPmIcon.pxWidth(); break;
case Button::Callback: iconWidth = st::msgInvSendingImg.pxWidth(); break;
}

View File

@ -821,6 +821,7 @@ struct HistoryMessageReplyMarkup : public BaseComponent<HistoryMessageReplyMarku
RequestPhone,
RequestLocation,
SwitchInline,
SwitchInlineSame,
};
Type type;
QString text;

View File

@ -3570,8 +3570,17 @@ void HistoryWidget::notify_inlineKeyboardMoved(const HistoryItem *item, int oldK
}
}
bool HistoryWidget::notify_switchInlineBotButtonReceived(const QString &query) {
if (UserData *bot = _peer ? _peer->asUser() : 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<Data::Draft>(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;

View File

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

View File

@ -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) {

View File

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