refactor: Remove duplicated copyMap method and fix type error
dynamic.copy returned a type error so I reverted the previous change of the copyMap method to an extension. Also I found out that we have used two copyMap methods in the SDK which did exactly the same. I deleted the old one and changed the tests.
This commit is contained in:
parent
0b29c3a034
commit
c3c770bc33
|
|
@ -28,6 +28,7 @@ import 'package:matrix/encryption/olm_manager.dart';
|
||||||
import 'package:matrix/encryption/ssss.dart';
|
import 'package:matrix/encryption/ssss.dart';
|
||||||
import 'package:matrix/encryption/utils/bootstrap.dart';
|
import 'package:matrix/encryption/utils/bootstrap.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
import 'package:matrix/src/utils/copy_map.dart';
|
||||||
import 'package:matrix/src/utils/run_in_root.dart';
|
import 'package:matrix/src/utils/run_in_root.dart';
|
||||||
|
|
||||||
class Encryption {
|
class Encryption {
|
||||||
|
|
|
||||||
|
|
@ -672,7 +672,7 @@ class HiveCollectionsDatabase extends DatabaseApi {
|
||||||
final copiedRaws = raws.entries.map((entry) {
|
final copiedRaws = raws.entries.map((entry) {
|
||||||
final copiedRaw = copyMap(entry.value);
|
final copiedRaw = copyMap(entry.value);
|
||||||
copiedRaw['id'] = int.parse(entry.key);
|
copiedRaw['id'] = int.parse(entry.key);
|
||||||
copiedRaw['content'] = jsonDecode(copiedRaw['content']);
|
copiedRaw['content'] = jsonDecode(copiedRaw['content'] as String);
|
||||||
return copiedRaw;
|
return copiedRaw;
|
||||||
}).toList();
|
}).toList();
|
||||||
return copiedRaws.map((raw) => QueuedToDeviceEvent.fromJson(raw)).toList();
|
return copiedRaws.map((raw) => QueuedToDeviceEvent.fromJson(raw)).toList();
|
||||||
|
|
@ -1620,5 +1620,3 @@ class TupleKey {
|
||||||
@override
|
@override
|
||||||
int get hashCode => Object.hashAll(parts);
|
int get hashCode => Object.hashAll(parts);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<String, dynamic> copyMap(Map map) => map.copy;
|
|
||||||
|
|
|
||||||
|
|
@ -28,6 +28,7 @@ import 'package:matrix/encryption/utils/outbound_group_session.dart';
|
||||||
import 'package:matrix/encryption/utils/ssss_cache.dart';
|
import 'package:matrix/encryption/utils/ssss_cache.dart';
|
||||||
import 'package:matrix/encryption/utils/stored_inbound_group_session.dart';
|
import 'package:matrix/encryption/utils/stored_inbound_group_session.dart';
|
||||||
import 'package:matrix/matrix.dart';
|
import 'package:matrix/matrix.dart';
|
||||||
|
import 'package:matrix/src/utils/copy_map.dart';
|
||||||
import 'package:matrix/src/utils/queued_to_device_event.dart';
|
import 'package:matrix/src/utils/queued_to_device_event.dart';
|
||||||
import 'package:matrix/src/utils/run_benchmarked.dart';
|
import 'package:matrix/src/utils/run_benchmarked.dart';
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -354,7 +354,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
for (final entry in raws.entries) {
|
for (final entry in raws.entries) {
|
||||||
accountData[entry.key] = BasicEvent(
|
accountData[entry.key] = BasicEvent(
|
||||||
type: entry.key,
|
type: entry.key,
|
||||||
content: entry.value.copy,
|
content: copyMap(entry.value),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
return accountData;
|
return accountData;
|
||||||
|
|
@ -378,7 +378,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
Future<Event?> getEventById(String eventId, Room room) async {
|
Future<Event?> getEventById(String eventId, Room room) async {
|
||||||
final raw = await _eventsBox.get(TupleKey(room.id, eventId).toString());
|
final raw = await _eventsBox.get(TupleKey(room.id, eventId).toString());
|
||||||
if (raw == null) return null;
|
if (raw == null) return null;
|
||||||
return Event.fromJson(raw.copy, room);
|
return Event.fromJson(copyMap(raw), room);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Loads a whole list of events at once from the store for a specific room
|
/// Loads a whole list of events at once from the store for a specific room
|
||||||
|
|
@ -391,7 +391,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
final rawEvents = await _eventsBox.getAll(keys);
|
final rawEvents = await _eventsBox.getAll(keys);
|
||||||
return rawEvents
|
return rawEvents
|
||||||
.whereType<Map>()
|
.whereType<Map>()
|
||||||
.map((rawEvent) => Event.fromJson(rawEvent.copy, room))
|
.map((rawEvent) => Event.fromJson(copyMap(rawEvent), room))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -448,7 +448,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
) async {
|
) async {
|
||||||
final raw = await _inboundGroupSessionsBox.get(sessionId);
|
final raw = await _inboundGroupSessionsBox.get(sessionId);
|
||||||
if (raw == null) return null;
|
if (raw == null) return null;
|
||||||
return StoredInboundGroupSession.fromJson(raw.copy);
|
return StoredInboundGroupSession.fromJson(copyMap(raw));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
@ -460,7 +460,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
.take(50)
|
.take(50)
|
||||||
.map(
|
.map(
|
||||||
(json) => StoredInboundGroupSession.fromJson(
|
(json) => StoredInboundGroupSession.fromJson(
|
||||||
json.copy,
|
copyMap(json),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.toList();
|
.toList();
|
||||||
|
|
@ -479,7 +479,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> storeOlmSession(String identityKey, String sessionId,
|
Future<void> storeOlmSession(String identityKey, String sessionId,
|
||||||
String pickle, int lastReceived) async {
|
String pickle, int lastReceived) async {
|
||||||
final rawSessions = (await _olmSessionsBox.get(identityKey))?.copy ?? {};
|
final rawSessions = copyMap((await _olmSessionsBox.get(identityKey)) ?? {});
|
||||||
rawSessions[sessionId] = {
|
rawSessions[sessionId] = {
|
||||||
'identity_key': identityKey,
|
'identity_key': identityKey,
|
||||||
'pickle': pickle,
|
'pickle': pickle,
|
||||||
|
|
@ -517,7 +517,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
String roomId, String userId) async {
|
String roomId, String userId) async {
|
||||||
final raw = await _outboundGroupSessionsBox.get(roomId);
|
final raw = await _outboundGroupSessionsBox.get(roomId);
|
||||||
if (raw == null) return null;
|
if (raw == null) return null;
|
||||||
return OutboundGroupSession.fromJson(raw.copy, userId);
|
return OutboundGroupSession.fromJson(copyMap(raw), userId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
@ -526,7 +526,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
// Get raw room from database:
|
// Get raw room from database:
|
||||||
final roomData = await _roomsBox.get(roomId);
|
final roomData = await _roomsBox.get(roomId);
|
||||||
if (roomData == null) return null;
|
if (roomData == null) return null;
|
||||||
final room = Room.fromJson(roomData.copy, client);
|
final room = Room.fromJson(copyMap(roomData), client);
|
||||||
|
|
||||||
// Get important states:
|
// Get important states:
|
||||||
if (loadImportantStates) {
|
if (loadImportantStates) {
|
||||||
|
|
@ -536,7 +536,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
final rawStates = await _preloadRoomStateBox.getAll(dbKeys);
|
final rawStates = await _preloadRoomStateBox.getAll(dbKeys);
|
||||||
for (final rawState in rawStates) {
|
for (final rawState in rawStates) {
|
||||||
if (rawState == null || rawState[''] == null) continue;
|
if (rawState == null || rawState[''] == null) continue;
|
||||||
room.setState(Event.fromJson(rawState[''].copy, room));
|
room.setState(Event.fromJson(copyMap(rawState['']), room));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -552,7 +552,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
|
|
||||||
for (final raw in rawRooms.values) {
|
for (final raw in rawRooms.values) {
|
||||||
// Get the room
|
// Get the room
|
||||||
final room = Room.fromJson(raw.copy, client);
|
final room = Room.fromJson(copyMap(raw), client);
|
||||||
|
|
||||||
// Add to the list and continue.
|
// Add to the list and continue.
|
||||||
rooms[room.id] = room;
|
rooms[room.id] = room;
|
||||||
|
|
@ -569,7 +569,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
}
|
}
|
||||||
final states = entry.value;
|
final states = entry.value;
|
||||||
final stateEvents = states.values
|
final stateEvents = states.values
|
||||||
.map((raw) => Event.fromJson(raw.copy, room))
|
.map((raw) => Event.fromJson(copyMap(raw), room))
|
||||||
.toList();
|
.toList();
|
||||||
for (final state in stateEvents) {
|
for (final state in stateEvents) {
|
||||||
room.setState(state);
|
room.setState(state);
|
||||||
|
|
@ -581,7 +581,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
for (final entry in roomAccountDataRaws.entries) {
|
for (final entry in roomAccountDataRaws.entries) {
|
||||||
final keys = TupleKey.fromString(entry.key);
|
final keys = TupleKey.fromString(entry.key);
|
||||||
final basicRoomEvent = BasicRoomEvent.fromJson(
|
final basicRoomEvent = BasicRoomEvent.fromJson(
|
||||||
entry.value.copy,
|
copyMap(entry.value),
|
||||||
);
|
);
|
||||||
final roomId = keys.parts.first;
|
final roomId = keys.parts.first;
|
||||||
if (rooms.containsKey(roomId)) {
|
if (rooms.containsKey(roomId)) {
|
||||||
|
|
@ -602,14 +602,14 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
Future<SSSSCache?> getSSSSCache(String type) async {
|
Future<SSSSCache?> getSSSSCache(String type) async {
|
||||||
final raw = await _ssssCacheBox.get(type);
|
final raw = await _ssssCacheBox.get(type);
|
||||||
if (raw == null) return null;
|
if (raw == null) return null;
|
||||||
return SSSSCache.fromJson(raw.copy);
|
return SSSSCache.fromJson(copyMap(raw));
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<List<QueuedToDeviceEvent>> getToDeviceEventQueue() async {
|
Future<List<QueuedToDeviceEvent>> getToDeviceEventQueue() async {
|
||||||
final raws = await _toDeviceQueueBox.getAllValues();
|
final raws = await _toDeviceQueueBox.getAllValues();
|
||||||
final copiedRaws = raws.entries.map((entry) {
|
final copiedRaws = raws.entries.map((entry) {
|
||||||
final copiedRaw = entry.value.copy;
|
final copiedRaw = copyMap(entry.value);
|
||||||
copiedRaw['id'] = int.parse(entry.key);
|
copiedRaw['id'] = int.parse(entry.key);
|
||||||
copiedRaw['content'] = jsonDecode(copiedRaw['content'] as String);
|
copiedRaw['content'] = jsonDecode(copiedRaw['content'] as String);
|
||||||
return copiedRaw;
|
return copiedRaw;
|
||||||
|
|
@ -629,8 +629,8 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
for (final key in keys) {
|
for (final key in keys) {
|
||||||
final states = await _nonPreloadRoomStateBox.get(key);
|
final states = await _nonPreloadRoomStateBox.get(key);
|
||||||
if (states == null) continue;
|
if (states == null) continue;
|
||||||
unimportantEvents
|
unimportantEvents.addAll(
|
||||||
.addAll(states.values.map((raw) => Event.fromJson(raw.copy, room)));
|
states.values.map((raw) => Event.fromJson(copyMap(raw), room)));
|
||||||
}
|
}
|
||||||
return unimportantEvents;
|
return unimportantEvents;
|
||||||
}
|
}
|
||||||
|
|
@ -640,7 +640,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
final state =
|
final state =
|
||||||
await _roomMembersBox.get(TupleKey(room.id, userId).toString());
|
await _roomMembersBox.get(TupleKey(room.id, userId).toString());
|
||||||
if (state == null) return null;
|
if (state == null) return null;
|
||||||
return Event.fromJson(state.copy, room).asUser;
|
return Event.fromJson(copyMap(state), room).asUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
|
|
@ -670,14 +670,14 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
(key) {
|
(key) {
|
||||||
final userDeviceKey = userDeviceKeys[key];
|
final userDeviceKey = userDeviceKeys[key];
|
||||||
if (userDeviceKey == null) return null;
|
if (userDeviceKey == null) return null;
|
||||||
return userDeviceKey.copy;
|
return copyMap(userDeviceKey);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
final crossSigningEntries = crossSigningKeysBoxKeys.map(
|
final crossSigningEntries = crossSigningKeysBoxKeys.map(
|
||||||
(key) {
|
(key) {
|
||||||
final crossSigningKey = userCrossSigningKeys[key];
|
final crossSigningKey = userCrossSigningKeys[key];
|
||||||
if (crossSigningKey == null) return null;
|
if (crossSigningKey == null) return null;
|
||||||
return crossSigningKey.copy;
|
return copyMap(crossSigningKey);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
res[userId] = DeviceKeysList.fromDbJson(
|
res[userId] = DeviceKeysList.fromDbJson(
|
||||||
|
|
@ -708,7 +708,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
final states = await _roomMembersBox.getAll(keys);
|
final states = await _roomMembersBox.getAll(keys);
|
||||||
states.removeWhere((state) => state == null);
|
states.removeWhere((state) => state == null);
|
||||||
for (final state in states) {
|
for (final state in states) {
|
||||||
users.add(Event.fromJson(state!.copy, room).asUser);
|
users.add(Event.fromJson(copyMap(state!), room).asUser);
|
||||||
}
|
}
|
||||||
|
|
||||||
return users;
|
return users;
|
||||||
|
|
@ -768,7 +768,9 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> markInboundGroupSessionAsUploaded(
|
Future<void> markInboundGroupSessionAsUploaded(
|
||||||
String roomId, String sessionId) async {
|
String roomId, String sessionId) async {
|
||||||
final raw = (await _inboundGroupSessionsBox.get(sessionId) ?? {}).copy;
|
final raw = copyMap(
|
||||||
|
await _inboundGroupSessionsBox.get(sessionId) ?? {},
|
||||||
|
);
|
||||||
if (raw.isEmpty) {
|
if (raw.isEmpty) {
|
||||||
Logs().w(
|
Logs().w(
|
||||||
'Tried to mark inbound group session as uploaded which was not found in the database!');
|
'Tried to mark inbound group session as uploaded which was not found in the database!');
|
||||||
|
|
@ -783,7 +785,9 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
Future<void> markInboundGroupSessionsAsNeedingUpload() async {
|
Future<void> markInboundGroupSessionsAsNeedingUpload() async {
|
||||||
final keys = await _inboundGroupSessionsBox.getAllKeys();
|
final keys = await _inboundGroupSessionsBox.getAllKeys();
|
||||||
for (final sessionId in keys) {
|
for (final sessionId in keys) {
|
||||||
final raw = (await _inboundGroupSessionsBox.get(sessionId) ?? {}).copy;
|
final raw = copyMap(
|
||||||
|
await _inboundGroupSessionsBox.get(sessionId) ?? {},
|
||||||
|
);
|
||||||
if (raw.isEmpty) continue;
|
if (raw.isEmpty) continue;
|
||||||
raw['uploaded'] = false;
|
raw['uploaded'] = false;
|
||||||
await _inboundGroupSessionsBox.put(sessionId, raw);
|
await _inboundGroupSessionsBox.put(sessionId, raw);
|
||||||
|
|
@ -831,10 +835,11 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> setBlockedUserCrossSigningKey(
|
Future<void> setBlockedUserCrossSigningKey(
|
||||||
bool blocked, String userId, String publicKey) async {
|
bool blocked, String userId, String publicKey) async {
|
||||||
final raw = (await _userCrossSigningKeysBox
|
final raw = copyMap(
|
||||||
.get(TupleKey(userId, publicKey).toString()) ??
|
await _userCrossSigningKeysBox
|
||||||
{})
|
.get(TupleKey(userId, publicKey).toString()) ??
|
||||||
.copy;
|
{},
|
||||||
|
);
|
||||||
raw['blocked'] = blocked;
|
raw['blocked'] = blocked;
|
||||||
await _userCrossSigningKeysBox.put(
|
await _userCrossSigningKeysBox.put(
|
||||||
TupleKey(userId, publicKey).toString(),
|
TupleKey(userId, publicKey).toString(),
|
||||||
|
|
@ -846,10 +851,9 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> setBlockedUserDeviceKey(
|
Future<void> setBlockedUserDeviceKey(
|
||||||
bool blocked, String userId, String deviceId) async {
|
bool blocked, String userId, String deviceId) async {
|
||||||
final raw =
|
final raw = copyMap(
|
||||||
(await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ??
|
await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ?? {},
|
||||||
{})
|
);
|
||||||
.copy;
|
|
||||||
raw['blocked'] = blocked;
|
raw['blocked'] = blocked;
|
||||||
await _userDeviceKeysBox.put(
|
await _userDeviceKeysBox.put(
|
||||||
TupleKey(userId, deviceId).toString(),
|
TupleKey(userId, deviceId).toString(),
|
||||||
|
|
@ -861,10 +865,9 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> setLastActiveUserDeviceKey(
|
Future<void> setLastActiveUserDeviceKey(
|
||||||
int lastActive, String userId, String deviceId) async {
|
int lastActive, String userId, String deviceId) async {
|
||||||
final raw =
|
final raw = copyMap(
|
||||||
(await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ??
|
await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ?? {},
|
||||||
{})
|
);
|
||||||
.copy;
|
|
||||||
|
|
||||||
raw['last_active'] = lastActive;
|
raw['last_active'] = lastActive;
|
||||||
await _userDeviceKeysBox.put(
|
await _userDeviceKeysBox.put(
|
||||||
|
|
@ -876,10 +879,9 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> setLastSentMessageUserDeviceKey(
|
Future<void> setLastSentMessageUserDeviceKey(
|
||||||
String lastSentMessage, String userId, String deviceId) async {
|
String lastSentMessage, String userId, String deviceId) async {
|
||||||
final raw =
|
final raw = copyMap(
|
||||||
(await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ??
|
await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ?? {},
|
||||||
{})
|
);
|
||||||
.copy;
|
|
||||||
raw['last_sent_message'] = lastSentMessage;
|
raw['last_sent_message'] = lastSentMessage;
|
||||||
await _userDeviceKeysBox.put(
|
await _userDeviceKeysBox.put(
|
||||||
TupleKey(userId, deviceId).toString(),
|
TupleKey(userId, deviceId).toString(),
|
||||||
|
|
@ -892,7 +894,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
String? prevBatch, String roomId, Client client) async {
|
String? prevBatch, String roomId, Client client) async {
|
||||||
final raw = await _roomsBox.get(roomId);
|
final raw = await _roomsBox.get(roomId);
|
||||||
if (raw == null) return;
|
if (raw == null) return;
|
||||||
final room = Room.fromJson(raw.copy, client);
|
final room = Room.fromJson(copyMap(raw), client);
|
||||||
room.prev_batch = prevBatch;
|
room.prev_batch = prevBatch;
|
||||||
await _roomsBox.put(roomId, room.toJson());
|
await _roomsBox.put(roomId, room.toJson());
|
||||||
return;
|
return;
|
||||||
|
|
@ -901,10 +903,11 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> setVerifiedUserCrossSigningKey(
|
Future<void> setVerifiedUserCrossSigningKey(
|
||||||
bool verified, String userId, String publicKey) async {
|
bool verified, String userId, String publicKey) async {
|
||||||
final raw = ((await _userCrossSigningKeysBox
|
final raw = copyMap(
|
||||||
.get(TupleKey(userId, publicKey).toString())) ??
|
(await _userCrossSigningKeysBox
|
||||||
{})
|
.get(TupleKey(userId, publicKey).toString())) ??
|
||||||
.copy;
|
{},
|
||||||
|
);
|
||||||
raw['verified'] = verified;
|
raw['verified'] = verified;
|
||||||
await _userCrossSigningKeysBox.put(
|
await _userCrossSigningKeysBox.put(
|
||||||
TupleKey(userId, publicKey).toString(),
|
TupleKey(userId, publicKey).toString(),
|
||||||
|
|
@ -916,10 +919,9 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
@override
|
@override
|
||||||
Future<void> setVerifiedUserDeviceKey(
|
Future<void> setVerifiedUserDeviceKey(
|
||||||
bool verified, String userId, String deviceId) async {
|
bool verified, String userId, String deviceId) async {
|
||||||
final raw =
|
final raw = copyMap(
|
||||||
(await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ??
|
await _userDeviceKeysBox.get(TupleKey(userId, deviceId).toString()) ?? {},
|
||||||
{})
|
);
|
||||||
.copy;
|
|
||||||
raw['verified'] = verified;
|
raw['verified'] = verified;
|
||||||
await _userDeviceKeysBox.put(
|
await _userDeviceKeysBox.put(
|
||||||
TupleKey(userId, deviceId).toString(),
|
TupleKey(userId, deviceId).toString(),
|
||||||
|
|
@ -978,7 +980,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
final prevStatus = prevEvent == null
|
final prevStatus = prevEvent == null
|
||||||
? null
|
? null
|
||||||
: () {
|
: () {
|
||||||
final json = prevEvent.copy;
|
final json = copyMap(prevEvent);
|
||||||
final statusInt = json.tryGet<int>('status') ??
|
final statusInt = json.tryGet<int>('status') ??
|
||||||
json
|
json
|
||||||
.tryGetMap<String, dynamic>('unsigned')
|
.tryGetMap<String, dynamic>('unsigned')
|
||||||
|
|
@ -1087,7 +1089,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
eventUpdate.roomID,
|
eventUpdate.roomID,
|
||||||
type,
|
type,
|
||||||
).toString();
|
).toString();
|
||||||
final stateMap = (await roomStateBox.get(key) ?? {}).copy;
|
final stateMap = copyMap(await roomStateBox.get(key) ?? {});
|
||||||
// store state events and new messages, that either are not an edit or an edit of the lastest message
|
// store state events and new messages, that either are not an edit or an edit of the lastest message
|
||||||
// An edit is an event, that has an edit relation to the latest event. In some cases for the second edit, we need to compare if both have an edit relation to the same event instead.
|
// An edit is an event, that has an edit relation to the latest event. In some cases for the second edit, we need to compare if both have an edit relation to the same event instead.
|
||||||
if (eventUpdate.content
|
if (eventUpdate.content
|
||||||
|
|
@ -1240,7 +1242,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
membership: membership,
|
membership: membership,
|
||||||
).toJson());
|
).toJson());
|
||||||
} else if (roomUpdate is JoinedRoomUpdate) {
|
} else if (roomUpdate is JoinedRoomUpdate) {
|
||||||
final currentRoom = Room.fromJson((currentRawRoom).copy, client);
|
final currentRoom = Room.fromJson(copyMap(currentRawRoom), client);
|
||||||
await _roomsBox.put(
|
await _roomsBox.put(
|
||||||
roomId,
|
roomId,
|
||||||
Room(
|
Room(
|
||||||
|
|
@ -1397,7 +1399,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
'Tried to update inbound group session indexes of a session which was not found in the database!');
|
'Tried to update inbound group session indexes of a session which was not found in the database!');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
final json = raw.copy;
|
final json = copyMap(raw);
|
||||||
json['indexes'] = indexes;
|
json['indexes'] = indexes;
|
||||||
await _inboundGroupSessionsBox.put(sessionId, json);
|
await _inboundGroupSessionsBox.put(sessionId, json);
|
||||||
return;
|
return;
|
||||||
|
|
@ -1407,7 +1409,7 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
Future<List<StoredInboundGroupSession>> getAllInboundGroupSessions() async {
|
Future<List<StoredInboundGroupSession>> getAllInboundGroupSessions() async {
|
||||||
final rawSessions = await _inboundGroupSessionsBox.getAllValues();
|
final rawSessions = await _inboundGroupSessionsBox.getAllValues();
|
||||||
return rawSessions.values
|
return rawSessions.values
|
||||||
.map((raw) => StoredInboundGroupSession.fromJson(raw.copy))
|
.map((raw) => StoredInboundGroupSession.fromJson(copyMap(raw)))
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1592,6 +1594,6 @@ class MatrixSdkDatabase extends DatabaseApi {
|
||||||
final rawPresence = await _presencesBox.get(userId);
|
final rawPresence = await _presencesBox.get(userId);
|
||||||
if (rawPresence == null) return null;
|
if (rawPresence == null) return null;
|
||||||
|
|
||||||
return CachedPresence.fromJson(rawPresence.copy);
|
return CachedPresence.fromJson(copyMap(rawPresence));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,20 @@
|
||||||
/// The database always gives back an `_InternalLinkedHasMap<dynamic, dynamic>`.
|
/// The database always gives back an `_InternalLinkedHasMap<dynamic, dynamic>`.
|
||||||
/// This creates a deep copy of the json and makes sure that the format is
|
/// This creates a deep copy of the json and makes sure that the format is
|
||||||
/// always `Map<String, Object?>`.
|
/// always `Map<String, Object?>`.
|
||||||
extension CopyMapExtension on Map {
|
Map<String, Object?> copyMap(Map map) {
|
||||||
Map<String, Object?> get copy {
|
final copy = Map<String, dynamic>.from(map);
|
||||||
final copy = Map<String, dynamic>.from(this);
|
for (final entry in copy.entries) {
|
||||||
for (final entry in copy.entries) {
|
copy[entry.key] = _castValue(entry.value);
|
||||||
copy[entry.key] = _castValue(entry.value);
|
|
||||||
}
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
|
|
||||||
dynamic _castValue(dynamic value) {
|
|
||||||
if (value is Map) {
|
|
||||||
return value.copy;
|
|
||||||
}
|
|
||||||
if (value is List) {
|
|
||||||
return value.map(_castValue).toList();
|
|
||||||
}
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
|
return copy;
|
||||||
|
}
|
||||||
|
|
||||||
|
dynamic _castValue(dynamic value) {
|
||||||
|
if (value is Map) {
|
||||||
|
return copyMap(value);
|
||||||
|
}
|
||||||
|
if (value is List) {
|
||||||
|
return value.map(_castValue).toList();
|
||||||
|
}
|
||||||
|
return value;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
/*
|
|
||||||
* Famedly Matrix SDK
|
|
||||||
* Copyright (C) 2020, 2021 Famedly GmbH
|
|
||||||
*
|
|
||||||
* This program is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU Affero General Public License as
|
|
||||||
* published by the Free Software Foundation, either version 3 of the
|
|
||||||
* License, or (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU Affero General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU Affero General Public License
|
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
extension MapCopyExtension on Map<String, dynamic> {
|
|
||||||
/// Deep-copies a given json map
|
|
||||||
Map<String, dynamic> copy() {
|
|
||||||
final copy = Map<String, dynamic>.from(this);
|
|
||||||
for (final entry in copy.entries) {
|
|
||||||
if (entry.value is Map<String, dynamic>) {
|
|
||||||
copy[entry.key] = (entry.value as Map<String, dynamic>).copy();
|
|
||||||
}
|
|
||||||
if (entry.value is List) {
|
|
||||||
copy[entry.key] = List.from(entry.value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return copy;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -18,7 +18,7 @@
|
||||||
|
|
||||||
import 'package:test/test.dart';
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
import 'package:matrix/src/utils/map_copy_extension.dart';
|
import 'package:matrix/src/utils/copy_map.dart';
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
group('Map-copy-extension', () {
|
group('Map-copy-extension', () {
|
||||||
|
|
@ -30,11 +30,11 @@ void main() {
|
||||||
'list': [1, 2],
|
'list': [1, 2],
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
final copy = original.copy();
|
final copy = copyMap(original);
|
||||||
original['child']['attr'] = 'raccoon';
|
original['child']['attr'] = 'raccoon';
|
||||||
expect(copy['child']['attr'], 'bunny');
|
expect((copy['child'] as Map)['attr'], 'bunny');
|
||||||
original['child']['list'].add(3);
|
original['child']['list'].add(3);
|
||||||
expect(copy['child']['list'], [1, 2]);
|
expect((copy['child'] as Map)['list'], [1, 2]);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue