diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt index 1dadeea048..bc3c872a1d 100644 --- a/Telegram/CMakeLists.txt +++ b/Telegram/CMakeLists.txt @@ -484,6 +484,8 @@ PRIVATE data/data_msg_id.h data/data_peer.cpp data/data_peer.h + data/data_peer_bot_command.cpp + data/data_peer_bot_command.h data/data_peer_id.cpp data/data_peer_id.h data/data_peer_values.cpp diff --git a/Telegram/SourceFiles/api/api_updates.cpp b/Telegram/SourceFiles/api/api_updates.cpp index ef66b8cddf..f6028f0bf1 100644 --- a/Telegram/SourceFiles/api/api_updates.cpp +++ b/Telegram/SourceFiles/api/api_updates.cpp @@ -20,6 +20,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "mtproto/mtproto_config.h" #include "mtproto/mtproto_dc_options.h" #include "data/notify/data_notify_settings.h" +#include "data/data_peer_bot_command.h" #include "data/stickers/data_stickers.h" #include "data/data_session.h" #include "data/data_user.h" diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index a9f4eb19e4..25e6a45ce9 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -16,6 +16,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_file_origin.h" #include "data/data_session.h" #include "data/stickers/data_stickers.h" +#include "data/data_peer_bot_command.h" #include "menu/menu_send.h" // SendMenu::FillSendMenu #include "chat_helpers/stickers_lottie.h" #include "chat_helpers/message_field.h" // PrepareMentionTag. @@ -480,7 +481,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { bool hasUsername = _filter.indexOf('@') > 0; base::flat_map< not_null, - not_null*>> bots; + not_null*>> bots; int32 cnt = 0; if (_chat) { if (_chat->noParticipantInfo()) { @@ -527,7 +528,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { if (cnt) { const auto make = [&]( not_null user, - const BotCommand &command) { + const Data::BotCommand &command) { return BotCommandRow{ user, command.command, diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index dcea3b92da..8a339eda22 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -18,6 +18,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_histories.h" #include "data/data_group_call.h" #include "data/data_message_reactions.h" +#include "data/data_peer_bot_command.h" #include "main/main_session.h" #include "main/session/send_as_peers.h" #include "base/unixtime.h" diff --git a/Telegram/SourceFiles/data/data_channel.h b/Telegram/SourceFiles/data/data_channel.h index 11b312123c..ed5b57aa4b 100644 --- a/Telegram/SourceFiles/data/data_channel.h +++ b/Telegram/SourceFiles/data/data_channel.h @@ -12,6 +12,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_location.h" #include "data/data_chat_participant_status.h" +namespace Data { +struct BotCommand; +} // namespace Data + struct ChannelLocation { QString address; Data::LocationPoint point; @@ -90,7 +94,7 @@ public: UserId botId, const MTPVector &data); [[nodiscard]] auto botCommands() const - -> const base::flat_map> & { + -> const base::flat_map> & { return _botCommands; } @@ -120,7 +124,7 @@ public: private: ChatData *_migratedFrom = nullptr; ChannelLocation _location; - base::flat_map> _botCommands; + base::flat_map> _botCommands; }; diff --git a/Telegram/SourceFiles/data/data_chat.cpp b/Telegram/SourceFiles/data/data_chat.cpp index 044231db88..2e5bb7e4c9 100644 --- a/Telegram/SourceFiles/data/data_chat.cpp +++ b/Telegram/SourceFiles/data/data_chat.cpp @@ -13,6 +13,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_changes.h" #include "data/data_group_call.h" #include "data/data_message_reactions.h" +#include "data/data_peer_bot_command.h" #include "history/history.h" #include "main/main_session.h" #include "apiwrap.h" diff --git a/Telegram/SourceFiles/data/data_chat.h b/Telegram/SourceFiles/data/data_chat.h index 66df6e0a7e..402daf1b0b 100644 --- a/Telegram/SourceFiles/data/data_chat.h +++ b/Telegram/SourceFiles/data/data_chat.h @@ -10,6 +10,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_peer.h" #include "data/data_chat_participant_status.h" +namespace Data { +struct BotCommand; +} // namespace Data + enum class ChatAdminRight; enum class ChatDataFlag { @@ -153,7 +157,7 @@ public: UserId botId, const MTPVector &data); [[nodiscard]] auto botCommands() const - -> const base::flat_map> & { + -> const base::flat_map> & { return _botCommands; } @@ -201,7 +205,7 @@ private: std::unique_ptr _call; PeerId _callDefaultJoinAs = 0; - base::flat_map> _botCommands; + base::flat_map> _botCommands; ChannelData *_migratedTo = nullptr; rpl::lifetime _lifetime; diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 8c9f045d90..01fe66a84b 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -88,88 +88,6 @@ PeerId FakePeerIdForJustName(const QString &name) { return peerFromUser(kShift + std::abs(base)); } -bool UpdateBotCommands( - std::vector &commands, - const MTPVector *data) { - if (!data) { - const auto changed = !commands.empty(); - commands.clear(); - return changed; - } - const auto &v = data->v; - commands.reserve(v.size()); - auto result = false; - auto index = 0; - for (const auto &command : v) { - command.match([&](const MTPDbotCommand &data) { - const auto command = qs(data.vcommand()); - const auto description = qs(data.vdescription()); - if (commands.size() <= index) { - commands.push_back({ - .command = command, - .description = description, - }); - result = true; - } else { - auto &entry = commands[index]; - if (entry.command != command - || entry.description != description) { - entry.command = command; - entry.description = description; - result = true; - } - } - ++index; - }); - } - if (index < commands.size()) { - result = true; - } - commands.resize(index); - return result; -} - -bool UpdateBotCommands( - base::flat_map> &commands, - UserId botId, - const MTPVector *data) { - return (!data || data->v.isEmpty()) - ? commands.remove(botId) - : UpdateBotCommands(commands[botId], data); -} - -bool UpdateBotCommands( - base::flat_map> &commands, - const MTPVector &data) { - auto result = false; - auto filled = base::flat_set(); - filled.reserve(data.v.size()); - for (const auto &item : data.v) { - item.match([&](const MTPDbotInfo &data) { - if (!data.vuser_id()) { - LOG(("API Error: BotInfo without UserId for commands map.")); - return; - } - const auto id = UserId(*data.vuser_id()); - if (!filled.emplace(id).second) { - LOG(("API Error: Two BotInfo for a single bot.")); - return; - } else if (UpdateBotCommands(commands, id, data.vcommands())) { - result = true; - } - }); - } - for (auto i = begin(commands); i != end(commands);) { - if (filled.contains(i->first)) { - ++i; - } else { - i = commands.erase(i); - result = true; - } - } - return result; -} - bool ApplyBotMenuButton( not_null info, const MTPBotMenuButton *button) { diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index f6dbff8e40..9725b8ad0c 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -20,11 +20,6 @@ class ChannelData; enum class ChatRestriction; -struct BotCommand { - QString command; - QString description; -}; - namespace Ui { class EmptyUserpic; } // namespace Ui @@ -100,16 +95,6 @@ struct UnavailableReason { } }; -bool UpdateBotCommands( - std::vector &commands, - const MTPVector *data); -bool UpdateBotCommands( - base::flat_map> &commands, - UserId botId, - const MTPVector *data); -bool UpdateBotCommands( - base::flat_map> &commands, - const MTPVector &data); bool ApplyBotMenuButton( not_null info, const MTPBotMenuButton *button); diff --git a/Telegram/SourceFiles/data/data_peer_bot_command.cpp b/Telegram/SourceFiles/data/data_peer_bot_command.cpp new file mode 100644 index 0000000000..6d1b60bb79 --- /dev/null +++ b/Telegram/SourceFiles/data/data_peer_bot_command.cpp @@ -0,0 +1,96 @@ +/* +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 "data/data_peer_bot_command.h" + +#include "data/data_user.h" + +namespace Data { + +bool UpdateBotCommands( + std::vector &commands, + const MTPVector *data) { + if (!data) { + const auto changed = !commands.empty(); + commands.clear(); + return changed; + } + const auto &v = data->v; + commands.reserve(v.size()); + auto result = false; + auto index = 0; + for (const auto &command : v) { + command.match([&](const MTPDbotCommand &data) { + const auto command = qs(data.vcommand()); + const auto description = qs(data.vdescription()); + if (commands.size() <= index) { + commands.push_back({ + .command = command, + .description = description, + }); + result = true; + } else { + auto &entry = commands[index]; + if (entry.command != command + || entry.description != description) { + entry.command = command; + entry.description = description; + result = true; + } + } + ++index; + }); + } + if (index < commands.size()) { + result = true; + } + commands.resize(index); + return result; +} + +bool UpdateBotCommands( + base::flat_map> &commands, + UserId botId, + const MTPVector *data) { + return (!data || data->v.isEmpty()) + ? commands.remove(botId) + : UpdateBotCommands(commands[botId], data); +} + +bool UpdateBotCommands( + base::flat_map> &commands, + const MTPVector &data) { + auto result = false; + auto filled = base::flat_set(); + filled.reserve(data.v.size()); + for (const auto &item : data.v) { + item.match([&](const MTPDbotInfo &data) { + if (!data.vuser_id()) { + LOG(("API Error: BotInfo without UserId for commands map.")); + return; + } + const auto id = UserId(*data.vuser_id()); + if (!filled.emplace(id).second) { + LOG(("API Error: Two BotInfo for a single bot.")); + return; + } else if (UpdateBotCommands(commands, id, data.vcommands())) { + result = true; + } + }); + } + for (auto i = begin(commands); i != end(commands);) { + if (filled.contains(i->first)) { + ++i; + } else { + i = commands.erase(i); + result = true; + } + } + return result; +} + +} // namespace Data diff --git a/Telegram/SourceFiles/data/data_peer_bot_command.h b/Telegram/SourceFiles/data/data_peer_bot_command.h new file mode 100644 index 0000000000..2aacef5c48 --- /dev/null +++ b/Telegram/SourceFiles/data/data_peer_bot_command.h @@ -0,0 +1,36 @@ +/* +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 + +namespace Data { + +struct BotCommand final { + QString command; + QString description; + + inline bool operator==(const BotCommand &other) const { + return (command == other.command) + && (description == other.description); + } + inline bool operator!=(const BotCommand &other) const { + return !(*this == other); + } +}; + +bool UpdateBotCommands( + std::vector &commands, + const MTPVector *data); +bool UpdateBotCommands( + base::flat_map> &commands, + UserId botId, + const MTPVector *data); +bool UpdateBotCommands( + base::flat_map> &commands, + const MTPVector &data); + +} // namespace Data diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp index 37378c882f..d3b1892acd 100644 --- a/Telegram/SourceFiles/data/data_session.cpp +++ b/Telegram/SourceFiles/data/data_session.cpp @@ -47,6 +47,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_channel.h" #include "data/data_chat.h" #include "data/data_user.h" +#include "data/data_peer_bot_command.h" #include "data/data_file_origin.h" #include "data/data_download_manager.h" #include "data/data_photo.h" diff --git a/Telegram/SourceFiles/data/data_types.h b/Telegram/SourceFiles/data/data_types.h index 6071a142b8..f8d869848c 100644 --- a/Telegram/SourceFiles/data/data_types.h +++ b/Telegram/SourceFiles/data/data_types.h @@ -92,7 +92,6 @@ class PeerData; class UserData; class ChatData; class ChannelData; -struct BotCommand; struct BotInfo; namespace Data { diff --git a/Telegram/SourceFiles/data/data_user.cpp b/Telegram/SourceFiles/data/data_user.cpp index 7dfe6a038e..0521a1c936 100644 --- a/Telegram/SourceFiles/data/data_user.cpp +++ b/Telegram/SourceFiles/data/data_user.cpp @@ -11,6 +11,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "main/main_session.h" #include "data/data_session.h" #include "data/data_changes.h" +#include "data/data_peer_bot_command.h" #include "ui/text/text_options.h" #include "apiwrap.h" #include "lang/lang_keys.h" diff --git a/Telegram/SourceFiles/data/data_user.h b/Telegram/SourceFiles/data/data_user.h index a16da999a1..1528f4b087 100644 --- a/Telegram/SourceFiles/data/data_user.h +++ b/Telegram/SourceFiles/data/data_user.h @@ -11,6 +11,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_chat_participant_status.h" #include "dialogs/dialogs_key.h" +namespace Data { +struct BotCommand; +} // namespace Data + struct BotInfo { bool inited = false; bool readsAllHistory = false; @@ -18,7 +22,7 @@ struct BotInfo { bool supportsAttachMenu = false; int version = 0; QString description, inlinePlaceholder; - std::vector commands; + std::vector commands; Ui::Text::String text = { int(st::msgMinWidth) }; // description QString botMenuButtonText; diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp index 931b3da0e7..0e37fdcc99 100644 --- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp +++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp @@ -14,6 +14,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "data/data_changes.h" #include "data/data_user.h" #include "data/notify/data_notify_settings.h" +#include "data/data_peer_bot_command.h" #include "ui/wrap/vertical_layout.h" #include "ui/wrap/padding_wrap.h" #include "ui/wrap/slide_wrap.h"