From a8de145e01bf9b3fb8a2632698b8f6a4aef6942f Mon Sep 17 00:00:00 2001
From: 23rd <23rd@vivaldi.net>
Date: Tue, 14 Nov 2023 17:09:39 +0300
Subject: [PATCH] Moved out click handler for sponsored messages to separate
 module.

---
 Telegram/CMakeLists.txt                       |  2 +
 .../history_view_sponsored_click_handler.cpp  | 57 ++++++++++++++++++
 .../history_view_sponsored_click_handler.h    | 17 ++++++
 .../history/view/history_view_view_button.cpp | 60 ++-----------------
 4 files changed, 80 insertions(+), 56 deletions(-)
 create mode 100644 Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.cpp
 create mode 100644 Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.h

diff --git a/Telegram/CMakeLists.txt b/Telegram/CMakeLists.txt
index f32c071f0..1934e0a3f 100644
--- a/Telegram/CMakeLists.txt
+++ b/Telegram/CMakeLists.txt
@@ -792,6 +792,8 @@ PRIVATE
     history/view/history_view_service_message.h
     history/view/history_view_spoiler_click_handler.cpp
     history/view/history_view_spoiler_click_handler.h
+    history/view/history_view_sponsored_click_handler.cpp
+    history/view/history_view_sponsored_click_handler.h
     history/view/history_view_sticker_toast.cpp
     history/view/history_view_sticker_toast.h
     history/view/history_view_transcribe_button.cpp
diff --git a/Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.cpp b/Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.cpp
new file mode 100644
index 000000000..c6df994b1
--- /dev/null
+++ b/Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.cpp
@@ -0,0 +1,57 @@
+/*
+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 "history/view/history_view_sponsored_click_handler.h"
+
+#include "api/api_chat_invite.h"
+#include "core/click_handler_types.h"
+#include "core/file_utilities.h"
+#include "data/data_session.h"
+#include "data/data_sponsored_messages.h"
+#include "main/main_session.h"
+#include "window/window_session_controller.h"
+
+namespace HistoryView {
+
+ClickHandlerPtr SponsoredLink(const QString &externalLink) {
+	if (!externalLink.isEmpty()) {
+		class ClickHandler : public UrlClickHandler {
+		public:
+			using UrlClickHandler::UrlClickHandler;
+
+			QString copyToClipboardContextItemText() const override {
+				return QString();
+			}
+
+		};
+
+		return std::make_shared<ClickHandler>(externalLink, false);
+	} else {
+		return std::make_shared<LambdaClickHandler>([](ClickContext context) {
+			const auto my = context.other.value<ClickHandlerContext>();
+			const auto controller = my.sessionWindow.get();
+			if (!controller) {
+				return;
+			}
+			const auto &data = controller->session().data();
+			const auto details = data.sponsoredMessages().lookupDetails(
+				my.itemId);
+			if (!details.externalLink.isEmpty()) {
+				File::OpenUrl(details.externalLink);
+			} else if (details.hash) {
+				Api::CheckChatInvite(controller, *details.hash);
+			} else if (details.peer) {
+				controller->showPeerHistory(
+					details.peer,
+					Window::SectionShow::Way::Forward,
+					details.msgId);
+			}
+		});
+	}
+}
+
+} // namespace HistoryView
diff --git a/Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.h b/Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.h
new file mode 100644
index 000000000..7849d8e5f
--- /dev/null
+++ b/Telegram/SourceFiles/history/view/history_view_sponsored_click_handler.h
@@ -0,0 +1,17 @@
+/*
+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
+
+class ClickHandler;
+
+namespace HistoryView {
+
+[[nodiscard]] std::shared_ptr<ClickHandler> SponsoredLink(
+	const QString &externalLink);
+
+} // namespace HistoryView
diff --git a/Telegram/SourceFiles/history/view/history_view_view_button.cpp b/Telegram/SourceFiles/history/view/history_view_view_button.cpp
index 3d9142761..90a0152ba 100644
--- a/Telegram/SourceFiles/history/view/history_view_view_button.cpp
+++ b/Telegram/SourceFiles/history/view/history_view_view_button.cpp
@@ -7,29 +7,17 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
 */
 #include "history/view/history_view_view_button.h"
 
-#include "api/api_chat_invite.h"
-#include "core/application.h"
 #include "core/click_handler_types.h"
-#include "core/file_utilities.h"
-#include "data/data_cloud_themes.h"
-#include "data/data_session.h"
-#include "data/data_sponsored_messages.h"
-#include "data/data_user.h"
-#include "data/data_web_page.h"
-#include "history/view/history_view_cursor_state.h"
-#include "history/history_item_components.h"
 #include "history/history.h"
+#include "history/history_item_components.h"
+#include "history/view/history_view_cursor_state.h"
+#include "history/view/history_view_sponsored_click_handler.h"
 #include "lang/lang_keys.h"
-#include "main/main_session.h"
-#include "ui/click_handler.h"
 #include "ui/effects/ripple_animation.h"
 #include "ui/painter.h"
-#include "ui/round_rect.h"
 #include "ui/text/text_utilities.h" // Ui::Text::ToUpper
 #include "window/window_session_controller.h"
 #include "styles/style_chat.h"
-#include "styles/style_widgets.h"
-#include "styles/style_window.h"
 
 namespace HistoryView {
 namespace {
@@ -79,46 +67,6 @@ inline auto SponsoredPhrase(SponsoredType type) {
 	return Ui::Text::Upper(tr::lng_prizes_how_works(tr::now));
 }
 
-[[nodiscard]] ClickHandlerPtr SponsoredLink(
-		not_null<HistoryMessageSponsored*> sponsored) {
-	if (!sponsored->externalLink.isEmpty()) {
-		class ClickHandler : public UrlClickHandler {
-		public:
-			using UrlClickHandler::UrlClickHandler;
-
-			QString copyToClipboardContextItemText() const override {
-				return QString();
-			}
-
-		};
-
-		return std::make_shared<ClickHandler>(
-			sponsored->externalLink,
-			false);
-	} else {
-		return std::make_shared<LambdaClickHandler>([](ClickContext context) {
-			const auto my = context.other.value<ClickHandlerContext>();
-			const auto controller = my.sessionWindow.get();
-			if (!controller) {
-				return;
-			}
-			const auto &data = controller->session().data();
-			const auto details = data.sponsoredMessages().lookupDetails(
-				my.itemId);
-			if (!details.externalLink.isEmpty()) {
-				File::OpenUrl(details.externalLink);
-			} else if (details.hash) {
-				Api::CheckChatInvite(controller, *details.hash);
-			} else if (details.peer) {
-				controller->showPeerHistory(
-					details.peer,
-					Window::SectionShow::Way::Forward,
-					details.msgId);
-			}
-		});
-	}
-}
-
 } // namespace
 
 struct ViewButton::Inner {
@@ -155,7 +103,7 @@ ViewButton::Inner::Inner(
 	uint8 colorIndex,
 	Fn<void()> updateCallback)
 : margins(st::historyViewButtonMargins)
-, link(SponsoredLink(sponsored))
+, link(SponsoredLink(sponsored->externalLink))
 , updateCallback(std::move(updateCallback))
 , colorIndex(colorIndex)
 , externalLink((sponsored->type == SponsoredType::ExternalLink) ? 1 : 0)