fluffychat/lib/utils/url_launcher.dart

145 lines
5.2 KiB
Dart
Raw Normal View History

2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2020-01-19 19:28:12 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-04-27 13:36:39 +02:00
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
2020-01-19 19:28:12 +01:00
import 'package:fluffychat/components/matrix.dart';
2020-12-11 14:14:33 +01:00
import 'package:fluffychat/app_config.dart';
2020-01-19 19:28:12 +01:00
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/chat.dart';
2020-12-20 16:53:37 +01:00
import 'package:fluffychat/views/discover_view.dart';
2020-01-19 19:28:12 +01:00
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
2020-09-19 19:21:33 +02:00
import 'matrix_identifier_string_extension.dart';
2020-01-19 19:28:12 +01:00
class UrlLauncher {
final String url;
final BuildContext context;
const UrlLauncher(this.context, this.url);
void launchUrl() {
2020-12-11 14:14:33 +01:00
if (url.startsWith(AppConfig.inviteLinkPrefix) ||
2020-09-05 13:45:03 +02:00
{'#', '@', '!', '+', '\$'}.contains(url[0])) {
2020-01-19 19:28:12 +01:00
return openMatrixToUrl();
}
launch(url);
}
void openMatrixToUrl() async {
final matrix = Matrix.of(context);
2020-12-11 14:14:33 +01:00
final identifier = url.replaceAll(AppConfig.inviteLinkPrefix, '');
2020-09-05 13:45:03 +02:00
if (identifier[0] == '#' || identifier[0] == '!') {
2020-09-19 19:21:33 +02:00
// sometimes we have identifiers which have an event id and additional query parameters
// we want to separate those.
final identityParts = identifier.parseIdentifierIntoParts();
if (identityParts == null) {
return; // no match, nothing to do
}
final roomIdOrAlias = identityParts.roomIdOrAlias;
final event = identityParts.eventId;
final query = identityParts.queryString;
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
var servers = <String>{};
if (room == null && roomIdOrAlias.startsWith('#')) {
2020-09-05 13:45:03 +02:00
// we were unable to find the room locally...so resolve it
final response =
await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-09-19 19:21:33 +02:00
matrix.client.requestRoomAliasInformations(roomIdOrAlias),
2020-09-05 13:45:03 +02:00
);
if (response != false) {
roomId = response.roomId;
2020-09-19 19:21:33 +02:00
servers.addAll(response.servers);
2020-09-05 13:45:03 +02:00
room = matrix.client.getRoomById(roomId);
}
}
2020-09-19 19:21:33 +02:00
if (query != null) {
// the query information might hold additional servers to try, so let's try them!
// as there might be multiple "via" tags we can't just use Uri.splitQueryString, we need to do our own thing
for (final parameter in query.split('&')) {
final index = parameter.indexOf('=');
if (index == -1) {
continue;
}
if (Uri.decodeQueryComponent(parameter.substring(0, index)) !=
'via') {
continue;
}
servers.add(Uri.decodeQueryComponent(parameter.substring(index + 1)));
}
}
2020-09-05 13:45:03 +02:00
if (room != null) {
// we have the room, so....just open it!
await Navigator.pushAndRemoveUntil(
context,
2020-09-19 19:21:33 +02:00
AppRoute.defaultRoute(
context, ChatView(room.id, scrollToEventId: event)),
2020-09-05 13:45:03 +02:00
(r) => r.isFirst,
);
return;
}
2020-12-20 16:53:37 +01:00
if (roomIdOrAlias.sigil == '!') {
2020-09-19 19:21:33 +02:00
roomId = roomIdOrAlias;
2020-09-05 13:45:03 +02:00
final response =
await SimpleDialogs(context).tryRequestWithLoadingDialog(
matrix.client.joinRoomOrAlias(
roomIdOrAlias,
servers: servers.isNotEmpty ? servers.toList() : null,
2020-09-05 13:45:03 +02:00
),
);
if (response == false) return;
// wait for two seconds so that it probably came down /sync
await SimpleDialogs(context).tryRequestWithLoadingDialog(
Future.delayed(const Duration(seconds: 2)));
2020-09-05 13:45:03 +02:00
await Navigator.pushAndRemoveUntil(
context,
2020-09-19 19:21:33 +02:00
AppRoute.defaultRoute(
context, ChatView(response, scrollToEventId: event)),
2020-09-05 13:45:03 +02:00
(r) => r.isFirst,
);
2020-12-20 16:53:37 +01:00
} else if (identifier.sigil == '#') {
await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(
context,
DiscoverView(alias: identifier),
),
2020-05-09 13:06:18 +02:00
(r) => r.isFirst,
2020-01-19 19:28:12 +01:00
);
2020-12-20 16:53:37 +01:00
} else if (identifier.sigil == '@') {
final user = User(
identifier,
room: Room(id: '', client: matrix.client),
);
var roomId = matrix.client.getDirectChatFromUserId(identifier);
2020-09-05 13:45:03 +02:00
if (roomId != null) {
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(roomId)),
(r) => r.isFirst,
);
2020-12-20 16:53:37 +01:00
return;
}
if (await showOkCancelAlertDialog(
context: context,
title: 'Message user $identifier',
) ==
OkCancelResult.ok) {
roomId = await SimpleDialogs(context)
.tryRequestWithLoadingDialog(user.startDirectChat());
Navigator.of(context).pop();
if (roomId != null) {
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(roomId)),
(r) => r.isFirst,
);
}
2020-09-05 13:45:03 +02:00
}
2020-01-19 19:28:12 +01:00
}
}
}
}