From 9f637ab925d2abca29ffb46759724128f10ed394 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Mon, 22 Mar 2021 16:02:26 +0100 Subject: [PATCH] Improve notification handling --- src/neochatroom.cpp | 26 ----------------- src/roomlistmodel.cpp | 66 ++++++++++++++++++++++++++++--------------- src/roomlistmodel.h | 2 +- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index dd8992184..bb74d001c 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -46,32 +46,6 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS setFileUploadingProgress(0); setHasFileUploading(false); }); - connect(this, &NeoChatRoom::notificationCountChanged, this, [this]() { - if (messageEvents().size() == 0) { - return; - } - const RoomEvent *lastEvent = messageEvents().rbegin()->get(); - if (lastEvent->originTimestamp() < QDateTime::currentDateTime().addSecs(-60)) { - return; - } - if (lastEvent->isStateEvent()) { - return; - } - User *sender = user(lastEvent->senderId()); - if (sender == localUser()) { - return; - } - - QImage avatar_image; - if (!sender->avatarUrl(this).isEmpty()) { - avatar_image = sender->avatar(128, this); - } else { - avatar_image = this->avatar(128); - } - - NotificationsManager::instance().postNotification(this, displayName(), sender->displayname(this), - eventToString(*lastEvent), avatar_image, !lastEvent->id().isEmpty() ? lastEvent->id() : lastEvent->transactionId()); - }); connect(this, &Room::aboutToAddHistoricalMessages, this, &NeoChatRoom::readMarkerLoadedChanged); diff --git a/src/roomlistmodel.cpp b/src/roomlistmodel.cpp index 51825a7ec..95f394ec5 100644 --- a/src/roomlistmodel.cpp +++ b/src/roomlistmodel.cpp @@ -24,6 +24,9 @@ #include #include +#include "csapi/notifications.h" +#include "notificationsmanager.h" + #ifndef Q_OS_ANDROID bool useUnityCounter() { static const auto Result = QDBusInterface( @@ -120,6 +123,7 @@ void RoomListModel::setConnection(Connection *connection) doResetModel(); + handleNotifications(); Q_EMIT connectionChanged(); } @@ -175,28 +179,7 @@ void RoomListModel::connectRoomSignals(NeoChatRoom *room) connect(room, &Room::addedMessages, this, [=] { refresh(room, {LastEventRole}); }); - connect(room, &Room::notificationCountChanged, this, [=] { - if (room->notificationCount() == 0) { - return; - } - if (room->timelineSize() == 0) { - return; - } - auto *lastEvent = room->lastEvent(); - - if (!lastEvent) { - return; - } - - if (lastEvent->isStateEvent()) { - return; - } - User *sender = room->user(lastEvent->senderId()); - if (sender == room->localUser()) { - return; - } - Q_EMIT newMessage(room->id(), lastEvent->id(), room->displayName(), sender->displayname(), room->eventToString(*lastEvent), room->avatar(128)); - }); + connect(room, &Room::notificationCountChanged, this, &RoomListModel::handleNotifications); connect(room, &Room::highlightCountChanged, this, [=] { if (room->highlightCount() == 0) { return; @@ -222,6 +205,45 @@ void RoomListModel::connectRoomSignals(NeoChatRoom *room) connect(room, &Room::notificationCountChanged, this, &RoomListModel::refreshNotificationCount); } +void RoomListModel::handleNotifications() +{ + static bool initial = true; + static QStringList oldNotifications; + auto job = m_connection->callApi(); + + connect(job, &BaseJob::success, this, [=](){ + 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()); + 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), room->displayName(), sender->displayname(room), notification["event"].toObject()["content"].toObject()["body"].toString(), avatar_image, notification["event"].toObject()["event_id"].toString()); + } + }); +} + void RoomListModel::refreshNotificationCount() { int count = 0; diff --git a/src/roomlistmodel.h b/src/roomlistmodel.h index a4b324456..93d8f8aca 100644 --- a/src/roomlistmodel.h +++ b/src/roomlistmodel.h @@ -98,12 +98,12 @@ private: int m_notificationCount = 0; void connectRoomSignals(NeoChatRoom *room); + void handleNotifications(); Q_SIGNALS: void connectionChanged(); void notificationCountChanged(); void roomAdded(NeoChatRoom *_t1); - void newMessage(const QString &_t1, const QString &_t2, const QString &_t3, const QString &_t4, const QString &_t5, const QImage &_t6); void newHighlight(const QString &_t1, const QString &_t2, const QString &_t3, const QString &_t4, const QString &_t5, const QImage &_t6); };