Register for push notifications
This sets it up so the homeserver will give us push notifications, but we aren't handling them yet.
This commit is contained in:
@@ -42,7 +42,15 @@
|
|||||||
#include "trayicon_sni.h"
|
#include "trayicon_sni.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifdef HAVE_KUNIFIEDPUSH
|
||||||
|
#include <QCoro>
|
||||||
|
#include <Quotient/csapi/pusher.h>
|
||||||
|
#include <Quotient/networkaccessmanager.h>
|
||||||
|
#include <kunifiedpush/connector.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
using namespace Quotient;
|
using namespace Quotient;
|
||||||
|
using namespace Qt::StringLiterals;
|
||||||
|
|
||||||
Controller::Controller(QObject *parent)
|
Controller::Controller(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
@@ -99,6 +107,57 @@ Controller::Controller(QObject *parent)
|
|||||||
}
|
}
|
||||||
oldAccountCount = m_accountRegistry.size();
|
oldAccountCount = m_accountRegistry.size();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
#ifdef HAVE_KUNIFIEDPUSH
|
||||||
|
auto connector = new KUnifiedPush::Connector(QStringLiteral("org.kde.neochat"));
|
||||||
|
connect(connector, &KUnifiedPush::Connector::endpointChanged, this, &Controller::setupPushNotifications);
|
||||||
|
|
||||||
|
connector->registerClient(i18n("Receiving push notifications"));
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
QCoro::Task<void> 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<PostPusherJob>(
|
||||||
|
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()
|
Controller &Controller::instance()
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
#include <Quotient/accountregistry.h>
|
#include <Quotient/accountregistry.h>
|
||||||
#include <Quotient/jobs/basejob.h>
|
#include <Quotient/jobs/basejob.h>
|
||||||
#include <Quotient/settings.h>
|
#include <Quotient/settings.h>
|
||||||
|
#include <qcorotask.h>
|
||||||
|
|
||||||
class NeoChatRoom;
|
class NeoChatRoom;
|
||||||
class TrayIcon;
|
class TrayIcon;
|
||||||
@@ -124,6 +125,10 @@ public:
|
|||||||
private:
|
private:
|
||||||
explicit Controller(QObject *parent = nullptr);
|
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<void> setupPushNotifications(QString endpoint);
|
||||||
|
|
||||||
QPointer<NeoChatConnection> m_connection;
|
QPointer<NeoChatConnection> m_connection;
|
||||||
TrayIcon *m_trayIcon = nullptr;
|
TrayIcon *m_trayIcon = nullptr;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user