summary refs log tree commit diff
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-22 12:26:41 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-22 12:26:41 +0300
commitc0e355f485b3aef8feaa8f2061b0eda9ded67a0a (patch)
tree5f4bc671ba394014b798429099fae16212ab6bc8
parentStrip whitespace from room names for QuickSwitcher (diff)
downloadnheko-c0e355f485b3aef8feaa8f2061b0eda9ded67a0a.tar.xz
Properly clean stale invites
-rw-r--r--include/Cache.h7
-rw-r--r--src/Cache.cc17
2 files changed, 21 insertions, 3 deletions
diff --git a/include/Cache.h b/include/Cache.h
index 9a151aba..78a35a65 100644
--- a/include/Cache.h
+++ b/include/Cache.h
@@ -155,6 +155,7 @@ public:
 
         void deleteData();
 
+        void removeInvite(lmdb::txn &txn, const std::string &room_id);
         void removeInvite(const std::string &room_id);
         void removeRoom(lmdb::txn &txn, const std::string &roomid);
         void removeRoom(const std::string &roomid);
@@ -335,8 +336,12 @@ private:
         void removeLeftRooms(lmdb::txn &txn,
                              const std::map<std::string, mtx::responses::LeftRoom> &rooms)
         {
-                for (const auto &room : rooms)
+                for (const auto &room : rooms) {
                         removeRoom(txn, room.first);
+
+                        // Clean up leftover invites.
+                        removeInvite(txn, room.first);
+                }
         }
 
         lmdb::dbi getInviteStatesDb(lmdb::txn &txn, const std::string &room_id)
diff --git a/src/Cache.cc b/src/Cache.cc
index 4dadff06..2e210f6b 100644
--- a/src/Cache.cc
+++ b/src/Cache.cc
@@ -39,7 +39,7 @@ static const lmdb::val CACHE_FORMAT_VERSION_KEY("cache_format_version");
 //! Contains UI information for the joined rooms. (i.e name, topic, avatar url etc).
 //! Format: room_id -> RoomInfo
 static constexpr const char *ROOMS_DB   = "rooms";
-static constexpr const char *INVITES_DB = "rooms";
+static constexpr const char *INVITES_DB = "invites";
 //! Keeps already downloaded media for reuse.
 //! Format: matrix_url -> binary data.
 static constexpr const char *MEDIA_DB = "media";
@@ -170,10 +170,18 @@ Cache::image(const QString &url) const
 }
 
 void
+Cache::removeInvite(lmdb::txn &txn, const std::string &room_id)
+{
+        lmdb::dbi_del(txn, invitesDb_, lmdb::val(room_id), nullptr);
+        lmdb::dbi_drop(txn, getInviteStatesDb(txn, room_id), true);
+        lmdb::dbi_drop(txn, getInviteMembersDb(txn, room_id), true);
+}
+
+void
 Cache::removeInvite(const std::string &room_id)
 {
         auto txn = lmdb::txn::begin(env_);
-        lmdb::dbi_del(txn, invitesDb_, lmdb::val(room_id), nullptr);
+        removeInvite(txn, room_id);
         txn.commit();
 }
 
@@ -181,6 +189,8 @@ void
 Cache::removeRoom(lmdb::txn &txn, const std::string &roomid)
 {
         lmdb::dbi_del(txn, roomsDb_, lmdb::val(roomid), nullptr);
+        lmdb::dbi_drop(txn, getStatesDb(txn, roomid), true);
+        lmdb::dbi_drop(txn, getMembersDb(txn, roomid), true);
 }
 
 void
@@ -386,6 +396,9 @@ Cache::saveState(const mtx::responses::Sync &res)
                   txn, roomsDb_, lmdb::val(room.first), lmdb::val(json(updatedInfo).dump()));
 
                 updateReadReceipt(txn, room.first, room.second.ephemeral.receipts);
+
+                // Clean up non-valid invites.
+                removeInvite(txn, room.first);
         }
 
         saveInvites(txn, res.rooms.invite);