notifyChange method

  1. @override
Future<String?> notifyChange(
  1. NotificationParams notificationParams
)
override

Notifies the NotificationParams.atKey to notificationParams.atKey.sharedWith user of the atSign. Optionally, operation, value and metadata can be set along with key to notify.

  • Throws LateInitializationError when NotificationParams.atKey is not initialized
  • Throws AtKeyException when invalid NotificationParams.atKey.key is formed or when invalid metadata is provided.
  • Throws InvalidAtSignException on invalid NotificationParams.atKey.sharedWith or NotificationParams.atKey.sharedBy
  • Throws AtClientException when keys to encrypt the data are not found.
  • Throws AtClientException when notificationParams.notifier is null when notificationParams.strategy is set to latest.
  • Throws AtClientException when fails to connect to cloud secondary server.

e.g alice is the current atsign

  1. To notify a update of key to @bob
  var key = AtKey()..key='phone'
                   ..sharedWith='@bob'
  var notificationParams = NotificationParams().._atKey = key
                                               .._operation = OperationEnum.update
                                               .._messageType = MessageTypeEnum.key;
  notifyChange(notificationParams);
  1. To notify and cache a key - value in @bob
  var metaData = Metadata()..ttr='6000000';
  var key = AtKey()..key='phone'
                   ..sharedWith='@bob'
                   ..metadata=metaData
  var value='+1 999 9999'
  var notificationParams = NotificationParams().._atKey = key
                                               .._operation = OperationEnum.update
                                               .._value = value
                                               .._messageType = MessageTypeEnum.key;
  notifyChange(notificationParams);
  1. To notify a text message
  var key = AtKey()..key='phone'
                   ..sharedWith='@bob'
  var notificationParams = NotificationParams().._atKey = key
                                               .._operation = OperationEnum.update
                                               .._messageType = MessageTypeEnum.text;
  notifyChange(notificationParams);
  1. To notify a deletion of a key to @bob.
  var key = AtKey()..key='phone'
                   ..sharedWith='@bob'
  var notificationParams = NotificationParams().._atKey = key
                                               .._operation = OperationEnum.delete
                                               .._messageType = MessageTypeEnum.key;
  notifyChange(notificationParams);

Implementation

@override
Future<String?> notifyChange(NotificationParams notificationParams) async {
  // Check for internet. Since notify invoke remote secondary directly, network connection
  // is mandatory.
  if (!await NetworkUtil.isNetworkAvailable()) {
    throw AtClientException(at_client_error_codes['AtClientException'],
        'No network availability');
  }
  // validate sharedWith atSign
  AtUtils.fixAtSign(notificationParams.atKey.sharedWith!);
  // Check if sharedWith AtSign exists
  AtClientValidation.isAtSignExists(notificationParams.atKey.sharedWith!,
      _preference!.rootDomain, _preference!.rootPort);
  // validate sharedBy atSign
  notificationParams.atKey.sharedBy ??= getCurrentAtSign();
  AtUtils.fixAtSign(notificationParams.atKey.sharedBy!);
  // validate atKey
  AtClientValidation.validateKey(notificationParams.atKey.key);
  // validate metadata
  AtClientValidation.validateMetadata(notificationParams.atKey.metadata);
  // If namespaceAware is set to true, append nameSpace to key.
  if (notificationParams.atKey.metadata != null &&
      notificationParams.atKey.metadata!.namespaceAware) {
    notificationParams.atKey.key =
        _getKeyWithNamespace(notificationParams.atKey.key!);
  }
  notificationParams.atKey.sharedBy ??= currentAtSign;

  var builder = NotifyVerbBuilder()
    ..atKey = notificationParams.atKey.key
    ..sharedBy = notificationParams.atKey.sharedBy
    ..sharedWith = notificationParams.atKey.sharedWith
    ..operation = notificationParams.operation
    ..messageType = notificationParams.messageType
    ..priority = notificationParams.priority
    ..strategy = notificationParams.strategy
    ..latestN = notificationParams.latestN
    ..notifier = notificationParams.notifier;

  // If value is not null, encrypt the value
  if (notificationParams.value != null &&
      notificationParams.value!.isNotEmpty) {
    // 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) {
      try {
        builder.value = await _encryptionService!.encrypt(
            notificationParams.atKey.key,
            notificationParams.value!,
            notificationParams.atKey.sharedWith!);
      } on KeyNotFoundException catch (e) {
        var errorCode = AtClientExceptionUtil.getErrorCode(e);
        return Future.error(AtClientException(
            errorCode, AtClientExceptionUtil.getErrorDescription(errorCode)));
      }
    }
    // If sharedWith is currentAtSign, encrypt data with currentAtSign encryption public key.
    if (notificationParams.atKey.sharedWith == null ||
        notificationParams.atKey.sharedWith == currentAtSign) {
      builder.value = await _encryptionService!.encryptForSelf(
          notificationParams.atKey.key, notificationParams.value!);
    }
  }
  // If metadata is not null, add metadata to notify builder object.
  if (notificationParams.atKey.metadata != null) {
    builder.ttl = notificationParams.atKey.metadata!.ttl;
    builder.ttb = notificationParams.atKey.metadata!.ttb;
    builder.ttr = notificationParams.atKey.metadata!.ttr;
    builder.ccd = notificationParams.atKey.metadata!.ccd;
    builder.isPublic = notificationParams.atKey.metadata!.isPublic!;
  }
  if (notificationParams.atKey.key!.startsWith(AT_PKAM_PRIVATE_KEY) ||
      notificationParams.atKey.key!.startsWith(AT_PKAM_PUBLIC_KEY)) {
    builder.sharedBy = null;
  }
  return await getRemoteSecondary()?.executeVerb(builder);
}