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();