Forráskód Böngészése

Content CLI: refactor channels related logic

iorveth 4 éve
szülő
commit
f4c9acaf58

+ 1 - 1
cli/examples/content/UpdateChannel.json

@@ -4,7 +4,7 @@
       "Urls": ["https://joystream.org/WorkingGroupOpening.schema.json"]
     }
   ],
-  "new_meta": {
+  "meta": {
     "title": "Channel Title",
     "description": "Cool Description",
     "isPublic": true,

+ 25 - 1
cli/src/Types.ts

@@ -11,7 +11,7 @@ import { Opening, StakingPolicy, ApplicationStageKeys } from '@joystream/types/h
 import { Validator } from 'inquirer'
 import { NewAsset} from '@joystream/types/content'
 import { Bytes } from '@polkadot/types/primitive';
-import { VideoMetadata } from '@joystream/content-metadata-protobuf'
+import { VideoMetadata, ChannelMetadata } from '@joystream/content-metadata-protobuf'
 
 // KeyringPair type extended with mandatory "meta.name"
 // It's used for accounts/keys management within CLI.
@@ -219,4 +219,28 @@ export type VideoCreationParametersInput = {
 export type VideoCreationParameters = {
   assets: Vec<NewAsset>,
   meta: Bytes,
+}
+
+export type ChannelCreationParametersInput = {
+  assets: Vec<NewAsset>,
+  meta: ChannelMetadata.AsObject,
+  reward_account: Option<AccountId>,
+}
+
+export type ChannelCreationParameters = {
+  assets: Vec<NewAsset>,
+  meta: Bytes,
+  reward_account: Option<AccountId>,
+}
+
+export type ChannelUpdateParametersInput = {
+  assets: Option<Vec<NewAsset>>,
+  meta: ChannelMetadata.AsObject,
+  reward_account: Option<AccountId>,
+}
+
+export type ChannelUpdateParameters = {
+  assets: Option<Vec<NewAsset>>,
+  new_meta: Bytes,
+  reward_account: Option<AccountId>,
 }

+ 4 - 30
cli/src/commands/content/createChannel.ts

@@ -1,22 +1,7 @@
 import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
 import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
-import { NewAsset} from '@joystream/types/content'
-import {ChannelMetadata} from '@joystream/content-metadata-protobuf'
-import { Vec, Option} from '@polkadot/types';
-import AccountId from '@polkadot/types/generic/AccountId';
-import { Bytes } from '@polkadot/types/primitive';
-
-type ChannelCreationParametersInput = {
-  assets: Vec<NewAsset>,
-  meta: ChannelMetadata.AsObject,
-  reward_account: Option<AccountId>,
-}
-
-type ChannelCreationParameters = {
-  assets: Vec<NewAsset>,
-  meta: Bytes,
-  reward_account: Option<AccountId>,
-}
+import { ChannelCreationParameters, ChannelCreationParametersInput } from '../../Types'
+import { channelMetadataFromInput } from '../../helpers/serialization'
 
 export default class CreateChannelCommand extends ContentDirectoryCommandBase {
   static description = 'Create channel inside content directory.'
@@ -39,21 +24,10 @@ export default class CreateChannelCommand extends ContentDirectoryCommandBase {
 
     if (input) {
       let channelCreationParametersInput = await getInputJson<ChannelCreationParametersInput>(input)
-      let channelMetadata = new ChannelMetadata()
-      channelMetadata.setTitle(channelCreationParametersInput.meta.title!)
-      channelMetadata.setDescription(channelCreationParametersInput.meta.description!)
-      channelMetadata.setIsPublic(channelCreationParametersInput.meta.isPublic!)
-      channelMetadata.setLanguage(channelCreationParametersInput.meta.language!)
-      channelMetadata.setCoverPhoto(channelCreationParametersInput.meta.coverPhoto!)
-      channelMetadata.setAvatarPhoto(channelCreationParametersInput.meta.avatarPhoto!)
-      channelMetadata.setCategory(channelCreationParametersInput.meta.category!)
-
-      const serialized = channelMetadata.serializeBinary();
-
+  
       const api = await this.getOriginalApi()
       
-      const metaRaw = api.createType('Raw', serialized)
-      const meta = new Bytes(api.registry, metaRaw)
+      const meta = channelMetadataFromInput(api, channelCreationParametersInput)
 
       let channelCreationParameters: ChannelCreationParameters = {
         assets: channelCreationParametersInput.assets,

+ 5 - 31
cli/src/commands/content/updateChannel.ts

@@ -1,22 +1,7 @@
 import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
 import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
-import { NewAsset} from '@joystream/types/content'
-import {ChannelMetadata} from '@joystream/content-metadata-protobuf'
-import { Vec, Option} from '@polkadot/types';
-import AccountId from '@polkadot/types/generic/AccountId';
-import { Bytes } from '@polkadot/types/primitive';
-
-type ChannelUpdateParametersInput = {
-  assets: Option<Vec<NewAsset>>,
-  new_meta: ChannelMetadata.AsObject,
-  reward_account: Option<AccountId>,
-}
-
-type ChannelUpdateParameters = {
-  assets: Option<Vec<NewAsset>>,
-  new_meta: Bytes,
-  reward_account: Option<AccountId>,
-}
+import { channelMetadataFromInput } from '../../helpers/serialization'
+import { ChannelUpdateParameters, ChannelUpdateParametersInput } from '../../Types'
 
 export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
   static description = 'Update existing content directory channel.'
@@ -50,21 +35,10 @@ export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
 
     if (input) {
       let channelUpdateParametersInput = await getInputJson<ChannelUpdateParametersInput>(input)
-      let channelMetadata = new ChannelMetadata()
-      channelMetadata.setTitle(channelUpdateParametersInput.new_meta.title!)
-      channelMetadata.setDescription(channelUpdateParametersInput.new_meta.description!)
-      channelMetadata.setIsPublic(channelUpdateParametersInput.new_meta.isPublic!)
-      channelMetadata.setLanguage(channelUpdateParametersInput.new_meta.language!)
-      channelMetadata.setCoverPhoto(channelUpdateParametersInput.new_meta.coverPhoto!)
-      channelMetadata.setAvatarPhoto(channelUpdateParametersInput.new_meta.avatarPhoto!)
-      channelMetadata.setCategory(channelUpdateParametersInput.new_meta.category!)
-
-      const serialized = channelMetadata.serializeBinary();
-
+   
       const api = await this.getOriginalApi()
 
-      const metaRaw = api.createType('Raw', serialized)
-      const meta = new Bytes(api.registry, metaRaw)
+      const meta = channelMetadataFromInput(api, channelUpdateParametersInput)
 
       let channelUpdateParameters: ChannelUpdateParameters = {
         assets: channelUpdateParametersInput.assets,
@@ -76,7 +50,7 @@ export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
       const confirmed = await this.simplePrompt({ type: 'confirm', message: 'Do you confirm the provided input?' })
 
       if (confirmed && channelUpdateParameters)  {
-        saveOutputJson(output, `${channelUpdateParametersInput.new_meta.title}Channel.json`, channelUpdateParametersInput)
+        saveOutputJson(output, `${channelUpdateParametersInput.meta.title}Channel.json`, channelUpdateParametersInput)
         this.log('Sending the extrinsic...')
 
         await this.sendAndFollowNamedTx(currentAccount, 'content', 'updateChannel', [actor, channelId, channelUpdateParameters])

+ 17 - 3
cli/src/helpers/serialization.ts

@@ -1,5 +1,5 @@
-import {VideoMetadata, PublishedBeforeJoystream, License, MediaType} from '@joystream/content-metadata-protobuf'
-import { VideoUpdateParametersInput, VideoCreationParametersInput } from '../Types'
+import {VideoMetadata, PublishedBeforeJoystream, License, MediaType, ChannelMetadata} from '@joystream/content-metadata-protobuf'
+import { VideoUpdateParametersInput, VideoCreationParametersInput, ChannelUpdateParametersInput, ChannelCreationParametersInput } from '../Types'
 import { ApiPromise } from '@polkadot/api'
 import { Bytes } from '@polkadot/types/primitive';
 
@@ -42,6 +42,20 @@ export function videoMetadataFromInput(api: ApiPromise, videoParametersInput: Vi
     videoMetadata.setLicense(license)
     videoMetadata.setPublishedBeforeJoystream(publishedBeforeJoystream)
 
-    const serialized = videoMetadata.serializeBinary();
+    const serialized = videoMetadata.serializeBinary()
+    return BinaryToMeta(api, serialized)
+}
+
+export function channelMetadataFromInput(api: ApiPromise, channelParametersInput: ChannelCreationParametersInput | ChannelUpdateParametersInput): Bytes {
+    let channelMetadata = new ChannelMetadata()
+    channelMetadata.setTitle(channelParametersInput.meta.title!)
+    channelMetadata.setDescription(channelParametersInput.meta.description!)
+    channelMetadata.setIsPublic(channelParametersInput.meta.isPublic!)
+    channelMetadata.setLanguage(channelParametersInput.meta.language!)
+    channelMetadata.setCoverPhoto(channelParametersInput.meta.coverPhoto!)
+    channelMetadata.setAvatarPhoto(channelParametersInput.meta.avatarPhoto!)
+    channelMetadata.setCategory(channelParametersInput.meta.category!)
+
+    const serialized = channelMetadata.serializeBinary()
     return BinaryToMeta(api, serialized)
 }