From b073810bab1f6b081ad3308b667cd5d16eb2f709 Mon Sep 17 00:00:00 2001
From: AlexeyZavar <sltkval1@gmail.com>
Date: Fri, 27 Sep 2024 20:36:34 +0300
Subject: [PATCH] feat: ability to disable save deleted messages in bot dialogs

---
 Telegram/SourceFiles/ayu/ayu_settings.cpp     |  6 +++++
 Telegram/SourceFiles/ayu/ayu_settings.h       |  5 ++++
 .../SourceFiles/ayu/data/messages_storage.cpp |  3 ++-
 .../ayu/ui/message_history/history_item.cpp   | 26 +++++++++++-------
 .../ayu/ui/settings/settings_ayu.cpp          | 27 +++++++++++++++++--
 Telegram/SourceFiles/data/data_session.cpp    | 27 ++++++++++++-------
 .../info/profile/info_profile_actions.cpp     |  2 +-
 .../SourceFiles/window/window_peer_menu.cpp   |  4 +--
 8 files changed, 76 insertions(+), 24 deletions(-)

diff --git a/Telegram/SourceFiles/ayu/ayu_settings.cpp b/Telegram/SourceFiles/ayu/ayu_settings.cpp
index ecf8bba0b..ba32e257a 100644
--- a/Telegram/SourceFiles/ayu/ayu_settings.cpp
+++ b/Telegram/SourceFiles/ayu/ayu_settings.cpp
@@ -210,6 +210,8 @@ AyuGramSettings::AyuGramSettings() {
 	saveDeletedMessages = true;
 	saveMessagesHistory = true;
 
+	saveForBots = false;
+
 	// ~ Message filters
 	hideFromBlocked = false;
 
@@ -354,6 +356,10 @@ void AyuGramSettings::set_saveMessagesHistory(bool val) {
 	saveMessagesHistory = val;
 }
 
+void AyuGramSettings::set_saveForBots(bool val) {
+	saveForBots = val;
+}
+
 void AyuGramSettings::set_hideFromBlocked(bool val) {
 	hideFromBlocked = val;
 	hideFromBlockedReactive = val;
diff --git a/Telegram/SourceFiles/ayu/ayu_settings.h b/Telegram/SourceFiles/ayu/ayu_settings.h
index abb6c79ca..793a28491 100644
--- a/Telegram/SourceFiles/ayu/ayu_settings.h
+++ b/Telegram/SourceFiles/ayu/ayu_settings.h
@@ -30,6 +30,8 @@ public:
 	bool saveDeletedMessages;
 	bool saveMessagesHistory;
 
+	bool saveForBots;
+
 	bool hideFromBlocked;
 
 	bool disableAds;
@@ -105,6 +107,8 @@ public:
 	void set_saveDeletedMessages(bool val);
 	void set_saveMessagesHistory(bool val);
 
+	void set_saveForBots(bool val);
+
 	void set_hideFromBlocked(bool val);
 
 	void set_disableAds(bool val);
@@ -178,6 +182,7 @@ NLOHMANN_DEFINE_TYPE_NON_INTRUSIVE_WITH_DEFAULT(
 	sendWithoutSound,
 	saveDeletedMessages,
 	saveMessagesHistory,
+	saveForBots,
 	hideFromBlocked,
 	disableAds,
 	disableStories,
diff --git a/Telegram/SourceFiles/ayu/data/messages_storage.cpp b/Telegram/SourceFiles/ayu/data/messages_storage.cpp
index 48bc04e80..523c89e7b 100644
--- a/Telegram/SourceFiles/ayu/data/messages_storage.cpp
+++ b/Telegram/SourceFiles/ayu/data/messages_storage.cpp
@@ -59,6 +59,7 @@ void map(not_null<HistoryItem*> item, AyuMessageBase &message) {
 	message.fwdFlags = 0;
 	message.fwdFromId = 0;
 	// message.fwdName
+	message.fwdDate = 0;
 	// message.fwdPostAuthor
 	if (const auto msgsigned = item->Get<HistoryMessageSigned>()) {
 		message.postAuthor = msgsigned->author.toStdString();
@@ -67,7 +68,7 @@ void map(not_null<HistoryItem*> item, AyuMessageBase &message) {
 	message.replyMessageId = 0;
 	message.replyPeerId = 0;
 	message.replyTopId = 0;
-	message.replyForumTopic = 0;
+	message.replyForumTopic = false;
 	// message.replySerialized
 	// message.replyMarkupSerialized
 	message.entityCreateDate = base::unixtime::now();
diff --git a/Telegram/SourceFiles/ayu/ui/message_history/history_item.cpp b/Telegram/SourceFiles/ayu/ui/message_history/history_item.cpp
index 90f40be8a..ef74c2a6d 100644
--- a/Telegram/SourceFiles/ayu/ui/message_history/history_item.cpp
+++ b/Telegram/SourceFiles/ayu/ui/message_history/history_item.cpp
@@ -76,9 +76,6 @@ void GenerateItems(
 	if (!from) {
 		from = reinterpret_cast<PeerData*>(history->owner().chatLoaded(message.fromId));
 	}
-	if (!from) {
-		return;
-	}
 	const auto date = message.entityCreateDate;
 	const auto addPart = [&](
 		not_null<HistoryItem*> item,
@@ -88,17 +85,28 @@ void GenerateItems(
 		return callback(OwnedItem(delegate, item), sentDate, realId);
 	};
 
-	const auto fromName = from->name();
-	const auto fromLink = from->createOpenLink();
-	const auto fromLinkText = Ui::Text::Link(fromName, QString());
-
 	const auto makeSimpleTextMessage = [&](TextWithEntities &&text)
 	{
+		base::flags<MessageFlag> flags = MessageFlag::AdminLogEntry;
+		if (from) {
+			flags |= MessageFlag::HasFromId;
+		} else {
+			flags |= MessageFlag::HasPostAuthor;
+		}
+		if (!message.postAuthor.empty()) {
+			flags |= MessageFlag::HasPostAuthor;
+		}
+
 		return history->makeMessage({
 										.id = history->nextNonHistoryEntryId(),
-										.flags = MessageFlag::HasFromId | MessageFlag::AdminLogEntry,
-										.from = from->id,
+										.flags = flags,
+										.from = from ? from->id : 0,
 										.date = date,
+										.postAuthor = !message.postAuthor.empty()
+														  ? QString::fromStdString(message.postAuthor)
+														  : from
+																? QString()
+																: QString("unknown user: %1").arg(message.fromId),
 									},
 									std::move(text),
 									MTP_messageMediaEmpty());
diff --git a/Telegram/SourceFiles/ayu/ui/settings/settings_ayu.cpp b/Telegram/SourceFiles/ayu/ui/settings/settings_ayu.cpp
index 06edd51ae..62e547945 100644
--- a/Telegram/SourceFiles/ayu/ui/settings/settings_ayu.cpp
+++ b/Telegram/SourceFiles/ayu/ui/settings/settings_ayu.cpp
@@ -6,7 +6,6 @@
 // Copyright @Radolyn, 2024
 #include "settings_ayu.h"
 
-
 #include "ayu/ayu_settings.h"
 #include "ayu/ui/boxes/edit_deleted_mark.h"
 #include "ayu/ui/boxes/edit_edited_mark.h"
@@ -508,7 +507,8 @@ void SetupGhostEssentials(not_null<Ui::VerticalLayout*> container) {
 	SetupGhostModeToggle(container);
 
 	auto markReadAfterActionVal = container->lifetime().make_state<rpl::variable<bool>>(settings->markReadAfterAction);
-	auto useScheduledMessagesVal = container->lifetime().make_state<rpl::variable<bool>>(settings->useScheduledMessages);
+	auto useScheduledMessagesVal = container->lifetime().make_state<rpl::variable<
+		bool>>(settings->useScheduledMessages);
 
 	AddButtonWithIcon(
 		container,
@@ -628,6 +628,29 @@ void SetupSpyEssentials(not_null<Ui::VerticalLayout*> container) {
 			AyuSettings::save();
 		},
 		container->lifetime());
+
+	AddSkip(container);
+	AddDivider(container);
+	AddSkip(container);
+
+	AddButtonWithIcon(
+		container,
+		tr::ayu_MessageSavingSaveForBots(),
+		st::settingsButtonNoIcon
+	)->toggleOn(
+		rpl::single(settings->saveForBots)
+	)->toggledValue(
+	) | rpl::filter(
+		[=](bool enabled)
+		{
+			return (enabled != settings->saveForBots);
+		}) | start_with_next(
+		[=](bool enabled)
+		{
+			settings->set_saveForBots(enabled);
+			AyuSettings::save();
+		},
+		container->lifetime());
 }
 
 void SetupMessageFilters(not_null<Ui::VerticalLayout*> container) {
diff --git a/Telegram/SourceFiles/data/data_session.cpp b/Telegram/SourceFiles/data/data_session.cpp
index 899aab1fa..f4fd74ed8 100644
--- a/Telegram/SourceFiles/data/data_session.cpp
+++ b/Telegram/SourceFiles/data/data_session.cpp
@@ -213,6 +213,19 @@ void CheckForSwitchInlineButton(not_null<HistoryItem*> item) {
 			double(std::numeric_limits<int>::max())));
 }
 
+bool NeedSaveMessage(not_null<HistoryItem *> item) {
+	const auto settings = &AyuSettings::getInstance();
+
+	if (!settings->saveDeletedMessages) {
+		return false;
+	}
+
+	if (const auto possiblyBot = item->history()->peer->asUser()) {
+		return !possiblyBot->isBot() || settings->saveForBots && possiblyBot->isBot();
+	}
+	return true;
+}
+
 } // namespace
 
 Session::Session(not_null<Main::Session*> session)
@@ -2549,25 +2562,23 @@ void Session::processMessagesDeleted(
 		return;
 	}
 
-	const auto settings = &AyuSettings::getInstance();
-
 	auto historiesToCheck = base::flat_set<not_null<History*>>();
 	for (const auto &messageId : data) {
 		const auto i = list ? list->find(messageId.v) : Messages::iterator();
 		if (list && i != list->end()) {
 			const auto history = i->second->history();
 
-			if (!settings->saveDeletedMessages) {
+			if (!NeedSaveMessage(i->second)) {
 				i->second->destroy();
 			} else {
 				i->second->setDeleted();
 				AyuMessages::addDeletedMessage(i->second);
 			}
 
-			if (!history->chatListMessageKnown() && !settings->saveDeletedMessages) {
+			if (!history->chatListMessageKnown()) {
 				historiesToCheck.emplace(history);
 			}
-		} else if (affected && !settings->saveDeletedMessages) {
+		} else if (affected) {
 			affected->unknownMessageDeleted(messageId.v);
 		}
 	}
@@ -2577,21 +2588,19 @@ void Session::processMessagesDeleted(
 }
 
 void Session::processNonChannelMessagesDeleted(const QVector<MTPint> &data) {
-	const auto settings = &AyuSettings::getInstance();
-
 	auto historiesToCheck = base::flat_set<not_null<History*>>();
 	for (const auto &messageId : data) {
 		if (const auto item = nonChannelMessage(messageId.v)) {
 			const auto history = item->history();
 
-			if (!settings->saveDeletedMessages) {
+			if (!NeedSaveMessage(item)) {
 				item->destroy();
 			} else {
 				item->setDeleted();
 				AyuMessages::addDeletedMessage(item);
 			}
 
-			if (!history->chatListMessageKnown() && !settings->saveDeletedMessages) {
+			if (!history->chatListMessageKnown()) {
 				historiesToCheck.emplace(history);
 			}
 		}
diff --git a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp
index 728d0f2b2..8e046bfae 100644
--- a/Telegram/SourceFiles/info/profile/info_profile_actions.cpp
+++ b/Telegram/SourceFiles/info/profile/info_profile_actions.cpp
@@ -1359,7 +1359,7 @@ object_ptr<Ui::RpWidget> DetailsFiller::setupInfo() {
 
 			idInfo.text->setClickHandlerFilter([=, peer = _peer](auto &&...)
 			{
-				const auto idText = IDString(peer);
+				const auto idText = IDString(peer->forumTopicFor(topicRootId)->topicRootId());
 				if (!idText.isEmpty()) {
 					QGuiApplication::clipboard()->setText(idText);
 					const auto msg = tr::ayu_IDCopiedToast(tr::now);
diff --git a/Telegram/SourceFiles/window/window_peer_menu.cpp b/Telegram/SourceFiles/window/window_peer_menu.cpp
index cebf4c63d..1d6d1df82 100644
--- a/Telegram/SourceFiles/window/window_peer_menu.cpp
+++ b/Telegram/SourceFiles/window/window_peer_menu.cpp
@@ -1431,7 +1431,7 @@ void Filler::fillHistoryActions() {
 	addExportChat();
 	addTranslate();
 	addReport();
-	AyuUi::AddDeletedMessagesActions(_peer, _topic ? _topic : _thread, _controller, _addAction);
+	AyuUi::AddDeletedMessagesActions(_peer, _thread, _controller, _addAction);
 	addClearHistory();
 	addDeleteChat();
 	addLeaveChat();
@@ -1470,7 +1470,7 @@ void Filler::fillRepliesActions() {
 	addCreatePoll();
 	addToggleTopicClosed();
 	addDeleteTopic();
-	AyuUi::AddDeletedMessagesActions(_peer, _topic ? _topic : _thread, _controller, _addAction);
+	AyuUi::AddDeletedMessagesActions(_peer, _thread, _controller, _addAction);
 }
 
 void Filler::fillScheduledActions() {