famedlysdk/lib/src/utils/StatesMap.dart

39 lines
1.3 KiB
Dart
Raw Normal View History

2019-11-20 13:02:23 +00:00
import 'package:famedlysdk/famedlysdk.dart';
/// 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) {
//print("[Warning] This method will be depracated in the future!");
2019-11-20 13:02:23 +00:00
if (key.startsWith("@") && key.contains(":")) {
if (!states.containsKey("m.room.member")) states["m.room.member"] = {};
return states["m.room.member"][key];
}
if (!states.containsKey(key)) states[key] = {};
2020-01-02 14:09:49 +00:00
if (states[key][""] is Event)
2019-11-20 13:02:23 +00:00
return states[key][""];
else if (states[key].length == 0)
return null;
else
return states[key];
}
2020-01-02 14:09:49 +00:00
void operator []=(String key, Event val) {
//print("[Warning] This method will be depracated in the future!");
2019-11-20 13:02:23 +00:00
if (key.startsWith("@") && key.contains(":")) {
if (!states.containsKey("m.room.member")) states["m.room.member"] = {};
states["m.room.member"][key] = val;
}
if (!states.containsKey(key)) states[key] = {};
states[key][val.stateKey ?? ""] = val;
}
bool containsKey(String key) => states.containsKey(key);
void forEach(f) => states.forEach(f);
}