Page MenuHomePhorge

D37.1732597604.diff
No OneTemporary

Size
8 KB
Referenced Files
None
Subscribers
None

D37.1732597604.diff

diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -33,6 +33,7 @@
matrix-sticker.cpp
matrix-sticker-pack-list.cpp
matrix-sticker-pack-source.cpp
+ matrix-user-given-attrs-map.cpp
qt-promise-handler.cpp
device-mgmt/matrix-device.cpp
diff --git a/src/matrix-user-given-attrs-map.hpp b/src/matrix-user-given-attrs-map.hpp
new file mode 100644
--- /dev/null
+++ b/src/matrix-user-given-attrs-map.hpp
@@ -0,0 +1,51 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#pragma once
+#include <libkazv-config.hpp>
+#include <immer/config.hpp> // https://github.com/arximboldi/immer/issues/168
+#include <QObject>
+#include <QQmlEngine>
+#include <QJsonObject>
+#include <lager/reader.hpp>
+#include <lager/extra/qt.hpp>
+#include <event.hpp>
+#include <client.hpp>
+
+class QJsonValue;
+class MatrixPromise;
+
+class MatrixUserGivenAttrsMap : public QObject
+{
+ Q_OBJECT
+ QML_ELEMENT
+ QML_UNCREATABLE("")
+
+ lager::reader<Kazv::Event> m_event;
+ std::function<Kazv::Client::PromiseT(Kazv::Event)> m_setter;
+
+public:
+ explicit MatrixUserGivenAttrsMap(
+ lager::reader<Kazv::Event> event,
+ std::function<Kazv::Client::PromiseT(Kazv::json)> setter,
+ QObject *parent = 0
+ );
+ ~MatrixUserGivenAttrsMap() override;
+
+ LAGER_QT_READER(QJsonObject, map);
+
+ /**
+ * Set the user given attribute of the matrix object with @c id
+ * and return the modified json.
+ *
+ * @param id The id of the matrix object.
+ * @param data The value to set for the matrix object. Use null to unset.
+ * @return The modified event content.
+ */
+ Kazv::json set(const QString &id, const QJsonValue &data) const;
+
+ Q_INVOKABLE MatrixPromise *setAndUpload(const QString &id, const QJsonValue &data);
+};
diff --git a/src/matrix-user-given-attrs-map.cpp b/src/matrix-user-given-attrs-map.cpp
new file mode 100644
--- /dev/null
+++ b/src/matrix-user-given-attrs-map.cpp
@@ -0,0 +1,55 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libkazv-config.hpp>
+#include <immer/config.hpp> // https://github.com/arximboldi/immer/issues/168
+#include <QJsonValue>
+#include "matrix-promise.hpp"
+#include "qt-json.hpp"
+#include "matrix-user-given-attrs-map.hpp"
+
+using namespace Kazv;
+
+MatrixUserGivenAttrsMap::MatrixUserGivenAttrsMap(
+ lager::reader<Event> event,
+ std::function<Client::PromiseT(json)> setter,
+ QObject *parent
+)
+ : QObject(parent)
+ , m_event(event)
+ , m_setter(setter)
+ , LAGER_QT(map)(m_event.map([](Event e) {
+ auto content = e.content().get();
+ return content.is_object()
+ ? content.template get<QJsonObject>()
+ : QJsonObject();
+ }))
+{
+}
+
+MatrixUserGivenAttrsMap::~MatrixUserGivenAttrsMap() = default;
+
+Kazv::json MatrixUserGivenAttrsMap::set(const QString &id, const QJsonValue &data) const
+{
+ auto content = m_event.get().content().get();
+ if (!content.is_object()) {
+ content = json::object();
+ }
+ auto idStd = id.toStdString();
+ if (data.isNull()) {
+ if (content.contains(idStd)) {
+ content.erase(idStd);
+ }
+ } else {
+ content[idStd] = json(data);
+ }
+ return content;
+}
+
+MatrixPromise *MatrixUserGivenAttrsMap::setAndUpload(const QString &id, const QJsonValue &data)
+{
+ return new MatrixPromise(m_setter(set(id, data)));
+}
diff --git a/src/register-types.cpp b/src/register-types.cpp
--- a/src/register-types.cpp
+++ b/src/register-types.cpp
@@ -29,6 +29,7 @@
#include "matrix-sticker.hpp"
#include "matrix-sticker-pack.hpp"
#include "matrix-sticker-pack-list.hpp"
+#include "matrix-user-given-attrs-map.hpp"
void registerKazvQmlTypes()
{
@@ -52,6 +53,7 @@
qmlRegisterUncreatableType<MatrixSticker>("moe.kazv.mxc.kazv", 0, 0, "MatrixSticker", "");
qmlRegisterUncreatableType<MatrixStickerPack>("moe.kazv.mxc.kazv", 0, 0, "MatrixStickerPack", "");
qmlRegisterUncreatableType<MatrixStickerPackList>("moe.kazv.mxc.kazv", 0, 0, "MatrixStickerPackList", "");
+ qmlRegisterUncreatableType<MatrixUserGivenAttrsMap>("moe.kazv.mxc.kazv", 0, 0, "MatrixUserGivenAttrsMap", "");
qmlRegisterSingletonInstance<KazvUtil>("moe.kazv.mxc.kazv", 0, 0, "KazvUtil", new KazvUtil());
qmlRegisterSingletonInstance<ShortcutUtil>("moe.kazv.mxc.kazvshortcuts", 0, 0, "ShortcutUtil", new ShortcutUtil());
}
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -19,6 +19,7 @@
kazv-markdown-test.cpp
matrix-sticker-pack-test.cpp
matrix-link-test.cpp
+ matrix-user-given-attrs-map-test.cpp
LINK_LIBRARIES Qt5::Test kazvtestlib
)
diff --git a/src/tests/matrix-user-given-attrs-map-test.cpp b/src/tests/matrix-user-given-attrs-map-test.cpp
new file mode 100644
--- /dev/null
+++ b/src/tests/matrix-user-given-attrs-map-test.cpp
@@ -0,0 +1,111 @@
+/*
+ * This file is part of kazv.
+ * SPDX-FileCopyrightText: 2024 tusooa <tusooa@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libkazv-config.hpp>
+#include <immer/config.hpp> // https://github.com/arximboldi/immer/issues/168
+
+#include <vector>
+#include <QtTest>
+
+#include "qt-promise-handler.hpp"
+#include "qt-json.hpp"
+#include "matrix-promise.hpp"
+#include "test-utils.hpp"
+#include "matrix-user-given-attrs-map.hpp"
+
+using namespace Kazv;
+
+class MatrixUserGivenAttrsMapTest : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void testGet();
+ void testSet();
+};
+
+static Event emptyEvent{};
+// https://github.com/matrix-org/matrix-spec-proposals/pull/3865/files
+static Event nicknameEvent = json{
+ {"type", "work.banananet.msc3865.user_given.user.displayname"},
+ {"content", {
+ {"@alice:example.com", "My Wife"},
+ {"@bob:example.com", "Bobby"},
+ {"@eve:example.com", "My Lifelong Rival"},
+ }},
+};
+
+namespace
+{
+ struct FakeSetter
+ {
+ QObject *executor{0};
+ std::shared_ptr<int> called;
+ std::shared_ptr<json> lastVal;
+
+ Client::PromiseT operator()(json val)
+ {
+ if (called) { ++*called; }
+ if (lastVal) { *lastVal = val; }
+ return QtPromise<EffectStatus>(executor, EffectStatus());
+ }
+ };
+}
+
+void MatrixUserGivenAttrsMapTest::testGet()
+{
+ {
+ auto map = MatrixUserGivenAttrsMap(lager::make_constant(emptyEvent), FakeSetter());
+ QCOMPARE(map.map(), QJsonObject());
+ }
+
+ {
+ auto map = MatrixUserGivenAttrsMap(lager::make_constant(nicknameEvent), FakeSetter());
+ QCOMPARE(map.map(),
+ nicknameEvent.content().get().template get<QJsonObject>());
+ }
+}
+
+void MatrixUserGivenAttrsMapTest::testSet()
+{
+ {
+ auto map = MatrixUserGivenAttrsMap(lager::make_constant(emptyEvent), FakeSetter());
+ QCOMPARE(map.set("@a:example.com", QJsonValue()), json::object());
+ QCOMPARE(map.set("@a:example.com", "k"), (json{{"@a:example.com", "k"}}));
+ }
+
+ {
+ auto called = std::make_shared<int>(0);
+ auto lastVal = std::make_shared<json>();
+ auto setter = FakeSetter{this, called, lastVal};
+ auto map = MatrixUserGivenAttrsMap(lager::make_constant(nicknameEvent), setter);
+
+ QCOMPARE(map.set("@alice:example.com", QJsonValue()), (json{
+ {"@bob:example.com", "Bobby"},
+ {"@eve:example.com", "My Lifelong Rival"},
+ }));
+
+ QCOMPARE(map.set("@alice:example.com", "Something"), (json{
+ {"@alice:example.com", "Something"},
+ {"@bob:example.com", "Bobby"},
+ {"@eve:example.com", "My Lifelong Rival"},
+ }));
+
+ QCOMPARE(*called, 0);
+
+ auto p = toUniquePtr(map.setAndUpload("@alice:example.com", "Something"));
+ QCOMPARE(*called, 1);
+ QCOMPARE(*lastVal, (json{
+ {"@alice:example.com", "Something"},
+ {"@bob:example.com", "Bobby"},
+ {"@eve:example.com", "My Lifelong Rival"},
+ }));
+ }
+}
+
+QTEST_MAIN(MatrixUserGivenAttrsMapTest)
+
+#include "matrix-user-given-attrs-map-test.moc"

File Metadata

Mime Type
text/plain
Expires
Mon, Nov 25, 9:06 PM (11 h, 16 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
39852
Default Alt Text
D37.1732597604.diff (8 KB)

Event Timeline