FurryChat/lib/utils/url_launcher.dart

106 lines
3.3 KiB
Dart
Raw Normal View History

2020-01-19 18:28:12 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-04-27 11:36:39 +00:00
import 'package:fluffychat/components/dialogs/simple_dialogs.dart';
2020-01-19 18:28:12 +00:00
import 'package:fluffychat/components/matrix.dart';
import 'package:fluffychat/utils/app_route.dart';
import 'package:fluffychat/views/chat.dart';
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() {
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] == '!') {
var room = matrix.client.getRoomByAlias(identifier);
room ??= matrix.client.getRoomById(identifier);
var roomId = room?.id;
var servers = <String>[];
if (room == null && identifier == '#') {
// we were unable to find the room locally...so resolve it
final response =
await SimpleDialogs(context).tryRequestWithLoadingDialog(
matrix.client.requestRoomAliasInformations(identifier),
);
if (response != false) {
roomId = response.roomId;
servers = response.servers;
room = matrix.client.getRoomById(roomId);
}
}
if (room != null) {
// we have the room, so....just open it!
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(room.id)),
(r) => r.isFirst,
);
return;
}
if (identifier == '!') {
roomId = identifier;
}
if (roomId == null) {
// we haven't found this room....so let's ignore it
return;
}
if (await SimpleDialogs(context)
.askConfirmation(titleText: 'Join room $identifier')) {
final response =
await SimpleDialogs(context).tryRequestWithLoadingDialog(
matrix.client.joinRoomOrAlias(
Uri.encodeComponent(roomId),
servers: servers,
),
);
if (response == false) return;
await Navigator.pushAndRemoveUntil(
context,
AppRoute.defaultRoute(context, ChatView(response['room_id'])),
(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
}
}
}
}