diff --git a/Telegram/SourceFiles/api/api_chat_participants.cpp b/Telegram/SourceFiles/api/api_chat_participants.cpp index 8900ca8dba..9e09257bbe 100644 --- a/Telegram/SourceFiles/api/api_chat_participants.cpp +++ b/Telegram/SourceFiles/api/api_chat_participants.cpp @@ -151,9 +151,8 @@ void ApplyLastList( } if (user->isBot()) { channel->mgInfo->bots.insert(user); - if ((channel->mgInfo->botStatus != 0) - && (channel->mgInfo->botStatus < 2)) { - channel->mgInfo->botStatus = 2; + if (channel->mgInfo->botStatus == Data::BotStatus::NoBots) { + channel->mgInfo->botStatus = Data::BotStatus::HasBots; } } if (!p.rank().isEmpty()) { @@ -189,7 +188,7 @@ void ApplyBotsList( Members list) { const auto history = channel->owner().historyLoaded(channel); channel->mgInfo->bots.clear(); - channel->mgInfo->botStatus = -1; + channel->mgInfo->botStatus = Data::BotStatus::NoBots; auto needBotsInfos = false; auto botStatus = channel->mgInfo->botStatus; @@ -199,7 +198,7 @@ void ApplyBotsList( const auto user = participant->asUser(); if (user && user->isBot()) { channel->mgInfo->bots.insert(user); - botStatus = 2;// (botStatus > 0/* || !i.key()->botInfo->readsAllHistory*/) ? 2 : 1; + botStatus = Data::BotStatus::HasBots; if (!user->botInfo->inited) { needBotsInfos = true; } @@ -516,7 +515,7 @@ void ChatParticipants::requestBots(not_null channel) { _botsRequests.remove(channel); if (error.type() == u"CHANNEL_MONOFORUM_UNSUPPORTED"_q) { channel->mgInfo->bots.clear(); - channel->mgInfo->botStatus = -1; + channel->mgInfo->botStatus = Data::BotStatus::NoBots; channel->session().changes().peerUpdated( channel, Data::PeerUpdate::Flag::FullInfo); diff --git a/Telegram/SourceFiles/chat_helpers/bot_command.cpp b/Telegram/SourceFiles/chat_helpers/bot_command.cpp index e827426020..7771bb3052 100644 --- a/Telegram/SourceFiles/chat_helpers/bot_command.cpp +++ b/Telegram/SourceFiles/chat_helpers/bot_command.cpp @@ -40,8 +40,8 @@ QString WrapCommandInChat( ? peer->asChat()->botStatus : peer->isMegagroup() ? peer->asChannel()->mgInfo->botStatus - : -1; - return ((command.indexOf('@') < 2) && (botStatus == 0 || botStatus == 2)) + : Data::BotStatus::NoBots; + return ((command.indexOf('@') < 2) && (botStatus != Data::BotStatus::NoBots)) ? command + '@' + bot->username() : command; } diff --git a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp index 6e29eb20fc..0dcef40550 100644 --- a/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp +++ b/Telegram/SourceFiles/chat_helpers/field_autocomplete.cpp @@ -584,7 +584,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { bots.emplace(_user, &_user->botInfo->commands); } else if (_channel && _channel->isMegagroup()) { if (_channel->mgInfo->bots.empty()) { - if (!_channel->mgInfo->botStatus) { + if (_channel->mgInfo->botStatus == Data::BotStatus::Unknown) { _channel->session().api().chatParticipants().requestBots( _channel); } @@ -614,7 +614,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { }; }; brows.reserve(cnt); - int32 botStatus = _chat ? _chat->botStatus : ((_channel && _channel->isMegagroup()) ? _channel->mgInfo->botStatus : -1); + const auto botStatus = _chat ? _chat->botStatus : ((_channel && _channel->isMegagroup()) ? _channel->mgInfo->botStatus : Data::BotStatus::NoBots); if (_chat) { for (const auto &user : _chat->lastAuthors) { if (!user->isBot()) { @@ -626,7 +626,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { } for (const auto &command : *i->second) { if (!listAllSuggestions) { - auto toFilter = (hasUsername || botStatus == 0 || botStatus == 2) + auto toFilter = (hasUsername || botStatus != Data::BotStatus::NoBots) ? command.command + '@' + PrimaryUsername(user) : command.command; if (!toFilter.startsWith(_filter, Qt::CaseInsensitive)/* || toFilter.size() == _filter.size()*/) { @@ -644,8 +644,7 @@ void FieldAutocomplete::updateFiltered(bool resetScroll) { for (const auto &command : *i->second) { if (!listAllSuggestions) { const auto toFilter = (hasUsername - || botStatus == 0 - || botStatus == 2) + || botStatus != Data::BotStatus::NoBots) ? command.command + '@' + PrimaryUsername(user) : command.command; if (!toFilter.startsWith(_filter, Qt::CaseInsensitive)/* || toFilter.size() == _filter.size()*/) continue; @@ -1130,8 +1129,8 @@ void FieldAutocomplete::Inner::paintEvent(QPaintEvent *e) { } auto toHighlight = row.command; - int32 botStatus = _parent->chat() ? _parent->chat()->botStatus : ((_parent->channel() && _parent->channel()->isMegagroup()) ? _parent->channel()->mgInfo->botStatus : -1); - if (hasUsername || botStatus == 0 || botStatus == 2) { + const auto botStatus = _parent->chat() ? _parent->chat()->botStatus : ((_parent->channel() && _parent->channel()->isMegagroup()) ? _parent->channel()->mgInfo->botStatus : Data::BotStatus::NoBots); + if (hasUsername || botStatus != Data::BotStatus::NoBots) { toHighlight += '@' + PrimaryUsername(user); } user->loadUserpic(); @@ -1299,10 +1298,9 @@ bool FieldAutocomplete::Inner::chooseAtIndex( ? _parent->chat()->botStatus : ((_parent->channel() && _parent->channel()->isMegagroup()) ? _parent->channel()->mgInfo->botStatus - : -1); + : Data::BotStatus::NoBots); - const auto insertUsername = (botStatus == 0 - || botStatus == 2 + const auto insertUsername = (botStatus != Data::BotStatus::NoBots || _parent->filter().indexOf('@') > 0); const auto commandString = QString("/%1%2").arg( command, diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index 203495d52d..4aee47b812 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -458,8 +458,8 @@ void ChannelData::applyEditAdmin( setMembersCount(membersCount() + 1); if (user->isBot() && !mgInfo->bots.contains(user)) { mgInfo->bots.insert(user); - if (mgInfo->botStatus != 0 && mgInfo->botStatus < 2) { - mgInfo->botStatus = 2; + if (mgInfo->botStatus == Data::BotStatus::NoBots) { + mgInfo->botStatus = Data::BotStatus::HasBots; } } } @@ -576,8 +576,8 @@ void ChannelData::applyEditBanned( setKickedCount(kickedCount() + 1); if (mgInfo->bots.contains(user)) { mgInfo->bots.remove(user); - if (mgInfo->bots.empty() && mgInfo->botStatus > 0) { - mgInfo->botStatus = -1; + if (mgInfo->bots.empty() && mgInfo->botStatus == Data::BotStatus::HasBots) { + mgInfo->botStatus = Data::BotStatus::NoBots; } } } diff --git a/Telegram/SourceFiles/data/data_channel.h b/Telegram/SourceFiles/data/data_channel.h index 38078150a6..0325eacb98 100644 --- a/Telegram/SourceFiles/data/data_channel.h +++ b/Telegram/SourceFiles/data/data_channel.h @@ -145,7 +145,7 @@ public: base::flat_map memberRanks; UserData *creator = nullptr; // nullptr means unknown - int botStatus = 0; // -1 - no bots, 0 - unknown, 1 - one bot, that sees all history, 2 - other + Data::BotStatus botStatus = Data::BotStatus::Unknown; bool joinedMessageFound = false; bool adminsLoaded = false; StickerSetIdentifier stickerSet; diff --git a/Telegram/SourceFiles/data/data_chat.cpp b/Telegram/SourceFiles/data/data_chat.cpp index cd24f785d8..ec5afcd19d 100644 --- a/Telegram/SourceFiles/data/data_chat.cpp +++ b/Telegram/SourceFiles/data/data_chat.cpp @@ -126,7 +126,7 @@ void ChatData::invalidateParticipants() { setAdminRights(ChatAdminRights()); //setDefaultRestrictions(ChatRestrictions()); invitedByMe.clear(); - botStatus = 0; + botStatus = Data::BotStatus::Unknown; session().changes().peerUpdated( this, UpdateFlag::Members | UpdateFlag::Admins); @@ -176,10 +176,14 @@ void ChatData::setDefaultRestrictions(ChatRestrictions rights) { void ChatData::refreshBotStatus() { if (participants.empty()) { - botStatus = 0; + botStatus = Data::BotStatus::Unknown; } else { - const auto bot = ranges::none_of(participants, &UserData::isBot); - botStatus = bot ? -1 : 2; + const auto noBots = ranges::none_of( + participants, + &UserData::isBot); + botStatus = noBots + ? Data::BotStatus::NoBots + : Data::BotStatus::HasBots; } } @@ -346,7 +350,7 @@ void ApplyChatUpdate( if (chat->count > 0) { // If the count is known. ++chat->count; } - chat->botStatus = 0; + chat->botStatus = Data::BotStatus::Unknown; } else { chat->participants.emplace(user); if (UserId(update.vinviter_id()) == session->userId()) { @@ -356,7 +360,7 @@ void ApplyChatUpdate( } ++chat->count; if (user->isBot()) { - chat->botStatus = 2; + chat->botStatus = Data::BotStatus::HasBots; if (!user->botInfo->inited) { session->api().requestFullPeer(user); } @@ -386,7 +390,7 @@ void ApplyChatUpdate( if (chat->count > 0) { chat->count--; } - chat->botStatus = 0; + chat->botStatus = Data::BotStatus::Unknown; } else { chat->participants.erase(user); chat->count--; @@ -400,7 +404,7 @@ void ApplyChatUpdate( history->clearLastKeyboard(); } } - if (chat->botStatus > 0 && user->isBot()) { + if (chat->botStatus == Data::BotStatus::HasBots && user->isBot()) { chat->refreshBotStatus(); } } diff --git a/Telegram/SourceFiles/data/data_chat.h b/Telegram/SourceFiles/data/data_chat.h index 459a29a260..da67c63e2f 100644 --- a/Telegram/SourceFiles/data/data_chat.h +++ b/Telegram/SourceFiles/data/data_chat.h @@ -179,7 +179,7 @@ public: std::deque> lastAuthors; base::flat_set> markupSenders; base::flat_map memberRanks; - int botStatus = 0; // -1 - no bots, 0 - unknown, 1 - one bot, that sees all history, 2 - other + Data::BotStatus botStatus = Data::BotStatus::Unknown; private: Flags _flags; diff --git a/Telegram/SourceFiles/data/data_peer_bot_commands.h b/Telegram/SourceFiles/data/data_peer_bot_commands.h index e0730b4e43..bf46e49635 100644 --- a/Telegram/SourceFiles/data/data_peer_bot_commands.h +++ b/Telegram/SourceFiles/data/data_peer_bot_commands.h @@ -11,6 +11,12 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL namespace Data { +enum class BotStatus : int { + NoBots = -1, + Unknown = 0, + HasBots = 2, +}; + struct BotCommands final { UserId userId; std::vector commands; diff --git a/Telegram/SourceFiles/history/history.cpp b/Telegram/SourceFiles/history/history.cpp index 625d67f501..7d5e2500cb 100644 --- a/Telegram/SourceFiles/history/history.cpp +++ b/Telegram/SourceFiles/history/history.cpp @@ -1040,8 +1040,8 @@ not_null History::addNewToBack( auto mgInfo = megagroup->mgInfo.get(); Assert(mgInfo != nullptr); mgInfo->bots.insert(user); - if (mgInfo->botStatus != 0 && mgInfo->botStatus < 2) { - mgInfo->botStatus = 2; + if (mgInfo->botStatus == Data::BotStatus::NoBots) { + mgInfo->botStatus = Data::BotStatus::HasBots; } } } @@ -1103,7 +1103,7 @@ not_null History::addNewToBack( item->from()->asUser()); } else if (peer->isMegagroup()) { botNotInChat = item->from()->isUser() - && (peer->asChannel()->mgInfo->botStatus != 0 + && (peer->asChannel()->mgInfo->botStatus != Data::BotStatus::Unknown || !Data::CanSendAnything(peer)) && !peer->asChannel()->mgInfo->bots.contains( item->from()->asUser()); @@ -1160,8 +1160,8 @@ void History::applyServiceChanges( } if (user->isBot()) { mgInfo->bots.insert(user); - if (mgInfo->botStatus != 0 && mgInfo->botStatus < 2) { - mgInfo->botStatus = 2; + if (mgInfo->botStatus == Data::BotStatus::NoBots) { + mgInfo->botStatus = Data::BotStatus::HasBots; } } }; @@ -1229,8 +1229,8 @@ void History::applyServiceChanges( Data::PeerUpdate::Flag::Admins); } mgInfo->bots.remove(user); - if (mgInfo->bots.empty() && mgInfo->botStatus > 0) { - mgInfo->botStatus = -1; + if (mgInfo->bots.empty() && mgInfo->botStatus == Data::BotStatus::HasBots) { + mgInfo->botStatus = Data::BotStatus::NoBots; } } Data::ChannelAdminChanges(megagroup).remove(uid); @@ -1813,7 +1813,7 @@ void History::addItemsToLists( && !peer->asChat()->participants.contains(item->author()->asUser()); } else if (peer->isMegagroup()) { botNotInChat = (!Data::CanSendAnything(peer) - || peer->asChannel()->mgInfo->botStatus != 0) + || peer->asChannel()->mgInfo->botStatus != Data::BotStatus::Unknown) && item->author()->isUser() && !peer->asChannel()->mgInfo->bots.contains(item->author()->asUser()); } diff --git a/Telegram/SourceFiles/history/history_widget.cpp b/Telegram/SourceFiles/history/history_widget.cpp index abb8025a0d..ba8f2f355a 100644 --- a/Telegram/SourceFiles/history/history_widget.cpp +++ b/Telegram/SourceFiles/history/history_widget.cpp @@ -5629,10 +5629,10 @@ bool HistoryWidget::insertBotCommand(const QString &cmd) { ? _peer->asChat()->botStatus : _peer->isMegagroup() ? _peer->asChannel()->mgInfo->botStatus - : -1; + : Data::BotStatus::NoBots; if ((toInsert.indexOf('@') < 0) && !username.isEmpty() - && (botStatus == 0 || botStatus == 2)) { + && (botStatus != Data::BotStatus::NoBots)) { toInsert += '@' + username; } } @@ -5844,9 +5844,9 @@ bool HistoryWidget::updateCmdStartShown() { if (_history && _peer && (false - || (_peer->isChat() && _peer->asChat()->botStatus > 0) + || (_peer->isChat() && _peer->asChat()->botStatus == Data::BotStatus::HasBots) || (_peer->isMegagroup() - && _peer->asChannel()->mgInfo->botStatus > 0))) { + && _peer->asChannel()->mgInfo->botStatus == Data::BotStatus::HasBots))) { if (!isBotStart() && !isBlocked() && !_keyboard->hasMarkup() @@ -9393,7 +9393,7 @@ void HistoryWidget::handlePeerUpdate() { == UserData::CallsStatus::Unknown))) { session().api().requestFullPeer(_peer); } else if (auto channel = _peer->asMegagroup()) { - if (!channel->mgInfo->botStatus) { + if (channel->mgInfo->botStatus == Data::BotStatus::Unknown) { session().api().chatParticipants().requestBots(channel); } if (!channel->mgInfo->adminsLoaded) { diff --git a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp index 9a8d235674..e81e6b12bd 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_compose_controls.cpp @@ -1184,7 +1184,7 @@ void ComposeControls::setHistory(SetHistoryArgs &&args) { if (peer->isChat() && peer->asChat()->noParticipantInfo()) { session().api().requestFullPeer(peer); } else if (const auto channel = peer->asMegagroup()) { - if (!channel->mgInfo->botStatus) { + if (channel->mgInfo->botStatus == Data::BotStatus::Unknown) { session().api().chatParticipants().requestBots(channel); } } else if (hasSilentBroadcastToggle()) { @@ -3698,8 +3698,8 @@ bool ComposeControls::updateBotCommandShown() { const auto peer = _history ? _history->peer.get() : nullptr; if (_botCommandStart && peer - && ((peer->isChat() && peer->asChat()->botStatus > 0) - || (peer->isMegagroup() && peer->asChannel()->mgInfo->botStatus > 0) + && ((peer->isChat() && peer->asChat()->botStatus == Data::BotStatus::HasBots) + || (peer->isMegagroup() && peer->asChannel()->mgInfo->botStatus == Data::BotStatus::HasBots) || (peer->isUser() && peer->asUser()->isBot()))) { if (!HasSendText(_field)) { shown = true; diff --git a/Telegram/SourceFiles/ui/item_text_options.cpp b/Telegram/SourceFiles/ui/item_text_options.cpp index 57ccc44c5f..eec63d2be0 100644 --- a/Telegram/SourceFiles/ui/item_text_options.cpp +++ b/Telegram/SourceFiles/ui/item_text_options.cpp @@ -24,11 +24,11 @@ bool UseBotTextOptions( return true; } } else if (const auto chat = history->peer->asChat()) { - if (chat->botStatus >= 0) { + if (chat->botStatus != Data::BotStatus::NoBots) { return true; } } else if (const auto group = history->peer->asMegagroup()) { - if (group->mgInfo->botStatus >= 0) { + if (group->mgInfo->botStatus != Data::BotStatus::NoBots) { return true; } }