Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85630938
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
13 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index f5d28ae..ba703dc 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,92 +1,93 @@
set(KAZV_DATA_DIR ${KDE_INSTALL_DATADIR})
set(KAZV_L10N_DIR ${KAZV_DATA_DIR}/l10n)
if(WIN32)
set(KAZV_IS_WINDOWS 1)
else()
set(KAZV_IS_WINDOWS 0)
endif()
configure_file(kazv-path-config.hpp.in kazv-path-config.hpp)
configure_file(kazv-version.cpp.in kazv-version.cpp)
configure_file(kazv-platform.hpp.in kazv-platform.hpp)
set(kazvprivlib_SRCS
qt-job-handler.cpp
qt-job.cpp
${CMAKE_CURRENT_BINARY_DIR}/kazv-version.cpp
matrix-sdk.cpp
matrix-room.cpp
matrix-room-list.cpp
matrix-room-timeline.cpp
matrix-room-member.cpp
matrix-room-member-list-model.cpp
matrix-event-reader.cpp
matrix-event-reader-list-model.cpp
matrix-event.cpp
matrix-link.cpp
meta-types.cpp
l10n-provider.cpp
qt-rand-adapter.cpp
helper.cpp
matrix-promise.cpp
kazv-util.cpp
matrix-sticker-pack.cpp
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
device-mgmt/matrix-device-list.cpp
kazv-config.cpp
kazv-io-manager.cpp
kazv-io-job.cpp
kazv-file.cpp
upload-job-model.cpp
kazv-markdown.cpp
shortcuts/shortcut-util.cpp
register-types.cpp
)
ecm_qt_declare_logging_category(kazvprivlib_SRCS
HEADER kazv-log.hpp
IDENTIFIER kazvLog
CATEGORY_NAME moe.kazv.mxc.kazv
)
add_library(kazvprivlib STATIC ${kazvprivlib_SRCS})
target_link_libraries(kazvprivlib PUBLIC
libkazv::kazvall
Qt5::Core
Qt5::Network
Threads::Threads
Qt5::Gui Qt5::Qml Qt5::Quick Qt5::QuickControls2 Qt5::Svg Qt5::Concurrent Qt5::Widgets
KF5::ConfigCore KF5::KIOCore KF5::Notifications
${CMARK_TARGET_NAME}
)
target_include_directories(kazvprivlib PUBLIC ${CMAKE_CURRENT_BINARY_DIR})
target_include_directories(kazvprivlib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/device-mgmt)
set(kazv_SRCS
main.cpp
resources.qrc
)
add_executable(kazv ${kazv_SRCS})
target_link_libraries(kazv
PRIVATE
kazvprivlib
)
install(TARGETS kazv ${KF5_INSTALL_TARGETS_DEFAULT_ARGS})
install(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/l10n/
DESTINATION ${KAZV_L10N_DIR}
FILES_MATCHING PATTERN "*.ftl"
PATTERN "*.json"
)
add_subdirectory(tests)
diff --git a/src/matrix-user-given-attrs-map.cpp b/src/matrix-user-given-attrs-map.cpp
new file mode 100644
index 0000000..793a596
--- /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/matrix-user-given-attrs-map.hpp b/src/matrix-user-given-attrs-map.hpp
new file mode 100644
index 0000000..834fd68
--- /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/register-types.cpp b/src/register-types.cpp
index 5f4e6f4..5e67093 100644
--- a/src/register-types.cpp
+++ b/src/register-types.cpp
@@ -1,57 +1,59 @@
/*
* This file is part of kazv.
* SPDX-FileCopyrightText: 2021-2024 tusooa <tusooa@kazv.moe>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#include <libkazv-config.hpp>
#include <QtQml>
#include "matrix-sdk.hpp"
#include "matrix-room-list.hpp"
#include "matrix-room.hpp"
#include "matrix-room-timeline.hpp"
#include "matrix-room-member.hpp"
#include "matrix-room-member-list-model.hpp"
#include "matrix-event-reader.hpp"
#include "matrix-event-reader-list-model.hpp"
#include "matrix-event.hpp"
#include "l10n-provider.hpp"
#include "kazv-config.hpp"
#include "kazv-io-manager.hpp"
#include "shortcuts/shortcut-util.hpp"
#include "kazv-io-job.hpp"
#include "device-mgmt/matrix-device-list.hpp"
#include "device-mgmt/matrix-device.hpp"
#include "matrix-promise.hpp"
#include "kazv-util.hpp"
#include "matrix-sticker.hpp"
#include "matrix-sticker-pack.hpp"
#include "matrix-sticker-pack-list.hpp"
+#include "matrix-user-given-attrs-map.hpp"
void registerKazvQmlTypes()
{
qmlRegisterType<MatrixSdk>("moe.kazv.mxc.kazv", 0, 0, "MatrixSdk");
qmlRegisterType<L10nProvider>("moe.kazv.mxc.kazv", 0, 0, "L10nProvider");
qmlRegisterType<KazvConfig>("moe.kazv.mxc.kazv", 0, 0, "KazvConfig");
qmlRegisterType<KazvIOBaseJob>("moe.kazv.mxc.kazv", 0, 0, "KazvIOBaseJob");
qmlRegisterType<KazvIOManager>("moe.kazv.mxc.kazv", 0, 0, "KazvIOManager");
qmlRegisterType<UploadJobModel>("moe.kazv.mxc.kazv", 0, 0, "UploadJobModel");
qmlRegisterUncreatableType<MatrixRoomList>("moe.kazv.mxc.kazv", 0, 0, "MatrixRoomList", "");
qmlRegisterUncreatableType<MatrixRoom>("moe.kazv.mxc.kazv", 0, 0, "MatrixRoom", "");
qmlRegisterUncreatableType<MatrixRoomMember>("moe.kazv.mxc.kazv", 0, 0, "MatrixRoomMember", "");
qmlRegisterUncreatableType<MatrixRoomMemberListModel>("moe.kazv.mxc.kazv", 0, 0, "MatrixRoomMemberListModel", "");
qmlRegisterUncreatableType<MatrixRoomTimeline>("moe.kazv.mxc.kazv", 0, 0, "MatrixRoomTimeline", "");
qmlRegisterUncreatableType<MatrixEvent>("moe.kazv.mxc.kazv", 0, 0, "MatrixEvent", "");
qmlRegisterUncreatableType<MatrixEventReader>("moe.kazv.mxc.kazv", 0, 0, "MatrixEventReader", "");
qmlRegisterUncreatableType<MatrixEventReaderListModel>("moe.kazv.mxc.kazv", 0, 0, "MatrixEventReaderListModel", "");
qmlRegisterUncreatableType<MatrixDeviceList>("moe.kazv.mxc.kazv", 0, 0, "MatrixDeviceList", "");
qmlRegisterUncreatableType<MatrixDevice>("moe.kazv.mxc.kazv", 0, 0, "MatrixDevice", "");
qmlRegisterUncreatableType<MatrixPromise>("moe.kazv.mxc.kazv", 0, 0, "MatrixPromise", "");
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
index cf6d355..31c0b33 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,30 +1,31 @@
set(kazvtestlib_SRCS
test-model.cpp
)
add_library(kazvtestlib STATIC ${kazvtestlib_SRCS})
target_link_libraries(kazvtestlib PUBLIC kazvprivlib libkazv::kazvtestfixtures)
ecm_add_tests(
qt-job-handler-test.cpp
qt-promise-handler-test.cpp
qt-json-test.cpp
device-mgmt-test.cpp
matrix-sdk-test.cpp
matrix-promise-test.cpp
matrix-room-timeline-test.cpp
matrix-room-member-list-model-test.cpp
matrix-room-list-test.cpp
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
)
ecm_add_test(
quick-test.cpp
TEST_NAME quicktest
LINK_LIBRARIES Qt5::QuickTest kazvtestlib
)
set_tests_properties(quicktest PROPERTIES WORKING_DIRECTORY ${CMAKE_SOURCE_DIR})
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
index 0000000..3076268
--- /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
Details
Attached
Mime Type
text/x-diff
Expires
Mon, Aug 10, 3:09 AM (1 d, 19 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1724862
Default Alt Text
(13 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment