From db47a4d30e51a5e236c569c630241bef9e16e049 Mon Sep 17 00:00:00 2001
From: Christian <christian-pauly@posteo.de>
Date: Thu, 27 Jun 2019 17:34:15 +0000
Subject: [PATCH] [RoomList] Enhance onlyLeft lists and add more tests.

---
 lib/src/RoomList.dart   |  8 ++++---
 test/RoomList_test.dart | 49 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 54 insertions(+), 3 deletions(-)

diff --git a/lib/src/RoomList.dart b/lib/src/RoomList.dart
index 4a5c928..47bae38 100644
--- a/lib/src/RoomList.dart
+++ b/lib/src/RoomList.dart
@@ -78,9 +78,10 @@ class RoomList {
       if (rooms[j].id == chatUpdate.id) break;
     }
     final bool found = (j < rooms.length - 1 && rooms[j].id == chatUpdate.id);
+    final bool isLeftRoom = chatUpdate.membership == "leave";
 
     // Does the chat already exist in the list rooms?
-    if (!found && chatUpdate.membership != "leave") {
+    if (!found && ((!onlyLeft && !isLeftRoom) || (onlyLeft && isLeftRoom))) {
       num position = chatUpdate.membership == "invite" ? 0 : j;
       // Add the new chat to the list
       Room newRoom = Room(
@@ -93,8 +94,9 @@ class RoomList {
       rooms.insert(position, newRoom);
       if (onInsert != null) onInsert(position);
     }
-    // If the membership is "leave" then remove the item and stop here
-    else if (found && chatUpdate.membership == "leave") {
+    // If the membership is "leave" or not "leave" but onlyLeft=true then remove the item and stop here
+    else if (found &&
+        ((!onlyLeft && isLeftRoom) || (onlyLeft && !isLeftRoom))) {
       rooms.removeAt(j);
       if (onRemove != null) onRemove(j);
     }
diff --git a/test/RoomList_test.dart b/test/RoomList_test.dart
index 60f3287..7ece531 100644
--- a/test/RoomList_test.dart
+++ b/test/RoomList_test.dart
@@ -168,5 +168,54 @@ void main() {
       expect(roomList.rooms[0].lastMessage, "Testcase 2");
       expect(roomList.rooms[0].timeCreated, now);
     });
+
+    test("onlyLeft", () async {
+      final Client client = Client("testclient");
+      client.homeserver = "https://testserver.abc";
+
+      int updateCount = 0;
+      List<int> insertList = [];
+      List<int> removeList = [];
+
+      RoomList roomList = RoomList(
+          client: client,
+          onlyLeft: true,
+          rooms: [],
+          onUpdate: () {
+            updateCount++;
+          },
+          onInsert: (int insertID) {
+            insertList.add(insertID);
+          },
+          onRemove: (int removeID) {
+            insertList.add(removeID);
+          });
+
+      client.connection.onRoomUpdate.add(RoomUpdate(
+        id: "1",
+        membership: "join",
+        notification_count: 2,
+        highlight_count: 1,
+        limitedTimeline: false,
+        prev_batch: "1234",
+      ));
+      client.connection.onRoomUpdate.add(RoomUpdate(
+        id: "2",
+        membership: "leave",
+        notification_count: 2,
+        highlight_count: 1,
+        limitedTimeline: false,
+        prev_batch: "1234",
+      ));
+
+      await new Future.delayed(new Duration(milliseconds: 50));
+
+      expect(roomList.eventSub != null, true);
+      expect(roomList.roomSub != null, true);
+      expect(roomList.rooms[0].id, "2");
+      expect(updateCount, 2);
+      expect(insertList, [0]);
+      expect(removeList, []);
+    });
   });
 }