feat: implement mofifying widgets
- add predefined MatrixWidget factories - allow to create widgets - allow to delete widgets Signed-off-by: TheOneWithTheBraid <the-one@with-the-braid.cf>
This commit is contained in:
parent
b5336cf589
commit
c362ead49a
|
|
@ -368,8 +368,7 @@ class Room {
|
||||||
|
|
||||||
/// Returns all present Widgets in the room.
|
/// Returns all present Widgets in the room.
|
||||||
List<MatrixWidget> get widgets => {
|
List<MatrixWidget> get widgets => {
|
||||||
...states['m.widget'] ?? {},
|
...states['m.widget'] ?? states['im.vector.modular.widgets'] ?? {},
|
||||||
...states['im.vector.modular.widgets'] ?? {},
|
|
||||||
}.values.expand((e) {
|
}.values.expand((e) {
|
||||||
try {
|
try {
|
||||||
return [MatrixWidget.fromJson(e.content, this)];
|
return [MatrixWidget.fromJson(e.content, this)];
|
||||||
|
|
@ -378,6 +377,31 @@ class Room {
|
||||||
}
|
}
|
||||||
}).toList();
|
}).toList();
|
||||||
|
|
||||||
|
Future<String> 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<String> deleteWidget(String widgetId) {
|
||||||
|
return client.setRoomStateWithKey(
|
||||||
|
id,
|
||||||
|
'im.vector.modular.widgets',
|
||||||
|
widgetId,
|
||||||
|
{},
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// Your current client instance.
|
/// Your current client instance.
|
||||||
final Client client;
|
final Client client;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,6 +38,55 @@ class MatrixWidget {
|
||||||
: false,
|
: 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<Uri> buildWidgetUrl() async {
|
Future<Uri> buildWidgetUrl() async {
|
||||||
// See https://github.com/matrix-org/matrix-doc/issues/1236 for a
|
// See https://github.com/matrix-org/matrix-doc/issues/1236 for a
|
||||||
// description, specifically the section
|
// description, specifically the section
|
||||||
|
|
@ -66,4 +115,14 @@ class MatrixWidget {
|
||||||
|
|
||||||
return Uri.parse(parsedUri);
|
return Uri.parse(parsedUri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Map<String, dynamic> toJson() => {
|
||||||
|
'creatorUserId': creatorUserId,
|
||||||
|
'data': data,
|
||||||
|
'id': id,
|
||||||
|
'name': name,
|
||||||
|
'type': type,
|
||||||
|
'url': url,
|
||||||
|
'waitForIframeLoad': waitForIframeLoad,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue