From 12bdddee03641d11c51b039f99d18b8d6a92df3e Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 16 Jun 2020 14:26:37 +0200 Subject: [PATCH 1/8] Better format switching to WAL mode --- lib/src/database/database.dart | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index 3aa14dc..8abde1d 100644 --- a/lib/src/database/database.dart +++ b/lib/src/database/database.dart @@ -4,7 +4,6 @@ import 'dart:convert'; import 'package:famedlysdk/famedlysdk.dart' as sdk; import 'package:famedlysdk/matrix_api.dart' as api; import 'package:olm/olm.dart' as olm; -import 'package:pedantic/pedantic.dart'; import '../../matrix_api.dart'; @@ -21,20 +20,6 @@ class Database extends _$Database { int get maxFileSize => 1 * 1024 * 1024; - @override - Future beforeOpen( - QueryExecutor executor, OpeningDetails details) async { - await super.beforeOpen(executor, details); - if (executor.dialect == SqlDialect.sqlite) { - unawaited(customSelect('PRAGMA journal_mode=WAL').get().then((ret) { - if (ret.isNotEmpty) { - print('[Moor] Switched database to mode ' + - ret.first.data['journal_mode'].toString()); - } - })); - } - } - @override MigrationStrategy get migration => MigrationStrategy( onCreate: (Migrator m) { @@ -61,6 +46,15 @@ class Database extends _$Database { await m.createTable(outboundGroupSessions); } }, + beforeOpen: (_) async { + if (executor.dialect == SqlDialect.sqlite) { + final ret = await customSelect('PRAGMA journal_mode=WAL').get(); + if (ret.isNotEmpty) { + print('[Moor] Switched database to mode ' + + ret.first.data['journal_mode'].toString()); + } + } + }, ); Future getClient(String name) async { From 9f8d170dd47ca673491f38e5d011c86dee894645 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 19 Jun 2020 14:00:32 +0200 Subject: [PATCH 2/8] Fix sending sort order --- lib/src/timeline.dart | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/src/timeline.dart b/lib/src/timeline.dart index 63388dd..42ddb0f 100644 --- a/lib/src/timeline.dart +++ b/lib/src/timeline.dart @@ -156,8 +156,10 @@ class Timeline { : null); if (i < events.length) { + final tempSortOrder = events[i].sortOrder; events[i] = Event.fromJson( eventUpdate.content, room, eventUpdate.sortOrder); + events[i].sortOrder = tempSortOrder; } } else { Event newEvent; From ee2974e51e3c179bde305cb7a07a0d69818c23b7 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 19 Jun 2020 14:05:53 +0200 Subject: [PATCH 3/8] Fix send again --- lib/src/event.dart | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/src/event.dart b/lib/src/event.dart index bb6a637..4beeede 100644 --- a/lib/src/event.dart +++ b/lib/src/event.dart @@ -307,7 +307,10 @@ class Event extends MatrixEvent { Future sendAgain({String txid}) async { if (status != -1) return null; await remove(); - final eventID = await room.sendTextEvent(text, txid: txid); + final eventID = await room.sendEvent( + content, + txid: txid ?? unsigned['transaction_id'], + ); return eventID; } From f1579a5f0f6beb41318d87ca1db268d71bda65a5 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Fri, 19 Jun 2020 15:28:03 +0200 Subject: [PATCH 4/8] Database transaction workaround --- lib/src/database/database.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index 8abde1d..28ea9d1 100644 --- a/lib/src/database/database.dart +++ b/lib/src/database/database.dart @@ -20,6 +20,18 @@ class Database extends _$Database { int get maxFileSize => 1 * 1024 * 1024; + @override + Future transaction(Future Function() action) async { + try { + await super.transaction(() async { + await customSelect('SELECT 1').get(); + }); + } catch (_) { + return action(); + } + return super.transaction(action); + } + @override MigrationStrategy get migration => MigrationStrategy( onCreate: (Migrator m) { From 2999ceec9e3644c815f1df38ff3872d4eac744dd Mon Sep 17 00:00:00 2001 From: Sorunome Date: Mon, 22 Jun 2020 06:48:30 +0000 Subject: [PATCH 5/8] potentially fix direct chats going missing --- lib/src/client.dart | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index ef6b2a4..bcfc3ff 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -168,13 +168,12 @@ class Client { if (accountData['m.direct'] != null && accountData['m.direct'].content[userId] is List && accountData['m.direct'].content[userId].length > 0) { - if (getRoomById(accountData['m.direct'].content[userId][0]) != null) { - return accountData['m.direct'].content[userId][0]; + for (final roomId in accountData['m.direct'].content[userId]) { + final room = getRoomById(roomId); + if (room != null && room.membership == Membership.join) { + return roomId; + } } - (accountData['m.direct'].content[userId] as List) - .remove(accountData['m.direct'].content[userId][0]); - api.setAccountData(userId, 'm.direct', directChats); - return getDirectChatFromUserId(userId); } for (var i = 0; i < rooms.length; i++) { if (rooms[i].membership == Membership.invite && From 3d40a8d81ba5c0823220f623429d23a71524e16b Mon Sep 17 00:00:00 2001 From: Sorunome Date: Mon, 22 Jun 2020 17:27:06 +0200 Subject: [PATCH 6/8] remove transaction hack --- lib/src/database/database.dart | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index 28ea9d1..8abde1d 100644 --- a/lib/src/database/database.dart +++ b/lib/src/database/database.dart @@ -20,18 +20,6 @@ class Database extends _$Database { int get maxFileSize => 1 * 1024 * 1024; - @override - Future transaction(Future Function() action) async { - try { - await super.transaction(() async { - await customSelect('SELECT 1').get(); - }); - } catch (_) { - return action(); - } - return super.transaction(action); - } - @override MigrationStrategy get migration => MigrationStrategy( onCreate: (Migrator m) { From 439f6f0aa06b194d43413a45cf5fd3b005b1b2b2 Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 23 Jun 2020 11:42:01 +0200 Subject: [PATCH 7/8] Fix some olm sessions not being loaded from database --- lib/encryption/olm_manager.dart | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/lib/encryption/olm_manager.dart b/lib/encryption/olm_manager.dart index e116dc7..43871c0 100644 --- a/lib/encryption/olm_manager.dart +++ b/lib/encryption/olm_manager.dart @@ -400,6 +400,18 @@ class OlmManager { String type, Map payload) async { var data = >>{}; + // first check if any of our sessions we want to encrypt for are in the database + if (client.database != null) { + for (final device in deviceKeys) { + if (!olmSessions.containsKey(device.curve25519Key)) { + final sessions = await client.database.getSingleOlmSessions( + client.id, device.curve25519Key, client.userID); + if (sessions.isNotEmpty) { + _olmSessions[device.curve25519Key] = sessions; + } + } + } + } final deviceKeysWithoutSession = List.from(deviceKeys); deviceKeysWithoutSession.removeWhere((DeviceKeys deviceKeys) => olmSessions.containsKey(deviceKeys.curve25519Key)); From 1a9f6e38dcaefa810a851ab26bd7fc2e5d23316e Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 23 Jun 2020 13:35:22 +0200 Subject: [PATCH 8/8] add database.getRoom --- lib/src/database/database.g.dart | 23 +++++++++++++++++++++++ lib/src/database/database.moor | 1 + 2 files changed, 24 insertions(+) diff --git a/lib/src/database/database.g.dart b/lib/src/database/database.g.dart index 9b86794..488dee4 100644 --- a/lib/src/database/database.g.dart +++ b/lib/src/database/database.g.dart @@ -5406,6 +5406,29 @@ abstract class _$Database extends GeneratedDatabase { ); } + DbRoom _rowToDbRoom(QueryRow row) { + return DbRoom( + clientId: row.readInt('client_id'), + roomId: row.readString('room_id'), + membership: row.readString('membership'), + highlightCount: row.readInt('highlight_count'), + notificationCount: row.readInt('notification_count'), + prevBatch: row.readString('prev_batch'), + joinedMemberCount: row.readInt('joined_member_count'), + invitedMemberCount: row.readInt('invited_member_count'), + newestSortOrder: row.readDouble('newest_sort_order'), + oldestSortOrder: row.readDouble('oldest_sort_order'), + heroes: row.readString('heroes'), + ); + } + + Selectable getRoom(int client_id, String room_id) { + return customSelect( + 'SELECT * FROM rooms WHERE client_id = :client_id AND room_id = :room_id', + variables: [Variable.withInt(client_id), Variable.withString(room_id)], + readsFrom: {rooms}).map(_rowToDbRoom); + } + Selectable getEvent(int client_id, String event_id, String room_id) { return customSelect( 'SELECT * FROM events WHERE client_id = :client_id AND event_id = :event_id AND room_id = :room_id', diff --git a/lib/src/database/database.moor b/lib/src/database/database.moor index 073a71f..20db688 100644 --- a/lib/src/database/database.moor +++ b/lib/src/database/database.moor @@ -190,6 +190,7 @@ dbGetUser: SELECT * FROM room_states WHERE client_id = :client_id AND type = 'm. dbGetEventList: SELECT * FROM events WHERE client_id = :client_id AND room_id = :room_id GROUP BY event_id ORDER BY sort_order DESC; getStates: SELECT * FROM room_states WHERE client_id = :client_id AND room_id = :room_id; resetNotificationCount: UPDATE rooms SET notification_count = 0, highlight_count = 0 WHERE client_id = :client_id AND room_id = :room_id; +getRoom: SELECT * FROM rooms WHERE client_id = :client_id AND room_id = :room_id; getEvent: SELECT * FROM events WHERE client_id = :client_id AND event_id = :event_id AND room_id = :room_id; removeEvent: DELETE FROM events WHERE client_id = :client_id AND event_id = :event_id AND room_id = :room_id; removeRoom: DELETE FROM rooms WHERE client_id = :client_id AND room_id = :room_id;