File indexing completed on 2024-09-15 04:28:35
0001 // SPDX-FileCopyrightText: 2022 Snehit Sah <hi@snehit.dev> 0002 // SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0003 0004 #include "spacehierarchycache.h" 0005 0006 #include <Quotient/csapi/space_hierarchy.h> 0007 #include <Quotient/qt_connection_util.h> 0008 0009 #include "neochatroom.h" 0010 0011 using namespace Quotient; 0012 0013 SpaceHierarchyCache::SpaceHierarchyCache(QObject *parent) 0014 : QObject{parent} 0015 { 0016 } 0017 0018 void SpaceHierarchyCache::cacheSpaceHierarchy() 0019 { 0020 if (!m_connection) { 0021 return; 0022 } 0023 0024 const auto &roomList = m_connection->allRooms(); 0025 for (const auto &room : roomList) { 0026 const auto neoChatRoom = static_cast<NeoChatRoom *>(room); 0027 if (neoChatRoom->isSpace()) { 0028 populateSpaceHierarchy(neoChatRoom->id()); 0029 } else { 0030 connectSingleShot(neoChatRoom, &Room::baseStateLoaded, neoChatRoom, [this, neoChatRoom]() { 0031 if (neoChatRoom->isSpace()) { 0032 populateSpaceHierarchy(neoChatRoom->id()); 0033 } 0034 }); 0035 } 0036 } 0037 } 0038 0039 void SpaceHierarchyCache::populateSpaceHierarchy(const QString &spaceId) 0040 { 0041 if (!m_connection) { 0042 return; 0043 } 0044 auto job = m_connection->callApi<GetSpaceHierarchyJob>(spaceId); 0045 0046 connect(job, &BaseJob::success, this, [this, job, spaceId]() { 0047 const auto rooms = job->rooms(); 0048 QList<QString> roomList; 0049 for (unsigned long i = 0; i < rooms.size(); ++i) { 0050 for (const auto &state : rooms[i].childrenState) { 0051 roomList.push_back(state->stateKey()); 0052 } 0053 } 0054 m_spaceHierarchy.insert(spaceId, roomList); 0055 Q_EMIT spaceHierarchyChanged(); 0056 }); 0057 } 0058 0059 void SpaceHierarchyCache::addSpaceToHierarchy(Quotient::Room *room) 0060 { 0061 connectSingleShot(room, &Quotient::Room::baseStateLoaded, this, [this, room]() { 0062 const auto neoChatRoom = static_cast<NeoChatRoom *>(room); 0063 if (neoChatRoom->isSpace()) { 0064 populateSpaceHierarchy(neoChatRoom->id()); 0065 } 0066 }); 0067 } 0068 0069 void SpaceHierarchyCache::removeSpaceFromHierarchy(Quotient::Room *room) 0070 { 0071 const auto neoChatRoom = static_cast<NeoChatRoom *>(room); 0072 if (neoChatRoom->isSpace()) { 0073 m_spaceHierarchy.remove(neoChatRoom->id()); 0074 } 0075 } 0076 0077 bool SpaceHierarchyCache::isSpaceChild(const QString &spaceId, const QString &roomId) 0078 { 0079 return getRoomListForSpace(spaceId, false).contains(roomId); 0080 } 0081 0082 QList<QString> &SpaceHierarchyCache::getRoomListForSpace(const QString &spaceId, bool updateCache) 0083 { 0084 if (updateCache) { 0085 populateSpaceHierarchy(spaceId); 0086 } 0087 return m_spaceHierarchy[spaceId]; 0088 } 0089 0090 bool SpaceHierarchyCache::isChild(const QString &roomId) const 0091 { 0092 const auto childrens = m_spaceHierarchy.values(); 0093 for (const auto &children : childrens) { 0094 if (children.contains(roomId)) { 0095 return true; 0096 } 0097 } 0098 return false; 0099 } 0100 0101 NeoChatConnection *SpaceHierarchyCache::connection() const 0102 { 0103 return m_connection; 0104 } 0105 0106 void SpaceHierarchyCache::setConnection(NeoChatConnection *connection) 0107 { 0108 if (m_connection == connection) { 0109 return; 0110 } 0111 m_connection = connection; 0112 Q_EMIT connectionChanged(); 0113 m_spaceHierarchy.clear(); 0114 cacheSpaceHierarchy(); 0115 connect(connection, &Connection::joinedRoom, this, &SpaceHierarchyCache::addSpaceToHierarchy); 0116 connect(connection, &Connection::aboutToDeleteRoom, this, &SpaceHierarchyCache::removeSpaceFromHierarchy); 0117 } 0118 0119 #include "moc_spacehierarchycache.cpp"