2020-05-09 11:36:41 +00:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
import 'package:flutter_matrix_html/flutter_html.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
|
|
|
|
|
|
|
import 'matrix.dart';
|
|
|
|
|
|
|
|
class HtmlMessage extends StatelessWidget {
|
|
|
|
final String html;
|
|
|
|
final int maxLines;
|
2020-05-14 05:43:21 +00:00
|
|
|
final Room room;
|
2020-05-15 05:47:32 +00:00
|
|
|
final TextStyle defaultTextStyle;
|
|
|
|
final TextStyle linkStyle;
|
2020-05-09 11:36:41 +00:00
|
|
|
|
2020-05-22 10:21:16 +00:00
|
|
|
const HtmlMessage(
|
|
|
|
{this.html,
|
|
|
|
this.maxLines,
|
|
|
|
this.room,
|
|
|
|
this.defaultTextStyle,
|
|
|
|
this.linkStyle});
|
2020-05-09 11:36:41 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2020-07-05 16:41:27 +00:00
|
|
|
// riot-web is notorious for creating bad reply fallback events from invalid messages which, if
|
|
|
|
// not handled properly, can lead to impersination. As such, we strip the entire `<mx-reply>` tags
|
|
|
|
// here already, to prevent that from happening.
|
|
|
|
// We do *not* do this in an AST and just with simple regex here, as riot-web tends to create
|
|
|
|
// miss-matching tags, and this way we actually correctly identify what we want to strip and, well,
|
|
|
|
// strip it.
|
|
|
|
final renderHtml = html.replaceAll(RegExp('<mx-reply>.*<\/mx-reply>'), '');
|
|
|
|
|
2020-05-09 11:36:41 +00:00
|
|
|
// there is no need to pre-validate the html, as we validate it while rendering
|
2020-05-22 10:21:16 +00:00
|
|
|
|
2020-05-15 05:47:32 +00:00
|
|
|
final themeData = Theme.of(context);
|
2020-05-09 11:36:41 +00:00
|
|
|
return Html(
|
2020-07-05 16:41:27 +00:00
|
|
|
data: renderHtml,
|
2020-05-15 05:47:32 +00:00
|
|
|
defaultTextStyle: defaultTextStyle,
|
2020-05-22 10:21:16 +00:00
|
|
|
linkStyle: linkStyle ??
|
|
|
|
themeData.textTheme.bodyText2.copyWith(
|
|
|
|
color: themeData.accentColor,
|
|
|
|
decoration: TextDecoration.underline,
|
|
|
|
),
|
2020-05-09 11:36:41 +00:00
|
|
|
shrinkToFit: true,
|
|
|
|
maxLines: maxLines,
|
|
|
|
onLinkTap: (String url) {
|
|
|
|
if (url == null || url.isEmpty) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
launch(url);
|
|
|
|
},
|
|
|
|
getMxcUrl: (String mxc, double width, double height) {
|
|
|
|
final ratio = MediaQuery.of(context).devicePixelRatio;
|
|
|
|
return Uri.parse(mxc)?.getThumbnail(
|
|
|
|
Matrix.of(context).client,
|
|
|
|
width: (width ?? 800) * ratio,
|
|
|
|
height: (height ?? 800) * ratio,
|
|
|
|
method: ThumbnailMethod.scale,
|
|
|
|
);
|
|
|
|
},
|
2020-05-14 05:43:21 +00:00
|
|
|
getPillInfo: (String identifier) async {
|
|
|
|
if (room == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (identifier[0] == '@') {
|
|
|
|
// we have a user pill
|
|
|
|
final user = room.getState('m.room.member', identifier);
|
|
|
|
if (user != null) {
|
|
|
|
return user.content;
|
|
|
|
}
|
|
|
|
// there might still be a profile...
|
|
|
|
final profile = await room.client.getProfileFromUserId(identifier);
|
|
|
|
if (profile != null) {
|
|
|
|
return {
|
|
|
|
'displayname': profile.displayname,
|
|
|
|
'avatar_url': profile.avatarUrl.toString(),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (identifier[0] == '#') {
|
|
|
|
// we have an alias pill
|
|
|
|
for (final r in room.client.rooms) {
|
|
|
|
final state = r.getState('m.room.canonical_alias');
|
2020-05-22 10:21:16 +00:00
|
|
|
if (state != null &&
|
|
|
|
((state.content['alias'] is String &&
|
|
|
|
state.content['alias'] == identifier) ||
|
|
|
|
(state.content['alt_aliases'] is List &&
|
|
|
|
state.content['alt_aliases'].contains(identifier)))) {
|
2020-05-14 05:43:21 +00:00
|
|
|
// we have a room!
|
|
|
|
return {
|
|
|
|
'displayname': identifier,
|
|
|
|
'avatar_url': r.getState('m.room.avatar')?.content['url'],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (identifier[0] == '!') {
|
|
|
|
// we have a room ID pill
|
|
|
|
final r = room.client.getRoomById(identifier);
|
|
|
|
if (r == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
'displayname': r.canonicalAlias,
|
|
|
|
'avatar_url': r.getState('m.room.avatar')?.content['url'],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
},
|
2020-05-09 11:36:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|