[Room] Fix state handling
This commit is contained in:
		
							parent
							
								
									809adf4911
								
							
						
					
					
						commit
						4ff3146d63
					
				|  | @ -81,11 +81,12 @@ class Room { | ||||||
|   RoomState getState(String typeKey, [String stateKey = ""]) => |   RoomState getState(String typeKey, [String stateKey = ""]) => | ||||||
|       states.states[typeKey] != null ? states.states[typeKey][stateKey] : null; |       states.states[typeKey] != null ? states.states[typeKey][stateKey] : null; | ||||||
| 
 | 
 | ||||||
|   /// Sets the [RoomState] for the given [typeKey] and optional [stateKey]. |   /// Adds the [state] to this room and overwrites a state with the same | ||||||
|   /// If no [stateKey] is provided, it defaults to an empty string. |   /// typeKey/stateKey key pair if there is one. | ||||||
|   void setState(RoomState state, String typeKey, [String stateKey = ""]) { |   void setState(RoomState state) { | ||||||
|     if (!states.states.containsKey(typeKey)) states.states[typeKey] = {}; |     if (!states.states.containsKey(state.typeKey)) | ||||||
|     states.states[typeKey][stateKey] = state; |       states.states[state.typeKey] = {}; | ||||||
|  |     states.states[state.typeKey][state.stateKey ?? ""] = state; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /// ID of the fully read marker event. |   /// ID of the fully read marker event. | ||||||
|  | @ -167,7 +168,7 @@ class Room { | ||||||
| 
 | 
 | ||||||
|   Event get lastEvent { |   Event get lastEvent { | ||||||
|     ChatTime lastTime = ChatTime(0); |     ChatTime lastTime = ChatTime(0); | ||||||
|     Event lastEvent = states["m.room.message"]?.timelineEvent; |     Event lastEvent = getState("m.room.message")?.timelineEvent; | ||||||
|     if (lastEvent == null) |     if (lastEvent == null) | ||||||
|       states.forEach((final String key, final entry) { |       states.forEach((final String key, final entry) { | ||||||
|         if (!entry.containsKey("")) return; |         if (!entry.containsKey("")) return; | ||||||
|  | @ -636,15 +637,13 @@ class Room { | ||||||
|       roomAccountData: {}, |       roomAccountData: {}, | ||||||
|     ); |     ); | ||||||
| 
 | 
 | ||||||
|     Map<String, RoomState> newStates = {}; |     StatesMap newStates = StatesMap(); | ||||||
|     if (states != null) { |     if (states != null) { | ||||||
|       List<Map<String, dynamic>> rawStates = await states; |       List<Map<String, dynamic>> rawStates = await states; | ||||||
|       for (int i = 0; i < rawStates.length; i++) { |       for (int i = 0; i < rawStates.length; i++) { | ||||||
|         RoomState newState = RoomState.fromJson(rawStates[i], newRoom); |         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 = {}; |     Map<String, RoomAccountData> newRoomAccountData = {}; | ||||||
|  |  | ||||||
|  | @ -180,9 +180,10 @@ class RoomList { | ||||||
|         eventUpdate.type == "state" || |         eventUpdate.type == "state" || | ||||||
|         eventUpdate.type == "invite_state") { |         eventUpdate.type == "invite_state") { | ||||||
|       RoomState stateEvent = RoomState.fromJson(eventUpdate.content, rooms[j]); |       RoomState stateEvent = RoomState.fromJson(eventUpdate.content, rooms[j]); | ||||||
|       if (rooms[j].states[stateEvent.key] != null && |       RoomState prevState = | ||||||
|           rooms[j].states[stateEvent.key].time > stateEvent.time) return; |           rooms[j].getState(stateEvent.typeKey, stateEvent.stateKey); | ||||||
|       rooms[j].states[stateEvent.key] = stateEvent; |       if (prevState != null && prevState.time > stateEvent.time) return; | ||||||
|  |       rooms[j].setState(stateEvent); | ||||||
|     } else if (eventUpdate.type == "account_data") { |     } else if (eventUpdate.type == "account_data") { | ||||||
|       rooms[j].roomAccountData[eventUpdate.eventType] = |       rooms[j].roomAccountData[eventUpdate.eventType] = | ||||||
|           RoomAccountData.fromJson(eventUpdate.content, rooms[j]); |           RoomAccountData.fromJson(eventUpdate.content, rooms[j]); | ||||||
|  |  | ||||||
|  | @ -124,6 +124,7 @@ class RoomState { | ||||||
| 
 | 
 | ||||||
|   /// The unique key of this event. For events with a [stateKey], it will be the |   /// 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. |   /// stateKey. Otherwise it will be the [type] as a string. | ||||||
|  |   @deprecated | ||||||
|   String get key => stateKey == null || stateKey.isEmpty ? typeKey : stateKey; |   String get key => stateKey == null || stateKey.isEmpty ? typeKey : stateKey; | ||||||
| 
 | 
 | ||||||
|   User get asUser => User.fromState( |   User get asUser => User.fromState( | ||||||
|  |  | ||||||
|  | @ -66,7 +66,6 @@ void main() { | ||||||
|       RoomState state = RoomState.fromJson(jsonObj, null); |       RoomState state = RoomState.fromJson(jsonObj, null); | ||||||
|       expect(state.eventId, id); |       expect(state.eventId, id); | ||||||
|       expect(state.stateKey, ""); |       expect(state.stateKey, ""); | ||||||
|       expect(state.key, "m.room.message"); |  | ||||||
|       expect(state.timelineEvent.status, 1); |       expect(state.timelineEvent.status, 1); | ||||||
|     }); |     }); | ||||||
|     test("Test all EventTypes", () async { |     test("Test all EventTypes", () async { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue