Fix notifications with libquotient 0.7

This commit is contained in:
Tobias Fella
2022-10-11 22:26:26 +02:00
parent 785a82df93
commit 1c9575ccfd
4 changed files with 93 additions and 2 deletions

View File

@@ -39,9 +39,15 @@
#include <csapi/profile.h> #include <csapi/profile.h>
#include <qt_connection_util.h> #include <qt_connection_util.h>
#ifdef QUOTIENT_07
#include <csapi/notifications.h>
#include <eventstats.h>
#endif
#include "neochatconfig.h" #include "neochatconfig.h"
#include "neochatroom.h" #include "neochatroom.h"
#include "neochatuser.h" #include "neochatuser.h"
#include "notificationsmanager.h"
#include "roommanager.h" #include "roommanager.h"
#include "windowcontroller.h" #include "windowcontroller.h"
@@ -111,8 +117,81 @@ Controller::Controller(QObject *parent)
sigaction(sig, &sa, nullptr); sigaction(sig, &sa, nullptr);
} }
#endif #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<GetNotificationsJob>();
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<NeoChatRoom *>(room),
sender->displayname(room),
notification["event"].toObject()["content"].toObject()["body"].toString(),
avatar_image,
notification["event"].toObject()["event_id"].toString(),
true);
}
}
});
}
#endif
Controller &Controller::instance() Controller &Controller::instance()
{ {
static Controller _instance; static Controller _instance;

View File

@@ -21,6 +21,7 @@ class QQuickTextDocument;
namespace Quotient namespace Quotient
{ {
class Connection; class Connection;
class Room;
} }
namespace QKeychain namespace QKeychain
@@ -111,9 +112,13 @@ private:
void loadSettings(); void loadSettings();
void saveSettings() const; void saveSettings() const;
bool m_isOnline = true; bool m_isOnline = true;
QMap<Quotient::Room *, int> m_notificationCounts;
KAboutData m_aboutData; KAboutData m_aboutData;
bool hasWindowSystem() const; bool hasWindowSystem() const;
#ifdef QUOTIENT_07
void handleNotifications();
#endif
private Q_SLOTS: private Q_SLOTS:
void invokeLogin(); void invokeLogin();

View File

@@ -5,7 +5,6 @@
#include "neochatconfig.h" #include "neochatconfig.h"
#include "neochatroom.h" #include "neochatroom.h"
#include "notificationsmanager.h"
#include "roommanager.h" #include "roommanager.h"
#include "user.h" #include "user.h"
@@ -20,7 +19,10 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <utility> #include <utility>
#ifndef QUOTIENT_07
#include "notificationsmanager.h"
#include <csapi/notifications.h> #include <csapi/notifications.h>
#endif
using namespace Quotient; using namespace Quotient;
@@ -115,7 +117,6 @@ void RoomListModel::setConnection(Connection *connection)
doResetModel(); doResetModel();
handleNotifications();
Q_EMIT connectionChanged(); Q_EMIT connectionChanged();
} }
@@ -171,7 +172,9 @@ void RoomListModel::connectRoomSignals(NeoChatRoom *room)
connect(room, &Room::addedMessages, this, [this, room] { connect(room, &Room::addedMessages, this, [this, room] {
refresh(room, {LastEventRole, SubtitleTextRole}); refresh(room, {LastEventRole, SubtitleTextRole});
}); });
#ifndef QUOTIENT_07
connect(room, &Room::notificationCountChanged, this, &RoomListModel::handleNotifications); connect(room, &Room::notificationCountChanged, this, &RoomListModel::handleNotifications);
#endif
connect(room, &Room::highlightCountChanged, this, [this, room] { connect(room, &Room::highlightCountChanged, this, [this, room] {
if (room->highlightCount() == 0) { if (room->highlightCount() == 0) {
return; return;
@@ -197,6 +200,7 @@ void RoomListModel::connectRoomSignals(NeoChatRoom *room)
connect(room, &Room::notificationCountChanged, this, &RoomListModel::refreshNotificationCount); connect(room, &Room::notificationCountChanged, this, &RoomListModel::refreshNotificationCount);
} }
#ifndef QUOTIENT_07
void RoomListModel::handleNotifications() void RoomListModel::handleNotifications()
{ {
static bool initial = true; static bool initial = true;
@@ -245,6 +249,7 @@ void RoomListModel::handleNotifications()
} }
}); });
} }
#endif
void RoomListModel::refreshNotificationCount() void RoomListModel::refreshNotificationCount()
{ {

View File

@@ -107,7 +107,9 @@ private:
QString m_activeSpaceId = ""; QString m_activeSpaceId = "";
void connectRoomSignals(NeoChatRoom *room); void connectRoomSignals(NeoChatRoom *room);
#ifndef QUOTIENT_07
void handleNotifications(); void handleNotifications();
#endif
Q_SIGNALS: Q_SIGNALS:
void connectionChanged(); void connectionChanged();