From c63f1f045236c0de6a144e3334ccea2473d3724a Mon Sep 17 00:00:00 2001 From: James Graham Date: Mon, 15 May 2023 19:11:44 +0000 Subject: [PATCH] Cache last event Cache the last room event so that the room list can be sorted on startup Fixes network/neochat#88 \ BUG: 455042 --- src/neochatroom.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/neochatroom.h | 4 ++++ 2 files changed, 48 insertions(+) diff --git a/src/neochatroom.cpp b/src/neochatroom.cpp index 63614f050..e6f003c00 100644 --- a/src/neochatroom.cpp +++ b/src/neochatroom.cpp @@ -52,6 +52,8 @@ #include "filetransferpseudojob.h" #include "texthandler.h" +#include +#include #ifndef Q_OS_ANDROID #include #endif @@ -73,6 +75,22 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS connect(this, &Room::aboutToAddHistoricalMessages, this, &NeoChatRoom::readMarkerLoadedChanged); + // Load cached event if available. + KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); + KConfigGroup eventCacheGroup(&dataResource, "EventCache"); + + if (eventCacheGroup.hasKey(id())) { + auto eventJson = QJsonDocument::fromJson(eventCacheGroup.readEntry(id(), QByteArray())).object(); + if (!eventJson.isEmpty()) { + auto event = loadEvent(eventJson); + + if (event != nullptr) { + m_cachedEvent = std::move(event); + } + } + } + connect(this, &Room::addedMessages, this, &NeoChatRoom::cacheLastEvent); + connect(this, &Quotient::Room::eventsHistoryJobChanged, this, &NeoChatRoom::lastActiveTimeChanged); connect(this, &Room::joinStateChanged, this, [this](JoinState oldState, JoinState newState) { @@ -300,9 +318,32 @@ const RoomEvent *NeoChatRoom::lastEvent() const } #endif } + + if (m_cachedEvent != nullptr) { + return std::to_address(m_cachedEvent); + } + return nullptr; } +void NeoChatRoom::cacheLastEvent() +{ + auto event = lastEvent(); + if (event != nullptr) { + KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation); + KConfigGroup eventCacheGroup(&dataResource, "EventCache"); + + auto eventJson = QJsonDocument(event->fullJson()).toJson(); + eventCacheGroup.writeEntry(id(), eventJson); + + auto uniqueEvent = loadEvent(event->fullJson()); + + if (event != nullptr) { + m_cachedEvent = std::move(uniqueEvent); + } + } +} + bool NeoChatRoom::lastEventIsSpoiler() const { if (auto event = lastEvent()) { @@ -378,6 +419,9 @@ void NeoChatRoom::countChanged() QDateTime NeoChatRoom::lastActiveTime() { if (timelineSize() == 0) { + if (m_cachedEvent != nullptr) { + return m_cachedEvent->originTimestamp(); + } return QDateTime(); } diff --git a/src/neochatroom.h b/src/neochatroom.h index 14a380038..3b5fdb190 100644 --- a/src/neochatroom.h +++ b/src/neochatroom.h @@ -774,6 +774,8 @@ private: QCoro::Task doDeleteMessagesByUser(const QString &user, QString reason); QCoro::Task doUploadFile(QUrl url, QString body = QString()); + std::unique_ptr m_cachedEvent; + QString m_chatBoxText; QString m_editText; QString m_chatBoxReplyId; @@ -790,6 +792,8 @@ private Q_SLOTS: void countChanged(); void updatePushNotificationState(QString type); + void cacheLastEvent(); + Q_SIGNALS: void cachedInputChanged(); void busyChanged();