Skip to content

Commit

Permalink
feat: Try out FluffyBox 2 database
Browse files Browse the repository at this point in the history
  • Loading branch information
krille-chan committed Nov 27, 2023
1 parent ee599c1 commit 6c6f671
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 16 deletions.
2 changes: 1 addition & 1 deletion android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ android {

defaultConfig {
applicationId "chat.fluffy.fluffychat"
minSdkVersion 19
minSdkVersion 21
targetSdkVersion 33
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
Expand Down
1 change: 1 addition & 0 deletions android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-keep class net.sqlcipher.** { *; }
4 changes: 3 additions & 1 deletion lib/utils/client_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:fluffychat/utils/custom_http_client.dart';
import 'package:fluffychat/utils/custom_image_resizer.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/flutter_hive_collections_database.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'matrix_sdk_extensions/flutter_matrix_sdk_database_builder.dart';

abstract class ClientManager {
static const String clientNamespace = 'im.fluffychat.store.clients';
Expand Down Expand Up @@ -102,7 +103,8 @@ abstract class ClientManager {
EventTypes.RoomPowerLevels,
},
logLevel: kReleaseMode ? Level.warning : Level.verbose,
databaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder,
databaseBuilder: flutterMatrixSdkDatabaseBuilder,
legacyDatabaseBuilder: FlutterHiveCollectionsDatabase.databaseBuilder,
supportedLoginTypes: {
AuthenticationTypes.password,
AuthenticationTypes.sso,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase {

final db = FlutterHiveCollectionsDatabase(
'hive_collections_${client.clientName.replaceAll(' ', '_').toLowerCase()}',
await _findDatabasePath(client),
await findDatabasePath(client),
key: hiverCipher,
);
try {
Expand All @@ -80,7 +80,7 @@ class FlutterHiveCollectionsDatabase extends HiveCollectionsDatabase {
return db;
}

static Future<String> _findDatabasePath(Client client) async {
static Future<String> findDatabasePath(Client client) async {
String path = client.clientName;
if (!kIsWeb) {
Directory directory;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:convert';
import 'dart:math';

import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:matrix/matrix.dart';
import 'package:path_provider/path_provider.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart' as ffi;
import 'package:sqflite_sqlcipher/sqflite.dart';
import 'package:universal_html/html.dart' as html;

import 'package:fluffychat/utils/platform_infos.dart';

Future<MatrixSdkDatabase> flutterMatrixSdkDatabaseBuilder(Client client) async {
final database = await _constructDatabase(client);
await database.open();
return database;
}

Future<MatrixSdkDatabase> _constructDatabase(Client client) async {
if (kIsWeb) {
html.window.navigator.storage?.persist();
return MatrixSdkDatabase(client.clientName);
}
if (PlatformInfos.isDesktop) {
final path = await getApplicationSupportDirectory();
return MatrixSdkDatabase(
client.clientName,
database: await ffi.databaseFactoryFfi.openDatabase(
'$path/${client.clientName}',
),
maxFileSize: 1024 * 1024 * 10,
fileStoragePath: path,
deleteFilesAfterDuration: const Duration(days: 30),
);
}

final path = await getDatabasesPath();
const passwordStorageKey = 'database_password';
String? password;

try {
// Workaround for secure storage is calling Platform.operatingSystem on web
if (kIsWeb) throw MissingPluginException();

const secureStorage = FlutterSecureStorage();
final containsEncryptionKey =
await secureStorage.read(key: passwordStorageKey) != null;
if (!containsEncryptionKey) {
final rng = Random.secure();
final list = Uint8List(32);
list.setAll(0, Iterable.generate(list.length, (i) => rng.nextInt(256)));
final newPassword = base64UrlEncode(list);
await secureStorage.write(
key: passwordStorageKey,
value: newPassword,
);
}
// workaround for if we just wrote to the key and it still doesn't exist
password = await secureStorage.read(key: passwordStorageKey);
if (password == null) throw MissingPluginException();
} on MissingPluginException catch (_) {
const FlutterSecureStorage()
.delete(key: passwordStorageKey)
.catchError((_) {});
Logs().i('Database encryption is not supported on this platform');
} catch (e, s) {
const FlutterSecureStorage()
.delete(key: passwordStorageKey)
.catchError((_) {});
Logs().w('Unable to init database encryption', e, s);
}

return MatrixSdkDatabase(
client.clientName,
database: await openDatabase(
'$path/${client.clientName}',
password: password,
),
maxFileSize: 1024 * 1024 * 10,
fileStoragePath: await getTemporaryDirectory(),
deleteFilesAfterDuration: const Duration(days: 30),
);
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import record_macos
import share_plus
import shared_preferences_foundation
import sqflite
import sqflite_sqlcipher
import url_launcher_macos
import video_compress
import video_player_avfoundation
Expand Down Expand Up @@ -60,6 +61,7 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharePlusMacosPlugin.register(with: registry.registrar(forPlugin: "SharePlusMacosPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
SqfliteSqlCipherPlugin.register(with: registry.registrar(forPlugin: "SqfliteSqlCipherPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
VideoCompressPlugin.register(with: registry.registrar(forPlugin: "VideoCompressPlugin"))
FVPVideoPlayerPlugin.register(with: registry.registrar(forPlugin: "FVPVideoPlayerPlugin"))
Expand Down
51 changes: 46 additions & 5 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.2"
idb_shim:
dependency: "direct main"
description:
name: idb_shim
sha256: cf1b3870ad63a6f5393edb351a9c9364e739731f2176c27f44d9645327c2cdd5
url: "https://pub.dev"
source: hosted
version: "2.3.2"
image:
dependency: transitive
description:
Expand Down Expand Up @@ -1160,10 +1168,11 @@ packages:
matrix:
dependency: "direct main"
description:
name: matrix
sha256: "4c94857b53674067b7aeb39723220ee7834128ffde3d54f8185c73a6784bc919"
url: "https://pub.dev"
source: hosted
path: "."
ref: "krille/add-matrix-sdk-database"
resolved-ref: "492a1c7f0b6d0f350a9c6a067802fc4b4bc5f095"
url: "https://github.com/famedly/matrix-dart-sdk.git"
source: git
version: "0.23.0"
matrix_api_lite:
dependency: transitive
Expand Down Expand Up @@ -1621,6 +1630,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.3.2"
sembast:
dependency: transitive
description:
name: sembast
sha256: "85ff944434f7b566fdc388be4f85b23e954736b7d6e51f965f4f419d966c15b1"
url: "https://pub.dev"
source: hosted
version: "3.5.0+1"
sentiment_dart:
dependency: transitive
description:
Expand Down Expand Up @@ -1723,7 +1740,7 @@ packages:
source: hosted
version: "1.10.0"
sqflite:
dependency: transitive
dependency: "direct main"
description:
name: sqflite
sha256: "591f1602816e9c31377d5f008c2d9ef7b8aca8941c3f89cc5fd9d84da0c38a9a"
Expand All @@ -1738,6 +1755,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.5.0+2"
sqflite_common_ffi:
dependency: "direct main"
description:
name: sqflite_common_ffi
sha256: "35d2fce1e971707c227cc4775cc017d5eafe06c2654c3435ebd5c3ad6c170f5f"
url: "https://pub.dev"
source: hosted
version: "2.3.0+4"
sqflite_sqlcipher:
dependency: "direct main"
description:
name: sqflite_sqlcipher
sha256: e1dfb55bf21ee5a18c43f28faa4291272a801da4ab34a6ba9973b6c0e1ed77da
url: "https://pub.dev"
source: hosted
version: "2.2.1"
sqlite3:
dependency: transitive
description:
name: sqlite3
sha256: db65233e6b99e99b2548932f55a987961bc06d82a31a0665451fa0b4fff4c3fb
url: "https://pub.dev"
source: hosted
version: "2.1.0"
stack_trace:
dependency: transitive
description:
Expand Down
8 changes: 8 additions & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dependencies:
hive: ^2.2.3
hive_flutter: ^1.1.0
http: ^0.13.6
idb_shim: ^2.3.0+2
image_picker: ^1.0.0
intl: any
just_audio: ^0.9.30
Expand All @@ -79,6 +80,9 @@ dependencies:
share_plus: ^7.2.1
shared_preferences: ^2.2.0 # Pinned because https://github.com/flutter/flutter/issues/118401
slugify: ^2.0.0
sqflite: ^2.2.8+2
sqflite_common_ffi: ^2.2.5
sqflite_sqlcipher: ^2.1.1+1
swipe_to_action: ^0.2.0
tor_detector_web: ^1.1.0
uni_links: ^0.5.1
Expand Down Expand Up @@ -158,6 +162,10 @@ dependency_overrides:
git:
url: https://github.com/TheOneWithTheBraid/keyboard_shortcuts.git
ref: null-safety
matrix:
git:
url: https://github.com/famedly/matrix-dart-sdk.git
ref: krille/add-matrix-sdk-database
# blocked upgrade of package_info_plus for null safety
# https://github.com/creativecreatorormaybenot/wakelock/pull/203
wakelock_windows:
Expand Down
16 changes: 9 additions & 7 deletions scripts/enable-android-google-services.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/android/app/build.gradle b/android/app/build.gradle
index 001fbd72..339b35af 100644
index bf972f30..46cebdc6 100644
--- a/android/app/build.gradle
+++ b/android/app/build.gradle
@@ -70,6 +70,10 @@
Expand Down Expand Up @@ -28,13 +28,15 @@ index 001fbd72..339b35af 100644
-//apply plugin: 'com.google.gms.google-services'
+apply plugin: 'com.google.gms.google-services'
diff --git a/android/app/proguard-rules.pro b/android/app/proguard-rules.pro
new file mode 100644
index 00000000..40570865
--- /dev/null
index d0e0fbc9..0a546da0 100644
--- a/android/app/proguard-rules.pro
+++ b/android/app/proguard-rules.pro
@@ -0,0 +1,41 @@
@@ -1 +1,42 @@
--keep class net.sqlcipher.** { *; }
\ No newline at end of file
+-optimizationpasses 5
+## Flutter wrapper
+-keep class net.sqlcipher.** { *; }
+-keep class io.flutter.app.** { *; }
+-keep class io.flutter.plugin.** { *; }
+-keep class io.flutter.util.** { *; }
Expand Down Expand Up @@ -110,7 +112,7 @@ index 1afc4606..894d1571 100644
return provideEngine(this)
}
diff --git a/android/build.gradle b/android/build.gradle
index 85aa8647..3b7e09e7 100644
index bd394967..2e9d54de 100644
--- a/android/build.gradle
+++ b/android/build.gradle
@@ -8,7 +8,7 @@ buildscript {
Expand Down Expand Up @@ -145,7 +147,7 @@ index 8e67ae92..da4da5c3 100644
DateTime? lastReceivedPush;

diff --git a/pubspec.yaml b/pubspec.yaml
index 6999d0b8..b2c9144f 100644
index 193e6ed6..f70e48d4 100644
--- a/pubspec.yaml
+++ b/pubspec.yaml
@@ -26,7 +26,7 @@ dependencies:
Expand Down

0 comments on commit 6c6f671

Please sign in to comment.