Moved out BotCommand struct to separated file.

This commit is contained in:
23rd 2022-05-14 23:50:02 +03:00 committed by John Preston
parent 8347318c90
commit 5f8608ed90
16 changed files with 160 additions and 105 deletions

View File

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

View File

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

View File

@ -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<UserData*>,
not_null<const std::vector<BotCommand>*>> bots;
not_null<const std::vector<Data::BotCommand>*>> 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<UserData*> user,
const BotCommand &command) {
const Data::BotCommand &command) {
return BotCommandRow{
user,
command.command,

View File

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

View File

@ -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<MTPBotCommand> &data);
[[nodiscard]] auto botCommands() const
-> const base::flat_map<UserId, std::vector<BotCommand>> & {
-> const base::flat_map<UserId, std::vector<Data::BotCommand>> & {
return _botCommands;
}
@ -120,7 +124,7 @@ public:
private:
ChatData *_migratedFrom = nullptr;
ChannelLocation _location;
base::flat_map<UserId, std::vector<BotCommand>> _botCommands;
base::flat_map<UserId, std::vector<Data::BotCommand>> _botCommands;
};

View File

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

View File

@ -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<MTPBotCommand> &data);
[[nodiscard]] auto botCommands() const
-> const base::flat_map<UserId, std::vector<BotCommand>> & {
-> const base::flat_map<UserId, std::vector<Data::BotCommand>> & {
return _botCommands;
}
@ -201,7 +205,7 @@ private:
std::unique_ptr<Data::GroupCall> _call;
PeerId _callDefaultJoinAs = 0;
base::flat_map<UserId, std::vector<BotCommand>> _botCommands;
base::flat_map<UserId, std::vector<Data::BotCommand>> _botCommands;
ChannelData *_migratedTo = nullptr;
rpl::lifetime _lifetime;

View File

@ -88,88 +88,6 @@ PeerId FakePeerIdForJustName(const QString &name) {
return peerFromUser(kShift + std::abs(base));
}
bool UpdateBotCommands(
std::vector<BotCommand> &commands,
const MTPVector<MTPBotCommand> *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<UserId, std::vector<BotCommand>> &commands,
UserId botId,
const MTPVector<MTPBotCommand> *data) {
return (!data || data->v.isEmpty())
? commands.remove(botId)
: UpdateBotCommands(commands[botId], data);
}
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
const MTPVector<MTPBotInfo> &data) {
auto result = false;
auto filled = base::flat_set<UserId>();
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<BotInfo*> info,
const MTPBotMenuButton *button) {

View File

@ -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<BotCommand> &commands,
const MTPVector<MTPBotCommand> *data);
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
UserId botId,
const MTPVector<MTPBotCommand> *data);
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
const MTPVector<MTPBotInfo> &data);
bool ApplyBotMenuButton(
not_null<BotInfo*> info,
const MTPBotMenuButton *button);

View File

@ -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<Data::BotCommand> &commands,
const MTPVector<MTPBotCommand> *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<UserId, std::vector<Data::BotCommand>> &commands,
UserId botId,
const MTPVector<MTPBotCommand> *data) {
return (!data || data->v.isEmpty())
? commands.remove(botId)
: UpdateBotCommands(commands[botId], data);
}
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<Data::BotCommand>> &commands,
const MTPVector<MTPBotInfo> &data) {
auto result = false;
auto filled = base::flat_set<UserId>();
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

View File

@ -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<BotCommand> &commands,
const MTPVector<MTPBotCommand> *data);
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
UserId botId,
const MTPVector<MTPBotCommand> *data);
bool UpdateBotCommands(
base::flat_map<UserId, std::vector<BotCommand>> &commands,
const MTPVector<MTPBotInfo> &data);
} // namespace Data

View File

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

View File

@ -92,7 +92,6 @@ class PeerData;
class UserData;
class ChatData;
class ChannelData;
struct BotCommand;
struct BotInfo;
namespace Data {

View File

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

View File

@ -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<BotCommand> commands;
std::vector<Data::BotCommand> commands;
Ui::Text::String text = { int(st::msgMinWidth) }; // description
QString botMenuButtonText;

View File

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