Add handler for KUnifiedPush messageReceived events
This commit is contained in:
@@ -116,6 +116,9 @@ Controller::Controller(QObject *parent)
|
|||||||
connection->setupPushNotifications(endpoint);
|
connection->setupPushNotifications(endpoint);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
connect(connector, &KUnifiedPush::Connector::messageReceived, this, [this](const QByteArray &data) {
|
||||||
|
NotificationsManager::instance().postPushNotification(data);
|
||||||
|
});
|
||||||
|
|
||||||
connector->registerClient(i18n("Receiving push notifications"));
|
connector->registerClient(i18n("Receiving push notifications"));
|
||||||
|
|
||||||
|
|||||||
@@ -262,21 +262,15 @@ QCoro::Task<void> NeoChatConnection::setupPushNotifications(QString endpoint)
|
|||||||
const auto &replyJson = QJsonDocument::fromJson(reply->readAll()).object();
|
const auto &replyJson = QJsonDocument::fromJson(reply->readAll()).object();
|
||||||
|
|
||||||
if (replyJson["unifiedpush"_L1]["gateway"_L1].toString() == QStringLiteral("matrix")) {
|
if (replyJson["unifiedpush"_L1]["gateway"_L1].toString() == QStringLiteral("matrix")) {
|
||||||
// FIXME: Currently hardcoded for ntfy URLs
|
callApi<PostPusherJob>(endpoint,
|
||||||
// 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();
|
|
||||||
|
|
||||||
callApi<PostPusherJob>(pushkey,
|
|
||||||
QStringLiteral("http"),
|
QStringLiteral("http"),
|
||||||
QStringLiteral("org.kde.neochat"),
|
QStringLiteral("org.kde.neochat"),
|
||||||
QStringLiteral("NeoChat"),
|
QStringLiteral("NeoChat"),
|
||||||
deviceId(),
|
deviceId(),
|
||||||
QString(), // profileTag is intentionally left empty for now, it's optional
|
QString(), // profileTag is intentionally left empty for now, it's optional
|
||||||
QStringLiteral("en"),
|
QStringLiteral("en-US"),
|
||||||
PostPusherJob::PusherData{QUrl::fromUserInput(gatewayEndpoint.toString()), QStringLiteral(" ")});
|
PostPusherJob::PusherData{QUrl::fromUserInput(gatewayEndpoint.toString()), QStringLiteral(" ")},
|
||||||
|
false);
|
||||||
|
|
||||||
qInfo() << "Registered for push notifications";
|
qInfo() << "Registered for push notifications";
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -292,6 +292,46 @@ void NotificationsManager::clearInvitationNotification(const QString &roomId)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void NotificationsManager::postPushNotification(const QByteArray &message)
|
||||||
|
{
|
||||||
|
const auto json = QJsonDocument::fromJson(message).object();
|
||||||
|
|
||||||
|
const auto type = json["notification"_ls]["type"_ls].toString();
|
||||||
|
|
||||||
|
// the only two types of push notifications we support right now
|
||||||
|
if (type == QStringLiteral("m.room.message") || type == QStringLiteral("m.room.encrypted")) {
|
||||||
|
auto notification = new KNotification("message"_ls);
|
||||||
|
|
||||||
|
const auto sender = json["notification"_ls]["sender_display_name"_ls].toString();
|
||||||
|
const auto roomName = json["notification"_ls]["room_name"_ls].toString();
|
||||||
|
const auto roomId = json["notification"_ls]["room_id"_ls].toString();
|
||||||
|
|
||||||
|
if (roomName.isEmpty() || sender == roomName) {
|
||||||
|
notification->setTitle(sender);
|
||||||
|
} else {
|
||||||
|
notification->setTitle(i18n("%1 (%2)", sender, roomName));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (type == QStringLiteral("m.room.message")) {
|
||||||
|
const auto text = json["notification"_ls]["content"_ls]["body"_ls].toString();
|
||||||
|
notification->setText(text.toHtmlEscaped());
|
||||||
|
} else if (type == QStringLiteral("m.room.encrypted")) {
|
||||||
|
notification->setText(i18n("Encrypted Message"));
|
||||||
|
}
|
||||||
|
|
||||||
|
auto openAction = notification->addAction(i18n("Open NeoChat"));
|
||||||
|
connect(openAction, &KNotificationAction::activated, this, [=]() {
|
||||||
|
WindowController::instance().showAndRaiseWindow(notification->xdgActivationToken());
|
||||||
|
});
|
||||||
|
|
||||||
|
notification->sendEvent();
|
||||||
|
|
||||||
|
m_notifications.insert(roomId, notification);
|
||||||
|
} else {
|
||||||
|
qWarning() << "Skipping unsupported push notification" << type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
QPixmap NotificationsManager::createNotificationImage(const QImage &icon, NeoChatRoom *room)
|
QPixmap NotificationsManager::createNotificationImage(const QImage &icon, NeoChatRoom *room)
|
||||||
{
|
{
|
||||||
// Handle avatars that are lopsided in one dimension
|
// Handle avatars that are lopsided in one dimension
|
||||||
|
|||||||
@@ -83,6 +83,11 @@ public:
|
|||||||
*/
|
*/
|
||||||
void clearInvitationNotification(const QString &roomId);
|
void clearInvitationNotification(const QString &roomId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Display a native notification for the given push notification.
|
||||||
|
*/
|
||||||
|
void postPushNotification(const QByteArray &message);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Handle the notifications for the given connection.
|
* @brief Handle the notifications for the given connection.
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user