diff --git a/lib/src/room.dart b/lib/src/room.dart index eea53852..aa03cb1c 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -368,8 +368,7 @@ class Room { /// Returns all present Widgets in the room. List get widgets => { - ...states['m.widget'] ?? {}, - ...states['im.vector.modular.widgets'] ?? {}, + ...states['m.widget'] ?? states['im.vector.modular.widgets'] ?? {}, }.values.expand((e) { try { return [MatrixWidget.fromJson(e.content, this)]; @@ -378,6 +377,31 @@ class Room { } }).toList(); + Future addWidget(MatrixWidget widget) { + final user = client.userID; + final widgetId = + widget.name!.toLowerCase().replaceAll(RegExp(r'\W'), '_') + '_' + user!; + + final json = widget.toJson(); + json['creatorUserId'] = user; + json['id'] = widgetId; + return client.setRoomStateWithKey( + id, + 'im.vector.modular.widgets', + widgetId, + json, + ); + } + + Future deleteWidget(String widgetId) { + return client.setRoomStateWithKey( + id, + 'im.vector.modular.widgets', + widgetId, + {}, + ); + } + /// Your current client instance. final Client client; diff --git a/lib/widget.dart b/lib/widget.dart index 22fae796..bc97ec30 100644 --- a/lib/widget.dart +++ b/lib/widget.dart @@ -38,6 +38,55 @@ class MatrixWidget { : false, ); + /// creates an `m.etherpad` [MatrixWidget] + factory MatrixWidget.etherpad(Room room, String name, Uri url) => + MatrixWidget( + room: room, + name: name, + type: 'm.etherpad', + url: url.toString(), + data: { + 'url': url.toString(), + }, + ); + + /// creates an `m.jitsi` [MatrixWidget] + factory MatrixWidget.jitsi(Room room, String name, Uri url, + {bool isAudioOnly = false}) => + MatrixWidget( + room: room, + name: name, + type: 'm.jitsi', + url: url.toString(), + data: { + 'domain': url.host, + 'conferenceId': url.pathSegments.last, + 'isAudioOnly': isAudioOnly, + }, + ); + + /// creates an `m.video` [MatrixWidget] + factory MatrixWidget.video(Room room, String name, Uri url) => MatrixWidget( + room: room, + name: name, + type: 'm.video', + url: url.toString(), + data: { + 'url': url.toString(), + }, + ); + + /// creates an `m.custom` [MatrixWidget] + factory MatrixWidget.custom(Room room, String name, Uri url) => MatrixWidget( + room: room, + name: name, + type: 'm.custom', + url: url.toString(), + data: { + 'url': url.toString(), + }, + ); + Future buildWidgetUrl() async { // See https://github.com/matrix-org/matrix-doc/issues/1236 for a // description, specifically the section @@ -66,4 +115,14 @@ class MatrixWidget { return Uri.parse(parsedUri); } + + Map toJson() => { + 'creatorUserId': creatorUserId, + 'data': data, + 'id': id, + 'name': name, + 'type': type, + 'url': url, + 'waitForIframeLoad': waitForIframeLoad, + }; }