From 0a1ddddd81da8354256994cea4995b78dc63d8ba Mon Sep 17 00:00:00 2001
From: John Preston <johnprestonmail@gmail.com>
Date: Tue, 13 Aug 2024 08:42:28 +0200
Subject: [PATCH] Allow paid reaction even after limit is reached.

---
 Telegram/SourceFiles/data/data_channel.cpp           | 3 ++-
 Telegram/SourceFiles/data/data_chat.cpp              | 3 ++-
 Telegram/SourceFiles/data/data_message_reactions.cpp | 7 +++++--
 Telegram/SourceFiles/history/history_item.cpp        | 2 +-
 4 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/Telegram/SourceFiles/data/data_channel.cpp b/Telegram/SourceFiles/data/data_channel.cpp
index 42c335fe1..7c2846d5d 100644
--- a/Telegram/SourceFiles/data/data_channel.cpp
+++ b/Telegram/SourceFiles/data/data_channel.cpp
@@ -966,7 +966,8 @@ void ChannelData::setAllowedReactions(Data::AllowedReactions value) {
 	if (_allowedReactions != value) {
 		const auto enabled = [](const Data::AllowedReactions &allowed) {
 			return (allowed.type != Data::AllowedReactionsType::Some)
-				|| !allowed.some.empty();
+				|| !allowed.some.empty()
+				|| allowed.paidEnabled;
 		};
 		const auto was = enabled(_allowedReactions);
 		_allowedReactions = std::move(value);
diff --git a/Telegram/SourceFiles/data/data_chat.cpp b/Telegram/SourceFiles/data/data_chat.cpp
index b5f5bcf66..6f285e805 100644
--- a/Telegram/SourceFiles/data/data_chat.cpp
+++ b/Telegram/SourceFiles/data/data_chat.cpp
@@ -293,7 +293,8 @@ void ChatData::setAllowedReactions(Data::AllowedReactions value) {
 	if (_allowedReactions != value) {
 		const auto enabled = [](const Data::AllowedReactions &allowed) {
 			return (allowed.type != Data::AllowedReactionsType::Some)
-				|| !allowed.some.empty();
+				|| !allowed.some.empty()
+				|| allowed.paidEnabled;
 		};
 		const auto was = enabled(_allowedReactions);
 		_allowedReactions = std::move(value);
diff --git a/Telegram/SourceFiles/data/data_message_reactions.cpp b/Telegram/SourceFiles/data/data_message_reactions.cpp
index bd743376e..1457eaddc 100644
--- a/Telegram/SourceFiles/data/data_message_reactions.cpp
+++ b/Telegram/SourceFiles/data/data_message_reactions.cpp
@@ -177,6 +177,7 @@ PossibleItemReactionsRef LookupPossibleReactions(
 	const auto &myTags = reactions->list(Reactions::Type::MyTags);
 	const auto &tags = reactions->list(Reactions::Type::Tags);
 	const auto &all = item->reactions();
+	const auto &allowed = PeerAllowedReactions(peer);
 	const auto limit = UniqueReactionsLimit(peer);
 	const auto premiumPossible = session->premiumPossible();
 	const auto limited = (all.size() >= limit) && [&] {
@@ -212,7 +213,10 @@ PossibleItemReactionsRef LookupPossibleReactions(
 		result.customAllowed = premiumPossible;
 		result.tags = true;
 	} else if (limited) {
-		result.recent.reserve(all.size());
+		result.recent.reserve((allowed.paidEnabled ? 1 : 0) + all.size());
+		if (allowed.paidEnabled) {
+			result.recent.push_back(reactions->lookupPaid());
+		}
 		add([&](const Reaction &reaction) {
 			return ranges::contains(all, reaction.id, &MessageReaction::id);
 		});
@@ -225,7 +229,6 @@ PossibleItemReactionsRef LookupPossibleReactions(
 			}
 		}
 	} else {
-		const auto &allowed = PeerAllowedReactions(peer);
 		result.recent.reserve((allowed.paidEnabled ? 1 : 0)
 			+ ((allowed.type == AllowedReactionsType::Some)
 				? allowed.some.size()
diff --git a/Telegram/SourceFiles/history/history_item.cpp b/Telegram/SourceFiles/history/history_item.cpp
index f2688e517..4f0b5fa8a 100644
--- a/Telegram/SourceFiles/history/history_item.cpp
+++ b/Telegram/SourceFiles/history/history_item.cpp
@@ -2572,7 +2572,7 @@ void HistoryItem::toggleReaction(
 		_reactions->add(reaction, addToRecent);
 	} else if (ranges::contains(_reactions->chosen(), reaction)) {
 		_reactions->remove(reaction);
-		if (_reactions->empty()) {
+		if (_reactions->empty() && !_reactions->localPaidData()) {
 			_reactions = nullptr;
 			_flags &= ~MessageFlag::CanViewReactions;
 		}