Browse Source

query node mappings - rough implementation I

ondratra 4 years ago
parent
commit
3f73052422
1 changed files with 269 additions and 68 deletions
  1. 269 68
      query-node/mappings/mappingsContent.ts

+ 269 - 68
query-node/mappings/mappingsContent.ts

@@ -1,52 +1,71 @@
+// TODO: add logging of mapping events (entity found/not found, entity updated/deleted, etc.)
+// TODO: update event list - some events were added/removed recently and are missing in this file
+// TODO: handling of Language, MediaType, etc.
+
 import { SubstrateEvent } from '@dzlzv/hydra-common'
 import { DatabaseManager } from '@dzlzv/hydra-db-utils'
 
+// protobuf definitions
+import {
+  ChannelMetadata,
+  ChannelCategoryMetadata
+} from '../../content-metadata-protobuf/compiled/proto/Channel_pb'
+import {
+  PublishedBeforeJoystream as PublishedBeforeJoystreamMetadata,
+  License as LicenseMetadata,
+  MediaType as MediaTypeMetadata,
+  VideoMetadata,
+  VideoCategoryMetadata,
+} from '../../content-metadata-protobuf/compiled/proto/Video_pb'
+
 // enums
 import { Network } from '../generated/graphql-server/src/modules/enums/enums'
 
 // input schema models
 import { Block } from '../generated/graphql-server/src/modules/block/block.model'
 import { Channel } from '../generated/graphql-server/src/modules/channel/channel.model'
+import { ChannelCategory } from '../generated/graphql-server/src/modules/channelCategory/channelCategory.model'
+import { Video } from '../generated/graphql-server/src/modules/video/video.model'
+import { VideoCategory } from '../generated/graphql-server/src/modules/videoCategory/videoCategory.model'
 
-/////////////////// Content directory mappings /////////////////////////////////
-
-/* Template from hydra-cli scaffold (generated with the newer version 2.0.1-beta.0)
-
-export async function balancesTransfer(
-  db: DatabaseManager,
-  event_: SubstrateEvent
-) {
-  const transfer = new Transfer()
-  transfer.from = Buffer.from(event.data.accountIds[0].toHex())
-  transfer.to = Buffer.from(event.data.accountIds[1].toHex())
-  transfer.value = event.data.balance.toBn()
-  transfer.block = event.ctx.blockNumber
-  transfer.comment = `Transferred ${transfer.value} from ${transfer.from} to ${transfer.to}`
-  transfer.insertedAt = new Date()
-  await db.save<Transfer>(transfer)
-}
-*/
 
 const currentNetwork = Network.BABYLON
 
 enum ProtobufEntity {
   Channel,
-
+  ChannelCategory,
+  Video,
+  VideoCategory,
 }
 
-function readProtobuf(type: ProtobufEntity, metadata: string) {
-  // TODO: implement protobuf read operations
-  //       - see npm package `google-protobuf`
-  //       - read format from folder `content-metadata-protobuf/compiled/`
-  //       - parse `metadata`
+function readProtobuf(type: ProtobufEntity, metadata: Uint8Array) {
+  // TODO: consider getting rid of this function - it makes sense to keep it only complex logic will be executed here
+  //       for example retriving language for channel, retrieving new assets (channel photo), etc.
 
   if (type == ProtobufEntity.Channel) {
+    return ChannelMetadata.deserializeBinary(metadata)
+    /*
     return {
+      title: 'TODO handle', // TODO: read from protobuf
+      description: 'TODO description', // TODO: read from protobuf
       coverPhoto: undefined, // TODO: read from protobuf
       avatarPhoto: undefined, // TODO: read from protobuf
       isPublic: true, // TODO: read from protobuf
       language: undefined, // TODO: read language from protobuf and connect it with existing Language (if any)
     }
+    */
+  }
+
+  if (type == ProtobufEntity.ChannelCategory) {
+    return ChannelCategoryMetadata.deserializeBinary(metadata)
+  }
+
+  if (type == ProtobufEntity.Video) {
+    return VideoMetadata.deserializeBinary(metadata)
+  }
+
+  if (type == ProtobufEntity.VideoCategory) {
+    return VideoCategoryMetadata.deserializeBinary(metadata)
   }
 
   throw `Not implemented type: ${type}`
@@ -61,6 +80,8 @@ function convertblockNumberToBlock(block: number): Block {
   })
 }
 
+/////////////////// Channel ////////////////////////////////////////////////////
+
 // eslint-disable-next-line @typescript-eslint/naming-convention
 export async function content_ChannelCreated(db: DatabaseManager, event: SubstrateEvent): Promise<void> {
   /* event arguments
@@ -72,15 +93,12 @@ export async function content_ChannelCreated(db: DatabaseManager, event: Substra
 
   const protobufContent = readProtobuf(ProtobufEntity.Channel, (event.params[3] as any).meta) // TODO: get rid of `any` typecast
 
-  // TODO
   const channel = new Channel({
     id: event.params[0].toString(), // ChannelId
-    title: 'TODO handle', // TODO
-    description: 'TODO description', // TODO
-    isCensored: false, // TODO: where this value comes from?
+    isCensored: false,
     videos: [],
     happenedIn: convertblockNumberToBlock(event.blockNumber),
-    ...protobufContent
+    ...protobufContent.toObject()
   })
 
   await db.save<Channel>(channel)
@@ -91,7 +109,25 @@ export async function content_ChannelUpdated(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  ChannelId,
+  Channel,
+  ChannelUpdateParameters<ContentParameters, AccountId>,
+  */
+
+  const channelId = event.params[1].toString()
+  const channel = await db.get(Channel, { where: { id: channelId } })
+
+  const protobufContent = readProtobuf(ProtobufEntity.Channel, (event.params[3] as any).meta) // TODO: get rid of `any` typecast
+
+  const updatedChannel = new Channel({
+    // TODO: check that this kind of new updated channel construction is valid or it should rather be edited as `channel.myProperty = something`
+    ...channel,
+    ...protobufContent.toObject()
+  })
+
+  await db.save<Channel>(updatedChannel)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -99,7 +135,10 @@ export async function content_ChannelDeleted(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  const channelId = event.params[1].toString()
+  const channel = await db.get(Channel, { where: { id: channelId } })
+
+  await db.remove<Channel>(channel)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -107,7 +146,18 @@ export async function content_ChannelCensored(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  ChannelId,
+  Vec<u8>
+  */
+
+  const channelId = event.params[1].toString()
+  const channel = await db.get(Channel, { where: { id: channelId } })
+
+  channel.isCensored = true;
+
+  await db.save<Channel>(channel)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -115,7 +165,18 @@ export async function content_ChannelUncensored(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  ChannelId,
+  Vec<u8>
+  */
+
+  const channelId = event.params[1].toString()
+  const channel = await db.get(Channel, { where: { id: channelId } })
+
+  channel.isCensored = false;
+
+  await db.save<Channel>(channel)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -123,7 +184,7 @@ export async function content_ChannelOwnershipTransferRequested(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  // TODO - is mapping for this event needed?
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -131,7 +192,7 @@ export async function content_ChannelOwnershipTransferRequestWithdrawn(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  // TODO - is mapping for this event needed?
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -142,12 +203,29 @@ export async function content_ChannelOwnershipTransferred(
   // TODO
 }
 
+/////////////////// ChannelCategory ////////////////////////////////////////////
+
 // eslint-disable-next-line @typescript-eslint/naming-convention
 export async function content_ChannelCategoryCreated(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ChannelCategoryId,
+  ChannelCategory,
+  ChannelCategoryCreationParameters,
+  */
+
+  const protobufContent = readProtobuf(ProtobufEntity.ChannelCategory, (event.params[2] as any).meta) // TODO: get rid of `any` typecast
+
+  const channelCategory = new ChannelCategory({
+    id: event.params[0].toString(), // ChannelCategoryId
+    channels: [],
+    happenedIn: convertblockNumberToBlock(event.blockNumber),
+    ...protobufContent.toObject()
+  })
+
+  await db.save<ChannelCategory>(channelCategory)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -155,7 +233,24 @@ export async function content_ChannelCategoryUpdated(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  ChannelCategoryId,
+  ChannelCategoryUpdateParameters,
+  */
+
+  const channelCategoryId = event.params[1].toString()
+  const channelCategory = await db.get(ChannelCategory, { where: { id: channelCategoryId } })
+
+  const protobufContent = readProtobuf(ProtobufEntity.ChannelCategory, (event.params[2] as any).meta) // TODO: get rid of `any` typecast
+
+  const updatedChannelCategory = new ChannelCategory({
+    // TODO: check that this kind of new updated channel construction is valid or it should rather be edited as `channel.myProperty = something`
+    ...channelCategory,
+    ...protobufContent.toObject()
+  })
+
+  await db.save<ChannelCategory>(channelCategory)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -163,15 +258,40 @@ export async function content_ChannelCategoryDeleted(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  ChannelCategoryId
+  */
+  const channelCategoryId = event.params[1].toString()
+  const channelCategory = await db.get(ChannelCategory, { where: { id: channelCategoryId } })
+
+  await db.remove<ChannelCategory>(channelCategory)
 }
 
+/////////////////// VideoCategory //////////////////////////////////////////////
+
 // eslint-disable-next-line @typescript-eslint/naming-convention
 export async function content_VideoCategoryCreated(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  VideoCategoryId,
+  VideoCategoryCreationParameters,
+  */
+
+  const protobufContent = readProtobuf(ProtobufEntity.VideoCategory, (event.params[2] as any).meta) // TODO: get rid of `any` typecast
+
+  const videoCategory = new VideoCategory({
+    id: event.params[0].toString(), // ChannelId
+    isCensored: false, // TODO: where this value comes from?
+    videos: [],
+    happenedIn: convertblockNumberToBlock(event.blockNumber),
+    ...protobufContent.toObject()
+  })
+
+  await db.save<VideoCategory>(videoCategory)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -179,7 +299,24 @@ export async function content_VideoCategoryUpdated(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  VideoCategoryId,
+  VideoCategoryUpdateParameters,
+  */
+
+  const videoCategoryId = event.params[1].toString()
+  const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId } })
+
+  const protobufContent = readProtobuf(ProtobufEntity.VideoCategory, (event.params[2] as any).meta) // TODO: get rid of `any` typecast
+
+  const updatedVideoCategory = new VideoCategory({
+    // TODO: check that this kind of new updated channel construction is valid or it should rather be edited as `channel.myProperty = something`
+    ...videoCategory,
+    ...protobufContent.toObject()
+  })
+
+  await db.save<VideoCategory>(videoCategory)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -187,15 +324,42 @@ export async function content_VideoCategoryDeleted(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  VideoCategoryId,
+  */
+
+  const videoCategoryId = event.params[1].toString()
+  const videoCategory = await db.get(VideoCategory, { where: { id: videoCategoryId } })
+
+  await db.remove<VideoCategory>(videoCategory)
 }
 
+/////////////////// Video //////////////////////////////////////////////////////
+
 // eslint-disable-next-line @typescript-eslint/naming-convention
 export async function content_VideoCreated(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  ChannelId,
+  VideoId,
+  VideoCreationParameters<ContentParameters>,
+  */
+
+  const protobufContent = readProtobuf(ProtobufEntity.Video, (event.params[3] as any).meta) // TODO: get rid of `any` typecast
+
+  const channel = new Video({
+    id: event.params[1].toString(), // ChannelId
+    isCensored: false,
+    channel: event.params[1],
+    happenedIn: convertblockNumberToBlock(event.blockNumber),
+    ...protobufContent.toObject()
+  })
+
+  await db.save<Video>(channel)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -203,7 +367,23 @@ export async function content_VideoUpdated(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  VideoId,
+  VideoUpdateParameters<ContentParameters>,
+  */
+  const videoId = event.params[1].toString()
+  const video = await db.get(Video, { where: { id: videoId } })
+
+  const protobufContent = readProtobuf(ProtobufEntity.Video, (event.params[2] as any).meta) // TODO: get rid of `any` typecast
+
+  const updatedVideo = new Video({
+    // TODO: check that this kind of new updated channel construction is valid or it should rather be edited as `channel.myProperty = something`
+    ...video,
+    ...protobufContent.toObject()
+  })
+
+  await db.save<Video>(video)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -211,7 +391,15 @@ export async function content_VideoDeleted(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  VideoCategoryId,
+  */
+
+  const videoId = event.params[1].toString()
+  const video = await db.get(Video, { where: { id: videoId } })
+
+  await db.remove<Video>(video)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -219,7 +407,18 @@ export async function content_VideoCensored(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  VideoId,
+  Vec<u8>
+  */
+
+  const videoId = event.params[1].toString()
+  const video = await db.get(Video, { where: { id: videoId } })
+
+  video.isCensored = true;
+
+  await db.save<Video>(video)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -227,7 +426,18 @@ export async function content_VideoUncensored(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
+  /* event arguments
+  ContentActor,
+  VideoId,
+  Vec<u8>
+  */
+
+  const channelId = event.params[1].toString()
+  const video = await db.get(Video, { where: { id: videoId } })
+
+  video.isCensored = false;
+
+  await db.save<Video>(video)
 }
 
 // eslint-disable-next-line @typescript-eslint/naming-convention
@@ -235,29 +445,20 @@ export async function content_FeaturedVideosSet(
   db: DatabaseManager,
   event: SubstrateEvent
 ) {
-  // TODO
-}
+  /* event arguments
+  ContentActor,
+  Vec<VideoId>,
+  */
 
-// eslint-disable-next-line @typescript-eslint/naming-convention
-export async function content_PlaylistCreated(
-  db: DatabaseManager,
-  event: SubstrateEvent
-) {
-  // TODO
-}
+  const videoIds = event.params[1]
 
-// eslint-disable-next-line @typescript-eslint/naming-convention
-export async function content_PlaylistUpdated(
-  db: DatabaseManager,
-  event: SubstrateEvent
-) {
-  // TODO
-}
+  for (let videoId in videoIds) {
+    const video = await db.get(Video, { where: { id: videoId } })
 
-// eslint-disable-next-line @typescript-eslint/naming-convention
-export async function content_PlaylistDeleted(
-  db: DatabaseManager,
-  event: SubstrateEvent
-) {
-  // TODO
+    video.isFeatured = true;
+
+    await db.save<Video>(video)
+  }
+
+  // TODO: remove featured flag from previously featured videos
 }