Browse Source

query node - license deletion

ondratra 3 years ago
parent
commit
ada621b714
2 changed files with 53 additions and 10 deletions
  1. 18 0
      query-node/mappings/src/content/utils.ts
  2. 35 10
      query-node/mappings/src/content/video.ts

+ 18 - 0
query-node/mappings/src/content/utils.ts

@@ -639,6 +639,11 @@ async function prepareLicense(licenseProtobuf: LicenseMetadata.AsObject | undefi
     return undefined
   }
 
+  // license is meant to be deleted
+  if (isLicenseEmpty(licenseProtobuf)) {
+    return new License({})
+  }
+
   // crete new license
   const license = new License({
     ...licenseProtobuf,
@@ -650,6 +655,19 @@ async function prepareLicense(licenseProtobuf: LicenseMetadata.AsObject | undefi
   return license
 }
 
+/*
+  Checks if protobof contains license with some fields filled or is empty object (`{}` or `{someKey: undefined, ...}`).
+  Empty object means deletion is requested.
+*/
+function isLicenseEmpty(licenseObject) {
+    let somePropertySet = Object.entries(licenseObject).reduce((acc, [key, value]) => {
+        return acc || value !== undefined
+    }, false)
+
+    return somePropertySet
+}
+
+
 function prepareVideoMetadata(videoProtobuf: VideoMetadata.AsObject, videoSize: number | undefined, blockNumber: number): RawVideoMetadata {
   const rawMeta = {
     encoding: {

+ 35 - 10
query-node/mappings/src/content/video.ts

@@ -188,6 +188,11 @@ export async function content_VideoCreated(
   // prepare video media metadata (if any)
   const fixedProtobuf = integrateVideoMediaMetadata(null, protobufContent, event.blockNumber)
 
+  const licenseIsEmpty = fixedProtobuf.license && !Object.keys(fixedProtobuf.license).length
+  if (licenseIsEmpty) { // license deletion was requested - ignore it and consider it empty
+    delete fixedProtobuf.license
+  }
+
   // create new video
   const video = new Video({
     // main data
@@ -271,16 +276,9 @@ export async function content_VideoUpdated(
 
     // license has changed - plan old license delete
     if (originalLicense && video.license != originalLicense) {
-      video.license = video.license
-        ? new License({
-          ...originalLicense,
-          ...video.license,
-        }) // update existing license
-        : undefined // unset license
-
-      if (!video.license) { // delete old license when requested
-        licenseToDelete = originalLicense
-      }
+      ([video.license, licenseToDelete] = handleLicenseUpdate(originalLicense, video.license))
+    } else if (!Object.keys(video.license || {}).length) { // license deletion was requested event no license exists?
+      delete video.license // ensure license is empty
     }
   }
 
@@ -472,3 +470,30 @@ function integrateVideoMediaMetadata(
     mediaMetadata
   }
 }
+
+// returns tuple `[newLicenseForVideo, oldLicenseToBeDeleted]`
+function handleLicenseUpdate(originalLicense, newLicense): [License | undefined, License | null] {
+  const isNewEmpty = !Object.keys(newLicense).length
+
+  if (!originalLicense && isNewEmpty) {
+    return [undefined, null]
+  }
+
+  if (!originalLicense) { // && !isNewEmpty
+    return [newLicense, null]
+  }
+
+  if (!isNewEmpty) { // && originalLicense
+    return [
+      new License({
+        ...originalLicense,
+        ...newLicense,
+      }),
+      null
+    ]
+  }
+
+  // originalLicense && isNewEmpty
+
+  return [originalLicense, null]
+}