Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add atSign to AtSignLoggers' names when feasible. Some code simplification and refactoring #870

Merged
merged 11 commits into from
Jan 11, 2023
Merged
5 changes: 1 addition & 4 deletions packages/at_client/example/bin/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ void main() async {
atClient.put(
AtKey.public('phone', namespace: namespace).build(), '+91 8908901234');

// Getting the NotificationService instance
NotificationService notificationService =
AtClientManager.getInstance().notificationService;
// Invoking the notify method
notificationService.notify(NotificationParams.forUpdate(
atClient.notificationService.notify(NotificationParams.forUpdate(
AtKey.shared('phone', namespace: namespace).build()));
}
3 changes: 3 additions & 0 deletions packages/at_client/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ version: 1.0.0
environment:
sdk: '>=2.14.4 <3.0.0'

dependency_overrides:
at_client:
path: ../../at_client

dependencies:
at_utils: ^3.0.11
Expand Down
1 change: 0 additions & 1 deletion packages/at_client/lib/at_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ export 'package:at_client/src/service/notification_service.dart';
export 'package:at_client/src/service/sync/sync_result.dart';
export 'package:at_client/src/service/sync/sync_status.dart';
export 'package:at_client/src/util/at_client_util.dart';
export 'package:at_client/src/util/encryption_util.dart';
export 'package:at_client/src/key_stream/key_stream.dart';
export 'package:at_client/src/service/sync/sync_conflict.dart';
export 'package:at_commons/at_commons.dart';
Expand Down
94 changes: 52 additions & 42 deletions packages/at_client/lib/src/client/at_client_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import 'package:at_client/src/response/response.dart';
import 'package:at_client/src/service/encryption_service.dart';
import 'package:at_client/src/service/file_transfer_service.dart';
import 'package:at_client/src/service/notification_service.dart';
import 'package:at_client/src/service/sync_service.dart';
import 'package:at_client/src/stream/at_stream_notification.dart';
import 'package:at_client/src/stream/at_stream_response.dart';
import 'package:at_client/src/stream/file_transfer_object.dart';
Expand Down Expand Up @@ -46,7 +47,7 @@ class AtClientImpl implements AtClient {
AtClientPreference? _preference;

AtClientPreference? get preference => _preference;
String? currentAtSign;
late final String _atSign;
String? _namespace;
SecondaryKeyStore? _localSecondaryKeyStore;
LocalSecondary? _localSecondary;
Expand All @@ -67,7 +68,6 @@ class AtClientImpl implements AtClient {
_telemetry = telemetryService;
_cascadeSetTelemetryService();
}

@override
@experimental
AtTelemetryService? get telemetry => _telemetry;
Expand All @@ -80,12 +80,28 @@ class AtClientImpl implements AtClient {
@override
AtChops? get atChops => _atChops;

late SyncService _syncService;
@override
set syncService (SyncService syncService) {
_syncService = syncService;
}
@override
SyncService get syncService => _syncService;

late NotificationService _notificationService;
@override
set notificationService (NotificationService notificationService) {
_notificationService = notificationService;
}
@override
NotificationService get notificationService => _notificationService;

@override
EncryptionService? get encryptionService => _encryptionService;

AtClientManager? _atClientManager;
late final AtClientManager _atClientManager;

final _logger = AtSignLogger('AtClientImpl');
late final AtSignLogger _logger;

@visibleForTesting
static final Map atClientInstanceMap = <String, AtClient>{};
Expand Down Expand Up @@ -116,13 +132,14 @@ class AtClientImpl implements AtClient {
return atClientInstanceMap[currentAtSign];
}

AtClientImpl._(String atSign, String? namespace,
AtClientImpl._(String theAtSign, String? namespace,
AtClientPreference preference, AtClientManager atClientManager,
{RemoteSecondary? remoteSecondary,
EncryptionService? encryptionService,
SecondaryKeyStore? localSecondaryKeyStore,
AtChops? atChops}) {
currentAtSign = AtUtils.formatAtSign(atSign);
_atSign = AtUtils.formatAtSign(theAtSign)!;
_logger = AtSignLogger('AtClientImpl ($_atSign)');
_preference = preference;
_preference?.namespace ??= namespace;
_namespace = namespace;
Expand All @@ -141,22 +158,22 @@ class AtClientImpl implements AtClient {
if (_preference!.isLocalStoreRequired) {
if (_localSecondaryKeyStore == null) {
var storageManager = StorageManager(preference);
await storageManager.init(currentAtSign!, preference!.keyStoreSecret);
await storageManager.init(_atSign, preference!.keyStoreSecret);
}

_localSecondary = LocalSecondary(this, keyStore: _localSecondaryKeyStore);
}

// Now using ??= because we may be injecting a RemoteSecondary
_remoteSecondary ??= RemoteSecondary(currentAtSign!, _preference!,
_remoteSecondary ??= RemoteSecondary(_atSign, _preference!,
privateKey: _preference!.privateKey);

// Now using ??= because we may be injecting an EncryptionService
_encryptionService ??= EncryptionService();
_encryptionService ??= EncryptionService(_atSign);

_encryptionService!.remoteSecondary = _remoteSecondary;
_encryptionService!.currentAtSign = currentAtSign;
_encryptionService!.localSecondary = _localSecondary;

_cascadeSetTelemetryService();
}

Expand Down Expand Up @@ -200,7 +217,7 @@ class AtClientImpl implements AtClient {
@override
@Deprecated("Use SyncManager.sync")
SyncManager? getSyncManager() {
return SyncManagerImpl.getInstance().getSyncManager(currentAtSign);
return SyncManagerImpl.getInstance().getSyncManager(_atSign);
}

@override
Expand Down Expand Up @@ -249,7 +266,7 @@ class AtClientImpl implements AtClient {
} else {
keyWithNamespace = atKey.key!;
}
atKey.sharedBy ??= currentAtSign;
atKey.sharedBy ??= _atSign;
var builder = DeleteVerbBuilder()
..isLocal = atKey.isLocal
..isCached = atKey.metadata!.isCached
Expand All @@ -273,7 +290,7 @@ class AtClientImpl implements AtClient {
var verbBuilder = GetRequestTransformer(this)
.transform(atKey, requestOptions: getRequestOptions);
// Execute the verb.
secondary = SecondaryManager.getSecondary(verbBuilder);
secondary = SecondaryManager.getSecondary(this, verbBuilder);
var getResponse = await secondary.executeVerb(verbBuilder);
// Return empty value if getResponse is null.
if (getResponse == null ||
Expand Down Expand Up @@ -409,7 +426,7 @@ class AtClientImpl implements AtClient {
AtClientValidation.validatePutRequest(atKey, value, preference!);
// Set sharedBy to currentAtSign if not set.
if (atKey.sharedBy.isNull) {
atKey.sharedBy = currentAtSign;
atKey.sharedBy = _atSign;
}
if (atKey.metadata!.namespaceAware) {
atKey.namespace ??= preference?.namespace;
Expand All @@ -425,7 +442,7 @@ class AtClientImpl implements AtClient {
var validationResult = AtKeyValidators.get().validate(
atKey.toString(),
ValidationContext()
..atSign = currentAtSign
..atSign = _atSign
..validateOwnership = true
..enforceNamespace = enforceNamespace);
// If the validationResult.isValid is false, validation of AtKey failed.
Expand All @@ -447,7 +464,7 @@ class AtClientImpl implements AtClient {
UpdateVerbBuilder verbBuilder = await PutRequestTransformer(this)
.transform(tuple, encryptionPrivateKey: encryptionPrivateKey);
// Execute the verb builder
var putResponse = await SecondaryManager.getSecondary(verbBuilder)
var putResponse = await SecondaryManager.getSecondary(this, verbBuilder)
.executeVerb(verbBuilder, sync: SyncUtil.shouldSync(atKey.key!));
// If putResponse is null or empty, return AtResponse with isError set to true
if (putResponse == null || putResponse.isEmpty) {
Expand All @@ -467,12 +484,11 @@ class AtClientImpl implements AtClient {
AtKeyValidators.get().validate(
atKey.toString(),
ValidationContext()
..atSign = currentAtSign
..atSign = _atSign
..validateOwnership = true);
final notificationParams =
NotificationParams.forUpdate(atKey, value: value);
final notifyResult =
await _atClientManager!.notificationService.notify(notificationParams);
final notifyResult = await notificationService.notify(notificationParams);
return notifyResult.notificationStatusEnum ==
NotificationStatusEnum.delivered;
}
Expand All @@ -486,8 +502,7 @@ class AtClientImpl implements AtClient {
atKey.sharedWith = sharedWith;
final notificationParams =
NotificationParams.forUpdate(atKey, value: value);
final notifyResult = await _atClientManager!.notificationService
.notify(notificationParams);
final notifyResult = await notificationService.notify(notificationParams);
returnMap.putIfAbsent(
sharedWith,
() => (notifyResult.notificationStatusEnum ==
Expand Down Expand Up @@ -532,7 +547,7 @@ class AtClientImpl implements AtClient {
var builder = UpdateVerbBuilder();
builder
..atKey = updateKey
..sharedBy = currentAtSign
..sharedBy = _atSign
..sharedWith = sharedWith
..ttl = metadata.ttl
..ttb = metadata.ttb
Expand Down Expand Up @@ -601,7 +616,7 @@ class AtClientImpl implements AtClient {
var command =
'stream:init$sharedWith namespace:$namespace $streamId $fileName ${encryptedData.length}\n';
_logger.finer('sending stream init:$command');
var remoteSecondary = RemoteSecondary(currentAtSign!, _preference!);
var remoteSecondary = RemoteSecondary(_atSign, _preference!);
var result = await remoteSecondary.executeCommand(command, auth: true);
_logger.finer('ack message:$result');
if (result != null && result.startsWith('stream:ack')) {
Expand Down Expand Up @@ -642,7 +657,7 @@ class AtClientImpl implements AtClient {
var notification = AtStreamNotification()
..streamId = streamId
..fileName = fileName
..currentAtSign = currentAtSign!
..currentAtSign = _atSign
..senderAtSign = senderAtSign
..fileLength = fileLength;
_logger.info('Sending ack for stream notification:$notification');
Expand Down Expand Up @@ -684,10 +699,10 @@ class AtClientImpl implements AtClient {
..metadata!.ttr = -1
// file transfer key will be deleted after 30 days
..metadata!.ttl = 2592000000
..sharedBy = currentAtSign;
..sharedBy = _atSign;

var notificationResult =
await _atClientManager!.notificationService.notify(
await notificationService.notify(
NotificationParams.forUpdate(
atKey,
value: jsonEncode(fileTransferObject.toJson()),
Expand Down Expand Up @@ -820,9 +835,7 @@ class AtClientImpl implements AtClient {
}

@override
String? getCurrentAtSign() {
return currentAtSign;
}
String? getCurrentAtSign() => _atSign;

@override
AtClientPreference? getPreferences() {
Expand All @@ -837,10 +850,8 @@ class AtClientImpl implements AtClient {
AtUtils.fixAtSign(notificationParams.atKey.sharedWith!);
// Check if sharedWith AtSign exists
await AtClientValidation().isAtSignExists(
AtClientManager.getInstance().secondaryAddressFinder!,
notificationParams.atKey.sharedWith!,
_preference!.rootDomain,
_preference!.rootPort);
_atClientManager.secondaryAddressFinder!,
notificationParams.atKey.sharedWith!);
// validate sharedBy atSign
if (notificationParams.atKey.sharedBy == null ||
notificationParams.atKey.sharedBy!.isEmpty) {
Expand All @@ -855,7 +866,7 @@ class AtClientImpl implements AtClient {
ValidationResult validationResult = AtKeyValidators.get().validate(
notificationParams.atKey.toString(),
ValidationContext()
..atSign = currentAtSign
..atSign = _atSign
..validateOwnership = true);
if (!validationResult.isValid) {
throw AtClientException('AT0014', validationResult.failureReason);
Expand All @@ -868,7 +879,7 @@ class AtClientImpl implements AtClient {
notificationParams.atKey.metadata!.namespaceAware) {
notifyKey = _getKeyWithNamespace(notifyKey!);
}
notificationParams.atKey.sharedBy ??= currentAtSign;
notificationParams.atKey.sharedBy ??= _atSign;

var builder = NotifyVerbBuilder()
..id = notificationParams.id
Expand All @@ -889,10 +900,10 @@ class AtClientImpl implements AtClient {
// If atKey is being notified to another atSign, encrypt data with other
// atSign encryption public key.
if (notificationParams.atKey.sharedWith != null &&
notificationParams.atKey.sharedWith != currentAtSign) {
notificationParams.atKey.sharedWith != _atSign) {
try {
final atKeyEncryption = AtKeyEncryptionManager(this)
.get(notificationParams.atKey, currentAtSign!);
.get(notificationParams.atKey, _atSign);
builder.value = await atKeyEncryption.encrypt(
notificationParams.atKey, notificationParams.value!);
} on KeyNotFoundException catch (e) {
Expand All @@ -902,10 +913,10 @@ class AtClientImpl implements AtClient {
}
// If sharedWith is currentAtSign, encrypt data with currentAtSign encryption public key.
if (notificationParams.atKey.sharedWith == null ||
notificationParams.atKey.sharedWith == currentAtSign) {
notificationParams.atKey.sharedWith == _atSign) {
try {
final atKeyEncryption = AtKeyEncryptionManager(this)
.get(notificationParams.atKey, currentAtSign!);
.get(notificationParams.atKey, _atSign);
builder.value = await atKeyEncryption.encrypt(
notificationParams.atKey, notificationParams.value!);
} on KeyNotFoundException catch (e) {
Expand Down Expand Up @@ -968,11 +979,10 @@ class AtClientImpl implements AtClient {

@Deprecated("Use [create]")
AtClientImpl(
// ignore: no_leading_underscores_for_local_identifiers
String _atSign,
String atSign,
String? namespace,
AtClientPreference preference) {
currentAtSign = AtUtils.formatAtSign(_atSign);
_atSign = AtUtils.formatAtSign(atSign)!;
_preference = preference;
_namespace = namespace;
}
Expand Down
13 changes: 11 additions & 2 deletions packages/at_client/lib/src/client/at_client_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:at_client/src/preference/at_client_preference.dart';
import 'package:at_client/src/response/response.dart';
import 'package:at_client/src/service/encryption_service.dart';
import 'package:at_client/src/service/notification_service.dart';
import 'package:at_client/src/service/sync_service.dart';
import 'package:at_client/src/stream/at_stream_response.dart';
import 'package:at_client/src/stream/file_transfer_object.dart';
import 'package:at_commons/at_commons.dart';
Expand All @@ -18,7 +19,7 @@ import 'package:meta/meta.dart';
abstract class AtClient {
/// Returns a singleton instance of [SyncManager] that is responsible for syncing data between
/// local secondary server and remote secondary server.
/// [Deprecated] Use [AtClientManager.syncService]
/// [Deprecated] Use [AtClient.syncService]
@Deprecated("Use SyncManager.sync")
SyncManager? getSyncManager();

Expand All @@ -37,6 +38,12 @@ abstract class AtClient {

AtChops? get atChops;

set syncService (SyncService syncService);
SyncService get syncService;

set notificationService (NotificationService notificationService);
NotificationService get notificationService;

/// Sets the preferences such as sync strategy, storage path etc., for the client.
void setPreferences(AtClientPreference preference);

Expand Down Expand Up @@ -354,7 +361,7 @@ abstract class AtClient {
/// latestN:3,
/// Notifier: ‘wavi’);
///```
///[Deprecated] Use [AtClientManager.notificationService]
///[Deprecated] Use [AtClient.notificationService]
@Deprecated("Use NotificationService")
Future<bool> notify(AtKey key, String value, OperationEnum operation,
{MessageTypeEnum? messageType,
Expand Down Expand Up @@ -533,6 +540,8 @@ abstract class AtClient {
List<FileStatus> fileStatus,
{DateTime? date});

/// Note - this method name is misleading as 'current' implies the atSign
/// could change - but an AtClient should only ever have one atSign.
String? getCurrentAtSign();

EncryptionService? get encryptionService;
Expand Down
Loading