Initial work to add push rule support

This commit adds the ability to set the master push rule and set push rules for individual rooms as per the matrix spec. See https://spec.matrix.org/v1.3/client-server-api/#push-rules.

The master push rule is just on/off and uses the existing notification setting in general setting to enable/disable the server default master push rule .m.rule.master.

For each room there is now a page in the room setting that allows the following to be set:
- Default
- All messages
- @mentions and keywords
- off

New room or override rules are added/removed to achieve this.

There is also functionality to check the master/room notification state whenever the setting menu is entered. This allows the status to be updated if changed in another client or get the initial state for a room as it isn't stored.

Note - There is currently no menu items in the room list for setting the room push rule settings. This will be added in a later commit, the aim is to focus on making sure the technical implementation is good for now.
This commit is contained in:
James Graham
2022-09-09 16:41:03 +00:00
parent c2fc4e44a7
commit 4bba505da6
11 changed files with 353 additions and 5 deletions

View File

@@ -16,6 +16,9 @@
#endif
#include <KNotificationReplyAction>
#include "csapi/pushrules.h"
#include "jobs/basejob.h"
#include "controller.h"
#include "neochatconfig.h"
#include "roommanager.h"
@@ -30,6 +33,10 @@ NotificationsManager &NotificationsManager::instance()
NotificationsManager::NotificationsManager(QObject *parent)
: QObject(parent)
{
// Can't connect the signal up until the active connection has been established by the controller
connect(&Controller::instance(), &Controller::activeConnectionChanged, this, [this]() {
connect(Controller::instance().activeConnection(), &Connection::accountDataChanged, this, &NotificationsManager::updateGlobalNotificationsEnabled);
});
}
void NotificationsManager::postNotification(NeoChatRoom *room,
@@ -73,7 +80,6 @@ void NotificationsManager::postNotification(NeoChatRoom *room,
notification->setHint(QStringLiteral("x-kde-origin-name"), room->localUser()->id());
notification->sendEvent();
m_notifications.insert(room->id(), notification);
@@ -98,11 +104,11 @@ void NotificationsManager::postInviteNotification(NeoChatRoom *room, const QStri
WindowController::instance().showAndRaiseWindow(notification->xdgActivationToken());
});
notification->setActions({i18n("Accept Invitation"), i18n("Reject Invitation")});
connect(notification, &KNotification::action1Activated, this, [this, room, notification]() {
connect(notification, &KNotification::action1Activated, this, [room, notification]() {
room->acceptInvitation();
notification->close();
});
connect(notification, &KNotification::action2Activated, this, [this, room, notification]() {
connect(notification, &KNotification::action2Activated, this, [room, notification]() {
RoomManager::instance().leaveRoom(room);
notification->close();
});
@@ -122,3 +128,42 @@ void NotificationsManager::clearInvitationNotification(const QString &roomId)
m_invitations[roomId]->close();
}
}
/**
* The master push rule sets all notifications to off when enabled
* see https://spec.matrix.org/v1.3/client-server-api/#default-override-rules
* therefore to enable push rules the master rule needs to be disabled and vice versa
*/
void NotificationsManager::setGlobalNotificationsEnabled(bool enabled)
{
using namespace Quotient;
auto job = Controller::instance().activeConnection()->callApi<IsPushRuleEnabledJob>("global", "override", ".m.rule.master");
connect(job, &BaseJob::success, this, [this, job, enabled]() {
if (job->enabled() == enabled) {
Controller::instance().activeConnection()->callApi<SetPushRuleEnabledJob>("global", "override", ".m.rule.master", !enabled);
m_globalNotificationsEnabled = enabled;
Q_EMIT globalNotificationsEnabledChanged(m_globalNotificationsEnabled);
}
});
}
void NotificationsManager::updateGlobalNotificationsEnabled(QString type)
{
if (type != "m.push_rules") {
return;
}
QJsonObject accountData = Controller::instance().activeConnection()->accountDataJson("m.push_rules");
QJsonArray overrideRuleArray = accountData.value("global").toObject().value("override").toArray();
for (const auto &i : overrideRuleArray) {
QJsonObject overrideRule = i.toObject();
if (overrideRule.value("rule_id") == ".m.rule.master") {
bool ruleEnabled = overrideRule.value("enabled").toBool();
m_globalNotificationsEnabled = !ruleEnabled;
NeoChatConfig::self()->setShowNotifications(m_globalNotificationsEnabled);
Q_EMIT globalNotificationsEnabledChanged(m_globalNotificationsEnabled);
}
}
}