2020-02-15 07:48:41 +00:00
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
import 'package:famedlysdk/famedlysdk.dart';
|
|
|
|
|
|
|
|
class FakeStore implements StoreAPI {
|
|
|
|
/// Whether this is a simple store which only stores the client credentials and
|
|
|
|
/// end to end encryption stuff or the whole sync payloads.
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
final bool extended = false;
|
|
|
|
|
|
|
|
Map<String, dynamic> storeMap = {};
|
|
|
|
|
|
|
|
/// Link back to the client.
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
Client client;
|
|
|
|
|
|
|
|
FakeStore(this.client, this.storeMap) {
|
|
|
|
_init();
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
Future<void> _init() async {
|
2020-02-15 07:48:41 +00:00
|
|
|
final credentialsStr = await getItem(client.clientName);
|
|
|
|
|
|
|
|
if (credentialsStr == null || credentialsStr.isEmpty) {
|
|
|
|
client.onLoginStateChanged.add(LoginState.loggedOut);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-30 09:08:38 +00:00
|
|
|
print('[Matrix] Restoring account credentials');
|
2020-02-15 07:48:41 +00:00
|
|
|
final Map<String, dynamic> credentials = json.decode(credentialsStr);
|
|
|
|
client.connect(
|
2020-03-30 09:08:38 +00:00
|
|
|
newDeviceID: credentials['deviceID'],
|
|
|
|
newDeviceName: credentials['deviceName'],
|
|
|
|
newHomeserver: credentials['homeserver'],
|
|
|
|
newLazyLoadMembers: credentials['lazyLoadMembers'],
|
|
|
|
newMatrixVersions: List<String>.from(credentials['matrixVersions']),
|
|
|
|
newToken: credentials['token'],
|
|
|
|
newUserID: credentials['userID'],
|
|
|
|
newPrevBatch: credentials['prev_batch'],
|
|
|
|
newOlmAccount: credentials['olmAccount'],
|
2020-02-15 07:48:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Will be automatically called when the client is logged in successfully.
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
Future<void> storeClient() async {
|
2020-03-30 09:08:38 +00:00
|
|
|
final credentials = {
|
|
|
|
'deviceID': client.deviceID,
|
|
|
|
'deviceName': client.deviceName,
|
|
|
|
'homeserver': client.homeserver,
|
|
|
|
'lazyLoadMembers': client.lazyLoadMembers,
|
|
|
|
'matrixVersions': client.matrixVersions,
|
|
|
|
'token': client.accessToken,
|
|
|
|
'userID': client.userID,
|
|
|
|
'olmAccount': client.pickledOlmAccount,
|
2020-02-15 07:48:41 +00:00
|
|
|
};
|
|
|
|
await setItem(client.clientName, json.encode(credentials));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Clears all tables from the database.
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
Future<void> clear() async {
|
|
|
|
storeMap = {};
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
Future<dynamic> getItem(String key) async {
|
|
|
|
return storeMap[key];
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
Future<void> setItem(String key, String value) async {
|
|
|
|
storeMap[key] = value;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
String get _UserDeviceKeysKey => '${client.clientName}.user_device_keys';
|
2020-02-15 07:48:41 +00:00
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
Future<Map<String, DeviceKeysList>> getUserDeviceKeys() async {
|
|
|
|
final deviceKeysListString = await getItem(_UserDeviceKeysKey);
|
|
|
|
if (deviceKeysListString == null) return {};
|
|
|
|
Map<String, dynamic> rawUserDeviceKeys = json.decode(deviceKeysListString);
|
2020-03-30 09:08:38 +00:00
|
|
|
var userDeviceKeys = <String, DeviceKeysList>{};
|
2020-02-15 07:48:41 +00:00
|
|
|
for (final entry in rawUserDeviceKeys.entries) {
|
|
|
|
userDeviceKeys[entry.key] = DeviceKeysList.fromJson(entry.value);
|
|
|
|
}
|
|
|
|
return userDeviceKeys;
|
|
|
|
}
|
|
|
|
|
2020-03-30 09:08:38 +00:00
|
|
|
@override
|
2020-02-15 07:48:41 +00:00
|
|
|
Future<void> storeUserDeviceKeys(
|
|
|
|
Map<String, DeviceKeysList> userDeviceKeys) async {
|
|
|
|
await setItem(_UserDeviceKeysKey, json.encode(userDeviceKeys));
|
|
|
|
}
|
|
|
|
}
|