2020-08-13 08:40:39 +00:00
|
|
|
import '../../famedlysdk.dart';
|
2020-06-03 10:16:01 +00:00
|
|
|
import '../../matrix_api.dart';
|
|
|
|
|
2019-11-20 13:02:23 +00:00
|
|
|
/// Matrix room states are addressed by a tuple of the [type] and an
|
|
|
|
/// optional [stateKey].
|
|
|
|
class StatesMap {
|
2020-01-02 14:09:49 +00:00
|
|
|
Map<String, Map<String, Event>> states = {};
|
2019-11-20 13:02:23 +00:00
|
|
|
|
2020-01-02 14:09:49 +00:00
|
|
|
/// Returns either the [Event] or a map of state_keys to [Event] objects.
|
2019-11-20 13:02:23 +00:00
|
|
|
/// If you just enter a MatrixID, it will try to return the corresponding m.room.member event.
|
|
|
|
dynamic operator [](String key) {
|
2019-11-20 13:42:08 +00:00
|
|
|
//print("[Warning] This method will be depracated in the future!");
|
2020-02-14 13:16:26 +00:00
|
|
|
if (key == null) return null;
|
2020-03-30 09:08:38 +00:00
|
|
|
if (key.startsWith('@') && key.contains(':')) {
|
2020-06-03 10:16:01 +00:00
|
|
|
if (!states.containsKey(EventTypes.RoomMember)) {
|
|
|
|
states[EventTypes.RoomMember] = {};
|
|
|
|
}
|
|
|
|
return states[EventTypes.RoomMember][key];
|
2019-11-20 13:02:23 +00:00
|
|
|
}
|
|
|
|
if (!states.containsKey(key)) states[key] = {};
|
2020-03-30 09:08:38 +00:00
|
|
|
if (states[key][''] is Event) {
|
|
|
|
return states[key][''];
|
2020-01-02 14:33:26 +00:00
|
|
|
} else if (states[key].isEmpty) {
|
2019-11-20 13:02:23 +00:00
|
|
|
return null;
|
2020-01-02 14:33:26 +00:00
|
|
|
} else {
|
2019-11-20 13:02:23 +00:00
|
|
|
return states[key];
|
2020-01-02 14:33:26 +00:00
|
|
|
}
|
2019-11-20 13:02:23 +00:00
|
|
|
}
|
|
|
|
|
2020-01-02 14:09:49 +00:00
|
|
|
void operator []=(String key, Event val) {
|
2019-11-20 13:42:08 +00:00
|
|
|
//print("[Warning] This method will be depracated in the future!");
|
2020-03-30 09:08:38 +00:00
|
|
|
if (key.startsWith('@') && key.contains(':')) {
|
2020-06-03 10:16:01 +00:00
|
|
|
if (!states.containsKey(EventTypes.RoomMember)) {
|
|
|
|
states[EventTypes.RoomMember] = {};
|
|
|
|
}
|
|
|
|
states[EventTypes.RoomMember][key] = val;
|
2019-11-20 13:02:23 +00:00
|
|
|
}
|
|
|
|
if (!states.containsKey(key)) states[key] = {};
|
2020-03-30 09:08:38 +00:00
|
|
|
states[key][val.stateKey ?? ''] = val;
|
2019-11-20 13:02:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool containsKey(String key) => states.containsKey(key);
|
|
|
|
|
|
|
|
void forEach(f) => states.forEach(f);
|
|
|
|
}
|