summary refs log tree commit diff
path: root/src/Cache_p.h
diff options
context:
space:
mode:
authorNicolas Werner <nicolas.werner@hotmail.de>2021-06-16 00:09:45 +0200
committerNicolas Werner <nicolas.werner@hotmail.de>2021-06-25 11:34:17 +0200
commit12ce7686ce8a7cae411c280d30a12934b8707550 (patch)
tree3c5e769ed366c53afdaaed155f08f39ea06e8e94 /src/Cache_p.h
parentMerge pull request #611 from balsoft/allow-edits-of-pending-messages (diff)
downloadnheko-12ce7686ce8a7cae411c280d30a12934b8707550.tar.xz
Show some spaces in the community sidebar
Diffstat (limited to 'src/Cache_p.h')
-rw-r--r--src/Cache_p.h72
1 files changed, 61 insertions, 11 deletions
diff --git a/src/Cache_p.h b/src/Cache_p.h
index 669f1895..064f4882 100644
--- a/src/Cache_p.h
+++ b/src/Cache_p.h
@@ -72,6 +72,7 @@ public:
         std::optional<mtx::events::state::CanonicalAlias> getRoomAliases(const std::string &roomid);
         QHash<QString, RoomInfo> invites();
         std::optional<RoomInfo> invite(std::string_view roomid);
+        QMap<QString, std::optional<RoomInfo>> spaces();
 
         //! Calculate & return the name of the room.
         QString getRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
@@ -84,6 +85,8 @@ public:
         QString getRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
         //! Retrieve the version of the room if any.
         QString getRoomVersion(lmdb::txn &txn, lmdb::dbi &statesdb);
+        //! Retrieve if the room is a space
+        bool getRoomIsSpace(lmdb::txn &txn, lmdb::dbi &statesdb);
 
         //! Get a specific state event
         template<typename T>
@@ -146,7 +149,6 @@ public:
 
         RoomInfo singleRoomInfo(const std::string &room_id);
         std::vector<std::string> roomsWithStateUpdates(const mtx::responses::Sync &res);
-        std::vector<std::string> roomsWithTagUpdates(const mtx::responses::Sync &res);
         std::map<QString, RoomInfo> getRoomInfo(const std::vector<std::string> &rooms);
 
         //! Calculates which the read status of a room.
@@ -154,9 +156,6 @@ public:
         bool calculateRoomReadStatus(const std::string &room_id);
         void calculateRoomReadStatus();
 
-        std::vector<RoomSearchResult> searchRooms(const std::string &query,
-                                                  std::uint8_t max_items = 5);
-
         void markSentNotification(const std::string &event_id);
         //! Removes an event from the sent notifications.
         void removeReadNotification(const std::string &event_id);
@@ -222,6 +221,8 @@ public:
         void deleteOldData() noexcept;
         //! Retrieve all saved room ids.
         std::vector<std::string> getRoomIds(lmdb::txn &txn);
+        std::vector<std::string> getParentRoomIds(const std::string &room_id);
+        std::vector<std::string> getChildRoomIds(const std::string &room_id);
 
         //! Mark a room that uses e2e encryption.
         void setEncryptedRoom(lmdb::txn &txn, const std::string &room_id);
@@ -327,6 +328,7 @@ private:
         QString getInviteRoomName(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
         QString getInviteRoomTopic(lmdb::txn &txn, lmdb::dbi &statesdb);
         QString getInviteRoomAvatarUrl(lmdb::txn &txn, lmdb::dbi &statesdb, lmdb::dbi &membersdb);
+        bool getInviteRoomIsSpace(lmdb::txn &txn, lmdb::dbi &db);
 
         std::optional<MemberInfo> getMember(const std::string &room_id, const std::string &user_id);
 
@@ -430,20 +432,22 @@ private:
 
                 if (room_id.empty())
                         return std::nullopt;
+                const auto typeStr = to_string(type);
 
                 std::string_view value;
                 if (state_key.empty()) {
                         auto db = getStatesDb(txn, room_id);
-                        if (!db.get(txn, to_string(type), value)) {
+                        if (!db.get(txn, typeStr, value)) {
                                 return std::nullopt;
                         }
                 } else {
-                        auto db               = getStatesKeyDb(txn, room_id);
-                        std::string d         = json::object({{"key", state_key}}).dump();
-                        std::string_view data = d;
+                        auto db                   = getStatesKeyDb(txn, room_id);
+                        std::string d             = json::object({{"key", state_key}}).dump();
+                        std::string_view data     = d;
+                        std::string_view typeStrV = typeStr;
 
                         auto cursor = lmdb::cursor::open(txn, db);
-                        if (!cursor.get(state_key, data, MDB_GET_BOTH))
+                        if (!cursor.get(typeStrV, data, MDB_GET_BOTH))
                                 return std::nullopt;
 
                         try {
@@ -463,6 +467,47 @@ private:
                 }
         }
 
+        template<typename T>
+        std::vector<mtx::events::StateEvent<T>> getStateEventsWithType(lmdb::txn &txn,
+                                                                       const std::string &room_id)
+
+        {
+                constexpr auto type = mtx::events::state_content_to_type<T>;
+                static_assert(type != mtx::events::EventType::Unsupported,
+                              "Not a supported type in state events.");
+
+                if (room_id.empty())
+                        return {};
+
+                std::vector<mtx::events::StateEvent<T>> events;
+
+                {
+                        auto db                   = getStatesKeyDb(txn, room_id);
+                        auto eventsDb             = getEventsDb(txn, room_id);
+                        const auto typeStr        = to_string(type);
+                        std::string_view typeStrV = typeStr;
+                        std::string_view data;
+                        std::string_view value;
+
+                        auto cursor = lmdb::cursor::open(txn, db);
+                        bool first  = true;
+                        if (cursor.get(typeStrV, data, MDB_SET)) {
+                                while (cursor.get(
+                                  typeStrV, data, first ? MDB_FIRST_DUP : MDB_NEXT_DUP)) {
+                                        first = false;
+
+                                        if (eventsDb.get(txn,
+                                                         json::parse(data)["id"].get<std::string>(),
+                                                         value))
+                                                events.push_back(
+                                                  json::parse(value)
+                                                    .get<mtx::events::StateEvent<T>>());
+                                }
+                        }
+                }
+
+                return events;
+        }
         void saveInvites(lmdb::txn &txn,
                          const std::map<std::string, mtx::responses::InvitedRoom> &rooms);
 
@@ -482,6 +527,10 @@ private:
                 }
         }
 
+        void updateSpaces(lmdb::txn &txn,
+                          const std::set<std::string> &spaces_with_updates,
+                          std::set<std::string> rooms_with_updates);
+
         lmdb::dbi getPendingReceiptsDb(lmdb::txn &txn)
         {
                 return lmdb::dbi::open(txn, "pending_receipts", MDB_CREATE);
@@ -548,8 +597,8 @@ private:
 
         lmdb::dbi getStatesKeyDb(lmdb::txn &txn, const std::string &room_id)
         {
-                auto db =
-                  lmdb::dbi::open(txn, std::string(room_id + "/state_by_key").c_str(), MDB_CREATE);
+                auto db = lmdb::dbi::open(
+                  txn, std::string(room_id + "/state_by_key").c_str(), MDB_CREATE | MDB_DUPSORT);
                 lmdb::dbi_set_dupsort(txn, db, compare_state_key);
                 return db;
         }
@@ -611,6 +660,7 @@ private:
         lmdb::env env_;
         lmdb::dbi syncStateDb_;
         lmdb::dbi roomsDb_;
+        lmdb::dbi spacesChildrenDb_, spacesParentsDb_;
         lmdb::dbi invitesDb_;
         lmdb::dbi readReceiptsDb_;
         lmdb::dbi notificationsDb_;