Improve the handling of notifications
The aim is to put some additional filtering in place to better stop floods of old notifications. This is achieved with a couple of new filters and better tracking of old notifications. - Make sure to paginate through all notification on initialization to ensure they are all added to old notifications. While we were not previously putting a limit on the number of returned notifications the server can and will do this when there are a very large amount. - Find the newest timestamp for each connection on initialization and don't post any notifications with an earlier timestamp. - Track old notifications on a per-connection basis. Closes network/neochat#358 and network/neochat#423
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include <QJsonArray>
|
||||
#include <QGuiApplication>
|
||||
|
||||
#include <KLocalizedString>
|
||||
#include <KNotification>
|
||||
@@ -18,6 +18,7 @@
|
||||
#endif
|
||||
|
||||
#include <connection.h>
|
||||
#include <csapi/notifications.h>
|
||||
#include <csapi/pushrules.h>
|
||||
#include <jobs/basejob.h>
|
||||
#include <user.h>
|
||||
@@ -48,6 +49,148 @@ NotificationsManager::NotificationsManager(QObject *parent)
|
||||
});
|
||||
}
|
||||
|
||||
#ifdef QUOTIENT_07
|
||||
void NotificationsManager::handleNotifications(QPointer<Connection> connection)
|
||||
{
|
||||
if (!m_connActiveJob.contains(connection->user()->id())) {
|
||||
auto job = connection->callApi<GetNotificationsJob>();
|
||||
m_connActiveJob.append(connection->user()->id());
|
||||
connect(job, &BaseJob::success, this, [this, job, connection]() {
|
||||
m_connActiveJob.removeAll(connection->user()->id());
|
||||
processNotificationJob(connection, job, !m_oldNotifications.contains(connection->user()->id()));
|
||||
});
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void NotificationsManager::processNotificationJob(QPointer<Quotient::Connection> connection, Quotient::GetNotificationsJob *job, bool initialization)
|
||||
{
|
||||
if (job == nullptr) {
|
||||
return;
|
||||
}
|
||||
if (connection == nullptr) {
|
||||
qWarning() << QStringLiteral("No connection for GetNotificationsJob %1").arg(job->objectName());
|
||||
return;
|
||||
}
|
||||
|
||||
const auto connectionId = connection->user()->id();
|
||||
|
||||
// If pagination has occurred set off the next job
|
||||
auto nextToken = job->jsonData()["next_token"].toString();
|
||||
if (!nextToken.isEmpty()) {
|
||||
auto nextJob = connection->callApi<GetNotificationsJob>(nextToken);
|
||||
m_connActiveJob.append(connectionId);
|
||||
connect(nextJob, &BaseJob::success, this, [this, nextJob, connection, initialization]() {
|
||||
m_connActiveJob.removeAll(connection->user()->id());
|
||||
processNotificationJob(connection, nextJob, initialization);
|
||||
});
|
||||
}
|
||||
|
||||
const auto notifications = job->jsonData()["notifications"].toArray();
|
||||
if (initialization) {
|
||||
m_oldNotifications[connectionId] = QStringList();
|
||||
for (const auto &n : notifications) {
|
||||
if (!m_initialTimestamp.contains(connectionId)) {
|
||||
m_initialTimestamp[connectionId] = n.toObject()["ts"].toDouble();
|
||||
} else {
|
||||
qint64 timestamp = n.toObject()["ts"].toDouble();
|
||||
if (timestamp > m_initialTimestamp[connectionId]) {
|
||||
m_initialTimestamp[connectionId] = timestamp;
|
||||
}
|
||||
}
|
||||
|
||||
auto connectionNotifications = m_oldNotifications.value(connectionId);
|
||||
connectionNotifications += n.toObject()["event"].toObject()["event_id"].toString();
|
||||
m_oldNotifications[connectionId] = connectionNotifications;
|
||||
}
|
||||
return;
|
||||
}
|
||||
for (const auto &n : notifications) {
|
||||
const auto notification = n.toObject();
|
||||
if (notification["read"].toBool()) {
|
||||
continue;
|
||||
}
|
||||
auto connectionNotifications = m_oldNotifications.value(connectionId);
|
||||
if (connectionNotifications.contains(notification["event"].toObject()["event_id"].toString())) {
|
||||
continue;
|
||||
}
|
||||
connectionNotifications += notification["event"].toObject()["event_id"].toString();
|
||||
m_oldNotifications[connectionId] = connectionNotifications;
|
||||
|
||||
auto room = connection->room(notification["room_id"].toString());
|
||||
if (shouldPostNotification(connection, n)) {
|
||||
// The room might have been deleted (for example rejected invitation).
|
||||
auto sender = room->user(notification["event"].toObject()["sender"].toString());
|
||||
|
||||
QString body;
|
||||
|
||||
if (notification["event"].toObject()["type"].toString() == "org.matrix.msc3381.poll.start") {
|
||||
body = notification["event"]
|
||||
.toObject()["content"]
|
||||
.toObject()["org.matrix.msc3381.poll.start"]
|
||||
.toObject()["question"]
|
||||
.toObject()["body"]
|
||||
.toString();
|
||||
} else {
|
||||
body = notification["event"].toObject()["content"].toObject()["body"].toString();
|
||||
}
|
||||
|
||||
if (notification["event"]["type"] == "m.room.encrypted") {
|
||||
#ifdef Quotient_E2EE_ENABLED
|
||||
auto decrypted = connection->decryptNotification(notification);
|
||||
body = decrypted["content"].toObject()["body"].toString();
|
||||
#endif
|
||||
if (body.isEmpty()) {
|
||||
body = i18n("Encrypted Message");
|
||||
}
|
||||
}
|
||||
|
||||
QImage avatar_image;
|
||||
if (!sender->avatarUrl(room).isEmpty()) {
|
||||
avatar_image = sender->avatar(128, room);
|
||||
} else {
|
||||
avatar_image = room->avatar(128);
|
||||
}
|
||||
postNotification(dynamic_cast<NeoChatRoom *>(room),
|
||||
sender->displayname(room),
|
||||
body,
|
||||
avatar_image,
|
||||
notification["event"].toObject()["event_id"].toString(),
|
||||
true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool NotificationsManager::shouldPostNotification(QPointer<Quotient::Connection> connection, const QJsonValue ¬ification)
|
||||
{
|
||||
if (connection == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto room = connection->room(notification["room_id"].toString());
|
||||
if (room == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the room is the current room and the application is active the notification
|
||||
// should not be shown.
|
||||
// This is setup so that if the application is inactive the notification will
|
||||
// always be posted, even if the room is the current room.
|
||||
bool isCurrentRoom = RoomManager::instance().currentRoom() && room->id() == RoomManager::instance().currentRoom()->id();
|
||||
if (isCurrentRoom && QGuiApplication::applicationState() == Qt::ApplicationActive) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// If the notification timestamp is earlier than the initial timestamp assume
|
||||
// the notification is old and shouldn't be posted.
|
||||
qint64 timestamp = notification["ts"].toDouble();
|
||||
if (timestamp < m_initialTimestamp[connection->user()->id()]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void NotificationsManager::postNotification(NeoChatRoom *room,
|
||||
const QString &sender,
|
||||
const QString &text,
|
||||
|
||||
Reference in New Issue
Block a user