From 1c9575ccfd30627b878df53d28fc4e7cb6486b81 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Tue, 11 Oct 2022 22:26:26 +0200 Subject: [PATCH] Fix notifications with libquotient 0.7 --- src/controller.cpp | 79 +++++++++++++++++++++++++++++++++++++++++++ src/controller.h | 5 +++ src/roomlistmodel.cpp | 9 +++-- src/roomlistmodel.h | 2 ++ 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/controller.cpp b/src/controller.cpp index 05c4ec41d..d7def8f72 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -39,9 +39,15 @@ #include #include +#ifdef QUOTIENT_07 +#include +#include +#endif + #include "neochatconfig.h" #include "neochatroom.h" #include "neochatuser.h" +#include "notificationsmanager.h" #include "roommanager.h" #include "windowcontroller.h" @@ -111,8 +117,81 @@ Controller::Controller(QObject *parent) sigaction(sig, &sa, nullptr); } #endif + +#ifdef QUOTIENT_07 + static int oldAccountCount = 0; + connect(&AccountRegistry::instance(), &AccountRegistry::accountCountChanged, this, [=]() { + if (AccountRegistry::instance().size() > oldAccountCount) { + auto connection = AccountRegistry::instance().accounts()[AccountRegistry::instance().size() - 1]; + connect(connection, &Connection::syncDone, this, [=]() { + bool changes = false; + for (const auto &room : connection->allRooms()) { + if (m_notificationCounts[room] != room->unreadStats().notableCount) { + m_notificationCounts[room] = room->unreadStats().notableCount; + changes = true; + } + } + if (changes) { + handleNotifications(); + } + }); + } + oldAccountCount = AccountRegistry::instance().size(); + }); +#endif } +#ifdef QUOTIENT_07 +void Controller::handleNotifications() +{ + static bool initial = true; + static QStringList oldNotifications; + auto job = m_connection->callApi(); + + connect(job, &BaseJob::success, this, [this, job]() { + const auto notifications = job->jsonData()["notifications"].toArray(); + if (initial) { + initial = false; + for (const auto &n : notifications) { + oldNotifications += n.toObject()["event"].toObject()["event_id"].toString(); + } + return; + } + for (const auto &n : notifications) { + const auto notification = n.toObject(); + if (notification["read"].toBool()) { + oldNotifications.removeOne(notification["event"].toObject()["event_id"].toString()); + continue; + } + if (oldNotifications.contains(notification["event"].toObject()["event_id"].toString())) { + continue; + } + oldNotifications += notification["event"].toObject()["event_id"].toString(); + auto room = m_connection->room(notification["room_id"].toString()); + + // If room exists, room is NOT active OR the application is NOT active, show notification + if (room && !(room->id() == RoomManager::instance().currentRoom()->id() && QGuiApplication::applicationState() == Qt::ApplicationActive)) { + // The room might have been deleted (for example rejected invitation). + auto sender = room->user(notification["event"].toObject()["sender"].toString()); + + QImage avatar_image; + if (!sender->avatarUrl(room).isEmpty()) { + avatar_image = sender->avatar(128, room); + } else { + avatar_image = room->avatar(128); + } + NotificationsManager::instance().postNotification(dynamic_cast(room), + sender->displayname(room), + notification["event"].toObject()["content"].toObject()["body"].toString(), + avatar_image, + notification["event"].toObject()["event_id"].toString(), + true); + } + } + }); +} +#endif + Controller &Controller::instance() { static Controller _instance; diff --git a/src/controller.h b/src/controller.h index 1637c2ca5..c3069f2aa 100644 --- a/src/controller.h +++ b/src/controller.h @@ -21,6 +21,7 @@ class QQuickTextDocument; namespace Quotient { class Connection; +class Room; } namespace QKeychain @@ -111,9 +112,13 @@ private: void loadSettings(); void saveSettings() const; bool m_isOnline = true; + QMap m_notificationCounts; KAboutData m_aboutData; bool hasWindowSystem() const; +#ifdef QUOTIENT_07 + void handleNotifications(); +#endif private Q_SLOTS: void invokeLogin(); diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index dd57c9607..07e7b5e0a 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -5,7 +5,6 @@ #include "neochatconfig.h" #include "neochatroom.h" -#include "notificationsmanager.h" #include "roommanager.h" #include "user.h" @@ -20,7 +19,10 @@ #include #include +#ifndef QUOTIENT_07 +#include "notificationsmanager.h" #include +#endif using namespace Quotient; @@ -115,7 +117,6 @@ void RoomListModel::setConnection(Connection *connection) doResetModel(); - handleNotifications(); Q_EMIT connectionChanged(); } @@ -171,7 +172,9 @@ void RoomListModel::connectRoomSignals(NeoChatRoom *room) connect(room, &Room::addedMessages, this, [this, room] { refresh(room, {LastEventRole, SubtitleTextRole}); }); +#ifndef QUOTIENT_07 connect(room, &Room::notificationCountChanged, this, &RoomListModel::handleNotifications); +#endif connect(room, &Room::highlightCountChanged, this, [this, room] { if (room->highlightCount() == 0) { return; @@ -197,6 +200,7 @@ void RoomListModel::connectRoomSignals(NeoChatRoom *room) connect(room, &Room::notificationCountChanged, this, &RoomListModel::refreshNotificationCount); } +#ifndef QUOTIENT_07 void RoomListModel::handleNotifications() { static bool initial = true; @@ -245,6 +249,7 @@ void RoomListModel::handleNotifications() } }); } +#endif void RoomListModel::refreshNotificationCount() { diff --git a/src/roomlistmodel.h b/src/roomlistmodel.h index b4059d756..1287ad055 100644 --- a/src/roomlistmodel.h +++ b/src/roomlistmodel.h @@ -107,7 +107,9 @@ private: QString m_activeSpaceId = ""; void connectRoomSignals(NeoChatRoom *room); +#ifndef QUOTIENT_07 void handleNotifications(); +#endif Q_SIGNALS: void connectionChanged();