2020-01-19 18:28:12 +00:00
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:url_launcher/url_launcher.dart';
|
2020-10-15 13:51:24 +00:00
|
|
|
|
|
|
|
import '../components/dialogs/simple_dialogs.dart';
|
|
|
|
import '../components/matrix.dart';
|
|
|
|
import '../views/chat.dart';
|
|
|
|
import 'app_route.dart';
|
2020-09-19 17:21:33 +00:00
|
|
|
import 'matrix_identifier_string_extension.dart';
|
2020-01-19 18:28:12 +00:00
|
|
|
|
|
|
|
class UrlLauncher {
|
|
|
|
final String url;
|
|
|
|
final BuildContext context;
|
|
|
|
const UrlLauncher(this.context, this.url);
|
|
|
|
|
|
|
|
void launchUrl() {
|
2020-09-05 11:45:03 +00:00
|
|
|
if (url.startsWith('https://matrix.to/#/') ||
|
|
|
|
{'#', '@', '!', '+', '\$'}.contains(url[0])) {
|
2020-01-19 18:28:12 +00:00
|
|
|
return openMatrixToUrl();
|
|
|
|
}
|
|
|
|
launch(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
void openMatrixToUrl() async {
|
|
|
|
final matrix = Matrix.of(context);
|
2020-05-13 13:58:59 +00:00
|
|
|
final identifier = url.replaceAll('https://matrix.to/#/', '');
|
2020-09-05 11:45:03 +00:00
|
|
|
if (identifier[0] == '#' || identifier[0] == '!') {
|
2020-09-19 17:21:33 +00: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 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>{};
|
2020-09-28 13:14:16 +00:00
|
|
|
if (room == null && roomIdOrAlias.startsWith('#')) {
|
2020-09-05 11:45:03 +00:00
|
|
|
// we were unable to find the room locally...so resolve it
|
|
|
|
final response =
|
|
|
|
await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
2020-09-19 17:21:33 +00:00
|
|
|
matrix.client.requestRoomAliasInformations(roomIdOrAlias),
|
2020-09-05 11:45:03 +00:00
|
|
|
);
|
|
|
|
if (response != false) {
|
|
|
|
roomId = response.roomId;
|
2020-09-19 17:21:33 +00:00
|
|
|
servers.addAll(response.servers);
|
2020-09-05 11:45:03 +00:00
|
|
|
room = matrix.client.getRoomById(roomId);
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 17:21:33 +00: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 11:45:03 +00:00
|
|
|
if (room != null) {
|
|
|
|
// we have the room, so....just open it!
|
|
|
|
await Navigator.pushAndRemoveUntil(
|
|
|
|
context,
|
2020-09-19 17:21:33 +00:00
|
|
|
AppRoute.defaultRoute(
|
|
|
|
context, ChatView(room.id, scrollToEventId: event)),
|
2020-09-05 11:45:03 +00:00
|
|
|
(r) => r.isFirst,
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2020-09-19 17:21:33 +00:00
|
|
|
if (roomIdOrAlias[0] == '!') {
|
|
|
|
roomId = roomIdOrAlias;
|
2020-09-05 11:45:03 +00:00
|
|
|
}
|
|
|
|
if (await SimpleDialogs(context)
|
2020-09-19 17:21:33 +00:00
|
|
|
.askConfirmation(titleText: 'Join room $roomIdOrAlias')) {
|
2020-09-05 11:45:03 +00:00
|
|
|
final response =
|
|
|
|
await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
|
|
|
matrix.client.joinRoomOrAlias(
|
2020-09-28 13:14:16 +00:00
|
|
|
roomIdOrAlias,
|
|
|
|
servers: servers.isNotEmpty ? servers.toList() : null,
|
2020-09-05 11:45:03 +00:00
|
|
|
),
|
|
|
|
);
|
|
|
|
if (response == false) return;
|
2020-09-28 13:14:16 +00:00
|
|
|
// wait for two seconds so that it probably came down /sync
|
|
|
|
await SimpleDialogs(context).tryRequestWithLoadingDialog(
|
|
|
|
Future.delayed(const Duration(seconds: 2)));
|
2020-09-05 11:45:03 +00:00
|
|
|
await Navigator.pushAndRemoveUntil(
|
|
|
|
context,
|
2020-09-19 17:21:33 +00:00
|
|
|
AppRoute.defaultRoute(
|
2020-09-28 13:14:16 +00:00
|
|
|
context, ChatView(response, scrollToEventId: event)),
|
2020-09-05 11:45:03 +00:00
|
|
|
(r) => r.isFirst,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (identifier[0] == '@') {
|
2020-05-13 13:58:59 +00:00
|
|
|
final user = User(
|
2020-01-19 18:28:12 +00:00
|
|
|
identifier,
|
2020-05-13 13:58:59 +00:00
|
|
|
room: Room(id: '', client: matrix.client),
|
2020-01-19 18:28:12 +00:00
|
|
|
);
|
2020-09-05 11:45:03 +00:00
|
|
|
var roomId = matrix.client.getDirectChatFromUserId(identifier);
|
|
|
|
if (roomId != null) {
|
2020-05-09 11:06:18 +00:00
|
|
|
await Navigator.pushAndRemoveUntil(
|
2020-01-19 18:28:12 +00:00
|
|
|
context,
|
2020-09-05 11:45:03 +00:00
|
|
|
AppRoute.defaultRoute(context, ChatView(roomId)),
|
2020-05-09 11:06:18 +00:00
|
|
|
(r) => r.isFirst,
|
2020-01-19 18:28:12 +00:00
|
|
|
);
|
2020-09-05 11:45:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (await SimpleDialogs(context)
|
|
|
|
.askConfirmation(titleText: 'Message user $identifier')) {
|
|
|
|
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-01-19 18:28:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|