Optimize room config

This commit is contained in:
Carl Schwan
2023-09-26 16:28:53 +02:00
committed by Carl Schwan
parent c04d8d6f59
commit 8945e004e2
4 changed files with 87 additions and 8 deletions

View File

@@ -127,6 +127,8 @@ add_library(neochat STATIC
mediasizehelper.h
eventhandler.cpp
enums/delegatetype.h
roomlastmessageprovider.cpp
roomlastmessageprovider.h
)
qt_add_qml_module(neochat URI org.kde.neochat NO_PLUGIN

View File

@@ -47,6 +47,7 @@
#include "filetransferpseudojob.h"
#include "neochatconfig.h"
#include "notificationsmanager.h"
#include "roomlastmessageprovider.h"
#include "texthandler.h"
#include "urlhelper.h"
#include "utils.h"
@@ -72,12 +73,10 @@ NeoChatRoom::NeoChatRoom(Connection *connection, QString roomId, JoinState joinS
connect(this, &Room::aboutToAddHistoricalMessages, this, &NeoChatRoom::readMarkerLoadedChanged);
// Load cached event if available.
KConfig dataResource("data"_ls, KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
KConfigGroup eventCacheGroup(&dataResource, "EventCache"_ls);
const auto &roomLastMessageProvider = RoomLastMessageProvider::self();
if (eventCacheGroup.hasKey(id())) {
auto eventJson = QJsonDocument::fromJson(eventCacheGroup.readEntry(id(), QByteArray())).object();
if (roomLastMessageProvider.hasKey(id())) {
auto eventJson = QJsonDocument::fromJson(roomLastMessageProvider.read(id())).object();
if (!eventJson.isEmpty()) {
auto event = loadEvent<RoomEvent>(eventJson);
@@ -309,11 +308,10 @@ void NeoChatRoom::cacheLastEvent()
{
auto event = lastEvent();
if (event != nullptr) {
KConfig dataResource("data"_ls, KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
KConfigGroup eventCacheGroup(&dataResource, "EventCache"_ls);
auto &roomLastMessageProvider = RoomLastMessageProvider::self();
auto eventJson = QJsonDocument(event->fullJson()).toJson();
eventCacheGroup.writeEntry(id(), eventJson);
roomLastMessageProvider.write(id(), eventJson);
auto uniqueEvent = loadEvent<RoomEvent>(event->fullJson());

View File

@@ -0,0 +1,38 @@
// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.0-or-later
#include "roomlastmessagestate.h"
using namespace Qt::Literals::StringLiterals;
RoomLastMessageState::RoomLastMessageState()
: m_config(KSharedConfig::openConfig(u"data"_s, KConfig::SimpleConfig, QStandardPaths::AppDataLocation))
, m_configGroup(KConfigGroup(m_config, u"EventCache"_s))
{
}
RoomLastMessageState::~RoomLastMessageState()
{
m_config->sync();
}
RoomLastMessageState &RoomLastMessageState::self()
{
static RoomLastMessageState instance;
return instance;
}
bool RoomLastMessageState::hasKey(const QString &roomId) const
{
return m_configGroup.hasKey(roomId);
}
QByteArray RoomLastMessageState::read(const QString &roomId) const
{
return m_configGroup.readEntry(roomId, QByteArray{});
}
void RoomLastMessageState::write(const QString &roomId, const QByteArray &json)
{
m_configGroup.writeEntry(roomId, json);
}

View File

@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: 2023 Carl Schwan <carl@carlschwan.eu>
// SPDX-License-Identifier: LGPL-2.0-or-later
#pragma once
#include <KConfigGroup>
#include <KSharedConfig>
/**
* Store and retrieve the last message of a room.
*/
class RoomLastMessageProvider
{
public:
/**
* Get the global instance of RoomLastMessageProvider.
*/
static RoomLastMessageProvider &self();
~RoomLastMessageProvider();
/**
* Check if we have the last message content for the specified roomId.
*/
bool hasKey(const QString &roomId) const;
/**
* Read the last message content of the specified roomId.
*/
QByteArray read(const QString &roomId) const;
/**
* Write the last message content for the specified roomId.
*/
void write(const QString &roomId, const QByteArray &json);
private:
RoomLastMessageProvider();
KSharedConfig::Ptr m_config;
KConfigGroup m_configGroup;
};