Improve notification handling

This commit is contained in:
Tobias Fella
2021-03-22 16:02:26 +01:00
committed by Carl Schwan
parent a1fb3471c9
commit 9f637ab925
3 changed files with 45 additions and 49 deletions

View File

@@ -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);

View File

@@ -24,6 +24,9 @@
#include <KLocalizedString>
#include <utility>
#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<GetNotificationsJob>();
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<NeoChatRoom *>(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;

View File

@@ -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);
};