Cache last event

Cache the last room event so that the room list can be sorted on startup

Fixes network/neochat#88 \
BUG: 455042
This commit is contained in:
James Graham
2023-05-15 19:11:44 +00:00
parent e0df447998
commit c63f1f0452
2 changed files with 48 additions and 0 deletions

View File

@@ -52,6 +52,8 @@
#include "filetransferpseudojob.h"
#include "texthandler.h"
#include <KConfig>
#include <KConfigGroup>
#ifndef Q_OS_ANDROID
#include <KIO/Job>
#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<RoomEvent>(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<RoomEvent>(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();
}

View File

@@ -774,6 +774,8 @@ private:
QCoro::Task<void> doDeleteMessagesByUser(const QString &user, QString reason);
QCoro::Task<void> doUploadFile(QUrl url, QString body = QString());
std::unique_ptr<Quotient::RoomEvent> 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();