[Room] Fix state handling

This commit is contained in:
Christian Pauly 2019-11-21 14:10:24 +00:00
parent 809adf4911
commit 4ff3146d63
4 changed files with 14 additions and 14 deletions

View File

@ -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<String, RoomState> newStates = {};
StatesMap newStates = StatesMap();
if (states != null) {
List<Map<String, dynamic>> 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<String, RoomAccountData> newRoomAccountData = {};

View File

@ -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]);

View File

@ -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(

View File

@ -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 {