diff --git a/src/controller.cpp b/src/controller.cpp index deca40e98..a9c3d1429 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -128,16 +128,7 @@ Controller::Controller(QObject *parent) 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(); - } + handleNotifications(connection); }); } oldAccountCount = AccountRegistry::instance().size(); @@ -146,19 +137,16 @@ Controller::Controller(QObject *parent) } #ifdef QUOTIENT_07 -void Controller::handleNotifications() +void Controller::handleNotifications(QPointer connection) { - static bool initial = true; + static QStringList initial; static QStringList oldNotifications; - if (!m_connection) { - return; - } - auto job = m_connection->callApi(); + auto job = connection->callApi(); - connect(job, &BaseJob::success, this, [this, job]() { + connect(job, &BaseJob::success, this, [job, connection]() { const auto notifications = job->jsonData()["notifications"].toArray(); - if (initial) { - initial = false; + if (!initial.contains(connection->user()->id())) { + initial.append(connection->user()->id()); for (const auto &n : notifications) { oldNotifications += n.toObject()["event"].toObject()["event_id"].toString(); } @@ -174,7 +162,7 @@ void Controller::handleNotifications() continue; } oldNotifications += notification["event"].toObject()["event_id"].toString(); - auto room = m_connection->room(notification["room_id"].toString()); + auto room = 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)) { @@ -196,7 +184,7 @@ void Controller::handleNotifications() if (notification["event"]["type"] == "m.room.encrypted") { #ifdef Quotient_E2EE_ENABLED - auto decrypted = m_connection->decryptNotification(notification); + auto decrypted = connection->decryptNotification(notification); body = decrypted["content"].toObject()["body"].toString(); #endif if (body.isEmpty()) { diff --git a/src/controller.h b/src/controller.h index d3e0288e9..a1d3328fc 100644 --- a/src/controller.h +++ b/src/controller.h @@ -119,7 +119,7 @@ private: bool hasWindowSystem() const; #ifdef QUOTIENT_07 - void handleNotifications(); + void handleNotifications(QPointer connection); #endif private Q_SLOTS: diff --git a/src/notificationsmanager.cpp b/src/notificationsmanager.cpp index 51f322a99..8ed08486c 100644 --- a/src/notificationsmanager.cpp +++ b/src/notificationsmanager.cpp @@ -11,6 +11,12 @@ #include #include +#ifdef QUOTIENT_07 +#include +#else +#include "neochataccountregistry.h" +#endif + #include #include #include @@ -68,6 +74,13 @@ void NotificationsManager::postNotification(NeoChatRoom *room, notification->setDefaultAction(i18n("Open NeoChat in this room")); connect(notification, &KNotification::defaultActivated, this, [=]() { + if (room->localUser()->id() != Controller::instance().activeConnection()->userId()) { +#ifdef QUOTIENT_07 + Controller::instance().setActiveConnection(Accounts.get(room->localUser()->id())); +#else + Controller::instance().setActiveConnection(AccountRegistry::instance().get(room->localUser()->id())); +#endif + } RoomManager::instance().enterRoom(room); WindowController::instance().showAndRaiseWindow(notification->xdgActivationToken()); });