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_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

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,
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 });
}
}

View File

@ -3601,34 +3601,33 @@ void HistoryWidget::mouseReleaseEvent(QMouseEvent *e) {
}
}
void HistoryWidget::sendBotCommand(
not_null<PeerData*> 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();
}

View File

@ -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<PeerData*> peer,
UserData *bot,
const QString &cmd,
MsgId replyTo);
void sendBotCommand(Bot::SendCommandRequest request);
void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo);
bool insertBotCommand(const QString &cmd);

View File

@ -3029,34 +3029,4 @@ void ConfirmSendNowSelectedItems(not_null<ListWidget*> widget) {
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

View File

@ -599,13 +599,4 @@ void ConfirmDeleteSelectedItems(not_null<ListWidget*> widget);
void ConfirmForwardSelectedItems(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

View File

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

View File

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

View File

@ -827,12 +827,8 @@ crl::time MainWidget::highlightStartTime(not_null<const HistoryItem*> item) cons
return _history->highlightStartTime(item);
}
void MainWidget::sendBotCommand(
not_null<PeerData*> 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) {

View File

@ -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<const HistoryItem*> item) const;
void sendBotCommand(
not_null<PeerData*> peer,
UserData *bot,
const QString &cmd,
MsgId replyTo);
void sendBotCommand(Bot::SendCommandRequest request);
void hideSingleUseKeyboard(PeerData *peer, MsgId replyTo);
bool insertBotCommand(const QString &cmd);

View File

@ -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"