Skip to content

Commit

Permalink
Merge pull request #208 from syphon-org/bugfix-small-tweaks
Browse files Browse the repository at this point in the history
[bugfix] small tweaks
  • Loading branch information
ereio authored Dec 16, 2020
2 parents 5a2c2e2 + 39cb211 commit d4a0d97
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 16 deletions.
25 changes: 25 additions & 0 deletions lib/global/cache/index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,31 @@ void closeCache(Database cache) async {
}
}

Future<void> 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;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/global/colours.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down
6 changes: 6 additions & 0 deletions lib/store/auth/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -382,8 +383,13 @@ ThunkAction<AppState> 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);
Expand Down
14 changes: 13 additions & 1 deletion lib/store/crypto/events/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -312,12 +313,23 @@ ThunkAction<AppState> 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<String, dynamic> room = {roomId: {}};

return await store.dispatch(syncRooms(room));
} catch (error) {
debugPrint('[syncRooms|error] $error');
}
}
} catch (error) {
debugPrint('[decryptKeyEvent|error] $error');
Expand Down
70 changes: 59 additions & 11 deletions lib/store/rooms/actions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,60 @@ ThunkAction<AppState> syncRooms(Map roomData) {
* Takes a negligible amount of time
*
*/
ThunkAction<AppState> fetchRooms() {
ThunkAction<AppState> fetchRoom(String roomId) {
return (Store<AppState> 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<AppState> fetchRooms() {
return (Store<AppState> store) async {
try {
final data = await MatrixApi.fetchRoomIds(
protocol: protocol,
homeserver: store.state.authStore.user.homeserver,
Expand Down Expand Up @@ -768,19 +817,18 @@ ThunkAction<AppState> 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'));
}
};
}

/**
* Join Room (by id)
*
* Not sure if this process is / will be any different
* than accepting an invite
* Invite User (by id)
*
*/
ThunkAction<AppState> inviteUser({
Room room,
Expand Down Expand Up @@ -845,8 +893,9 @@ ThunkAction<AppState> 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'));
}
Expand Down Expand Up @@ -910,7 +959,6 @@ ThunkAction<AppState> 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 {
Expand Down
3 changes: 2 additions & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -44,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
Expand Down
4 changes: 2 additions & 2 deletions version.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
versionName=0.1.4
versionCode=140
versionName=0.1.5
versionCode=151

0 comments on commit d4a0d97

Please sign in to comment.