Browse Source

CLI - handle uniqueness check

Leszek Wiesner 4 years ago
parent
commit
ad85695cf4
1 changed files with 21 additions and 0 deletions
  1. 21 0
      cli/src/commands/media/createChannel.ts

+ 21 - 0
cli/src/commands/media/createChannel.ts

@@ -5,6 +5,7 @@ import { InputParser } from '@joystream/cd-schemas'
 import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
 import { JSONSchema } from '@apidevtools/json-schema-ref-parser'
 import { JsonSchemaCustomPrompts, JsonSchemaPrompter } from '../../helpers/JsonSchemaPrompt'
+import { cli } from 'cli-ux'
 
 import { flags } from '@oclif/command'
 import _ from 'lodash'
@@ -16,6 +17,19 @@ export default class CreateChannelCommand extends ContentDirectoryCommandBase {
     confirm: flags.boolean({ char: 'y', name: 'confirm', required: false, description: 'Confirm the provided input' }),
   }
 
+  async getExistingChannelHandles(): Promise<string[]> {
+    cli.action.start('Fetching chain data...')
+    const result = await Promise.all(
+      (await this.entitiesByClassAndOwner('Channel')).map(async ([, channel]) => {
+        const { handle } = await this.parseToEntityJson<ChannelEntity>(channel)
+        return handle
+      })
+    )
+    cli.action.stop()
+
+    return result
+  }
+
   async run() {
     const account = await this.getRequiredSelectedAccount()
     const memberId = await this.getRequiredMemberId()
@@ -27,9 +41,16 @@ export default class CreateChannelCommand extends ContentDirectoryCommandBase {
 
     const { input, output, confirm } = this.parse(CreateChannelCommand).flags
 
+    // Can potentially slow things down quite a bit
+    const existingHandles = await this.getExistingChannelHandles()
+
     let inputJson = await getInputJson<ChannelEntity>(input, channelJsonSchema)
     if (!inputJson) {
       const customPrompts: JsonSchemaCustomPrompts = [
+        [
+          'handle',
+          { validate: (h) => (existingHandles.includes(h) ? 'Channel with such handle already exists' : true) },
+        ],
         ['language', () => this.promptForEntityId('Choose channel language', 'Language', 'name')],
         ['isCensored', 'skip'],
       ]