From 60ce292b58bb0395feb4653a4d252eeebca1e297 Mon Sep 17 00:00:00 2001 From: Tobias Fella Date: Sun, 11 Feb 2024 23:50:19 +0100 Subject: [PATCH] Initially load space hierarchy from cache This brings us to the final result faster after starting the client, which leads to a nicer UX. It will still miss rooms if the room the "current" room is some descendant from the room in the space child event and the user has not joined all rooms between them. --- src/spacehierarchycache.cpp | 23 +++++++++++++++++++++++ src/spacehierarchycache.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/src/spacehierarchycache.cpp b/src/spacehierarchycache.cpp index fec32a4b2..76172eb5c 100644 --- a/src/spacehierarchycache.cpp +++ b/src/spacehierarchycache.cpp @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -50,11 +51,33 @@ void SpaceHierarchyCache::cacheSpaceHierarchy() } } +void SpaceHierarchyCache::populateFromState(const QString &spaceId, const QString &rootId) +{ + if (const auto &room = m_connection->room(spaceId)) { + for (const auto &event : room->currentState().eventsOfType("m.space.child"_ls)) { + if (const auto &child = m_connection->room(event->stateKey())) { + m_spaceHierarchy[rootId] += event->stateKey(); + auto successor = child; + do { + m_spaceHierarchy[rootId] += successor->id(); + } while ((successor = successor->successor(JoinState::Join))); + if (dynamic_cast(child)->isSpace()) { + populateFromState(child->id(), rootId); + } + } + } + } + Q_EMIT spaceHierarchyChanged(); +} + void SpaceHierarchyCache::populateSpaceHierarchy(const QString &spaceId) { if (!m_connection) { return; } + + populateFromState(spaceId, spaceId); + auto job = m_connection->callApi(spaceId); connect(job, &BaseJob::success, this, [this, job, spaceId]() { diff --git a/src/spacehierarchycache.h b/src/spacehierarchycache.h index 78ee31553..85bcc5396 100644 --- a/src/spacehierarchycache.h +++ b/src/spacehierarchycache.h @@ -102,6 +102,8 @@ private Q_SLOTS: private: explicit SpaceHierarchyCache(QObject *parent = nullptr); + void populateFromState(const QString &roomId, const QString &rootId); + QList m_activeSpaceRooms; QHash> m_spaceHierarchy; void cacheSpaceHierarchy();