From 87826dfd7e0223cb82c0cd1187f9c667406651a0 Mon Sep 17 00:00:00 2001 From: ereio Date: Tue, 15 Dec 2020 21:04:48 -0500 Subject: [PATCH 1/3] small bug fixes and tweaks for performance --- lib/global/cache/index.dart | 25 ++++++++++ lib/global/colours.dart | 3 +- lib/store/auth/actions.dart | 6 +++ lib/store/crypto/events/actions.dart | 14 +++++- lib/store/rooms/actions.dart | 70 +++++++++++++++++++++++----- pubspec.yaml | 1 + 6 files changed, 106 insertions(+), 13 deletions(-) diff --git a/lib/global/cache/index.dart b/lib/global/cache/index.dart index 6fd1ce51b..c4a12055b 100644 --- a/lib/global/cache/index.dart +++ b/lib/global/cache/index.dart @@ -95,6 +95,31 @@ void closeCache(Database cache) async { } } +Future deleteCache({Database cache}) async { + try { + var cacheFactory; + var cachePath = '${Cache.cacheKeyMain}.db'; + + if (Platform.isAndroid || Platform.isIOS) { + var directory = await getApplicationDocumentsDirectory(); + await directory.create(); + cachePath = join(directory.path, '${Cache.cacheKeyMain}.db'); + cacheFactory = databaseFactoryIo; + } + + if (Platform.isLinux || Platform.isWindows || Platform.isMacOS) { + cacheFactory = getDatabaseFactorySqflite( + sqflite_ffi.databaseFactoryFfi, + ); + } + + Cache.cacheMain = await cacheFactory.deleteDatabase(cachePath); + } catch (error) { + printError('[initCache] ${error}'); + return null; + } +} + String createIVKey() { return Key.fromSecureRandom(16).base64; } diff --git a/lib/global/colours.dart b/lib/global/colours.dart index 411630986..51cc6c65e 100644 --- a/lib/global/colours.dart +++ b/lib/global/colours.dart @@ -30,7 +30,8 @@ class Colours { static const chatTeal = 0xFF00796B; static const chatBlue = 0xFF1976D2; - static Color hashedColor(String hashable) { + static Color hashedColor(String string) { + final hashable = string ?? '123'; int hash = hashable.codeUnits.reduce((value, element) => value + element); return Colours.chatColors[hash % Colours.chatColors.length]; } diff --git a/lib/store/auth/actions.dart b/lib/store/auth/actions.dart index 96610f464..3e920071f 100644 --- a/lib/store/auth/actions.dart +++ b/lib/store/auth/actions.dart @@ -12,6 +12,7 @@ import 'package:device_info/device_info.dart'; import 'package:flutter_dotenv/flutter_dotenv.dart'; import 'package:redux/redux.dart'; import 'package:redux_thunk/redux_thunk.dart'; +import 'package:syphon/global/cache/index.dart'; // Project imports: import 'package:syphon/global/libs/matrix/auth.dart'; @@ -382,8 +383,13 @@ ThunkAction logoutUser() { } } + // wipe cache + await deleteCache(); + await initCache(); + // wipe cold storage await deleteStorage(); + await initStorage(); // tell authObserver to wipe auth user store.state.authStore.authObserver.add(null); diff --git a/lib/store/crypto/events/actions.dart b/lib/store/crypto/events/actions.dart index 61ea2f3c3..f5578481d 100644 --- a/lib/store/crypto/events/actions.dart +++ b/lib/store/crypto/events/actions.dart @@ -18,6 +18,7 @@ import 'package:syphon/store/crypto/actions.dart'; import 'package:syphon/store/crypto/model.dart'; import 'package:syphon/store/index.dart'; import 'package:syphon/store/events/model.dart'; +import 'package:syphon/store/rooms/actions.dart'; /** * Encrypt event content with loaded outbound session for room @@ -312,12 +313,23 @@ ThunkAction syncDevice(Map toDeviceRaw) { ); if (EventTypes.roomKey == eventDecrypted['type']) { - return await store.dispatch( + // save decrepted user session key under roomId + await store.dispatch( saveSessionKey( event: eventDecrypted, identityKey: identityKeySender, ), ); + + try { + // redecrypt events in the room with new key + final roomId = eventDecrypted['content']['room_id']; + Map room = {roomId: {}}; + + return await store.dispatch(syncRooms(room)); + } catch (error) { + debugPrint('[syncRooms|error] $error'); + } } } catch (error) { debugPrint('[decryptKeyEvent|error] $error'); diff --git a/lib/store/rooms/actions.dart b/lib/store/rooms/actions.dart index 639d88b5a..27583e7f9 100644 --- a/lib/store/rooms/actions.dart +++ b/lib/store/rooms/actions.dart @@ -202,11 +202,60 @@ ThunkAction syncRooms(Map roomData) { * Takes a negligible amount of time * */ -ThunkAction fetchRooms() { +ThunkAction fetchRoom(String roomId) { return (Store store) async { try { - store.dispatch(SetLoading(loading: true)); + final stateEvents = await MatrixApi.fetchStateEvents( + protocol: protocol, + homeserver: store.state.authStore.user.homeserver, + accessToken: store.state.authStore.user.accessToken, + roomId: roomId, + ); + + if (!(stateEvents is List) && stateEvents['errcode'] != null) { + throw stateEvents['error']; + } + + final messageEvents = await compute( + MatrixApi.fetchMessageEventsMapped, + { + "protocol": protocol, + "homeserver": store.state.authStore.user.homeserver, + "accessToken": store.state.authStore.user.accessToken, + "roomId": roomId, + "limit": 20, + }, + ); + + await store.dispatch(syncRooms({ + '${roomId}': { + 'state': { + 'events': stateEvents, + 'prev_batch': messageEvents['from'], + }, + 'timeline': { + 'events': messageEvents['chunk'], + } + }, + })); + } catch (error) { + debugPrint('[fetchRooms] ${roomId} $error'); + } finally { + store.dispatch(UpdateRoom(id: roomId, syncing: false)); + } + }; +} +/** + * + * Fetch Rooms (w/o /sync) + * + * Takes a negligible amount of time + * + */ +ThunkAction fetchRooms() { + return (Store store) async { + try { final data = await MatrixApi.fetchRoomIds( protocol: protocol, homeserver: store.state.authStore.user.homeserver, @@ -768,8 +817,9 @@ ThunkAction joinRoom({Room room}) { room: joinedRoom.copyWith(invite: false), )); - await store.dispatch(fetchRooms()); - await store.dispatch(fetchDirectRooms()); + store.dispatch(SetLoading(loading: true)); + await store.dispatch(fetchRoom(joinedRoom.id)); + store.dispatch(SetLoading(loading: false)); } catch (error) { store.dispatch(addAlert(error: error, origin: 'joinRoom')); } @@ -777,10 +827,8 @@ ThunkAction joinRoom({Room room}) { } /** - * Join Room (by id) - * - * Not sure if this process is / will be any different - * than accepting an invite + * Invite User (by id) + * */ ThunkAction inviteUser({ Room room, @@ -845,8 +893,9 @@ ThunkAction acceptRoom({Room room}) { room: joinedRoom.copyWith(invite: false), )); - await store.dispatch(fetchRooms()); - await store.dispatch(fetchDirectRooms()); + store.dispatch(SetLoading(loading: true)); + await store.dispatch(fetchRoom(joinedRoom.id)); + store.dispatch(SetLoading(loading: false)); } catch (error) { store.dispatch(addAlert(error: error, origin: 'acceptRoom')); } @@ -910,7 +959,6 @@ ThunkAction removeRoom({Room room}) { await store.dispatch(toggleDirectRoom(room: room, enabled: false)); } await store.dispatch(RemoveRoom(roomId: room.id)); - store.dispatch(SetLoading(loading: false)); } catch (error) { debugPrint('[removeRoom] $error'); } finally { diff --git a/pubspec.yaml b/pubspec.yaml index d07cc6cac..7355a0dfa 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -9,6 +9,7 @@ description: a privacy focused matrix client # $ flutter pub run build_runner build # $ flutter pub run build_runner watch --delete-conflicting-outputs # $ flutter pub run build_runner build --delete-conflicting-outputs +# $ adb shell && pm uninstall org.tether.tether (sometimes doesn't uninstall when debugging?) # troubleshooting # $ pub cache repair From dcfd04de17e04344082f34af91761e22d3d680a6 Mon Sep 17 00:00:00 2001 From: ereio Date: Tue, 15 Dec 2020 21:05:08 -0500 Subject: [PATCH 2/3] version code bump --- pubspec.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pubspec.yaml b/pubspec.yaml index 7355a0dfa..9d5006be0 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -45,7 +45,7 @@ description: a privacy focused matrix client # The following defines the version and build number for your application. # A version number is three numbers separated by dots, like 1.2.43 # followed by an optional build number separated by a +. -version: 0.1.5+150 +version: 0.1.5+151 environment: sdk: ">=2.9.0-13.0 <3.0.0" # <- modified to solve build_runner From 39cb2113bc6adc383c950a37cb259a76ac0a4293 Mon Sep 17 00:00:00 2001 From: ereio Date: Tue, 15 Dec 2020 21:06:08 -0500 Subject: [PATCH 3/3] version bump 0.1.5 fdroid --- version.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/version.txt b/version.txt index 872dd7052..5cfbbf1be 100644 --- a/version.txt +++ b/version.txt @@ -1,2 +1,2 @@ -versionName=0.1.4 -versionCode=140 +versionName=0.1.5 +versionCode=151