diff --git a/Telegram/Resources/langs/lang.strings b/Telegram/Resources/langs/lang.strings index 900448b117..d2af09b841 100644 --- a/Telegram/Resources/langs/lang.strings +++ b/Telegram/Resources/langs/lang.strings @@ -7848,6 +7848,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL "ayu_GhostModeShortcut" = "Enter with Ghost"; "ayu_SettingsShowMessageSeconds" = "Show Message Seconds"; "ayu_FilterZalgo" = "Filter Zalgo"; +"ayu_ImproveLinkPreviews" = "Improve Link Previews"; "ayu_ShowOnlyAddedEmojisAndStickers" = "Show Only Added Emojis & Stickers"; "ayu_ContextCopyID" = "Copy ID"; "ayu_IDCopiedToast" = "ID copied to clipboard."; diff --git a/Telegram/SourceFiles/apiwrap.cpp b/Telegram/SourceFiles/apiwrap.cpp index 3d4961c8f9..821c37273f 100644 --- a/Telegram/SourceFiles/apiwrap.cpp +++ b/Telegram/SourceFiles/apiwrap.cpp @@ -4225,7 +4225,7 @@ void ApiWrap::sendMessage( action.options.shortcutId); if (exactWebPage && !ignoreWebPage - && (manualWebPage || sending.empty())) { + && (manualWebPage || sending.empty() || message.webPage.previewChanged)) { histories.sendPreparedMessage( history, action.replyTo, diff --git a/Telegram/SourceFiles/ayu/ayu_settings.cpp b/Telegram/SourceFiles/ayu/ayu_settings.cpp index 35609bbb1d..9d102baad2 100644 --- a/Telegram/SourceFiles/ayu/ayu_settings.cpp +++ b/Telegram/SourceFiles/ayu/ayu_settings.cpp @@ -792,6 +792,12 @@ void AyuSettings::setAdaptiveCoverColor(bool val) { save(); } +void AyuSettings::setImproveLinkPreviews(bool val) { + if (_improveLinkPreviews.current() == val) return; + _improveLinkPreviews = val; + save(); +} + void AyuSettings::setCrashReporting(bool val) { if (_crashReporting.current() == val) return; _crashReporting = val; @@ -881,6 +887,7 @@ void to_json(nlohmann::json &j, const AyuSettings &s) { {"voiceConfirmation", s._voiceConfirmation.current()}, {"translationProvider", s._translationProvider.current()}, {"adaptiveCoverColor", s._adaptiveCoverColor.current()}, + {"improveLinkPreviews", s._improveLinkPreviews.current()}, {"crashReporting", s._crashReporting.current()} }; } @@ -972,5 +979,6 @@ void from_json(const nlohmann::json &j, AyuSettings &s) { s._voiceConfirmation = j.value("voiceConfirmation", defaults._voiceConfirmation.current()); s._translationProvider = j.value("translationProvider", defaults._translationProvider.current()); s._adaptiveCoverColor = j.value("adaptiveCoverColor", defaults._adaptiveCoverColor.current()); + s._improveLinkPreviews = j.value("improveLinkPreviews", defaults._improveLinkPreviews.current()); s._crashReporting = j.value("crashReporting", defaults._crashReporting.current()); } diff --git a/Telegram/SourceFiles/ayu/ayu_settings.h b/Telegram/SourceFiles/ayu/ayu_settings.h index 0fce288e54..483717ef50 100644 --- a/Telegram/SourceFiles/ayu/ayu_settings.h +++ b/Telegram/SourceFiles/ayu/ayu_settings.h @@ -234,6 +234,7 @@ public: [[nodiscard]] bool voiceConfirmation() const { return _voiceConfirmation.current(); } [[nodiscard]] const QString &translationProvider() const { return _translationProvider.current(); } [[nodiscard]] bool adaptiveCoverColor() const { return _adaptiveCoverColor.current(); } + [[nodiscard]] bool improveLinkPreviews() const { return _improveLinkPreviews.current(); } [[nodiscard]] bool crashReporting() const { return _crashReporting.current(); } void setSaveDeletedMessages(bool val); @@ -309,6 +310,7 @@ public: void setVoiceConfirmation(bool val); void setTranslationProvider(const QString &val); void setAdaptiveCoverColor(bool val); + void setImproveLinkPreviews(bool val); void setCrashReporting(bool val); [[nodiscard]] rpl::producer useGlobalGhostModeChanges() const { return _useGlobalGhostMode.value(); } @@ -385,6 +387,7 @@ public: [[nodiscard]] rpl::producer voiceConfirmationChanges() const { return _voiceConfirmation.value(); } [[nodiscard]] rpl::producer translationProviderChanges() const { return _translationProvider.value(); } [[nodiscard]] rpl::producer adaptiveCoverColorChanges() const { return _adaptiveCoverColor.value(); } + [[nodiscard]] rpl::producer improveLinkPreviewsChanges() const { return _improveLinkPreviews.value(); } [[nodiscard]] rpl::producer crashReportingChanges() const { return _crashReporting.value(); } friend void to_json(nlohmann::json &j, const AyuSettings &s); @@ -469,6 +472,7 @@ private: rpl::variable _voiceConfirmation = false; rpl::variable _translationProvider = QString("telegram"); rpl::variable _adaptiveCoverColor = true; + rpl::variable _improveLinkPreviews = true; rpl::variable _crashReporting = true; rpl::variable _useGlobalGhostMode = true; diff --git a/Telegram/SourceFiles/ayu/ui/settings/settings_general.cpp b/Telegram/SourceFiles/ayu/ui/settings/settings_general.cpp index 826a6992d4..e35d3e2aae 100644 --- a/Telegram/SourceFiles/ayu/ui/settings/settings_general.cpp +++ b/Telegram/SourceFiles/ayu/ui/settings/settings_general.cpp @@ -161,6 +161,7 @@ void SetupQoLToggles(not_null container, not_null peer, Fn ca } } +QString getBetterLinkPreview(const QString &url) { + const auto &settings = AyuSettings::getInstance(); + if (!settings.improveLinkPreviews()) { + return url; + } + + auto parsed = QUrl(url); + if (!parsed.isValid() || parsed.host().isEmpty()) { + return url; + } + + auto host = parsed.host().toLower(); + + if (host == u"twitter.com"_q || host == u"x.com"_q) { + parsed.setHost(u"fixupx.com"_q); + } else if (host == u"tiktok.com"_q || host.endsWith(u".tiktok.com"_q)) { + host.replace(u"tiktok.com"_q, u"kktiktok.com"_q); + parsed.setHost(host); + } else if (host == u"reddit.com"_q || host == u"www.reddit.com"_q) { + parsed.setHost(u"vxreddit.com"_q); + } else if (host == u"instagram.com"_q || host == u"www.instagram.com"_q) { + parsed.setHost(u"kkinstagram.com"_q); + } else if (host == u"pixiv.net"_q || host == u"www.pixiv.net"_q) { + parsed.setHost(u"phixiv.net"_q); + } else { + return url; + } + + return parsed.toString(); +} + void applyGhostScheduling( not_null session, Api::SendOptions &options, diff --git a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h index 7dc1543f31..c1db3b385e 100644 --- a/Telegram/SourceFiles/ayu/utils/telegram_helpers.h +++ b/Telegram/SourceFiles/ayu/utils/telegram_helpers.h @@ -123,8 +123,9 @@ bool prependPseudoReply( void getRegistrationDate(not_null peer, Fn callback); +QString getBetterLinkPreview(const QString &url); + void applyGhostScheduling( not_null session, Api::SendOptions &options, int delaySeconds = 12); - diff --git a/Telegram/SourceFiles/data/data_drafts.h b/Telegram/SourceFiles/data/data_drafts.h index 63872d1fa6..daa59f7536 100644 --- a/Telegram/SourceFiles/data/data_drafts.h +++ b/Telegram/SourceFiles/data/data_drafts.h @@ -42,6 +42,7 @@ struct WebPageDraft { bool invert : 1 = false; bool manual : 1 = false; bool removed : 1 = false; + bool previewChanged : 1 = false; friend inline bool operator==(const WebPageDraft&, const WebPageDraft&) = default; diff --git a/Telegram/SourceFiles/data/data_histories.cpp b/Telegram/SourceFiles/data/data_histories.cpp index 590a66f2ff..dbad55cbe3 100644 --- a/Telegram/SourceFiles/data/data_histories.cpp +++ b/Telegram/SourceFiles/data/data_histories.cpp @@ -125,11 +125,12 @@ MTPInputMedia WebPageForMTP( const Data::WebPageDraft &draft, bool required) { using Flag = MTPDinputMediaWebPage::Flag; + const auto url = getBetterLinkPreview(draft.url); return MTP_inputMediaWebPage( - MTP_flags(((false && required) ? Flag() : Flag::f_optional) + MTP_flags((draft.previewChanged ? Flag() : Flag::f_optional) | (draft.forceLargeMedia ? Flag::f_force_large_media : Flag()) | (draft.forceSmallMedia ? Flag::f_force_small_media : Flag())), - MTP_string(draft.url)); + MTP_string(url)); } Histories::Histories(not_null owner) diff --git a/Telegram/SourceFiles/history/view/controls/history_view_webpage_processor.cpp b/Telegram/SourceFiles/history/view/controls/history_view_webpage_processor.cpp index feb0a5edf8..9daf6e48c8 100644 --- a/Telegram/SourceFiles/history/view/controls/history_view_webpage_processor.cpp +++ b/Telegram/SourceFiles/history/view/controls/history_view_webpage_processor.cpp @@ -16,6 +16,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lang/lang_keys.h" #include "main/main_session.h" +// AyuGram includes +#include "ayu/utils/telegram_helpers.h" + + namespace HistoryView::Controls { WebPageText TitleAndDescriptionFromWebPage(not_null d) { @@ -156,7 +160,7 @@ void WebpageResolver::request(const QString &link, bool force) { _requestId = _api.request( MTPmessages_GetWebPagePreview( MTP_flags(0), - MTP_string(link), + MTP_string(getBetterLinkPreview(link)), MTPVector() )).done([=]( const MTPmessages_WebPagePreview &result, @@ -229,6 +233,7 @@ WebpageProcessor::WebpageProcessor( if (_data) { _draft.id = _data->id; _draft.url = _data->url; + _draft.previewChanged = (getBetterLinkPreview(link) != link); updateFromData(); } else { _links = QStringList(); @@ -392,6 +397,7 @@ void WebpageProcessor::checkPreview() { _data = page; _draft.id = _data->id; _draft.url = _data->url; + _draft.previewChanged = (getBetterLinkPreview(chosen) != chosen); } else { _data = nullptr; _draft = {};