fix(ui): use ogScrapeUrl for link attachments

This commit is contained in:
Salvatore Giordano
2022-04-26 11:53:58 +02:00
parent 76f6ce6d0f
commit ff028948cf
8 changed files with 28 additions and 22 deletions
@@ -18,14 +18,11 @@ class AttachmentTitle extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final normalizedTitleLink = attachment.titleLink?.replaceFirst( final ogScrapeUrl = attachment.ogScrapeUrl;
RegExp(r'https?://(www\.)?'),
'',
);
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
final titleLink = attachment.titleLink; final ogScrapeUrl = attachment.ogScrapeUrl;
if (titleLink != null) launchURL(context, titleLink); if (ogScrapeUrl != null) launchURL(context, ogScrapeUrl);
}, },
child: Padding( child: Padding(
padding: const EdgeInsets.all(8), padding: const EdgeInsets.all(8),
@@ -42,8 +39,8 @@ class AttachmentTitle extends StatelessWidget {
fontWeight: FontWeight.bold, fontWeight: FontWeight.bold,
), ),
), ),
if (normalizedTitleLink != null) if (ogScrapeUrl != null)
Text(normalizedTitleLink, style: messageTheme.messageTextStyle), Text(ogScrapeUrl, style: messageTheme.messageTextStyle),
], ],
), ),
), ),
@@ -37,11 +37,11 @@ class UrlAttachment extends StatelessWidget {
final chatThemeData = StreamChatTheme.of(context); final chatThemeData = StreamChatTheme.of(context);
return GestureDetector( return GestureDetector(
onTap: () { onTap: () {
final titleLink = urlAttachment.titleLink; final ogScrapeUrl = urlAttachment.ogScrapeUrl;
if (titleLink != null) { if (ogScrapeUrl != null) {
onLinkTap != null onLinkTap != null
? onLinkTap!(titleLink) ? onLinkTap!(ogScrapeUrl)
: launchURL(context, titleLink); : launchURL(context, ogScrapeUrl);
} }
}, },
child: Column( child: Column(
@@ -225,3 +225,12 @@ extension UserListX on List<User> {
return entries.map((e) => e.key).toList(growable: false); return entries.map((e) => e.key).toList(growable: false);
} }
} }
/// Extensions on [Uri]
extension UriX on Uri {
/// Return the URI adding the http scheme if it is missing
Uri get withScheme {
if (hasScheme) return this;
return Uri.parse('http://${toString()}');
}
}
@@ -1274,7 +1274,7 @@ class MessageInputState extends State<MessageInput> {
Widget _buildReplyToMessage() { Widget _buildReplyToMessage() {
if (!_hasQuotedMessage) return const Offstage(); if (!_hasQuotedMessage) return const Offstage();
final containsUrl = widget.quotedMessage!.attachments final containsUrl = widget.quotedMessage!.attachments
.any((element) => element.titleLink != null); .any((element) => element.ogScrapeUrl!= null);
return QuotedMessageWidget( return QuotedMessageWidget(
reverse: true, reverse: true,
showBorder: !containsUrl, showBorder: !containsUrl,
@@ -1091,7 +1091,7 @@ class _MessageListViewState extends State<MessageListView> {
final isOnlyEmoji = message.text?.isOnlyEmoji ?? false; final isOnlyEmoji = message.text?.isOnlyEmoji ?? false;
final hasUrlAttachment = final hasUrlAttachment =
message.attachments.any((it) => it.titleLink != null); message.attachments.any((it) => it.ogScrapeUrl != null);
final borderSide = final borderSide =
isOnlyEmoji || hasUrlAttachment || (isMyMessage && !hasFileAttachment) isOnlyEmoji || hasUrlAttachment || (isMyMessage && !hasFileAttachment)
@@ -574,11 +574,11 @@ class _MessageWidgetState extends State<MessageWidget>
bool get isOnlyEmoji => widget.message.text?.isOnlyEmoji == true; bool get isOnlyEmoji => widget.message.text?.isOnlyEmoji == true;
bool get hasNonUrlAttachments => widget.message.attachments bool get hasNonUrlAttachments => widget.message.attachments
.where((it) => it.titleLink == null || it.type == 'giphy') .where((it) => it.ogScrapeUrl == null || it.type == 'giphy')
.isNotEmpty; .isNotEmpty;
bool get hasUrlAttachments => widget.message.attachments bool get hasUrlAttachments => widget.message.attachments
.any((it) => it.titleLink != null && it.type != 'giphy'); .any((it) => it.ogScrapeUrl != null && it.type != 'giphy');
bool get showBottomRow => bool get showBottomRow =>
showThreadReplyIndicator || showThreadReplyIndicator ||
@@ -999,9 +999,9 @@ class _MessageWidgetState extends State<MessageWidget>
Widget _buildUrlAttachment() { Widget _buildUrlAttachment() {
final urlAttachment = widget.message.attachments final urlAttachment = widget.message.attachments
.firstWhere((element) => element.titleLink != null); .firstWhere((element) => element.ogScrapeUrl != null);
final host = Uri.parse(urlAttachment.titleLink!).host; final host = Uri.parse(urlAttachment.ogScrapeUrl!).withScheme.host;
final splitList = host.split('.'); final splitList = host.split('.');
final hostName = splitList.length == 3 ? splitList[1] : splitList[0]; final hostName = splitList.length == 3 ? splitList[1] : splitList[0];
final hostDisplayName = urlAttachment.authorName?.capitalize() ?? final hostDisplayName = urlAttachment.authorName?.capitalize() ??
@@ -1173,7 +1173,7 @@ class _MessageWidgetState extends State<MessageWidget>
widget.message.attachments widget.message.attachments
.where((element) => .where((element) =>
(element.titleLink == null && element.type != null) || (element.ogScrapeUrl == null && element.type != null) ||
element.type == 'giphy') element.type == 'giphy')
.forEach((e) { .forEach((e) {
if (attachmentGroups[e.type] == null) { if (attachmentGroups[e.type] == null) {
@@ -97,7 +97,7 @@ class QuotedMessageWidget extends StatelessWidget {
bool get _hasAttachments => message.attachments.isNotEmpty; bool get _hasAttachments => message.attachments.isNotEmpty;
bool get _containsLinkAttachment => bool get _containsLinkAttachment =>
message.attachments.any((element) => element.titleLink != null); message.attachments.any((element) => element.ogScrapeUrl != null);
bool get _containsText => message.text?.isNotEmpty == true; bool get _containsText => message.text?.isNotEmpty == true;
@@ -201,7 +201,7 @@ class QuotedMessageWidget extends StatelessWidget {
Attachment attachment; Attachment attachment;
if (_containsLinkAttachment) { if (_containsLinkAttachment) {
attachment = message.attachments.firstWhere( attachment = message.attachments.firstWhere(
(element) => element.titleLink != null, (element) => element.ogScrapeUrl != null,
); );
child = _buildUrlAttachment(attachment); child = _buildUrlAttachment(attachment);
} else { } else {
@@ -9,7 +9,7 @@ import 'package:url_launcher/url_launcher.dart';
/// Launch URL /// Launch URL
Future<void> launchURL(BuildContext context, String url) async { Future<void> launchURL(BuildContext context, String url) async {
try { try {
await launch(url); await launch(Uri.parse(url).withScheme.toString());
} catch (e) { } catch (e) {
ScaffoldMessenger.of(context).showSnackBar( ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.translations.launchUrlError)), SnackBar(content: Text(context.translations.launchUrlError)),