Page MenuHomePhorge

D216.1755011406.diff
No OneTemporary

Size
8 KB
Referenced Files
None
Subscribers
None

D216.1755011406.diff

diff --git a/src/client/CMakeLists.txt b/src/client/CMakeLists.txt
--- a/src/client/CMakeLists.txt
+++ b/src/client/CMakeLists.txt
@@ -24,6 +24,8 @@
push-rules-desc.cpp
notification-handler.cpp
power-levels-desc.cpp
+
+ get-content-job-v1.cpp
)
add_library(kazvclient ${kazvclient_SRCS})
diff --git a/src/client/actions/auth.cpp b/src/client/actions/auth.cpp
--- a/src/client/actions/auth.cpp
+++ b/src/client/actions/auth.cpp
@@ -158,6 +158,7 @@
ClientResult processResponse(ClientModel m, GetVersionsResponse r)
{
+ m.versions = r.versions();
return {
std::move(m),
[r](auto &&ctx) {
diff --git a/src/client/client-model.hpp b/src/client/client-model.hpp
--- a/src/client/client-model.hpp
+++ b/src/client/client-model.hpp
@@ -88,6 +88,8 @@
DeviceListTracker deviceLists;
DeviceTrustLevel trustLevelNeededToSendKeys{DeviceTrustLevel::Unseen};
+ immer::array<std::string /* version */> versions;
+
immer::flex_vector<std::string /* deviceId */> devicesToSendKeys(std::string userId) const;
/// rotate sessions for a room if there is a user in the room with
diff --git a/src/client/client.hpp b/src/client/client.hpp
--- a/src/client/client.hpp
+++ b/src/client/client.hpp
@@ -17,6 +17,7 @@
#include "client/client-model.hpp"
#include "client/actions/content.hpp"
#include "sdk-model-cursor-tag.hpp"
+#include "get-content-job-v1.hpp"
#include "room/room.hpp"
#include "notification-handler.hpp"
@@ -372,6 +373,18 @@
.make(serverName, mediaId).url();
}
+ /**
+ * Convert a MXC URI to an HTTP(s) URI that needs Authorization.
+ *
+ * The converted URI will be using the homeserver of
+ * this Client.
+ *
+ * @param mxcUri The MXC URI to convert.
+ * @return The HTTP(s) URI that has the content indicated
+ * by `mxcUri`.
+ */
+ std::string mxcUriToHttpV1(std::string mxcUri) const;
+
/**
* Download content from the content repository
*
@@ -553,6 +566,24 @@
ar << sdkCursor().get();
}
+ /**
+ * Get all supported versions.
+ *
+ * @param homeserver The base url of the homeserver. E.g. `https://tusooa.xyz`.
+ * @return A Promise that resolves when the versions has been set,
+ * or when there is an error.
+ */
+ PromiseT getVersions(std::string homeserver) const;
+
+ /**
+ * Get all supported versions.
+ *
+ * @return A lager::reader of a array contains all supported versions.
+ *
+ * See https://spec.matrix.org/v1.14/#specification-versions
+ */
+ auto supportVersions() const -> lager::reader<immer::array<std::string>>;
+
private:
void syncForever(std::optional<int> retryTime = std::nullopt) const;
diff --git a/src/client/client.cpp b/src/client/client.cpp
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -216,6 +216,14 @@
std::string{}});
}
+ std::string Client::mxcUriToHttpV1(std::string mxcUri) const {
+ using namespace CursorOp;
+ auto [serverName, mediaId] = mxcUriToMediaDesc(mxcUri);
+ return (+clientCursor())
+ .template job<GetContentJobV1>()
+ .make(serverName, mediaId).url();
+ }
+
auto Client::downloadContent(std::string mxcUri, std::optional<FileDesc> downloadTo) const
-> PromiseT
{
@@ -449,4 +457,14 @@
{
return NotificationHandler(clientCursor());
}
+
+ auto Client::getVersions(std::string homeserver) const -> PromiseT
+ {
+ return m_ctx.dispatch(GetVersionsAction{homeserver});
+ }
+
+ auto Client::supportVersions() const -> lager::reader<immer::array<std::string>>
+ {
+ return clientCursor()[&ClientModel::versions];
+ }
}
diff --git a/src/client/get-content-job-v1.hpp b/src/client/get-content-job-v1.hpp
new file mode 100644
--- /dev/null
+++ b/src/client/get-content-job-v1.hpp
@@ -0,0 +1,28 @@
+/*
+ * This file is part of libkazv.
+ * SPDX-FileCopyrightText: 2025 nannanko <nannanko@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include "basejob.hpp"
+#include "file-desc.hpp"
+
+#include <string>
+#include <optional>
+
+namespace Kazv {
+ class GetContentJobV1 : public Kazv::BaseJob
+ {
+ public:
+ explicit GetContentJobV1(std::string serverUrl, std::string token,
+ std::string serverName, std::string mediaId,
+ bool allowRemote = true,
+ std::optional<FileDesc> downloadTo = std::nullopt);
+
+ static BaseJob::Query buildQuery(bool allowRemote);
+ static constexpr auto needsAuth()
+ {
+ return true;
+ }
+ };
+}
diff --git a/src/client/get-content-job-v1.cpp b/src/client/get-content-job-v1.cpp
new file mode 100644
--- /dev/null
+++ b/src/client/get-content-job-v1.cpp
@@ -0,0 +1,32 @@
+/*
+ * This file is part of libkazv.
+ * SPDX-FileCopyrightText: 2025 nannanko <nannanko@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include "get-content-job-v1.hpp"
+#include "basejob.hpp"
+
+#include <string>
+#include <utility>
+
+using namespace std::literals::string_literals;
+
+namespace Kazv
+{
+ GetContentJobV1::GetContentJobV1(std::string serverUrl, std::string token,
+ std::string serverName, std::string mediaId, bool allowRemote,
+ std::optional<FileDesc> downloadTo)
+ : BaseJob(std::move(serverUrl),
+ "/_matrix/client/v1/media/download/"s + serverName + "/" + mediaId,
+ GET, "GetContent"s, token, ReturnType::File, BaseJob::EmptyBody{},
+ buildQuery(allowRemote), {}, downloadTo)
+ {}
+
+ BaseJob::Query GetContentJobV1::buildQuery(bool allowRemote)
+ {
+ BaseJob::Query query;
+ addToQueryIfNeeded(query, "allow_remotes"s, allowRemote);
+ return query;
+ }
+}
diff --git a/src/tests/CMakeLists.txt b/src/tests/CMakeLists.txt
--- a/src/tests/CMakeLists.txt
+++ b/src/tests/CMakeLists.txt
@@ -86,6 +86,7 @@
client/encryption-benchmark-test.cpp
client/logout-test.cpp
client/room/pinned-events-test.cpp
+ client/get-versions-test.cpp
EXTRA_LINK_LIBRARIES kazvclient kazveventemitter kazvjob client-test-lib kazvtestfixtures
EXTRA_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/client
)
diff --git a/src/tests/client/get-versions-test.cpp b/src/tests/client/get-versions-test.cpp
new file mode 100644
--- /dev/null
+++ b/src/tests/client/get-versions-test.cpp
@@ -0,0 +1,62 @@
+/*
+ * This file is part of libkazv.
+ * SPDX-FileCopyrightText: 2025 nannanko <nannanko@kazv.moe>
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+#include <libkazv-config.hpp>
+
+#include <catch2/catch_all.hpp>
+#include <boost/asio.hpp>
+#include <asio-promise-handler.hpp>
+
+#include "client-test-util.hpp"
+#include "factory.hpp"
+
+using namespace Kazv::Factory;
+
+TEST_CASE("Send get versions job", "[client][get-versions]")
+{
+ ClientModel model = makeClient({});
+ auto [next, _] = ClientModel::update(
+ model, GetVersionsAction{"https://example.com"});
+
+ assert1Job(next);
+ for1stJob(next, [] (const auto &job) {
+ REQUIRE(job.jobId() == "GetVersions");
+ REQUIRE(job.url().find("/versions") != std::string::npos);
+ });
+}
+
+TEST_CASE("Process get versions response", "[client][get-versions]")
+{
+ boost::asio::io_context io;
+ AsioPromiseHandler ph{io.get_executor()};
+ auto initialModel = makeClient();
+
+ auto store = createTestClientStoreFrom(initialModel, ph);
+ auto client = Client(store.reader().map([] (auto c) { return SdkModel{c}; }), store, std::nullopt);
+
+ WHEN("Success response")
+ {
+ auto succResponse = makeResponse("GetVersions", withResponseJsonBody(R"({
+ "versions": ["r0.0.1", "v1.1"]
+})"_json));
+ store.dispatch(ProcessResponseAction{succResponse})
+ .then([client] (auto stat) {
+ REQUIRE(stat.success());
+ REQUIRE(client.supportVersions().make().get() == immer::array{"r0.0.1"s, "v1.1"s});
+ });
+ }
+
+ WHEN("Failed response")
+ {
+ auto failResponse = makeResponse("GetVersions", withResponseStatusCode(403));
+ store.dispatch(ProcessResponseAction{failResponse})
+ .then([client] (auto stat) {
+ REQUIRE(!stat.success());
+ });
+ }
+
+ io.run();
+}

File Metadata

Mime Type
text/plain
Expires
Tue, Aug 12, 8:10 AM (9 h, 8 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
385468
Default Alt Text
D216.1755011406.diff (8 KB)

Event Timeline