Moved utils for sending bot commands to separated file.

This commit is contained in:
23rd 2021-07-26 23:06:14 +03:00
parent 05f1caf944
commit 34cac3092f
13 changed files with 111 additions and 72 deletions

View file

@ -265,6 +265,8 @@ PRIVATE
calls/calls_video_bubble.h calls/calls_video_bubble.h
calls/calls_video_incoming.cpp calls/calls_video_incoming.cpp
calls/calls_video_incoming.h calls/calls_video_incoming.h
chat_helpers/bot_command.cpp
chat_helpers/bot_command.h
chat_helpers/bot_keyboard.cpp chat_helpers/bot_keyboard.cpp
chat_helpers/bot_keyboard.h chat_helpers/bot_keyboard.h
chat_helpers/emoji_keywords.cpp chat_helpers/emoji_keywords.cpp

View file

@ -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<PeerData*> 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<PeerData*> peer,
const QString &command,
not_null<UserData*> 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

View file

@ -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<PeerData*> peer;
QString command;
FullMsgId context;
int replyTo = 0;
};
[[nodiscard]] QString WrapCommandInChat(
not_null<PeerData*> peer,
const QString &command,
const FullMsgId &context);
[[nodiscard]] QString WrapCommandInChat(
not_null<PeerData*> peer,
const QString &command,
not_null<UserData*> bot);
} // namespace Bot

View file

@ -63,7 +63,7 @@ void sendBotCommand(
UserData *bot, UserData *bot,
const QString &cmd, MsgId replyTo) { const QString &cmd, MsgId replyTo) {
if (const auto m = CheckMainWidget(&peer->session())) { if (const auto m = CheckMainWidget(&peer->session())) {
m->sendBotCommand(peer, bot, cmd, replyTo); m->sendBotCommand({ peer, /*bot,*/ cmd, FullMsgId(), replyTo });
} }
} }

View file

@ -3601,34 +3601,33 @@ void HistoryWidget::mouseReleaseEvent(QMouseEvent *e) {
} }
} }
void HistoryWidget::sendBotCommand( void HistoryWidget::sendBotCommand(Bot::SendCommandRequest request) {
not_null<PeerData*> peer, // replyTo != 0 from ReplyKeyboardMarkup, == 0 from command links
UserData *bot, if (_peer != request.peer.get()) {
const QString &cmd,
MsgId replyTo) { // replyTo != 0 from ReplyKeyboardMarkup, == 0 from cmd links
if (_peer != peer.get()) {
return; return;
} else if (showSlowmodeError()) { } else if (showSlowmodeError()) {
return; 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. // 'bot' may be nullptr in case of sending from FieldAutocomplete.
const auto toSend = (replyTo || !bot) const auto toSend = (request.replyTo/* || !bot*/)
? cmd ? request.command
: HistoryView::WrapBotCommandInChat(_peer, cmd, bot); : Bot::WrapCommandInChat(_peer, request.command, request.context);
auto message = ApiWrap::MessageToSend(_history); auto message = ApiWrap::MessageToSend(_history);
message.textWithTags = { toSend, TextWithTags::Tags() }; message.textWithTags = { toSend, TextWithTags::Tags() };
message.action.replyTo = replyTo message.action.replyTo = request.replyTo
? ((!_peer->isUser()/* && (botStatus == 0 || botStatus == 2)*/) ? ((!_peer->isUser()/* && (botStatus == 0 || botStatus == 2)*/)
? replyTo ? request.replyTo
: replyToId()) : replyToId())
: 0; : 0;
session().api().sendMessage(std::move(message)); session().api().sendMessage(std::move(message));
if (replyTo) { if (request.replyTo) {
if (_replyToId == replyTo) { if (_replyToId == request.replyTo) {
cancelReply(); cancelReply();
saveCloudDraft(); saveCloudDraft();
} }

View file

@ -10,6 +10,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "history/history_drag_area.h" #include "history/history_drag_area.h"
#include "ui/widgets/tooltip.h" #include "ui/widgets/tooltip.h"
#include "mainwidget.h" #include "mainwidget.h"
#include "chat_helpers/bot_command.h"
#include "chat_helpers/field_autocomplete.h" #include "chat_helpers/field_autocomplete.h"
#include "window/section_widget.h" #include "window/section_widget.h"
#include "ui/widgets/input_fields.h" #include "ui/widgets/input_fields.h"
@ -212,11 +213,7 @@ public:
void escape(); void escape();
void sendBotCommand( void sendBotCommand(Bot::SendCommandRequest request);
not_null<PeerData*> peer,
UserData *bot,
const QString &cmd,
MsgId replyTo);
void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo); void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo);
bool insertBotCommand(const QString &cmd); bool insertBotCommand(const QString &cmd);

View file

@ -3029,34 +3029,4 @@ void ConfirmSendNowSelectedItems(not_null<ListWidget*> widget) {
clearSelection); clearSelection);
} }
QString WrapBotCommandInChat(
not_null<PeerData*> 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<PeerData*> peer,
const QString &command,
not_null<UserData*> 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 } // namespace HistoryView

View file

@ -599,13 +599,4 @@ void ConfirmDeleteSelectedItems(not_null<ListWidget*> widget);
void ConfirmForwardSelectedItems(not_null<ListWidget*> widget); void ConfirmForwardSelectedItems(not_null<ListWidget*> widget);
void ConfirmSendNowSelectedItems(not_null<ListWidget*> widget); void ConfirmSendNowSelectedItems(not_null<ListWidget*> widget);
[[nodiscard]] QString WrapBotCommandInChat(
not_null<PeerData*> peer,
const QString &command,
const FullMsgId &context);
[[nodiscard]] QString WrapBotCommandInChat(
not_null<PeerData*> peer,
const QString &command,
not_null<UserData*> bot);
} // namespace HistoryView } // namespace HistoryView

View file

@ -1766,7 +1766,10 @@ bool RepliesWidget::listIsGoodForAroundPosition(
void RepliesWidget::listSendBotCommand( void RepliesWidget::listSendBotCommand(
const QString &command, const QString &command,
const FullMsgId &context) { 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); auto message = ApiWrap::MessageToSend(_history);
message.textWithTags = { text }; message.textWithTags = { text };
message.action.replyTo = replyToId(); message.action.replyTo = replyToId();

View file

@ -1181,7 +1181,10 @@ void ScheduledWidget::listSendBotCommand(
const QString &command, const QString &command,
const FullMsgId &context) { const FullMsgId &context) {
const auto callback = [=](Api::SendOptions options) { 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); auto message = ApiWrap::MessageToSend(_history);
message.textWithTags = { text }; message.textWithTags = { text };
message.action.options = options; message.action.options = options;

View file

@ -827,12 +827,8 @@ crl::time MainWidget::highlightStartTime(not_null<const HistoryItem*> item) cons
return _history->highlightStartTime(item); return _history->highlightStartTime(item);
} }
void MainWidget::sendBotCommand( void MainWidget::sendBotCommand(Bot::SendCommandRequest request) {
not_null<PeerData*> peer, _history->sendBotCommand(request);
UserData *bot,
const QString &cmd,
MsgId replyTo) {
_history->sendBotCommand(peer, bot, cmd, replyTo);
} }
void MainWidget::hideSingleUseKeyboard(PeerData *peer, MsgId replyTo) { void MainWidget::hideSingleUseKeyboard(PeerData *peer, MsgId replyTo) {

View file

@ -9,6 +9,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "base/timer.h" #include "base/timer.h"
#include "base/weak_ptr.h" #include "base/weak_ptr.h"
#include "chat_helpers/bot_command.h"
#include "ui/rp_widget.h" #include "ui/rp_widget.h"
#include "ui/effects/animations.h" #include "ui/effects/animations.h"
#include "media/player/media_player_float.h" #include "media/player/media_player_float.h"
@ -178,11 +179,7 @@ public:
// While HistoryInner is not HistoryView::ListWidget. // While HistoryInner is not HistoryView::ListWidget.
crl::time highlightStartTime(not_null<const HistoryItem*> item) const; crl::time highlightStartTime(not_null<const HistoryItem*> item) const;
void sendBotCommand( void sendBotCommand(Bot::SendCommandRequest request);
not_null<PeerData*> peer,
UserData *bot,
const QString &cmd,
MsgId replyTo);
void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo); void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo);
bool insertBotCommand(const QString &cmd); bool insertBotCommand(const QString &cmd);

View file

@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#pragma once #pragma once
#include "ui/rp_widget.h" #include "ui/rp_widget.h"
#include "chat_helpers/bot_command.h"
#include "dialogs/dialogs_key.h" #include "dialogs/dialogs_key.h"
#include "media/player/media_player_float.h" // FloatSectionDelegate #include "media/player/media_player_float.h" // FloatSectionDelegate
#include "base/object_ptr.h" #include "base/object_ptr.h"