refactor: Make client members read only

This commit is contained in:
Krille 2024-05-16 07:23:30 +02:00
parent 9217231527
commit 93bf30f376
No known key found for this signature in database
GPG Key ID: E067ECD60F1A0652
1 changed files with 25 additions and 20 deletions

View File

@ -70,7 +70,8 @@ class Client extends MatrixApi {
DatabaseApi? get database => _database; DatabaseApi? get database => _database;
Encryption? encryption; Encryption? get encryption => _encryption;
Encryption? _encryption;
Set<KeyVerificationMethod> verificationMethods; Set<KeyVerificationMethod> verificationMethods;
@ -92,7 +93,8 @@ class Client extends MatrixApi {
Future<void> Function(Client client)? onSoftLogout; Future<void> Function(Client client)? onSoftLogout;
DateTime? accessTokenExpiresAt; DateTime? get accessTokenExpiresAt => _accessTokenExpiresAt;
DateTime? _accessTokenExpiresAt;
// For CommandsClientExtension // For CommandsClientExtension
final Map<String, FutureOr<String?> Function(CommandArgs)> commands = {}; final Map<String, FutureOr<String?> Function(CommandArgs)> commands = {};
@ -100,7 +102,9 @@ class Client extends MatrixApi {
final NativeImplementations nativeImplementations; final NativeImplementations nativeImplementations;
String? syncFilterId; String? _syncFilterId;
String? get syncFilterId => _syncFilterId;
final ComputeCallback? compute; final ComputeCallback? compute;
@ -275,7 +279,7 @@ class Client extends MatrixApi {
final tokenExpiresAt = expiresInMs == null final tokenExpiresAt = expiresInMs == null
? null ? null
: DateTime.now().add(Duration(milliseconds: expiresInMs)); : DateTime.now().add(Duration(milliseconds: expiresInMs));
accessTokenExpiresAt = tokenExpiresAt; _accessTokenExpiresAt = tokenExpiresAt;
await database?.updateClient( await database?.updateClient(
homeserverUrl, homeserverUrl,
tokenResponse.accessToken, tokenResponse.accessToken,
@ -297,7 +301,8 @@ class Client extends MatrixApi {
String? _userID; String? _userID;
/// This points to the position in the synchronization history. /// This points to the position in the synchronization history.
String? prevBatch; String? get prevBatch => _prevBatch;
String? _prevBatch;
/// The device ID is an unique identifier for this device. /// The device ID is an unique identifier for this device.
String? get deviceID => _deviceID; String? get deviceID => _deviceID;
@ -1591,19 +1596,19 @@ class Client extends MatrixApi {
accessToken = this.accessToken = account['token']; accessToken = this.accessToken = account['token'];
final tokenExpiresAtMs = final tokenExpiresAtMs =
int.tryParse(account.tryGet<String>('token_expires_at') ?? ''); int.tryParse(account.tryGet<String>('token_expires_at') ?? '');
accessTokenExpiresAt = tokenExpiresAtMs == null _accessTokenExpiresAt = tokenExpiresAtMs == null
? null ? null
: DateTime.fromMillisecondsSinceEpoch(tokenExpiresAtMs); : DateTime.fromMillisecondsSinceEpoch(tokenExpiresAtMs);
userID = _userID = account['user_id']; userID = _userID = account['user_id'];
_deviceID = account['device_id']; _deviceID = account['device_id'];
_deviceName = account['device_name']; _deviceName = account['device_name'];
syncFilterId = account['sync_filter_id']; _syncFilterId = account['sync_filter_id'];
prevBatch = account['prev_batch']; _prevBatch = account['prev_batch'];
olmAccount = account['olm_account']; olmAccount = account['olm_account'];
} }
if (newToken != null) { if (newToken != null) {
accessToken = this.accessToken = newToken; accessToken = this.accessToken = newToken;
accessTokenExpiresAt = newTokenExpiresAt; _accessTokenExpiresAt = newTokenExpiresAt;
homeserver = newHomeserver; homeserver = newHomeserver;
userID = _userID = newUserID; userID = _userID = newUserID;
_deviceID = newDeviceID; _deviceID = newDeviceID;
@ -1611,7 +1616,7 @@ class Client extends MatrixApi {
olmAccount = newOlmAccount; olmAccount = newOlmAccount;
} else { } else {
accessToken = this.accessToken = newToken ?? accessToken; accessToken = this.accessToken = newToken ?? accessToken;
accessTokenExpiresAt = newTokenExpiresAt ?? accessTokenExpiresAt; _accessTokenExpiresAt = newTokenExpiresAt ?? accessTokenExpiresAt;
homeserver = newHomeserver ?? homeserver; homeserver = newHomeserver ?? homeserver;
userID = _userID = newUserID ?? userID; userID = _userID = newUserID ?? userID;
_deviceID = newDeviceID ?? _deviceID; _deviceID = newDeviceID ?? _deviceID;
@ -1646,7 +1651,7 @@ class Client extends MatrixApi {
} }
// we aren't logged in // we aren't logged in
await encryption?.dispose(); await encryption?.dispose();
encryption = null; _encryption = null;
onLoginStateChanged.add(LoginState.loggedOut); onLoginStateChanged.add(LoginState.loggedOut);
Logs().i('User is not logged in.'); Logs().i('User is not logged in.');
_initLock = false; _initLock = false;
@ -1658,11 +1663,11 @@ class Client extends MatrixApi {
// make sure to throw an exception if libolm doesn't exist // make sure to throw an exception if libolm doesn't exist
await olm.init(); await olm.init();
olm.get_library_version(); olm.get_library_version();
encryption = Encryption(client: this); _encryption = Encryption(client: this);
} catch (e) { } catch (e) {
Logs().e('Error initializing encryption $e'); Logs().e('Error initializing encryption $e');
await encryption?.dispose(); await encryption?.dispose();
encryption = null; _encryption = null;
} }
await encryption?.init(olmAccount); await encryption?.init(olmAccount);
@ -1764,12 +1769,12 @@ class Client extends MatrixApi {
_database = null; _database = null;
} }
_id = accessToken = syncFilterId = _id = accessToken = _syncFilterId =
homeserver = _userID = _deviceID = _deviceName = prevBatch = null; homeserver = _userID = _deviceID = _deviceName = _prevBatch = null;
_rooms = []; _rooms = [];
_eventsPendingDecryption.clear(); _eventsPendingDecryption.clear();
await encryption?.dispose(); await encryption?.dispose();
encryption = null; _encryption = null;
onLoginStateChanged.add(LoginState.loggedOut); onLoginStateChanged.add(LoginState.loggedOut);
} }
@ -1817,7 +1822,7 @@ class Client extends MatrixApi {
final userID = this.userID; final userID = this.userID;
if (syncFilterId == null && userID != null) { if (syncFilterId == null && userID != null) {
final syncFilterId = final syncFilterId =
this.syncFilterId = await defineFilter(userID, syncFilter); _syncFilterId = await defineFilter(userID, syncFilter);
await database?.storeSyncFilterId(syncFilterId); await database?.storeSyncFilterId(syncFilterId);
} }
return; return;
@ -1931,7 +1936,7 @@ class Client extends MatrixApi {
await _handleSync(syncResp, direction: Direction.f); await _handleSync(syncResp, direction: Direction.f);
} }
if (_disposed || _aborted) return; if (_disposed || _aborted) return;
prevBatch = syncResp.nextBatch; _prevBatch = syncResp.nextBatch;
onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp)); onSyncStatus.add(SyncStatusUpdate(SyncStatus.cleaningUp));
// ignore: unawaited_futures // ignore: unawaited_futures
database?.deleteOldFiles( database?.deleteOldFiles(
@ -3055,7 +3060,7 @@ class Client extends MatrixApi {
/// sessions and perform a new clean sync. /// sessions and perform a new clean sync.
Future<void> clearCache() async { Future<void> clearCache() async {
await abortSync(); await abortSync();
prevBatch = null; _prevBatch = null;
rooms.clear(); rooms.clear();
await database?.clearCache(); await database?.clearCache();
encryption?.keyManager.clearOutboundGroupSessions(); encryption?.keyManager.clearOutboundGroupSessions();
@ -3162,7 +3167,7 @@ class Client extends MatrixApi {
_disposed = true; _disposed = true;
await abortSync(); await abortSync();
await encryption?.dispose(); await encryption?.dispose();
encryption = null; _encryption = null;
try { try {
if (closeDatabase) { if (closeDatabase) {
final database = _database; final database = _database;