Selaa lähdekoodia

query node - mappings implementation V

ondratra 4 vuotta sitten
vanhempi
commit
1f40bdf58b

+ 1 - 1
query-node/mappings/package.json

@@ -2,7 +2,7 @@
   "name": "query-node-mappings",
   "version": "0.0.1",
   "description": "Mappings for hydra-processor",
-  "main": "lib/mappings/index.js",
+  "main": "lib/mappings/src/index.js",
   "license": "MIT",
   "scripts": {
     "build": "rm -rf lib && tsc --build tsconfig.json",

+ 3 - 1
query-node/mappings/src/common.ts

@@ -1,6 +1,7 @@
 import BN from 'bn.js'
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
+import { u64 } from '@polkadot/types/primitive';
 
 import { Block } from 'query-node/src/modules/block/block.model'
 import { Network } from 'query-node/src/modules/enums/enums'
@@ -49,7 +50,8 @@ export async function prepareAssetDataObject(contentParameters: ContentParameter
     owner: assetOwner,
     addedAt: block,
     typeId: contentParameters.type_id.toNumber(),
-    size: new BN(contentParameters.size),
+    // `size` is masked by `size` special name in struct so there needs to be `.get('size') as u64`
+    size: (contentParameters.get('size') as unknown as u64).toBn(),
     liaisonId: new BN(0), // TODO: proper id
     liaisonJudgement: LiaisonJudgement.PENDING, // TODO: proper judgement
     ipfsContentId: contentParameters.ipfs_content_id.toHex(),

+ 7 - 4
query-node/mappings/src/content/channel.ts

@@ -2,11 +2,14 @@ import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
 import ISO6391 from 'iso-639-1';
 
-import { GenericAccountId } from '@polkadot/types/generic';
+import { AccountId } from "@polkadot/types/interfaces";
 import { Option } from '@polkadot/types/codec';
 import { Content } from '../../../generated/types'
 import { readProtobuf } from './utils'
 
+
+// Asset
+import { AssetStorage } from 'query-node/src/modules/variants/variants.model'
 import {
   inconsistentState,
   prepareBlock,
@@ -93,11 +96,11 @@ export async function content_ChannelAssetsRemoved(
   const {contentId: contentIds} = new Content.ChannelAssetsRemovedEvent(event).data
 
   // load channel
-  const assets = await db.getMany(Asset, { where: { id: contentIds } })
+  const assets = await db.getMany(AssetStorage, { where: { id: contentIds } })
 
   // delete assets
   for (const asset in assets) {
-    await db.remove<Asset>(asset)
+    await db.remove<AssetStorage>(asset)
   }
 }
 
@@ -236,7 +239,7 @@ export async function content_ChannelCategoryDeleted(
 
 function handleChannelRewardAccountChange(
   channel: Channel, // will be modified inside of the function!
-  reward_account: Option<GenericAccountId>
+  reward_account: Option<AccountId>
 ) {
   // new different reward account set?
   if (reward_account.isSome) {

+ 36 - 4
query-node/mappings/src/content/utils.ts

@@ -4,6 +4,8 @@
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
 import ISO6391 from 'iso-639-1';
+import BN from 'bn.js'
+import { u64 } from '@polkadot/types/primitive';
 
 // protobuf definitions
 import {
@@ -109,7 +111,10 @@ export async function readProtobuf(
 
     // prepare media meta information if needed
     if (metaAsObject.mediaType) {
-      result.mediaMetadata = await prepareVideoMetadata(metaAsObject)
+      // prepare video file size if poosible
+      const videoSize = await extractVideoSize(assets, metaAsObject.video)
+
+      result.mediaMetadata = await prepareVideoMetadata(metaAsObject, videoSize)
       delete metaAsObject.mediaType
     }
 
@@ -154,7 +159,7 @@ export async function readProtobuf(
 function handlePublishedBeforeJoystream(video: Video, publishedAtString?: string) {
   // published elsewhere before Joystream
   if (publishedAtString) {
-    video.publishedBeforeJoystream = new Date(publishedAt)
+    video.publishedBeforeJoystream = new Date(publishedAtString)
   }
 
   // unset publish info
@@ -196,6 +201,30 @@ async function extractAsset(
   return convertAsset(assets[assetIndex], db, event)
 }
 
+async function extractVideoSize(assets: NewAsset[], assetIndex: number | undefined): Promise<BN | undefined> {
+  if (assetIndex === undefined) {
+    return undefined
+  }
+
+  if (assetIndex > assets.length) {
+    return inconsistentState()
+  }
+
+  const rawAsset = assets[assetIndex]
+
+  if (rawAsset.isUrls) {
+    return undefined
+  }
+
+  // !rawAsset.isUrls && rawAsset.isUpload
+
+  const contentParameters: ContentParameters = rawAsset.asUpload
+  // `size` is masked by `size` special name in struct so there needs to be `.get('size') as u64`
+  const videoSize = (contentParameters.get('size') as unknown as u64).toBn()
+
+  return videoSize
+}
+
 async function prepareLanguage(languageIso: string, db: DatabaseManager): Promise<Language> {
   const isValidIso = ISO6391.validate(languageIso);
 
@@ -222,16 +251,19 @@ async function prepareLicense(licenseProtobuf: LicenseMetadata.AsObject): Promis
   return license
 }
 
-async function prepareVideoMetadata(videoProtobuf: VideoMetadata.AsObject): Promise<VideoMediaMetadata> {
+async function prepareVideoMetadata(videoProtobuf: VideoMetadata.AsObject, videoSize: BN | undefined): Promise<VideoMediaMetadata> {
   const encoding = new VideoMediaEncoding(videoProtobuf.mediaType)
 
   const videoMeta = new VideoMediaMetadata({
     encoding,
     pixelWidth: videoProtobuf.mediaPixelWidth,
     pixelHeight: videoProtobuf.mediaPixelHeight,
-    size: 0, // TODO: retrieve proper file size
   })
 
+  if (videoSize !== undefined) {
+    videoMeta.size = videoSize
+  }
+
   return videoMeta
 }
 

+ 6 - 1
query-node/mappings/yarn.lock

@@ -83,7 +83,7 @@
   dependencies:
     bn.js "^5.1.3"
 
-"@dzlzv/hydra-db-utils@2.0.1-beta.15":
+"@dzlzv/hydra-db-utils@^2.0.1-beta.15":
   version "2.0.1-beta.15"
   resolved "https://registry.yarnpkg.com/@dzlzv/hydra-db-utils/-/hydra-db-utils-2.0.1-beta.15.tgz#eaf01ded617502cd3ac1968825862ecf962f0b4d"
   integrity sha512-vZvXZU2p0rz02r1bavO4a+H+CbTM+23ikX4QWvClU5/HPRbjlkRln+GrwYcRNk+Aj1pl7yacJaWzdyLF4j0B+w==
@@ -2454,6 +2454,11 @@ isexe@^2.0.0:
   resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
   integrity sha1-6PvzdNxVb/iUehDcsFctYz8s+hA=
 
+iso-639-1@^2.1.8:
+  version "2.1.8"
+  resolved "https://registry.yarnpkg.com/iso-639-1/-/iso-639-1-2.1.8.tgz#98fae683960db44cd14cf4ed2a37427e997d8c89"
+  integrity sha512-Ol0R5oepQmWTMrlzSx8giSQ4hQsh2SZI/fsAy+nYc5O+RsYeixy2tsGUYJNadqqoOcbanSbFTqK17PRKpuNCAw==
+
 isobject@^4.0.0:
   version "4.0.0"
   resolved "https://registry.yarnpkg.com/isobject/-/isobject-4.0.0.tgz#3f1c9155e73b192022a80819bacd0343711697b0"

+ 1 - 1
query-node/schema.graphql

@@ -345,7 +345,7 @@ type VideoMediaMetadata @entity {
   pixelHeight: Int
 
   "Video media size in bytes"
-  size: Int
+  size: BigInt
 
   video: Video @derivedFrom(field: "mediaMetadata")