From 58c43b0cd4cbd6bee67dfc2d2160ed3e081e209f Mon Sep 17 00:00:00 2001 From: James Graham Date: Wed, 21 Feb 2024 23:52:10 +0000 Subject: [PATCH] Make sure that the room isn't already in the model before appending. Make sure that the room isn't already in the model before appending. Move it if it's in the wrong catgeory --- src/models/roomtreemodel.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/models/roomtreemodel.cpp b/src/models/roomtreemodel.cpp index 220fd0b92..521e14f0a 100644 --- a/src/models/roomtreemodel.cpp +++ b/src/models/roomtreemodel.cpp @@ -58,6 +58,16 @@ void RoomTreeModel::newRoom(Room *r) { const auto room = dynamic_cast(r); const auto type = NeoChatRoomType::typeForRoom(room); + // Check if the room is already in the model. + const auto checkRoomIndex = indexForRoom(room); + if (checkRoomIndex.isValid()) { + // If the room is in the wrong type category for whatever reason, move it. + if (checkRoomIndex.parent().row() != type) { + moveRoom(room); + } + return; + } + beginInsertRows(index(type, 0), m_rooms[type].size(), m_rooms[type].size()); m_rooms[type].append(room); connectRoomSignals(room); @@ -315,11 +325,18 @@ QModelIndex RoomTreeModel::indexForRoom(NeoChatRoom *room) const return {}; } + // Try and find by checking type. const auto type = NeoChatRoomType::typeForRoom(room); auto row = m_rooms[type].indexOf(room); if (row >= 0) { return index(row, 0, index(type, 0)); } + // Double check that the room isn't in the wrong category. + for (const auto &key : m_rooms.keys()) { + if (m_rooms[key].contains(room)) { + return index(m_rooms[key].indexOf(room), 0, index(key, 0)); + } + } return {}; }