updateChannel.ts 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
  2. import ChannelEntitySchema from 'cd-schemas/schemas/entities/ChannelEntity.schema.json'
  3. import { ChannelEntity } from 'cd-schemas/types/entities/ChannelEntity'
  4. import { InputParser } from 'cd-schemas'
  5. import { IOFlags, getInputJson, saveOutputJson } from '../../helpers/InputOutput'
  6. import { JSONSchema } from '@apidevtools/json-schema-ref-parser'
  7. import { JsonSchemaCustomPrompts, JsonSchemaPrompter } from '../../helpers/JsonSchemaPrompt'
  8. import { Actor, Entity } from '@joystream/types/content-directory'
  9. import { flags } from '@oclif/command'
  10. import { createType } from '@joystream/types'
  11. export default class UpdateChannelCommand extends ContentDirectoryCommandBase {
  12. static description = 'Update one of the owned channels on Joystream (requires a membership).'
  13. static flags = {
  14. ...IOFlags,
  15. asCurator: flags.boolean({
  16. description: 'Provide this flag in order to use Curator context for the update',
  17. required: false,
  18. }),
  19. }
  20. static args = [
  21. {
  22. name: 'id',
  23. description: 'ID of the channel to update',
  24. required: false,
  25. },
  26. ]
  27. async run() {
  28. const {
  29. args: { id },
  30. flags: { asCurator },
  31. } = this.parse(UpdateChannelCommand)
  32. const account = await this.getRequiredSelectedAccount()
  33. let memberId: number | undefined, actor: Actor
  34. if (asCurator) {
  35. actor = await this.getCuratorContext(['Channel'])
  36. } else {
  37. memberId = await this.getRequiredMemberId()
  38. actor = createType('Actor', { Member: memberId })
  39. }
  40. await this.requestAccountDecoding(account)
  41. let channelEntity: Entity, channelId: number
  42. if (id) {
  43. channelId = parseInt(id)
  44. channelEntity = await this.getEntity(channelId, 'Channel', memberId)
  45. } else {
  46. const [id, channel] = await this.promptForEntityEntry('Select a channel to update', 'Channel', 'title', memberId)
  47. channelId = id.toNumber()
  48. channelEntity = channel
  49. }
  50. const currentValues = await this.parseToKnownEntityJson<ChannelEntity>(channelEntity)
  51. this.jsonPrettyPrint(JSON.stringify(currentValues))
  52. const channelJsonSchema = (ChannelEntitySchema as unknown) as JSONSchema
  53. const { input, output } = this.parse(UpdateChannelCommand).flags
  54. let inputJson = await getInputJson<ChannelEntity>(input, channelJsonSchema)
  55. if (!inputJson) {
  56. const customPrompts: JsonSchemaCustomPrompts<ChannelEntity> = [
  57. [
  58. 'language',
  59. () =>
  60. this.promptForEntityId('Choose channel language', 'Language', 'name', undefined, currentValues.language),
  61. ],
  62. ]
  63. if (!asCurator) {
  64. // Skip isCensored is it's not updated by the curator
  65. customPrompts.push(['isCensored', 'skip'])
  66. }
  67. const prompter = new JsonSchemaPrompter<ChannelEntity>(channelJsonSchema, currentValues, customPrompts)
  68. inputJson = await prompter.promptAll(true)
  69. }
  70. this.jsonPrettyPrint(JSON.stringify(inputJson))
  71. const confirmed = await this.simplePrompt({ type: 'confirm', message: 'Do you confirm the provided input?' })
  72. if (confirmed) {
  73. saveOutputJson(output, `${inputJson.title}Channel.json`, inputJson)
  74. const inputParser = InputParser.createWithKnownSchemas(this.getOriginalApi())
  75. const updateOperations = await inputParser.getEntityUpdateOperations(inputJson, 'Channel', channelId)
  76. this.log('Sending the extrinsic...')
  77. await this.sendAndFollowNamedTx(account, 'contentDirectory', 'transaction', [actor, updateOperations])
  78. }
  79. }
  80. }