diff --git a/lib/encryption/olm_manager.dart b/lib/encryption/olm_manager.dart index 5db0b78..b08ab1b 100644 --- a/lib/encryption/olm_manager.dart +++ b/lib/encryption/olm_manager.dart @@ -404,6 +404,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)); diff --git a/lib/src/client.dart b/lib/src/client.dart index f2b4faf..5ebb08e 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -181,13 +181,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 && diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index 0e64f84..58ffac5 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) { @@ -71,6 +56,15 @@ class Database extends _$Database { from++; } }, + 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 { diff --git a/lib/src/database/database.g.dart b/lib/src/database/database.g.dart index 52bf988..41485b6 100644 --- a/lib/src/database/database.g.dart +++ b/lib/src/database/database.g.dart @@ -6186,6 +6186,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 abe513d..49a108d 100644 --- a/lib/src/database/database.moor +++ b/lib/src/database/database.moor @@ -218,6 +218,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; 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; } 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;