2021-09-19 13:48:23 +02:00
|
|
|
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';
|
2021-09-19 13:48:23 +02:00
|
|
|
import 'package:matrix/encryption/utils/key_verification.dart';
|
|
|
|
import 'package:matrix/matrix.dart';
|
2021-11-16 15:44:09 +01:00
|
|
|
import 'package:path_provider/path_provider.dart';
|
2021-09-19 13:48:23 +02:00
|
|
|
|
2022-06-17 22:17:41 +02:00
|
|
|
import 'package:fluffychat/utils/custom_http_client.dart';
|
2022-04-14 10:37:56 +02:00
|
|
|
import 'package:fluffychat/utils/custom_image_resizer.dart';
|
2022-05-30 13:44:05 +02:00
|
|
|
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_hive_collections_database.dart';
|
2021-10-26 18:50:34 +02:00
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
2021-09-19 13:48:23 +02:00
|
|
|
import 'famedlysdk_store.dart';
|
2021-11-17 12:20:15 +01:00
|
|
|
import 'matrix_sdk_extensions.dart/fluffybox_database.dart';
|
2021-09-19 13:48:23 +02:00
|
|
|
|
|
|
|
abstract class ClientManager {
|
|
|
|
static const String clientNamespace = 'im.fluffychat.store.clients';
|
2022-04-14 07:34:55 +02:00
|
|
|
static Future<List<Client>> getClients({bool initialize = true}) async {
|
2021-09-19 14:22:26 +02:00
|
|
|
if (PlatformInfos.isLinux) {
|
|
|
|
Hive.init((await getApplicationSupportDirectory()).path);
|
|
|
|
} else {
|
|
|
|
await Hive.initFlutter();
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
final clientNames = <String>{};
|
|
|
|
try {
|
2021-11-16 13:20:58 +01:00
|
|
|
final rawClientNames = await Store().getItem(clientNamespace);
|
2021-09-19 13:48:23 +02:00
|
|
|
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);
|
|
|
|
}
|
2021-09-21 09:00:42 +02:00
|
|
|
if (clientNames.isEmpty) {
|
|
|
|
clientNames.add(PlatformInfos.clientName);
|
|
|
|
await Store().setItem(clientNamespace, jsonEncode(clientNames.toList()));
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
final clients = clientNames.map(createClient).toList();
|
2022-04-14 07:34:55 +02:00
|
|
|
if (initialize) {
|
|
|
|
await Future.wait(clients.map((client) => client
|
|
|
|
.init(
|
|
|
|
waitForFirstSync: false,
|
|
|
|
waitUntilLoadCompletedLoaded: false,
|
|
|
|
)
|
|
|
|
.catchError(
|
|
|
|
(e, s) => Logs().e('Unable to initialize client', e, s))));
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
if (clients.length > 1 && clients.any((c) => !c.isLogged())) {
|
|
|
|
final loggedOutClients = clients.where((c) => !c.isLogged()).toList();
|
|
|
|
for (final client in loggedOutClients) {
|
2021-09-20 11:52:02 +02:00
|
|
|
Logs().w(
|
|
|
|
'Multi account is enabled but client ${client.userID} is not logged in. Removing...');
|
2021-09-19 13:48:23 +02:00
|
|
|
clientNames.remove(client.clientName);
|
|
|
|
clients.remove(client);
|
|
|
|
}
|
2021-11-16 13:20:58 +01:00
|
|
|
await Store().setItem(clientNamespace, jsonEncode(clientNames.toList()));
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2021-11-24 18:39:40 +01:00
|
|
|
static Future<void> removeClientNameFromStore(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.remove(clientName);
|
|
|
|
await Store().setItem(clientNamespace, jsonEncode(clientNamesList));
|
|
|
|
}
|
|
|
|
|
2022-08-14 16:31:36 +02:00
|
|
|
static NativeImplementations get nativeImplementations => kIsWeb
|
2022-08-14 17:19:27 +02:00
|
|
|
? const NativeImplementationsDummy()
|
2022-08-14 16:31:36 +02:00
|
|
|
: NativeImplementationsIsolate(compute);
|
|
|
|
|
2022-06-17 22:17:41 +02:00
|
|
|
static Client createClient(String clientName) {
|
|
|
|
return Client(
|
|
|
|
clientName,
|
2022-06-21 14:50:27 +02:00
|
|
|
httpClient:
|
|
|
|
PlatformInfos.isAndroid ? CustomHttpClient.createHTTPClient() : null,
|
2022-06-17 22:17:41 +02:00
|
|
|
verificationMethods: {
|
|
|
|
KeyVerificationMethod.numbers,
|
|
|
|
if (kIsWeb || PlatformInfos.isMobile || PlatformInfos.isLinux)
|
|
|
|
KeyVerificationMethod.emoji,
|
|
|
|
},
|
|
|
|
importantStateEvents: <String>{
|
|
|
|
// To make room emotes work
|
|
|
|
'im.ponies.room_emotes',
|
|
|
|
// To check which story room we can post in
|
|
|
|
EventTypes.RoomPowerLevels,
|
|
|
|
},
|
2022-07-29 12:49:26 +02:00
|
|
|
logLevel: kReleaseMode ? Level.warning : Level.verbose,
|
2022-08-28 10:15:56 +02:00
|
|
|
databaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder,
|
|
|
|
legacyDatabaseBuilder: FlutterFluffyBoxDatabase.databaseBuilder,
|
2022-06-17 22:17:41 +02:00
|
|
|
supportedLoginTypes: {
|
|
|
|
AuthenticationTypes.password,
|
|
|
|
if (PlatformInfos.isMobile ||
|
|
|
|
PlatformInfos.isWeb ||
|
|
|
|
PlatformInfos.isMacOS)
|
|
|
|
AuthenticationTypes.sso
|
|
|
|
},
|
2022-08-14 16:31:36 +02:00
|
|
|
nativeImplementations: nativeImplementations,
|
2022-06-17 22:17:41 +02:00
|
|
|
customImageResizer: PlatformInfos.isMobile ? customImageResizer : null,
|
|
|
|
);
|
|
|
|
}
|
2021-09-19 13:48:23 +02:00
|
|
|
}
|