From b7b369923faee1c235b2d6c076e365878dfea04f Mon Sep 17 00:00:00 2001 From: Sorunome Date: Tue, 30 Jun 2020 13:41:52 +0200 Subject: [PATCH] only lazy-load m.room.member, not store presence --- lib/src/client.dart | 10 +-------- lib/src/database/database.dart | 16 --------------- lib/src/database/database.g.dart | 35 +++----------------------------- lib/src/database/database.moor | 7 +++---- lib/src/room.dart | 5 +++-- 5 files changed, 10 insertions(+), 63 deletions(-) diff --git a/lib/src/client.dart b/lib/src/client.dart index d431b33b..53d11863 100644 --- a/lib/src/client.dart +++ b/lib/src/client.dart @@ -651,7 +651,7 @@ class Client { } _sortRooms(); accountData = await database.getAccountData(id); - presences = await database.getPresences(id); + presences.clear(); } onLoginStateChanged.add(LoginState.logged); @@ -744,14 +744,6 @@ class Client { } if (sync.presence != null) { for (final newPresence in sync.presence) { - if (database != null) { - await database.storePresence( - id, - newPresence.type, - newPresence.senderId, - jsonEncode(newPresence.toJson()), - ); - } presences[newPresence.senderId] = newPresence; onPresence.add(newPresence); } diff --git a/lib/src/database/database.dart b/lib/src/database/database.dart index 7240a35f..dc6cdd62 100644 --- a/lib/src/database/database.dart +++ b/lib/src/database/database.dart @@ -192,22 +192,6 @@ class Database extends _$Database { return newAccountData; } - Future> getPresences(int clientId) async { - final newPresences = {}; - final rawPresences = await getAllPresences(clientId).get(); - for (final d in rawPresences) { - // TODO: Why is this not working? - try { - final content = sdk.Event.getMapFromPayload(d.content); - var presence = api.Presence.fromJson(content); - presence.senderId = d.sender; - presence.type = d.type; - newPresences[d.sender] = api.Presence.fromJson(content); - } catch (_) {} - } - return newPresences; - } - /// Stores a RoomUpdate object in the database. Must be called inside of /// [transaction]. final Set _ensuredRooms = {}; diff --git a/lib/src/database/database.g.dart b/lib/src/database/database.g.dart index 89714530..9e11f414 100644 --- a/lib/src/database/database.g.dart +++ b/lib/src/database/database.g.dart @@ -5999,35 +5999,6 @@ abstract class _$Database extends GeneratedDatabase { ); } - DbPresence _rowToDbPresence(QueryRow row) { - return DbPresence( - clientId: row.readInt('client_id'), - type: row.readString('type'), - sender: row.readString('sender'), - content: row.readString('content'), - ); - } - - Selectable getAllPresences(int client_id) { - return customSelect('SELECT * FROM presences WHERE client_id = :client_id', - variables: [Variable.withInt(client_id)], - readsFrom: {presences}).map(_rowToDbPresence); - } - - Future storePresence( - int client_id, String type, String sender, String content) { - return customInsert( - 'INSERT OR REPLACE INTO presences (client_id, type, sender, content) VALUES (:client_id, :type, :sender, :content)', - variables: [ - Variable.withInt(client_id), - Variable.withString(type), - Variable.withString(sender), - Variable.withString(content) - ], - updates: {presences}, - ); - } - Future updateEvent(String unsigned, String content, String prev_content, int client_id, String event_id, String room_id) { return customUpdate( @@ -6079,7 +6050,7 @@ abstract class _$Database extends GeneratedDatabase { Selectable getImportantRoomStates(int client_id) { return customSelect( - 'SELECT * FROM room_states WHERE client_id = :client_id AND type IN (\'m.room.name\', \'m.room.avatar\', \'m.room.message\', \'m.room.encrypted\', \'m.room.encryption\')', + 'SELECT * FROM room_states WHERE client_id = :client_id AND type <> \'m.room.member\'', variables: [Variable.withInt(client_id)], readsFrom: {roomStates}).map(_rowToDbRoomState); } @@ -6091,10 +6062,10 @@ abstract class _$Database extends GeneratedDatabase { readsFrom: {roomStates}).map(_rowToDbRoomState); } - Selectable getAllRoomStatesForRoom( + Selectable getUnimportantRoomStatesForRoom( int client_id, String room_id) { return customSelect( - 'SELECT * FROM room_states WHERE client_id = :client_id AND room_id = :room_id', + 'SELECT * FROM room_states WHERE client_id = :client_id AND room_id = :room_id AND type = \'m.room.member\'', variables: [Variable.withInt(client_id), Variable.withString(room_id)], readsFrom: {roomStates}).map(_rowToDbRoomState); } diff --git a/lib/src/database/database.moor b/lib/src/database/database.moor index b5459bf3..1dbdef1d 100644 --- a/lib/src/database/database.moor +++ b/lib/src/database/database.moor @@ -206,13 +206,12 @@ setRoomPrevBatch: UPDATE rooms SET prev_batch = :prev_batch WHERE client_id = :c updateRoomSortOrder: UPDATE rooms SET oldest_sort_order = :oldest_sort_order, newest_sort_order = :newest_sort_order WHERE client_id = :client_id AND room_id = :room_id; getAllAccountData: SELECT * FROM account_data WHERE client_id = :client_id; storeAccountData: INSERT OR REPLACE INTO account_data (client_id, type, content) VALUES (:client_id, :type, :content); -getAllPresences: SELECT * FROM presences WHERE client_id = :client_id; -storePresence: INSERT OR REPLACE INTO presences (client_id, type, sender, content) VALUES (:client_id, :type, :sender, :content); updateEvent: UPDATE events SET unsigned = :unsigned, content = :content, prev_content = :prev_content WHERE client_id = :client_id AND event_id = :event_id AND room_id = :room_id; updateEventStatus: UPDATE events SET status = :status, event_id = :new_event_id WHERE client_id = :client_id AND event_id = :old_event_id AND room_id = :room_id; -getImportantRoomStates: SELECT * FROM room_states WHERE client_id = :client_id AND type IN ('m.room.name', 'm.room.avatar', 'm.room.message', 'm.room.encrypted', 'm.room.encryption'); +--getImportantRoomStates: SELECT * FROM room_states WHERE client_id = :client_id AND type IN ('m.room.name', 'm.room.avatar', 'm.room.message', 'm.room.encrypted', 'm.room.encryption', 'im.ponies.room_emotes'); +getImportantRoomStates: SELECT * FROM room_states WHERE client_id = :client_id AND type <> 'm.room.member'; getAllRoomStates: SELECT * FROM room_states WHERE client_id = :client_id; -getAllRoomStatesForRoom: SELECT * FROM room_states WHERE client_id = :client_id AND room_id = :room_id; +getUnimportantRoomStatesForRoom: SELECT * FROM room_states WHERE client_id = :client_id AND room_id = :room_id AND type = 'm.room.member'; storeEvent: INSERT OR REPLACE INTO events (client_id, event_id, room_id, sort_order, origin_server_ts, sender, type, unsigned, content, prev_content, state_key, status) VALUES (:client_id, :event_id, :room_id, :sort_order, :origin_server_ts, :sender, :type, :unsigned, :content, :prev_content, :state_key, :status); storeRoomState: INSERT OR REPLACE INTO room_states (client_id, event_id, room_id, sort_order, origin_server_ts, sender, type, unsigned, content, prev_content, state_key) VALUES (:client_id, :event_id, :room_id, :sort_order, :origin_server_ts, :sender, :type, :unsigned, :content, :prev_content, :state_key); getAllRoomAccountData: SELECT * FROM room_account_data WHERE client_id = :client_id; diff --git a/lib/src/room.dart b/lib/src/room.dart index 10ec7589..55992dc0 100644 --- a/lib/src/room.dart +++ b/lib/src/room.dart @@ -106,8 +106,9 @@ class Room { if (!partial || client.database == null) { return; } - final allStates = - await client.database.getAllRoomStatesForRoom(client.id, id).get(); + final allStates = await client.database + .getUnimportantRoomStatesForRoom(client.id, id) + .get(); for (final state in allStates) { final newState = Event.fromDb(state, this); setState(newState);