From 225c0e0af370e3d88b02906b14295f146ae61512 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Fri, 12 Apr 2024 14:22:57 +0300 Subject: [PATCH] Added initial api support of reactions limit in channels and groups. --- Telegram/SourceFiles/data/data_channel.cpp | 8 +++- Telegram/SourceFiles/data/data_chat.cpp | 7 +++- Telegram/SourceFiles/data/data_peer.cpp | 4 +- Telegram/SourceFiles/data/data_peer.h | 1 + .../SourceFiles/data/data_peer_values.cpp | 42 +++++++++++++++---- 5 files changed, 50 insertions(+), 12 deletions(-) diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp index b088be2d7..d4e1499d0 100644 --- a/Telegram/SourceFiles/data/data_channel.cpp +++ b/Telegram/SourceFiles/data/data_channel.cpp @@ -1198,10 +1198,14 @@ void ApplyChannelUpdate( } channel->setThemeEmoji(qs(update.vtheme_emoticon().value_or_empty())); channel->setTranslationDisabled(update.is_translations_disabled()); + + const auto reactionsLimit = update.vreactions_limit().value_or_empty(); if (const auto allowed = update.vavailable_reactions()) { - channel->setAllowedReactions(Data::Parse(*allowed)); + auto parsed = Data::Parse(*allowed); + parsed.maxCount = reactionsLimit; + channel->setAllowedReactions(std::move(parsed)); } else { - channel->setAllowedReactions({}); + channel->setAllowedReactions({ .maxCount = reactionsLimit }); } channel->owner().stories().apply(channel, update.vstories()); channel->fullUpdated(); diff --git a/Telegram/SourceFiles/data/data_chat.cpp b/Telegram/SourceFiles/data/data_chat.cpp index 1e19a00d1..7f41c04b7 100644 --- a/Telegram/SourceFiles/data/data_chat.cpp +++ b/Telegram/SourceFiles/data/data_chat.cpp @@ -471,10 +471,13 @@ void ApplyChatUpdate(not_null chat, const MTPDchatFull &update) { chat->checkFolder(update.vfolder_id().value_or_empty()); chat->setThemeEmoji(qs(update.vtheme_emoticon().value_or_empty())); chat->setTranslationDisabled(update.is_translations_disabled()); + const auto reactionsLimit = update.vreactions_limit().value_or_empty(); if (const auto allowed = update.vavailable_reactions()) { - chat->setAllowedReactions(Data::Parse(*allowed)); + auto parsed = Data::Parse(*allowed); + parsed.maxCount = reactionsLimit; + chat->setAllowedReactions(std::move(parsed)); } else { - chat->setAllowedReactions({}); + chat->setAllowedReactions({ .maxCount = reactionsLimit }); } chat->fullUpdated(); chat->setAbout(qs(update.vabout())); diff --git a/Telegram/SourceFiles/data/data_peer.cpp b/Telegram/SourceFiles/data/data_peer.cpp index 9b5d6d379..6d5ac28d3 100644 --- a/Telegram/SourceFiles/data/data_peer.cpp +++ b/Telegram/SourceFiles/data/data_peer.cpp @@ -104,7 +104,9 @@ bool operator<( bool operator==( const AllowedReactions &a, const AllowedReactions &b) { - return (a.type == b.type) && (a.some == b.some); + return (a.type == b.type) + && (a.some == b.some) + && (a.maxCount == b.maxCount); } AllowedReactions Parse(const MTPChatReactions &value) { diff --git a/Telegram/SourceFiles/data/data_peer.h b/Telegram/SourceFiles/data/data_peer.h index b7f98e599..563f5a068 100644 --- a/Telegram/SourceFiles/data/data_peer.h +++ b/Telegram/SourceFiles/data/data_peer.h @@ -110,6 +110,7 @@ enum class AllowedReactionsType { struct AllowedReactions { std::vector some; AllowedReactionsType type = AllowedReactionsType::Some; + int maxCount = 0; }; bool operator<(const AllowedReactions &a, const AllowedReactions &b); diff --git a/Telegram/SourceFiles/data/data_peer_values.cpp b/Telegram/SourceFiles/data/data_peer_values.cpp index 1d321f80d..60a1c5f43 100644 --- a/Telegram/SourceFiles/data/data_peer_values.cpp +++ b/Telegram/SourceFiles/data/data_peer_values.cpp @@ -75,6 +75,10 @@ std::optional OnlineTextCommon(LastseenStatus status, TimeId now) { return std::nullopt; } +[[nodiscard]] int UniqueReactionsLimit(not_null config) { + return config->get("reactions_uniq_max", 11); +} + } // namespace inline auto AdminRightsValue(not_null channel) { @@ -562,21 +566,45 @@ const AllowedReactions &PeerAllowedReactions(not_null peer) { }); } -int UniqueReactionsLimit(not_null config) { - return config->get("reactions_uniq_max", 11); -} - int UniqueReactionsLimit(not_null peer) { + if (const auto channel = peer->asChannel()) { + if (const auto limit = channel->allowedReactions().maxCount) { + return limit; + } + } else if (const auto chat = peer->asChat()) { + if (const auto limit = chat->allowedReactions().maxCount) { + return limit; + } + } return UniqueReactionsLimit(&peer->session().appConfig()); } rpl::producer UniqueReactionsLimitValue( not_null peer) { - const auto config = &peer->session().appConfig(); - return config->value( - ) | rpl::map([=] { + auto configValue = peer->session().appConfig().value( + ) | rpl::map([config = &peer->session().appConfig()] { return UniqueReactionsLimit(config); }) | rpl::distinct_until_changed(); + if (const auto channel = peer->asChannel()) { + return rpl::combine( + PeerAllowedReactionsValue(peer), + std::move(configValue) + ) | rpl::map([=](const auto &allowedReactions, int limit) { + return allowedReactions.maxCount + ? allowedReactions.maxCount + : limit; + }); + } else if (const auto chat = peer->asChat()) { + return rpl::combine( + PeerAllowedReactionsValue(peer), + std::move(configValue) + ) | rpl::map([=](const auto &allowedReactions, int limit) { + return allowedReactions.maxCount + ? allowedReactions.maxCount + : limit; + }); + } + return configValue; } } // namespace Data