fix: exception on removed widgets

And widgets without a name.

fixes #267
This commit is contained in:
Nicolas Werner 2022-02-13 23:11:53 +01:00
parent c64e6b9c11
commit 4a52540006
3 changed files with 64 additions and 7 deletions

View File

@ -374,12 +374,13 @@ class Room {
List<MatrixWidget> get widgets => {
...states['m.widget'] ?? {},
...states['im.vector.modular.widgets'] ?? {},
}
.values
.map(
(e) => MatrixWidget.fromJson(e.content, this),
)
.toList();
}.values.expand((e) {
try {
return [MatrixWidget.fromJson(e.content, this)];
} catch (_) {
return <MatrixWidget>[];
}
}).toList();
/// Your current client instance.
final Client client;

View File

@ -5,7 +5,7 @@ class MatrixWidget {
final String? creatorUserId;
final Map<String, dynamic>? data;
final String? id;
final String name;
final String? name;
final String type;
/// use [buildWidgetUrl] instead

View File

@ -923,6 +923,62 @@ void main() {
test('Widgets', () {
expect(room.widgets.isEmpty, true);
room.states['m.widget'] = {
'test': Event.fromJson({
'content': {
'creatorUserId': '@rxl881:matrix.org',
'data': {'title': 'Bridges Dashboard', 'dateRange': '1y'},
'id': 'grafana_@rxl881:matrix.org_1514573757015',
'name': 'Grafana',
'type': 'm.grafana',
'url': 'https://matrix.org/grafana/whatever',
'waitForIframeLoad': true
},
'room_id': '!foo:bar',
'event_id': '\$15104760642668662QICBu:matrix.org',
'sender': '@rxl881:matrix.org',
'state_key': 'test',
'origin_server_ts': 1432735824653,
'type': 'm.widget'
}, room),
};
expect(room.widgets.length, 1);
room.states['m.widget'] = {
'test2': Event.fromJson({
'content': {
'creatorUserId': '@rxl881:matrix.org',
'data': {'title': 'Bridges Dashboard', 'dateRange': '1y'},
'id': 'grafana_@rxl881:matrix.org_1514573757016',
'type': 'm.grafana',
'url': 'https://matrix.org/grafana/whatever',
'waitForIframeLoad': true
},
'room_id': '!foo:bar',
'event_id': '\$15104760642668663QICBu:matrix.org',
'sender': '@rxl881:matrix.org',
'state_key': 'test2',
'origin_server_ts': 1432735824653,
'type': 'm.widget'
}, room),
};
expect(room.widgets.length, 1);
room.states['m.widget'] = {
'test3': Event.fromJson({
'content': {
'creatorUserId': '@rxl881:matrix.org',
'data': {'title': 'Bridges Dashboard', 'dateRange': '1y'},
'type': 'm.grafana',
'waitForIframeLoad': true
},
'room_id': '!foo:bar',
'event_id': '\$15104760642668662QICBu:matrix.org',
'sender': '@rxl881:matrix.org',
'state_key': 'test3',
'origin_server_ts': 1432735824655,
'type': 'm.widget'
}, room),
};
expect(room.widgets.length, 0);
});
test('Spaces', () async {