diff --git a/Telegram/SourceFiles/ayu/ayu_url_handlers.cpp b/Telegram/SourceFiles/ayu/ayu_url_handlers.cpp index 10546d936..0f5524389 100644 --- a/Telegram/SourceFiles/ayu/ayu_url_handlers.cpp +++ b/Telegram/SourceFiles/ayu/ayu_url_handlers.cpp @@ -17,6 +17,8 @@ #include "data/data_session.h" #include "data/data_user.h" +#include + namespace AyuUrlHandlers { @@ -67,4 +69,39 @@ bool HandleAyu( return true; } +bool TryHandleSpotify(const QString& url) +{ + if (!url.contains("spotify.com")) { + return false; + } + + // docs on their url scheme + // https://www.iana.org/assignments/uri-schemes/prov/spotify + + using namespace qthelp; + auto matchOptions = RegExOption::CaseInsensitive; + // https://regex101.com/r/l4Ogzf/2 + auto match = regex_match( + u"^(https?:\\/\\/)?([a-zA-Z0-9_]+)\\.spotify\\.com\\/(?track|album|artist|user|playlist)\\/(?[a-zA-Z0-9_\\/]+?)((\\?si=.+)?)$"_q, + url, + matchOptions); + if (match) { + const auto type = match->captured("type").toLower(); + const auto identifier = match->captured("identifier").replace("/", ":"); + + // '/' -> ':' for links like: + // https://open.spotify.com/user/1185903410/playlist/6YAnJeVC7tgOiocOG23Dd + // so it'll look like + // spotify:user:1185903410:playlist:6YAnJeVC7tgOiocOG23Dd + + const auto res = QString("spotify:%1:%2").arg(type).arg(identifier); + + if (!res.isEmpty()) { + return QDesktopServices::openUrl(QUrl(res)); + } + } + + return false; +} + } \ No newline at end of file diff --git a/Telegram/SourceFiles/ayu/ayu_url_handlers.h b/Telegram/SourceFiles/ayu/ayu_url_handlers.h index 6a85b650c..cf4c3b6ca 100644 --- a/Telegram/SourceFiles/ayu/ayu_url_handlers.h +++ b/Telegram/SourceFiles/ayu/ayu_url_handlers.h @@ -24,4 +24,6 @@ bool HandleAyu( const Match &match, const QVariant &context); +bool TryHandleSpotify(const QString& url); + } \ No newline at end of file diff --git a/Telegram/SourceFiles/core/ui_integration.cpp b/Telegram/SourceFiles/core/ui_integration.cpp index ba2593e7b..03d6dc762 100644 --- a/Telegram/SourceFiles/core/ui_integration.cpp +++ b/Telegram/SourceFiles/core/ui_integration.cpp @@ -30,6 +30,10 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "window/window_session_controller.h" #include "mainwindow.h" +// AyuGram includes +#include "ayu/ayu_url_handlers.h" + + namespace Core { namespace { @@ -234,6 +238,10 @@ bool UiIntegration::handleUrlClick( return true; } + if (AyuUrlHandlers::TryHandleSpotify(url)) { + return true; + } + auto parsed = UrlForAutoLogin(url); const auto domain = DomainForAutoLogin(parsed); const auto skip = context.value().skipBotAutoLogin;