Skip to content

Commit

Permalink
Merge pull request #1301 from atsign-foundation/fix_existing_metadata…
Browse files Browse the repository at this point in the history
…_overwritten

fix: Populate existing metadata for null values of new metadata
  • Loading branch information
sitaram-kalluri authored Apr 9, 2023
2 parents d1a0ce1 + 98d6046 commit 539da2d
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ abstract class AbstractUpdateVerbHandler extends ChangeVerbHandler {

atData.metaData = AtMetaData.fromCommonsMetadata(updateParams.metadata!);

atData.metaData =
_unsetOrRetainMetadata(atData.metaData!, existingAtMetaData);

notify(
sharedBy,
sharedWith,
Expand All @@ -117,7 +120,7 @@ abstract class AbstractUpdateVerbHandler extends ChangeVerbHandler {
SecondaryUtil.getNotificationPriority(verbParams[PRIORITY]),
atData.metaData!);

return UpdatePreProcessResult(atKey, atData, updateParams);
return UpdatePreProcessResult(atKey, atData);
}

UpdateParams getUpdateParams(HashMap<String, String?> verbParams) {
Expand Down Expand Up @@ -179,8 +182,9 @@ abstract class AbstractUpdateVerbHandler extends ChangeVerbHandler {
return;
}
key = '$forAtSign:$key$atSign';
int ttlInMillis = Duration(minutes: AtSecondaryConfig.notificationExpiryInMins)
.inMilliseconds;
int ttlInMillis =
Duration(minutes: AtSecondaryConfig.notificationExpiryInMins)
.inMilliseconds;

var atNotification = (AtNotificationBuilder()
..fromAtSign = atSign
Expand All @@ -197,12 +201,41 @@ abstract class AbstractUpdateVerbHandler extends ChangeVerbHandler {
unawaited(notificationManager.notify(atNotification));
return atNotification;
}

/// If metadata contains "null" string, then reset the metadata. So set it to null
/// If metadata contains null (null object), then fetch the existing metadata.If
/// existing metadata value is not null, set it the current AtMetaData obj.
AtMetaData _unsetOrRetainMetadata(
AtMetaData newAtMetadata, AtMetaData? existingAtMetadata) {
if (existingAtMetadata == null) {
return newAtMetadata;
}
var atMetaDataJson = newAtMetadata.toJson();
var existingAtMetaDataJson = existingAtMetadata.toJson();
atMetaDataJson.forEach((key, value) {
switch (value) {
// If command does not contains the attributes of a metadata, then regex named
// group, inserts null. For a key, if an attribute has a value in previously,
// fetch the value and update it.
case null:
if (existingAtMetaDataJson[key] != null) {
atMetaDataJson[key] = existingAtMetaDataJson[key];
}
break;
// In the command, if an attribute is explicitly set to null, then verbParams
// contains String value "null". Then reset the metadata. So, set it to null
case 'null':
atMetaDataJson[key] = null;
break;
}
});
return AtMetaData.fromJson(atMetaDataJson);
}
}

class UpdatePreProcessResult {
String atKey;
AtData atData;
UpdateParams updateParams;

UpdatePreProcessResult(this.atKey, this.atData, this.updateParams);
UpdatePreProcessResult(this.atKey, this.atData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import 'package:at_server_spec/at_verb_spec.dart';
class UpdateVerbHandler extends AbstractUpdateVerbHandler {
static Update update = Update();

UpdateVerbHandler(
SecondaryKeyStore keyStore, StatsNotificationService statsNotificationService, notificationManager)
UpdateVerbHandler(SecondaryKeyStore keyStore,
StatsNotificationService statsNotificationService, notificationManager)
: super(keyStore, statsNotificationService, notificationManager);

// Method to verify whether command is accepted or not
Expand All @@ -37,34 +37,32 @@ class UpdateVerbHandler extends AbstractUpdateVerbHandler {
Response response,
HashMap<String, String?> verbParams,
InboundConnection atConnection) async {

var updatePreProcessResult =
await super.preProcessAndNotify(response, verbParams, atConnection);

var updateParams = updatePreProcessResult.updateParams;

logger.finer(
'calling keyStore.put(${updatePreProcessResult.atKey}, ${updatePreProcessResult.atData}');

try {
// update the key in data store
var result = await keyStore.put(
updatePreProcessResult.atKey, updatePreProcessResult.atData,
time_to_live: updateParams.metadata!.ttl,
time_to_born: updateParams.metadata!.ttb,
time_to_refresh: updateParams.metadata!.ttr,
isCascade: updateParams.metadata!.ccd,
isBinary: updateParams.metadata!.isBinary,
isEncrypted: updateParams.metadata!.isEncrypted,
dataSignature: updateParams.metadata!.dataSignature,
sharedKeyEncrypted: updateParams.metadata!.sharedKeyEnc,
publicKeyChecksum: updateParams.metadata!.pubKeyCS,
encoding: updateParams.metadata!.encoding,
encKeyName: updateParams.metadata!.encKeyName,
encAlgo: updateParams.metadata!.encAlgo,
ivNonce: updateParams.metadata!.ivNonce,
skeEncKeyName: updateParams.metadata!.skeEncKeyName,
skeEncAlgo: updateParams.metadata!.skeEncAlgo);
time_to_live: updatePreProcessResult.atData.metaData!.ttl,
time_to_born: updatePreProcessResult.atData.metaData!.ttb,
time_to_refresh: updatePreProcessResult.atData.metaData!.ttr,
isCascade: updatePreProcessResult.atData.metaData!.isCascade,
isBinary: updatePreProcessResult.atData.metaData!.isBinary,
isEncrypted: updatePreProcessResult.atData.metaData!.isEncrypted,
dataSignature: updatePreProcessResult.atData.metaData!.dataSignature,
sharedKeyEncrypted:
updatePreProcessResult.atData.metaData!.sharedKeyEnc,
publicKeyChecksum: updatePreProcessResult.atData.metaData!.pubKeyCS,
encoding: updatePreProcessResult.atData.metaData!.encoding,
encKeyName: updatePreProcessResult.atData.metaData!.encKeyName,
encAlgo: updatePreProcessResult.atData.metaData!.encAlgo,
ivNonce: updatePreProcessResult.atData.metaData!.ivNonce,
skeEncKeyName: updatePreProcessResult.atData.metaData!.skeEncKeyName,
skeEncAlgo: updatePreProcessResult.atData.metaData!.skeEncAlgo);
response.data = result?.toString();
} catch (e, st) {
logger.warning('$e\n$st');
Expand Down
Loading

0 comments on commit 539da2d

Please sign in to comment.