Merge branch 'soru/less-sql-statements' into 'main'

chore: Reduce needed database accessess

See merge request famedly/famedlysdk!666
This commit is contained in:
Krille Fear 2021-03-09 18:12:40 +00:00
commit 57fde98fec
3 changed files with 17 additions and 8 deletions

View File

@ -159,6 +159,8 @@ class KeyManager {
if (uploaded) {
client.database
.markInboundGroupSessionAsUploaded(client.id, roomId, sessionId);
} else {
_haveKeysToUpload = true;
}
});
final room = client.getRoomById(roomId);
@ -680,18 +682,20 @@ class KeyManager {
}
bool _isUploadingKeys = false;
bool _haveKeysToUpload = true;
Future<void> backgroundTasks() async {
if (_isUploadingKeys || client.database == null) {
return;
}
_isUploadingKeys = true;
try {
if (!(await isCached())) {
if (!_haveKeysToUpload || !(await isCached())) {
return; // we can't backup anyways
}
final dbSessions =
await client.database.getInboundGroupSessionsToUpload().get();
if (dbSessions.isEmpty) {
_haveKeysToUpload = false;
return; // nothing to do
}
final privateKey =

View File

@ -1698,14 +1698,20 @@ sort order of ${prevState.sortOrder}. This should never happen...''');
}
}
bool _toDeviceQueueNeedsProcessing = true;
/// Processes the to_device queue and tries to send every entry.
/// This function MAY throw an error, which just means the to_device queue wasn't
/// proccessed all the way.
Future<void> processToDeviceQueue() async {
if (database == null) {
if (database == null || !_toDeviceQueueNeedsProcessing) {
return;
}
final entries = await database.getToDeviceQueue(id).get();
if (entries.isEmpty) {
_toDeviceQueueNeedsProcessing = false;
return;
}
for (final entry in entries) {
// ohgod what is this...
final data = (json.decode(entry.content) as Map).map((k, v) =>
@ -1736,6 +1742,7 @@ sort order of ${prevState.sortOrder}. This should never happen...''');
e,
s);
if (database != null) {
_toDeviceQueueNeedsProcessing = true;
await database.insertIntoToDeviceQueue(
id, eventType, txnId, json.encode(messages));
}

View File

@ -282,7 +282,8 @@ class Database extends _$Database {
);
roomList.add(room);
// let's see if we need any m.room.member events
final membersToPostload = <String>{};
// We always need the member event for ourself
final membersToPostload = <String>{client.userID};
// the lastEvent message preview might have an author we need to fetch, if it is a group chat
if (room.getState(api.EventTypes.Message) != null && !room.isDirectChat) {
membersToPostload.add(room.getState(api.EventTypes.Message).senderId);
@ -296,11 +297,8 @@ class Database extends _$Database {
// post-load the heroes
membersToPostload.addAll(room.mHeroes.where((h) => h.isNotEmpty));
}
// okay, only load from the database if we actually have stuff to load
if (membersToPostload.isNotEmpty) {
// save it for loading later
allMembersToPostload[room.id] = membersToPostload;
}
// save it for loading later
allMembersToPostload[room.id] = membersToPostload;
}
// now we postload all members, if thre are any
if (allMembersToPostload.isNotEmpty) {