diff --git a/lib/src/Room.dart b/lib/src/Room.dart index 67178d7..284bb3c 100644 --- a/lib/src/Room.dart +++ b/lib/src/Room.dart @@ -81,11 +81,12 @@ class Room { RoomState getState(String typeKey, [String stateKey = ""]) => states.states[typeKey] != null ? states.states[typeKey][stateKey] : null; - /// Sets the [RoomState] for the given [typeKey] and optional [stateKey]. - /// If no [stateKey] is provided, it defaults to an empty string. - void setState(RoomState state, String typeKey, [String stateKey = ""]) { - if (!states.states.containsKey(typeKey)) states.states[typeKey] = {}; - states.states[typeKey][stateKey] = state; + /// Adds the [state] to this room and overwrites a state with the same + /// typeKey/stateKey key pair if there is one. + void setState(RoomState state) { + if (!states.states.containsKey(state.typeKey)) + states.states[state.typeKey] = {}; + states.states[state.typeKey][state.stateKey ?? ""] = state; } /// ID of the fully read marker event. @@ -167,7 +168,7 @@ class Room { Event get lastEvent { ChatTime lastTime = ChatTime(0); - Event lastEvent = states["m.room.message"]?.timelineEvent; + Event lastEvent = getState("m.room.message")?.timelineEvent; if (lastEvent == null) states.forEach((final String key, final entry) { if (!entry.containsKey("")) return; @@ -636,15 +637,13 @@ class Room { roomAccountData: {}, ); - Map newStates = {}; + StatesMap newStates = StatesMap(); if (states != null) { List> rawStates = await states; for (int i = 0; i < rawStates.length; i++) { RoomState newState = RoomState.fromJson(rawStates[i], newRoom); - newStates[newState.key] = newState; + newStates.states[newState.typeKey][newState.stateKey] = newState; } - for (var entry in newStates.entries) - newRoom.states[entry.key] = entry.value; } Map newRoomAccountData = {}; diff --git a/lib/src/RoomList.dart b/lib/src/RoomList.dart index d04f869..2882e30 100644 --- a/lib/src/RoomList.dart +++ b/lib/src/RoomList.dart @@ -180,9 +180,10 @@ class RoomList { eventUpdate.type == "state" || eventUpdate.type == "invite_state") { RoomState stateEvent = RoomState.fromJson(eventUpdate.content, rooms[j]); - if (rooms[j].states[stateEvent.key] != null && - rooms[j].states[stateEvent.key].time > stateEvent.time) return; - rooms[j].states[stateEvent.key] = stateEvent; + RoomState prevState = + rooms[j].getState(stateEvent.typeKey, stateEvent.stateKey); + if (prevState != null && prevState.time > stateEvent.time) return; + rooms[j].setState(stateEvent); } else if (eventUpdate.type == "account_data") { rooms[j].roomAccountData[eventUpdate.eventType] = RoomAccountData.fromJson(eventUpdate.content, rooms[j]); diff --git a/lib/src/RoomState.dart b/lib/src/RoomState.dart index b6548ff..50791df 100644 --- a/lib/src/RoomState.dart +++ b/lib/src/RoomState.dart @@ -124,6 +124,7 @@ class RoomState { /// The unique key of this event. For events with a [stateKey], it will be the /// stateKey. Otherwise it will be the [type] as a string. + @deprecated String get key => stateKey == null || stateKey.isEmpty ? typeKey : stateKey; User get asUser => User.fromState( diff --git a/test/Event_test.dart b/test/Event_test.dart index c74ac44..6eeee16 100644 --- a/test/Event_test.dart +++ b/test/Event_test.dart @@ -66,7 +66,6 @@ void main() { RoomState state = RoomState.fromJson(jsonObj, null); expect(state.eventId, id); expect(state.stateKey, ""); - expect(state.key, "m.room.message"); expect(state.timelineEvent.status, 1); }); test("Test all EventTypes", () async {