fluffychat/lib/utils/url_launcher.dart

137 lines
5.0 KiB
Dart
Raw Normal View History

2020-11-14 09:08:13 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-01-16 11:46:38 +00:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2020-01-19 18:28:12 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-12-25 08:58:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2020-01-19 18:28:12 +00:00
import 'package:fluffychat/components/matrix.dart';
2020-12-11 13:14:33 +00:00
import 'package:fluffychat/app_config.dart';
2020-01-19 18:28:12 +00:00
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
class UrlLauncher {
final String url;
final BuildContext context;
const UrlLauncher(this.context, this.url);
void launchUrl() {
if (url.toLowerCase().startsWith(AppConfig.inviteLinkPrefix) ||
{'#', '@', '!', '+', '\$'}.contains(url[0]) ||
url.toLowerCase().startsWith(AppConfig.schemePrefix)) {
2020-01-19 18:28:12 +00:00
return openMatrixToUrl();
}
launch(url);
}
void openMatrixToUrl() async {
final matrix = Matrix.of(context);
// 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.
final identityParts = url.parseIdentifierIntoParts();
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 17:21:33 +00:00
var room = matrix.client.getRoomByAlias(roomIdOrAlias) ??
matrix.client.getRoomById(roomIdOrAlias);
2020-09-05 11:45:03 +00:00
var roomId = room?.id;
2020-09-19 17:21:33 +00: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
var servers = <String>{};
if (room == null && roomIdOrAlias.sigil == '#') {
2020-09-05 11:45:03 +00:00
// we were unable to find the room locally...so resolve it
2020-12-25 08:58:34 +00:00
final response = await showFutureLoadingDialog(
context: context,
future: () =>
matrix.client.requestRoomAliasInformations(roomIdOrAlias),
2020-09-05 11:45:03 +00:00
);
if (response.error != null) {
return; // nothing to do, the alias doesn't exist
2020-09-05 11:45:03 +00:00
}
roomId = response.result.roomId;
servers.addAll(response.result.servers);
room = matrix.client.getRoomById(roomId);
2020-09-05 11:45:03 +00:00
}
if (identityParts.via != null) {
servers.addAll(identityParts.via);
2020-09-19 17:21:33 +00:00
}
2020-09-05 11:45:03 +00:00
if (room != null) {
// we have the room, so....just open it
if (event != null) {
await AdaptivePageLayout.of(context)
.pushNamedAndRemoveUntilIsFirst('/rooms/${room.id}/$event');
} else {
await AdaptivePageLayout.of(context)
.pushNamedAndRemoveUntilIsFirst('/rooms/${room.id}');
}
2020-09-05 11:45:03 +00:00
return;
}
2020-12-20 15:53:37 +00:00
if (roomIdOrAlias.sigil == '!') {
if (await showOkCancelAlertDialog(
context: context,
title: 'Join room $roomIdOrAlias',
2021-02-24 11:17:23 +00:00
useRootNavigator: false,
) ==
OkCancelResult.ok) {
roomId = roomIdOrAlias;
final response = await showFutureLoadingDialog(
2020-12-25 08:58:34 +00:00
context: context,
future: () => matrix.client.joinRoomOrAlias(
roomIdOrAlias,
servers: servers.isNotEmpty ? servers.toList() : null,
),
);
if (response.error != null) return;
// wait for two seconds so that it probably came down /sync
await showFutureLoadingDialog(
context: context,
future: () => Future.delayed(const Duration(seconds: 2)));
if (event != null) {
await AdaptivePageLayout.of(context).pushNamedAndRemoveUntilIsFirst(
'/rooms/${response.result}/$event');
} else {
await AdaptivePageLayout.of(context)
.pushNamedAndRemoveUntilIsFirst('/rooms/${response.result}');
}
}
} else {
2021-01-16 11:46:38 +00:00
await AdaptivePageLayout.of(context)
2021-03-04 11:28:06 +00:00
.pushNamedAndRemoveUntilIsFirst('/discover/$roomIdOrAlias');
}
} else if (identityParts.primaryIdentifier.sigil == '@') {
final user = User(
identityParts.primaryIdentifier,
room: Room(id: '', client: matrix.client),
);
var roomId = matrix.client.getDirectChatFromUserId(user.id);
if (roomId != null) {
2021-01-16 11:46:38 +00:00
await AdaptivePageLayout.of(context)
2021-03-04 11:28:06 +00:00
.pushNamedAndRemoveUntilIsFirst('/rooms/$roomId');
2021-01-16 11:46:38 +00:00
return;
}
if (await showOkCancelAlertDialog(
context: context,
title: 'Message user ${user.id}',
2021-02-24 11:17:23 +00:00
useRootNavigator: false,
) ==
OkCancelResult.ok) {
roomId = (await showFutureLoadingDialog(
context: context,
future: () => user.startDirectChat(),
))
.result;
2020-09-05 11:45:03 +00:00
if (roomId != null) {
2021-01-16 11:46:38 +00:00
await AdaptivePageLayout.of(context)
2021-03-04 11:28:06 +00:00
.pushNamedAndRemoveUntilIsFirst('/rooms/$roomId');
2020-09-05 11:45:03 +00:00
}
2020-01-19 18:28:12 +00:00
}
}
}
}