fluffychat/lib/utils/client_manager.dart

99 lines
3.6 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'package:flutter/foundation.dart';
2021-10-26 18:50:34 +02:00
2021-09-19 14:22:26 +02:00
import 'package:hive_flutter/hive_flutter.dart';
import 'package:matrix/encryption/utils/key_verification.dart';
import 'package:matrix/matrix.dart';
2021-11-16 12:00:22 +01:00
import 'package:matrix/src/utils/run_benchmarked.dart';
2021-09-19 14:22:26 +02:00
import 'package:path_provider/path_provider.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
import 'famedlysdk_store.dart';
import 'matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart';
2021-11-11 18:09:04 +01:00
import 'matrix_sdk_extensions.dart/flutter_matrix_sembast_database.dart';
abstract class ClientManager {
static const String clientNamespace = 'im.fluffychat.store.clients';
static Future<List<Client>> getClients() async {
2021-11-16 12:34:53 +01:00
Logs().level = Level.verbose;
2021-09-19 14:22:26 +02:00
if (PlatformInfos.isLinux) {
Hive.init((await getApplicationSupportDirectory()).path);
} else {
await Hive.initFlutter();
}
final clientNames = <String>{};
try {
2021-11-16 12:00:22 +01:00
final rawClientNames = await runBenchmarked(
'Get client names',
() => Store().getItem(clientNamespace),
);
if (rawClientNames != null) {
final clientNamesList =
(jsonDecode(rawClientNames) as List).cast<String>();
clientNames.addAll(clientNamesList);
}
} catch (e, s) {
Logs().w('Client names in store are corrupted', e, s);
await Store().deleteItem(clientNamespace);
}
if (clientNames.isEmpty) {
clientNames.add(PlatformInfos.clientName);
await Store().setItem(clientNamespace, jsonEncode(clientNames.toList()));
}
final clients = clientNames.map(createClient).toList();
2021-11-16 12:00:22 +01:00
await Future.wait(clients.map((client) => runBenchmarked(
'Init client ${client.clientName}',
() => client.init(
waitForFirstSync: false,
waitUntilLoadCompletedLoaded: false,
))
.catchError((e, s) => Logs().e('Unable to initialize client', e, s))));
if (clients.length > 1 && clients.any((c) => !c.isLogged())) {
final loggedOutClients = clients.where((c) => !c.isLogged()).toList();
for (final client in loggedOutClients) {
Logs().w(
'Multi account is enabled but client ${client.userID} is not logged in. Removing...');
clientNames.remove(client.clientName);
clients.remove(client);
}
2021-11-16 12:00:22 +01:00
await runBenchmarked(
'Update clientNamespaces',
() =>
Store().setItem(clientNamespace, jsonEncode(clientNames.toList())),
);
}
return clients;
}
static Future<void> addClientNameToStore(String clientName) async {
final clientNamesList = <String>[];
final rawClientNames = await Store().getItem(clientNamespace);
if (rawClientNames != null) {
final stored = (jsonDecode(rawClientNames) as List).cast<String>();
clientNamesList.addAll(stored);
}
clientNamesList.add(clientName);
await Store().setItem(clientNamespace, jsonEncode(clientNamesList));
}
static Client createClient(String clientName) => Client(
clientName,
enableE2eeRecovery: true,
verificationMethods: {
KeyVerificationMethod.numbers,
if (PlatformInfos.isMobile || PlatformInfos.isLinux)
KeyVerificationMethod.emoji,
},
2021-11-11 18:09:04 +01:00
importantStateEvents: <String>{'im.ponies.room_emotes'},
databaseBuilder: FlutterMatrixSembastDatabase.databaseBuilder,
legacyDatabaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder,
supportedLoginTypes: {
AuthenticationTypes.password,
2021-11-15 12:14:00 +01:00
if (PlatformInfos.isMobile || PlatformInfos.isWeb)
AuthenticationTypes.sso
},
compute: compute,
);
}