Compare commits

...
Sign in to create a new pull request.

1 commit

Author SHA1 Message Date
Christian Pauly
e5eaea74c1 refactor: Switch to Hive Collections DB 2022-06-07 08:46:35 +02:00
8 changed files with 84 additions and 48 deletions

View file

@ -46,13 +46,11 @@ class SearchView extends StatelessWidget {
}).then((QueryPublicRoomsResponse res) { }).then((QueryPublicRoomsResponse res) {
final genericSearchTerm = controller.genericSearchTerm; final genericSearchTerm = controller.genericSearchTerm;
if (genericSearchTerm != null && if (genericSearchTerm != null &&
!res.chunk.any((room) => !res.chunk.any(
(room.aliases?.contains(controller.genericSearchTerm) ?? false) || (room) => room.canonicalAlias == controller.genericSearchTerm)) {
room.canonicalAlias == controller.genericSearchTerm)) {
// we have to tack on the original alias // we have to tack on the original alias
res.chunk.add( res.chunk.add(
PublicRoomsChunk( PublicRoomsChunk(
aliases: [genericSearchTerm],
name: genericSearchTerm, name: genericSearchTerm,
numJoinedMembers: 0, numJoinedMembers: 0,
roomId: '!unknown', roomId: '!unknown',

View file

@ -8,10 +8,10 @@ import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import 'package:fluffychat/utils/custom_image_resizer.dart'; import 'package:fluffychat/utils/custom_image_resizer.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_hive_collections_database.dart';
import 'package:fluffychat/utils/platform_infos.dart'; import 'package:fluffychat/utils/platform_infos.dart';
import 'famedlysdk_store.dart'; import 'famedlysdk_store.dart';
import 'matrix_sdk_extensions.dart/fluffybox_database.dart'; import 'matrix_sdk_extensions.dart/fluffybox_database.dart';
import 'matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart';
abstract class ClientManager { abstract class ClientManager {
static const String clientNamespace = 'im.fluffychat.store.clients'; static const String clientNamespace = 'im.fluffychat.store.clients';
@ -95,8 +95,8 @@ abstract class ClientManager {
// To check which story room we can post in // To check which story room we can post in
EventTypes.RoomPowerLevels, EventTypes.RoomPowerLevels,
}, },
databaseBuilder: FlutterFluffyBoxDatabase.databaseBuilder, databaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder,
legacyDatabaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder, legacyDatabaseBuilder: FlutterFluffyBoxDatabase.databaseBuilder,
supportedLoginTypes: { supportedLoginTypes: {
AuthenticationTypes.password, AuthenticationTypes.password,
if (PlatformInfos.isMobile || if (PlatformInfos.isMobile ||

View file

@ -14,6 +14,7 @@ import 'package:path_provider/path_provider.dart';
import '../client_manager.dart'; import '../client_manager.dart';
import '../famedlysdk_store.dart'; import '../famedlysdk_store.dart';
// ignore: deprecated_member_use
class FlutterFluffyBoxDatabase extends FluffyBoxDatabase { class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
FlutterFluffyBoxDatabase( FlutterFluffyBoxDatabase(
String name, String name,
@ -27,6 +28,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
static const String _cipherStorageKey = 'database_encryption_key'; static const String _cipherStorageKey = 'database_encryption_key';
// ignore: deprecated_member_use
static Future<FluffyBoxDatabase> databaseBuilder(Client client) async { static Future<FluffyBoxDatabase> databaseBuilder(Client client) async {
Logs().d('Open FluffyBox...'); Logs().d('Open FluffyBox...');
fluffybox.HiveAesCipher? hiverCipher; fluffybox.HiveAesCipher? hiverCipher;
@ -59,6 +61,7 @@ class FlutterFluffyBoxDatabase extends FluffyBoxDatabase {
rethrow; rethrow;
} }
// ignore: deprecated_member_use
final db = FluffyBoxDatabase( final db = FluffyBoxDatabase(
'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}', 'fluffybox_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
await _findDatabasePath(client), await _findDatabasePath(client),

View file

@ -2,78 +2,108 @@ import 'dart:convert';
import 'dart:io'; import 'dart:io';
import 'dart:typed_data'; import 'dart:typed_data';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart' hide Key;
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:hive/hive.dart'; import 'package:hive/hive.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart'; import 'package:path_provider/path_provider.dart';
import '../platform_infos.dart'; class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase {
FlutterHiveCollectionsDatabase(
class FlutterMatrixHiveStore extends FamedlySdkHiveDatabase { String name,
FlutterMatrixHiveStore(String name, {HiveCipher? encryptionCipher}) String path, {
: super( HiveCipher? key,
}) : super(
name, name,
encryptionCipher: encryptionCipher, path,
key: key,
); );
static bool _hiveInitialized = false; static const String _cipherStorageKey = 'database_encryption_key';
static const String _hiveCipherStorageKey = 'hive_encryption_key';
static Future<FamedlySdkHiveDatabase> hiveDatabaseBuilder( static Future<FlutterHiveCollectionsDatabase> databaseBuilder(
Client client) async { Client client) async {
if (!kIsWeb && !_hiveInitialized) { Logs().d('Open Hive...');
_hiveInitialized = true; HiveAesCipher? hiverCipher;
}
HiveCipher? hiverCipher;
try { try {
// Workaround for secure storage is calling Platform.operatingSystem on web // Workaround for secure storage is calling Platform.operatingSystem on web
if (kIsWeb || Platform.isLinux) throw MissingPluginException(); if (kIsWeb) throw MissingPluginException();
const secureStorage = FlutterSecureStorage(); const secureStorage = FlutterSecureStorage();
final containsEncryptionKey = final containsEncryptionKey =
await secureStorage.containsKey(key: _hiveCipherStorageKey); await secureStorage.containsKey(key: _cipherStorageKey);
if (!containsEncryptionKey) { if (!containsEncryptionKey) {
// do not try to create a buggy secure storage for new Linux users
if (Platform.isLinux) throw MissingPluginException();
final key = Hive.generateSecureKey(); final key = Hive.generateSecureKey();
await secureStorage.write( await secureStorage.write(
key: _hiveCipherStorageKey, key: _cipherStorageKey,
value: base64UrlEncode(key), value: base64UrlEncode(key),
); );
} }
// workaround for if we just wrote to the key and it still doesn't exist // workaround for if we just wrote to the key and it still doesn't exist
final rawEncryptionKey = final rawEncryptionKey = await secureStorage.read(key: _cipherStorageKey);
await secureStorage.read(key: _hiveCipherStorageKey);
if (rawEncryptionKey == null) throw MissingPluginException(); if (rawEncryptionKey == null) throw MissingPluginException();
final encryptionKey = base64Url.decode(rawEncryptionKey); hiverCipher = HiveAesCipher(base64Url.decode(rawEncryptionKey));
hiverCipher = HiveAesCipher(encryptionKey);
} on MissingPluginException catch (_) { } on MissingPluginException catch (_) {
Logs().i('Hive encryption is not supported on this platform'); Logs().i('Hive encryption is not supported on this platform');
} catch (_) {
const FlutterSecureStorage().delete(key: _cipherStorageKey);
rethrow;
} }
final db = FlutterMatrixHiveStore(
client.clientName, final db = FlutterHiveCollectionsDatabase(
encryptionCipher: hiverCipher, 'hive_collections_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
await _findDatabasePath(client),
key: hiverCipher,
); );
try { try {
await db.open(); await db.open();
} catch (e, s) { } catch (_) {
Logs().e('Unable to open Hive. Delete and try again...', e, s); Logs().w('Unable to open Hive. Delete database and storage key...');
const FlutterSecureStorage().delete(key: _cipherStorageKey);
await db.clear(); await db.clear();
await db.open(); rethrow;
} }
Logs().d('Hive is ready');
return db; return db;
} }
static Future<String> _findDatabasePath(Client client) async {
String path = client.clientName;
if (!kIsWeb) {
Directory directory;
try {
if (Platform.isLinux) {
directory = await getApplicationSupportDirectory();
} else {
directory = await getApplicationDocumentsDirectory();
}
} catch (_) {
try {
directory = await getLibraryDirectory();
} catch (_) {
directory = Directory.current;
}
}
// do not destroy your stable FluffyChat in debug mode
if (kDebugMode) {
directory = Directory(directory.uri.resolve('debug').toFilePath());
directory.create(recursive: true);
}
path = directory.path;
}
return path;
}
@override @override
int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0; int get maxFileSize => supportsFileStoring ? 100 * 1024 * 1024 : 0;
@override @override
bool get supportsFileStoring => (PlatformInfos.isIOS || bool get supportsFileStoring => !kIsWeb;
PlatformInfos.isAndroid ||
PlatformInfos.isDesktop);
Future<String> _getFileStoreDirectory() async { Future<String> _getFileStoreDirectory() async {
try { try {

View file

@ -41,9 +41,7 @@ class PublicRoomBottomSheet extends StatelessWidget {
} }
} }
bool _testRoom(PublicRoomsChunk r) => bool _testRoom(PublicRoomsChunk r) => r.canonicalAlias == roomAlias;
r.canonicalAlias == roomAlias ||
(r.aliases?.contains(roomAlias) ?? false);
Future<PublicRoomsChunk> _search(BuildContext context) async { Future<PublicRoomsChunk> _search(BuildContext context) async {
final chunk = this.chunk; final chunk = this.chunk;

View file

@ -395,6 +395,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "5.0.1" version: "5.0.1"
enhanced_enum:
dependency: transitive
description:
name: enhanced_enum
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.2"
fake_async: fake_async:
dependency: transitive dependency: transitive
description: description:
@ -788,7 +795,7 @@ packages:
name: hive name: hive
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.1.0" version: "2.2.1"
hive_flutter: hive_flutter:
dependency: "direct main" dependency: "direct main"
description: description:
@ -1012,21 +1019,21 @@ packages:
name: matrix name: matrix
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.9.6" version: "0.9.9"
matrix_api_lite: matrix_api_lite:
dependency: transitive dependency: transitive
description: description:
name: matrix_api_lite name: matrix_api_lite
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.5.3" version: "1.0.0"
matrix_homeserver_recommendations: matrix_homeserver_recommendations:
dependency: "direct main" dependency: "direct main"
description: description:
name: matrix_homeserver_recommendations name: matrix_homeserver_recommendations
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.2.0" version: "0.2.1"
matrix_link_text: matrix_link_text:
dependency: "direct main" dependency: "direct main"
description: description:

View file

@ -57,7 +57,7 @@ dependencies:
keyboard_shortcuts: ^0.1.4 keyboard_shortcuts: ^0.1.4
localstorage: ^4.0.0+1 localstorage: ^4.0.0+1
lottie: ^1.2.2 lottie: ^1.2.2
matrix: ^0.9.4 matrix: ^0.9.9
matrix_homeserver_recommendations: ^0.2.0 matrix_homeserver_recommendations: ^0.2.0
matrix_link_text: ^1.0.2 matrix_link_text: ^1.0.2
native_imaging: native_imaging:

View file

@ -2,7 +2,7 @@ import 'package:matrix/encryption/utils/key_verification.dart';
import 'package:matrix/matrix.dart'; import 'package:matrix/matrix.dart';
import 'package:matrix_api_lite/fake_matrix_api.dart'; import 'package:matrix_api_lite/fake_matrix_api.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_matrix_hive_database.dart'; import 'package:fluffychat/utils/matrix_sdk_extensions.dart/flutter_hive_collections_database.dart';
Future<Client> prepareTestClient({ Future<Client> prepareTestClient({
bool loggedIn = false, bool loggedIn = false,
@ -20,7 +20,7 @@ Future<Client> prepareTestClient({
importantStateEvents: <String>{ importantStateEvents: <String>{
'im.ponies.room_emotes', // we want emotes to work properly 'im.ponies.room_emotes', // we want emotes to work properly
}, },
databaseBuilder: FlutterMatrixHiveStore.hiveDatabaseBuilder, databaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder,
supportedLoginTypes: { supportedLoginTypes: {
AuthenticationTypes.password, AuthenticationTypes.password,
AuthenticationTypes.sso AuthenticationTypes.sso