Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
268 changes: 134 additions & 134 deletions lib/msc_extensions/msc_1236_widgets/src/widget.dart
Original file line number Diff line number Diff line change
@@ -1,134 +1,134 @@
import 'package:matrix/src/room.dart';
class MatrixWidget {
final Room room;
final String? creatorUserId;
final Map<String, dynamic>? data;
final String? id;
final String? name;
final String type;
/// use [buildWidgetUrl] instead
final String url;
final bool waitForIframeLoad;
MatrixWidget({
required this.room,
this.creatorUserId,
this.data = const {},
this.id,
required this.name,
required this.type,
required this.url,
this.waitForIframeLoad = false,
});
factory MatrixWidget.fromJson(Map<String, dynamic> json, Room room) =>
MatrixWidget(
room: room,
creatorUserId:
json.containsKey('creatorUserId') ? json['creatorUserId'] : null,
data: json.containsKey('data') ? json['data'] : {},
id: json.containsKey('id') ? json['id'] : null,
name: json['name'],
type: json['type'],
url: json['url'],
waitForIframeLoad: json.containsKey('waitForIframeLoad')
? json['waitForIframeLoad']
: 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 {
// See https://github.com/matrix-org/matrix-doc/issues/1236 for a
// description, specifically the section
// `What does the other stuff in content mean?`
final userProfile = await room.client.getUserProfile(room.client.userID!);
var parsedUri = url;
// a key-value map with the strings to be replaced
final replaceMap = {
r'$matrix_user_id': room.client.userID!,
r'$matrix_room_id': room.id,
r'$matrix_display_name': userProfile.displayname ?? '',
r'$matrix_avatar_url': userProfile.avatarUrl?.toString() ?? '',
// removing potentially dangerous keys containing anything but
// `[a-zA-Z0-9_-]` as well as non string values
if (data != null)
...Map.from(data!)
..removeWhere(
(key, value) =>
!RegExp(r'^[\w-]+$').hasMatch(key) || !value is String,
)
..map((key, value) => MapEntry('\$key', value)),
};
replaceMap.forEach((key, value) {
parsedUri = parsedUri.replaceAll(key, Uri.encodeComponent(value));
});
return Uri.parse(parsedUri);
}
Map<String, dynamic> toJson() => {
'creatorUserId': creatorUserId,
'data': data,
'id': id,
'name': name,
'type': type,
'url': url,
'waitForIframeLoad': waitForIframeLoad,
};
}
import 'package:matrix/src/room.dart';

class MatrixWidget {
final Room room;
final String? creatorUserId;
final Map<String, dynamic>? data;
final String? id;
final String? name;
final String type;

/// use [buildWidgetUrl] instead
final String url;
final bool waitForIframeLoad;

MatrixWidget({
required this.room,
this.creatorUserId,
this.data = const {},
this.id,
required this.name,
required this.type,
required this.url,
this.waitForIframeLoad = false,
});

factory MatrixWidget.fromJson(Map<String, dynamic> json, Room room) =>
MatrixWidget(
room: room,
creatorUserId:
json.containsKey('creatorUserId') ? json['creatorUserId'] : null,
data: json.containsKey('data') ? json['data'] : {},
id: json.containsKey('id') ? json['id'] : null,
name: json['name'],
type: json['type'],
url: json['url'],
waitForIframeLoad: json.containsKey('waitForIframeLoad')
? json['waitForIframeLoad']
: false,
);

/// creates an `m.etherpad` [MatrixWidget]
factory MatrixWidget.etherpad(Room room, String name, Uri url) =>
MatrixWidget(

Check warning on line 43 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L42-L43

Added lines #L42 - L43 were not covered by tests
room: room,
name: name,
type: 'm.etherpad',
url: url.toString(),
data: {
'url': url.toString(),

Check warning on line 49 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L47-L49

Added lines #L47 - L49 were not covered by tests
},
);

/// creates an `m.jitsi` [MatrixWidget]
factory MatrixWidget.jitsi(

Check warning on line 54 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L54

Added line #L54 was not covered by tests
Room room,
String name,
Uri url, {
bool isAudioOnly = false,
}) =>
MatrixWidget(

Check warning on line 60 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L60

Added line #L60 was not covered by tests
room: room,
name: name,
type: 'm.jitsi',
url: url.toString(),
data: {
'domain': url.host,
'conferenceId': url.pathSegments.last,

Check warning on line 67 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L64-L67

Added lines #L64 - L67 were not covered by tests
'isAudioOnly': isAudioOnly,
},
);

/// creates an `m.video` [MatrixWidget]
factory MatrixWidget.video(Room room, String name, Uri url) => MatrixWidget(

Check warning on line 73 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L73

Added line #L73 was not covered by tests
room: room,
name: name,
type: 'm.video',
url: url.toString(),
data: {
'url': url.toString(),

Check warning on line 79 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L77-L79

Added lines #L77 - L79 were not covered by tests
},
);

/// creates an `m.custom` [MatrixWidget]
factory MatrixWidget.custom(Room room, String name, Uri url) => MatrixWidget(

Check warning on line 84 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L84

Added line #L84 was not covered by tests
room: room,
name: name,
type: 'm.custom',
url: url.toString(),
data: {
'url': url.toString(),

Check warning on line 90 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L88-L90

Added lines #L88 - L90 were not covered by tests
},
);

Future<Uri> buildWidgetUrl() async {

Check warning on line 94 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L94

Added line #L94 was not covered by tests
// See https://github.com/matrix-org/matrix-doc/issues/1236 for a
// description, specifically the section
// `What does the other stuff in content mean?`
final userProfile = await room.client.getUserProfile(room.client.userID!);
var parsedUri = url;

Check warning on line 99 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L98-L99

Added lines #L98 - L99 were not covered by tests

// a key-value map with the strings to be replaced
final replaceMap = {
r'$matrix_user_id': room.client.userID!,
r'$matrix_room_id': room.id,
r'$matrix_display_name': userProfile.displayname ?? '',
r'$matrix_avatar_url': userProfile.avatarUrl?.toString() ?? '',

Check warning on line 106 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L102-L106

Added lines #L102 - L106 were not covered by tests
// removing potentially dangerous keys containing anything but
// `[a-zA-Z0-9_-]` as well as non string values
if (data != null)
...Map.from(data!)
..removeWhere(
(key, value) =>
!RegExp(r'^[\w-]+$').hasMatch(key) || !value is String,

Check warning on line 113 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L109-L113

Added lines #L109 - L113 were not covered by tests
)
..map((key, value) => MapEntry('\$key', value)),

Check warning on line 115 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L115

Added line #L115 was not covered by tests
};

replaceMap.forEach((key, value) {
parsedUri = parsedUri.replaceAll(key, Uri.encodeComponent(value));

Check warning on line 119 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L118-L119

Added lines #L118 - L119 were not covered by tests
});

return Uri.parse(parsedUri);

Check warning on line 122 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L122

Added line #L122 was not covered by tests
}

Map<String, dynamic> toJson() => {
'creatorUserId': creatorUserId,
'data': data,
'id': id,
'name': name,
'type': type,
'url': url,
'waitForIframeLoad': waitForIframeLoad,

Check warning on line 132 in lib/msc_extensions/msc_1236_widgets/src/widget.dart

View check run for this annotation

Codecov / codecov/patch

lib/msc_extensions/msc_1236_widgets/src/widget.dart#L125-L132

Added lines #L125 - L132 were not covered by tests
};
}
Loading