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:
@@ -52,6 +52,8 @@
|
|||||||
#include "filetransferpseudojob.h"
|
#include "filetransferpseudojob.h"
|
||||||
#include "texthandler.h"
|
#include "texthandler.h"
|
||||||
|
|
||||||
|
#include <KConfig>
|
||||||
|
#include <KConfigGroup>
|
||||||
#ifndef Q_OS_ANDROID
|
#ifndef Q_OS_ANDROID
|
||||||
#include <KIO/Job>
|
#include <KIO/Job>
|
||||||
#endif
|
#endif
|
||||||
@@ -73,6 +75,22 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS
|
|||||||
|
|
||||||
connect(this, &Room::aboutToAddHistoricalMessages, this, &NeoChatRoom::readMarkerLoadedChanged);
|
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, &Quotient::Room::eventsHistoryJobChanged, this, &NeoChatRoom::lastActiveTimeChanged);
|
||||||
|
|
||||||
connect(this, &Room::joinStateChanged, this, [this](JoinState oldState, JoinState newState) {
|
connect(this, &Room::joinStateChanged, this, [this](JoinState oldState, JoinState newState) {
|
||||||
@@ -300,9 +318,32 @@ const RoomEvent *NeoChatRoom::lastEvent() const
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_cachedEvent != nullptr) {
|
||||||
|
return std::to_address(m_cachedEvent);
|
||||||
|
}
|
||||||
|
|
||||||
return nullptr;
|
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
|
bool NeoChatRoom::lastEventIsSpoiler() const
|
||||||
{
|
{
|
||||||
if (auto event = lastEvent()) {
|
if (auto event = lastEvent()) {
|
||||||
@@ -378,6 +419,9 @@ void NeoChatRoom::countChanged()
|
|||||||
QDateTime NeoChatRoom::lastActiveTime()
|
QDateTime NeoChatRoom::lastActiveTime()
|
||||||
{
|
{
|
||||||
if (timelineSize() == 0) {
|
if (timelineSize() == 0) {
|
||||||
|
if (m_cachedEvent != nullptr) {
|
||||||
|
return m_cachedEvent->originTimestamp();
|
||||||
|
}
|
||||||
return QDateTime();
|
return QDateTime();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -774,6 +774,8 @@ private:
|
|||||||
QCoro::Task<void> doDeleteMessagesByUser(const QString &user, QString reason);
|
QCoro::Task<void> doDeleteMessagesByUser(const QString &user, QString reason);
|
||||||
QCoro::Task<void> doUploadFile(QUrl url, QString body = QString());
|
QCoro::Task<void> doUploadFile(QUrl url, QString body = QString());
|
||||||
|
|
||||||
|
std::unique_ptr<Quotient::RoomEvent> m_cachedEvent;
|
||||||
|
|
||||||
QString m_chatBoxText;
|
QString m_chatBoxText;
|
||||||
QString m_editText;
|
QString m_editText;
|
||||||
QString m_chatBoxReplyId;
|
QString m_chatBoxReplyId;
|
||||||
@@ -790,6 +792,8 @@ private Q_SLOTS:
|
|||||||
void countChanged();
|
void countChanged();
|
||||||
void updatePushNotificationState(QString type);
|
void updatePushNotificationState(QString type);
|
||||||
|
|
||||||
|
void cacheLastEvent();
|
||||||
|
|
||||||
Q_SIGNALS:
|
Q_SIGNALS:
|
||||||
void cachedInputChanged();
|
void cachedInputChanged();
|
||||||
void busyChanged();
|
void busyChanged();
|
||||||
|
|||||||
Reference in New Issue
Block a user