Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F85804993
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
11 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 3acf152..f5d28ae 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -1,91 +1,92 @@
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
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-link.cpp b/src/matrix-link.cpp
new file mode 100644
index 0000000..d0fe9a4
--- /dev/null
+++ b/src/matrix-link.cpp
@@ -0,0 +1,102 @@
+/*
+ * 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 <QUrl>
+#include <QUrlQuery>
+#include <QDebug>
+
+#include "matrix-link.hpp"
+
+std::pair<MatrixLink::Type, QString> parseMatrixToComp(QString comp)
+{
+ comp = QUrl("foo:#" + comp).fragment(QUrl::FullyDecoded);
+ if (comp.startsWith('@')) {
+ return {MatrixLink::User, comp};
+ } else if (comp.startsWith('#')) {
+ return {MatrixLink::RoomAlias, comp};
+ } else if (comp.startsWith('!')) {
+ return {MatrixLink::RoomId, comp};
+ } else if (comp.startsWith('$')) {
+ return {MatrixLink::Event, comp};
+ } else {
+ return {MatrixLink::None, comp};
+ }
+}
+
+QStringList parseMatrixToQuery(QString query)
+{
+ auto q = QUrlQuery(query);
+ return q.allQueryItemValues("via");
+}
+
+MatrixLink::MatrixLink(const QUrl &url)
+{
+ // matrix.to link
+ if (url.scheme() == "https"
+ && url.host() == "matrix.to") {
+ auto frag = url.fragment();
+ auto parts = frag.split('?');
+ QString ids = parts[0];
+ QString query = parts.size() > 1 ? parts[1] : "";
+
+ auto comps = ids.split('/');
+ if (comps.empty() || comps[0] != "") {
+ return;
+ }
+ comps.pop_front();
+ for (auto comp : comps) {
+ m_identifiers.push_back(parseMatrixToComp(comp));
+ }
+
+ m_routingServers = parseMatrixToQuery(query);
+ }
+}
+
+bool MatrixLink::isValid() const
+{
+ return !m_identifiers.empty()
+ && m_identifiers[0].first != None
+ && m_identifiers[0].first != Event
+ && (m_identifiers.size() == 1 || (
+ m_identifiers[1].first == Event
+ && (m_identifiers[0].first == RoomId
+ || m_identifiers[0].first == RoomAlias)));
+}
+
+bool MatrixLink::isUser() const
+{
+ return isValid() && m_identifiers[0].first == User;
+}
+
+bool MatrixLink::isRoom() const
+{
+ return isValid() && (
+ m_identifiers[0].first == RoomId
+ || m_identifiers[0].first == RoomAlias);
+}
+
+bool MatrixLink::isEvent() const
+{
+ return isValid() && m_identifiers.size() == 2;
+}
+
+QStringList MatrixLink::identifiers() const
+{
+ QStringList ret;
+ for (auto [_, id] : m_identifiers) {
+ ret.push_back(id);
+ }
+ return ret;
+}
+
+QStringList MatrixLink::routingServers() const
+{
+ return m_routingServers;
+}
diff --git a/src/matrix-link.hpp b/src/matrix-link.hpp
new file mode 100644
index 0000000..03c1d50
--- /dev/null
+++ b/src/matrix-link.hpp
@@ -0,0 +1,52 @@
+/*
+ * 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 <tuple>
+#include <vector>
+#include <QObject>
+#include <QStringList>
+
+class QUrl;
+
+/**
+ * A link that represents a Matrix object.
+ */
+class MatrixLink
+{
+ Q_GADGET
+ Q_PROPERTY(bool isUser READ isUser CONSTANT)
+ Q_PROPERTY(bool isRoom READ isRoom CONSTANT)
+ Q_PROPERTY(bool isEvent READ isEvent CONSTANT)
+ Q_PROPERTY(QStringList identifiers READ identifiers CONSTANT)
+ Q_PROPERTY(QStringList routingServers READ routingServers CONSTANT)
+
+public:
+ enum Type {
+ None,
+ User,
+ RoomId,
+ RoomAlias,
+ Event,
+ };
+
+ MatrixLink(const QUrl &url);
+
+ bool isValid() const;
+ bool isUser() const;
+ bool isRoom() const;
+ bool isEvent() const;
+ QStringList identifiers() const;
+ QStringList routingServers() const;
+
+private:
+ std::vector<std::pair<Type, QString>> m_identifiers;
+ QStringList m_routingServers;
+};
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
index e3d8b39..cf6d355 100644
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -1,29 +1,30 @@
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
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-link-test.cpp b/src/tests/matrix-link-test.cpp
new file mode 100644
index 0000000..32c6889
--- /dev/null
+++ b/src/tests/matrix-link-test.cpp
@@ -0,0 +1,106 @@
+/*
+ * 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 "matrix-link.hpp"
+
+class MatrixLinkTest : public QObject
+{
+ Q_OBJECT
+
+private Q_SLOTS:
+ void testInvalidLink();
+ void testUserLink();
+ void testRoomLink();
+ void testEventLink();
+ void testRoutingServers();
+};
+
+void MatrixLinkTest::testInvalidLink()
+{
+ std::vector<QUrl> links = {
+ QUrl("https://matrix.to/#"),
+ QUrl("https://matrix.to/#/"),
+ QUrl("https://matrix.to/#//"),
+ QUrl("https://matrix.to/#///"),
+ QUrl("http://matrix.to/#/%40alice%3Aexample.org"),
+ QUrl("https://lol.lol/#/%40alice%3Aexample.org"),
+ QUrl("https://matrix.to/#%40alice%3Aexample.org"),
+ };
+ for (auto link : links) {
+ MatrixLink ml(link);
+ QVERIFY(!ml.isValid());
+ }
+}
+
+void MatrixLinkTest::testUserLink()
+{
+ std::vector<QUrl> links = {
+ QUrl("https://matrix.to/#/%40alice%3Aexample.org"),
+ QUrl("https://matrix.to/#/@alice%3Aexample.org"),
+ QUrl("https://matrix.to/#/%40alice:example.org"),
+ QUrl("https://matrix.to/#/@alice:example.org"),
+ };
+ for (auto link : links) {
+ MatrixLink ml(link);
+ QVERIFY(ml.isUser());
+ QCOMPARE(ml.identifiers(), QStringList{"@alice:example.org"});
+ }
+}
+
+void MatrixLinkTest::testRoomLink()
+{
+ std::vector<std::tuple<QUrl, QString>> links = {
+ {QUrl("https://matrix.to/#/%23somewhere%3Aexample.org"), "#somewhere:example.org"},
+ {QUrl("https://matrix.to/#/%23somewhere:example.org"), "#somewhere:example.org"},
+ {QUrl("https://matrix.to/#/#somewhere%3Aexample.org"), "#somewhere:example.org"},
+ {QUrl("https://matrix.to/#/#somewhere:example.org"), "#somewhere:example.org"},
+ {QUrl("https://matrix.to/#/!somewhere%3Aexample.org?via=elsewhere.ca"), "!somewhere:example.org"},
+ {QUrl("https://matrix.to/#/!somewhere:example.org?via=elsewhere.ca"), "!somewhere:example.org"},
+ {QUrl("https://matrix.to/#/!somewhere%3Aexample.org"), "!somewhere:example.org"},
+ {QUrl("https://matrix.to/#/!somewhere:example.org"), "!somewhere:example.org"},
+ };
+ for (auto [link, pointed] : links) {
+ MatrixLink ml(link);
+ QVERIFY(ml.isRoom());
+ QCOMPARE(ml.identifiers(), QStringList{pointed});
+ }
+}
+
+void MatrixLinkTest::testEventLink()
+{
+ std::vector<std::tuple<QUrl, QString>> links = {
+ {QUrl("https://matrix.to/#/%23somewhere%3Aexample.org/%24event%3Aexample.org"), "#somewhere:example.org"},
+ {QUrl("https://matrix.to/#/%23somewhere%3Aexample.org/%24event:example.org"), "#somewhere:example.org"},
+ {QUrl("https://matrix.to/#/!somewhere%3Aexample.org/%24event%3Aexample.org?via=elsewhere.ca"), "!somewhere:example.org"},
+ {QUrl("https://matrix.to/#/!somewhere%3Aexample.org/$event:example.org?via=elsewhere.ca"), "!somewhere:example.org"},
+ };
+ for (auto [link, pointed] : links) {
+ MatrixLink ml(link);
+ QVERIFY(ml.isRoom());
+ QCOMPARE(ml.identifiers(), (QStringList{pointed, "$event:example.org"}));
+ }
+}
+
+void MatrixLinkTest::testRoutingServers()
+{
+ QCOMPARE(MatrixLink(QUrl("https://matrix.to/#/%23somewhere%3Aexample.org/%24event:example.org")).routingServers(), (QStringList{}));
+
+ QCOMPARE(MatrixLink(QUrl("https://matrix.to/#/%23somewhere%3Aexample.org/%24event:example.org?via=a.org")).routingServers(), (QStringList{"a.org"}));
+
+ QCOMPARE(MatrixLink(QUrl("https://matrix.to/#/%23somewhere%3Aexample.org/%24event:example.org?via=a.org&via=b.org")).routingServers(), (QStringList{"a.org", "b.org"}));
+
+ QCOMPARE(MatrixLink(QUrl("https://matrix.to/#/%23somewhere%3Aexample.org/%24event:example.org?foo=bar&via=a.org&via=b.org")).routingServers(), (QStringList{"a.org", "b.org"}));
+}
+
+QTEST_MAIN(MatrixLinkTest)
+
+#include "matrix-link-test.moc"
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Sat, Oct 10, 8:41 PM (1 d, 9 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1785446
Default Alt Text
(11 KB)
Attached To
Mode
rK kazv
Attached
Detach File
Event Timeline
Log In to Comment