From 3fc125a79879e938cf8ef5d2253e3ae3cef8b791 Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Fri, 10 Nov 2023 08:46:52 -0500 Subject: [PATCH] Move push notification setup to NeoChatConnection --- src/controller.cpp | 59 +++++++-------------------------------- src/controller.h | 6 +--- src/neochatconnection.cpp | 47 +++++++++++++++++++++++++++++++ src/neochatconnection.h | 5 ++++ 4 files changed, 63 insertions(+), 54 deletions(-) diff --git a/src/controller.cpp b/src/controller.cpp index 1cf79322d..4e53448b5 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -43,14 +43,10 @@ #endif #ifdef HAVE_KUNIFIEDPUSH -#include -#include -#include #include #endif using namespace Quotient; -using namespace Qt::StringLiterals; Controller::Controller(QObject *parent) : QObject(parent) @@ -104,62 +100,27 @@ Controller::Controller(QObject *parent) connect(connection, &NeoChatConnection::syncDone, this, [connection]() { NotificationsManager::instance().handleNotifications(connection); }); + connect(connection, &NeoChatConnection::connected, this, [this, connection]() { + connection->setupPushNotifications(m_endpoint); + }); } oldAccountCount = m_accountRegistry.size(); }); #ifdef HAVE_KUNIFIEDPUSH auto connector = new KUnifiedPush::Connector(QStringLiteral("org.kde.neochat")); - connect(connector, &KUnifiedPush::Connector::endpointChanged, this, &Controller::setupPushNotifications); + connect(connector, &KUnifiedPush::Connector::endpointChanged, this, [this](const QString &endpoint) { + m_endpoint = endpoint; + for (auto "ientConnection : m_accountRegistry) { + auto connection = dynamic_cast(quotientConnection); + connection->setupPushNotifications(endpoint); + } + }); connector->registerClient(i18n("Receiving push notifications")); #endif } -QCoro::Task Controller::setupPushNotifications(QString endpoint) -{ -#ifdef HAVE_KUNIFIEDPUSH - while (!activeConnection()) { - co_await qCoro(this, &Controller::activeConnectionChanged); - } - - QUrl gatewayEndpoint(endpoint); - gatewayEndpoint.setPath(QStringLiteral("/_matrix/push/v1/notify")); - - QNetworkRequest checkGateway(gatewayEndpoint); - auto reply = co_await NetworkAccessManager::instance()->get(checkGateway); - - // We want to check if this UnifiedPush server has a Matrix gateway - // This is because Matrix does not natively support UnifiedPush - const QJsonObject replyJson = QJsonDocument::fromJson(reply->readAll()).object(); - - if (replyJson["unifiedpush"_L1]["gateway"_L1].toString() == QStringLiteral("matrix")) { - // FIXME: Currently hardcoded for ntfy URLs - // We need to pass the ntfy topic as the pushkey. Is there a more generic way to handle this? - const QUrl endpointUrl(endpoint); - - // Pop the slash off of the path - const QString pushkey = endpointUrl.path().removeFirst(); - - Controller::instance().activeConnection()->callApi( - pushkey, - QStringLiteral("http"), - QStringLiteral("org.kde.neochat"), - QStringLiteral("NeoChat"), - Controller::instance().activeConnection()->deviceId(), - QString(), // FIXME: what is profileTag? - QStringLiteral("en"), - PostPusherJob::PusherData{QUrl::fromUserInput(gatewayEndpoint.toString()), QStringLiteral(" ")}); - - qInfo() << "Registered for push notifications"; - } else { - qWarning() << "There's no gateway, not setting up push notifications."; - } -#else - co_return; -#endif -} - Controller &Controller::instance() { static Controller _instance; diff --git a/src/controller.h b/src/controller.h index 40fedede2..85d6121e9 100644 --- a/src/controller.h +++ b/src/controller.h @@ -11,7 +11,6 @@ #include #include #include -#include class NeoChatRoom; class TrayIcon; @@ -125,10 +124,6 @@ public: private: explicit Controller(QObject *parent = nullptr); - // note: this is intentionally a copied QString because - // the reference could be destroyed before the task is finished - QCoro::Task setupPushNotifications(QString endpoint); - QPointer m_connection; TrayIcon *m_trayIcon = nullptr; @@ -140,6 +135,7 @@ private: Quotient::AccountRegistry m_accountRegistry; QStringList m_accountsLoading; + QString m_endpoint; private Q_SLOTS: void invokeLogin(); diff --git a/src/neochatconnection.cpp b/src/neochatconnection.cpp index f55817186..462473e2e 100644 --- a/src/neochatconnection.cpp +++ b/src/neochatconnection.cpp @@ -21,7 +21,14 @@ #include #include +#ifdef HAVE_KUNIFIEDPUSH +#include +#include +#include +#endif + using namespace Quotient; +using namespace Qt::StringLiterals; NeoChatConnection::NeoChatConnection(QObject *parent) : Connection(parent) @@ -241,6 +248,46 @@ void NeoChatConnection::openOrCreateDirectChat(User *user) requestDirectChat(user); } +QCoro::Task NeoChatConnection::setupPushNotifications(QString endpoint) +{ +#ifdef HAVE_KUNIFIEDPUSH + QUrl gatewayEndpoint(endpoint); + gatewayEndpoint.setPath(QStringLiteral("/_matrix/push/v1/notify")); + + QNetworkRequest checkGateway(gatewayEndpoint); + auto reply = co_await NetworkAccessManager::instance()->get(checkGateway); + + // We want to check if this UnifiedPush server has a Matrix gateway + // This is because Matrix does not natively support UnifiedPush + const QJsonObject replyJson = QJsonDocument::fromJson(reply->readAll()).object(); + + if (replyJson["unifiedpush"_L1]["gateway"_L1].toString() == QStringLiteral("matrix")) { + // FIXME: Currently hardcoded for ntfy URLs + // We need to pass the ntfy topic as the pushkey. Is there a more generic way to handle this? + const QUrl endpointUrl(endpoint); + + // Pop the slash off of the path + const QString pushkey = endpointUrl.path().removeFirst(); + + Controller::instance().activeConnection()->callApi( + pushkey, + QStringLiteral("http"), + QStringLiteral("org.kde.neochat"), + QStringLiteral("NeoChat"), + Controller::instance().activeConnection()->deviceId(), + QString(), // FIXME: what is profileTag? + QStringLiteral("en"), + PostPusherJob::PusherData{QUrl::fromUserInput(gatewayEndpoint.toString()), QStringLiteral(" ")}); + + qInfo() << "Registered for push notifications"; + } else { + qWarning() << "There's no gateway, not setting up push notifications."; + } +#else + co_return; +#endif +} + QString NeoChatConnection::deviceKey() const { return edKeyForUserDevice(userId(), deviceId()); diff --git a/src/neochatconnection.h b/src/neochatconnection.h index 4323289cf..48f565069 100644 --- a/src/neochatconnection.h +++ b/src/neochatconnection.h @@ -6,6 +6,7 @@ #include #include +#include #include class NeoChatConnection : public Quotient::Connection @@ -75,6 +76,10 @@ public: */ Q_INVOKABLE void openOrCreateDirectChat(Quotient::User *user); + // note: this is intentionally a copied QString because + // the reference could be destroyed before the task is finished + QCoro::Task setupPushNotifications(QString endpoint); + QString deviceKey() const; QString encryptionKey() const;