2021-10-26 18:50:34 +02:00
|
|
|
import 'package:flutter/material.dart';
|
2023-06-27 14:09:00 +02:00
|
|
|
import 'package:flutter/services.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
2022-01-29 12:35:03 +01:00
|
|
|
import 'package:collection/collection.dart' show IterableExtension;
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
2020-12-25 09:58:34 +01:00
|
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:punycode/punycode.dart';
|
2023-01-26 09:47:30 +01:00
|
|
|
import 'package:url_launcher/url_launcher_string.dart';
|
2021-05-23 13:11:55 +02:00
|
|
|
import 'package:vrouter/vrouter.dart';
|
2020-01-19 19:28:12 +01:00
|
|
|
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/config/app_config.dart';
|
2023-01-07 10:29:34 +01:00
|
|
|
import 'package:fluffychat/utils/adaptive_bottom_sheet.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/widgets/matrix.dart';
|
|
|
|
import 'package:fluffychat/widgets/profile_bottom_sheet.dart';
|
2021-11-14 18:57:48 +01:00
|
|
|
import 'package:fluffychat/widgets/public_room_bottom_sheet.dart';
|
2021-08-01 09:53:43 +02:00
|
|
|
import 'platform_infos.dart';
|
|
|
|
|
2020-01-19 19:28:12 +01:00
|
|
|
class UrlLauncher {
|
2022-01-29 12:35:03 +01:00
|
|
|
final String? url;
|
2020-01-19 19:28:12 +01:00
|
|
|
final BuildContext context;
|
2022-07-24 19:02:14 +02:00
|
|
|
|
2020-01-19 19:28:12 +01:00
|
|
|
const UrlLauncher(this.context, this.url);
|
|
|
|
|
2023-05-02 14:09:46 +02:00
|
|
|
void launchUrl() async {
|
2022-01-29 12:35:03 +01:00
|
|
|
if (url!.toLowerCase().startsWith(AppConfig.deepLinkPrefix) ||
|
|
|
|
url!.toLowerCase().startsWith(AppConfig.inviteLinkPrefix) ||
|
|
|
|
{'#', '@', '!', '+', '\$'}.contains(url![0]) ||
|
|
|
|
url!.toLowerCase().startsWith(AppConfig.schemePrefix)) {
|
2020-01-19 19:28:12 +01:00
|
|
|
return openMatrixToUrl();
|
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
final uri = Uri.tryParse(url!);
|
2021-07-31 21:11:13 +02:00
|
|
|
if (uri == null) {
|
|
|
|
// we can't open this thing
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2023-03-02 10:57:52 +01:00
|
|
|
SnackBar(content: Text(L10n.of(context)!.cantOpenUri(url!))),
|
|
|
|
);
|
2021-07-31 21:11:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2023-06-27 14:09:00 +02:00
|
|
|
final consent = await showConfirmationDialog(
|
2023-05-02 14:09:46 +02:00
|
|
|
context: context,
|
|
|
|
title: L10n.of(context)!.openLinkInBrowser,
|
|
|
|
message: url,
|
2023-06-27 14:09:00 +02:00
|
|
|
actions: [
|
|
|
|
AlertDialogAction(
|
|
|
|
key: null,
|
|
|
|
label: L10n.of(context)!.cancel,
|
|
|
|
),
|
|
|
|
AlertDialogAction(
|
|
|
|
key: _LaunchUrlResponse.copy,
|
|
|
|
label: L10n.of(context)!.copy,
|
|
|
|
),
|
|
|
|
AlertDialogAction(
|
|
|
|
key: _LaunchUrlResponse.launch,
|
|
|
|
label: L10n.of(context)!.ok,
|
|
|
|
),
|
|
|
|
],
|
2023-05-02 14:09:46 +02:00
|
|
|
);
|
2023-06-27 14:09:00 +02:00
|
|
|
if (consent == _LaunchUrlResponse.copy) {
|
|
|
|
await Clipboard.setData(ClipboardData(text: uri.toString()));
|
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
|
|
|
SnackBar(
|
|
|
|
content: Text(L10n.of(context)!.copiedToClipboard),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (consent != _LaunchUrlResponse.launch) return;
|
2023-05-02 14:09:46 +02:00
|
|
|
|
2021-07-31 21:11:13 +02:00
|
|
|
if (!{'https', 'http'}.contains(uri.scheme)) {
|
|
|
|
// just launch non-https / non-http uris directly
|
2021-08-01 09:53:43 +02:00
|
|
|
|
2021-08-29 14:30:29 +02:00
|
|
|
// we need to transmute geo URIs on desktop and on iOS
|
|
|
|
if ((!PlatformInfos.isMobile || PlatformInfos.isIOS) &&
|
2022-01-29 12:35:03 +01:00
|
|
|
uri.scheme == 'geo') {
|
2021-08-01 09:53:43 +02:00
|
|
|
final latlong = uri.path
|
|
|
|
.split(';')
|
|
|
|
.first
|
|
|
|
.split(',')
|
|
|
|
.map((s) => double.tryParse(s))
|
|
|
|
.toList();
|
|
|
|
if (latlong.length == 2 &&
|
|
|
|
latlong.first != null &&
|
|
|
|
latlong.last != null) {
|
2021-08-29 14:30:29 +02:00
|
|
|
if (PlatformInfos.isIOS) {
|
|
|
|
// iOS is great at not following standards, so we need to transmute the geo URI
|
|
|
|
// to an apple maps thingy
|
|
|
|
// https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html
|
|
|
|
final ll = '${latlong.first},${latlong.last}';
|
2023-01-26 09:47:30 +01:00
|
|
|
launchUrlString('https://maps.apple.com/?q=$ll&sll=$ll');
|
2021-08-29 14:30:29 +02:00
|
|
|
} else {
|
|
|
|
// transmute geo URIs on desktop to openstreetmap links, as those usually can't handle
|
|
|
|
// geo URIs
|
2023-01-26 09:47:30 +01:00
|
|
|
launchUrlString(
|
2023-03-02 10:57:52 +01:00
|
|
|
'https://www.openstreetmap.org/?mlat=${latlong.first}&mlon=${latlong.last}#map=16/${latlong.first}/${latlong.last}',
|
|
|
|
);
|
2021-08-29 14:30:29 +02:00
|
|
|
}
|
2021-08-01 09:53:43 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-01-26 09:47:30 +01:00
|
|
|
launchUrlString(url!);
|
2021-07-31 21:11:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
if (uri.host.isEmpty) {
|
2021-07-31 21:11:13 +02:00
|
|
|
ScaffoldMessenger.of(context).showSnackBar(
|
2023-03-02 10:57:52 +01:00
|
|
|
SnackBar(content: Text(L10n.of(context)!.cantOpenUri(url!))),
|
|
|
|
);
|
2021-07-31 21:11:13 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// okay, we have either an http or an https URI.
|
|
|
|
// As some platforms have issues with opening unicode URLs, we are going to help
|
|
|
|
// them out by punycode-encoding them for them ourself.
|
|
|
|
final newHost = uri.host.split('.').map((hostPartEncoded) {
|
|
|
|
final hostPart = Uri.decodeComponent(hostPartEncoded);
|
|
|
|
final hostPartPunycode = punycodeEncode(hostPart);
|
2022-08-14 16:59:21 +02:00
|
|
|
return hostPartPunycode != '$hostPart-'
|
2021-07-31 21:11:13 +02:00
|
|
|
? 'xn--$hostPartPunycode'
|
|
|
|
: hostPart;
|
|
|
|
}).join('.');
|
2023-02-18 21:42:09 +01:00
|
|
|
// Force LaunchMode.externalApplication, otherwise url_launcher will default
|
|
|
|
// to opening links in a webview on mobile platforms.
|
2023-03-02 10:57:52 +01:00
|
|
|
launchUrlString(
|
|
|
|
uri.replace(host: newHost).toString(),
|
|
|
|
mode: LaunchMode.externalApplication,
|
|
|
|
);
|
2020-01-19 19:28:12 +01:00
|
|
|
}
|
|
|
|
|
2021-10-16 09:59:38 +02:00
|
|
|
void openMatrixToUrl() async {
|
2020-01-19 19:28:12 +01:00
|
|
|
final matrix = Matrix.of(context);
|
2022-01-29 12:35:03 +01:00
|
|
|
final url = this.url!.replaceFirst(
|
2021-11-29 16:18:16 +01:00
|
|
|
AppConfig.deepLinkPrefix,
|
|
|
|
AppConfig.inviteLinkPrefix,
|
|
|
|
);
|
|
|
|
|
2020-12-27 11:46:53 +01:00
|
|
|
// The identifier might be a matrix.to url and needs escaping. Or, it might have multiple
|
|
|
|
// identifiers (room id & event id), or it might also have a query part.
|
|
|
|
// All this needs parsing.
|
2021-08-28 11:09:37 +02:00
|
|
|
final identityParts = url.parseIdentifierIntoParts() ??
|
2022-01-29 12:35:03 +01:00
|
|
|
Uri.tryParse(url)?.host.parseIdentifierIntoParts() ??
|
2021-08-28 11:09:37 +02:00
|
|
|
Uri.tryParse(url)
|
|
|
|
?.pathSegments
|
2022-01-29 12:35:03 +01:00
|
|
|
.lastWhereOrNull((_) => true)
|
2021-08-28 11:09:37 +02:00
|
|
|
?.parseIdentifierIntoParts();
|
2020-12-27 11:46:53 +01:00
|
|
|
if (identityParts == null) {
|
|
|
|
return; // no match, nothing to do
|
|
|
|
}
|
|
|
|
if (identityParts.primaryIdentifier.sigil == '#' ||
|
|
|
|
identityParts.primaryIdentifier.sigil == '!') {
|
|
|
|
// we got a room! Let's open that one
|
|
|
|
final roomIdOrAlias = identityParts.primaryIdentifier;
|
|
|
|
final event = identityParts.secondaryIdentifier;
|
2020-09-19 19:21:33 +02:00
|
|
|
var room = matrix.client.getRoomByAlias(roomIdOrAlias) ??
|
|
|
|
matrix.client.getRoomById(roomIdOrAlias);
|
2020-09-05 13:45:03 +02:00
|
|
|
var roomId = room?.id;
|
2020-09-19 19:21:33 +02:00
|
|
|
// we make the servers a set and later on convert to a list, so that we can easily
|
|
|
|
// deduplicate servers added via alias lookup and query parameter
|
2021-04-14 10:37:15 +02:00
|
|
|
final servers = <String>{};
|
2020-12-27 11:46:53 +01:00
|
|
|
if (room == null && roomIdOrAlias.sigil == '#') {
|
2020-09-05 13:45:03 +02:00
|
|
|
// we were unable to find the room locally...so resolve it
|
2020-12-25 09:58:34 +01:00
|
|
|
final response = await showFutureLoadingDialog(
|
|
|
|
context: context,
|
2021-05-20 13:59:55 +02:00
|
|
|
future: () => matrix.client.getRoomIdByAlias(roomIdOrAlias),
|
2020-09-05 13:45:03 +02:00
|
|
|
);
|
2021-01-19 18:09:58 +01:00
|
|
|
if (response.error != null) {
|
|
|
|
return; // nothing to do, the alias doesn't exist
|
2020-09-05 13:45:03 +02:00
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
roomId = response.result!.roomId;
|
|
|
|
servers.addAll(response.result!.servers!);
|
|
|
|
room = matrix.client.getRoomById(roomId!);
|
2020-09-19 19:21:33 +02:00
|
|
|
}
|
2022-01-29 12:35:03 +01:00
|
|
|
servers.addAll(identityParts.via);
|
2020-09-05 13:45:03 +02:00
|
|
|
if (room != null) {
|
2022-07-24 19:02:14 +02:00
|
|
|
if (room.isSpace) {
|
2022-08-30 20:24:36 +02:00
|
|
|
// TODO: Implement navigate to space
|
2022-07-24 19:02:14 +02:00
|
|
|
VRouter.of(context).toSegments(['rooms']);
|
|
|
|
return;
|
|
|
|
}
|
2021-01-19 17:41:37 +01:00
|
|
|
// we have the room, so....just open it
|
|
|
|
if (event != null) {
|
2023-03-02 10:57:52 +01:00
|
|
|
VRouter.of(context).toSegments(
|
|
|
|
['rooms', room.id],
|
|
|
|
queryParameters: {'event': event},
|
|
|
|
);
|
2021-01-19 17:41:37 +01:00
|
|
|
} else {
|
2021-08-15 13:26:16 +02:00
|
|
|
VRouter.of(context).toSegments(['rooms', room.id]);
|
2021-01-19 17:41:37 +01:00
|
|
|
}
|
2020-09-05 13:45:03 +02:00
|
|
|
return;
|
2021-11-29 16:18:16 +01:00
|
|
|
} else {
|
2023-01-07 10:29:34 +01:00
|
|
|
await showAdaptiveBottomSheet(
|
2021-11-29 16:18:16 +01:00
|
|
|
context: context,
|
|
|
|
builder: (c) => PublicRoomBottomSheet(
|
|
|
|
roomAlias: identityParts.primaryIdentifier,
|
|
|
|
outerContext: context,
|
|
|
|
),
|
|
|
|
);
|
2020-09-05 13:45:03 +02:00
|
|
|
}
|
2020-12-20 16:53:37 +01:00
|
|
|
if (roomIdOrAlias.sigil == '!') {
|
2020-12-27 11:46:53 +01:00
|
|
|
if (await showOkCancelAlertDialog(
|
2021-05-23 15:02:36 +02:00
|
|
|
useRootNavigator: false,
|
2020-12-27 11:46:53 +01:00
|
|
|
context: context,
|
|
|
|
title: 'Join room $roomIdOrAlias',
|
|
|
|
) ==
|
|
|
|
OkCancelResult.ok) {
|
|
|
|
roomId = roomIdOrAlias;
|
|
|
|
final response = await showFutureLoadingDialog(
|
2020-12-25 09:58:34 +01:00
|
|
|
context: context,
|
2021-05-20 13:59:55 +02:00
|
|
|
future: () => matrix.client.joinRoom(
|
2020-12-27 11:46:53 +01:00
|
|
|
roomIdOrAlias,
|
2021-08-18 17:24:59 +02:00
|
|
|
serverName: servers.isNotEmpty ? servers.toList() : null,
|
2020-12-27 11:46:53 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
if (response.error != null) return;
|
|
|
|
// wait for two seconds so that it probably came down /sync
|
|
|
|
await showFutureLoadingDialog(
|
2023-03-02 10:57:52 +01:00
|
|
|
context: context,
|
|
|
|
future: () => Future.delayed(const Duration(seconds: 2)),
|
|
|
|
);
|
2021-01-19 17:41:37 +01:00
|
|
|
if (event != null) {
|
2023-03-02 10:57:52 +01:00
|
|
|
VRouter.of(context).toSegments(
|
|
|
|
['rooms', response.result!],
|
|
|
|
queryParameters: {'event': event},
|
|
|
|
);
|
2021-01-19 17:41:37 +01:00
|
|
|
} else {
|
2022-01-29 12:35:03 +01:00
|
|
|
VRouter.of(context).toSegments(['rooms', response.result!]);
|
2021-01-19 17:41:37 +01:00
|
|
|
}
|
2020-12-27 11:46:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (identityParts.primaryIdentifier.sigil == '@') {
|
2023-01-07 10:29:34 +01:00
|
|
|
await showAdaptiveBottomSheet(
|
2021-08-24 14:15:35 +02:00
|
|
|
context: context,
|
2021-09-24 15:51:33 +02:00
|
|
|
builder: (c) => ProfileBottomSheet(
|
|
|
|
userId: identityParts.primaryIdentifier,
|
2021-09-22 15:03:57 +02:00
|
|
|
outerContext: context,
|
2021-08-24 14:15:35 +02:00
|
|
|
),
|
2020-12-27 11:46:53 +01:00
|
|
|
);
|
2020-01-19 19:28:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-27 14:09:00 +02:00
|
|
|
|
|
|
|
enum _LaunchUrlResponse {
|
|
|
|
launch,
|
|
|
|
copy,
|
|
|
|
}
|