Ver código fonte

Add collaborators validation

Leszek Wiesner 3 anos atrás
pai
commit
dde3c72e75

+ 1 - 1
cli/src/Api.ts

@@ -558,7 +558,7 @@ export default class Api {
     ])
   }
 
-  async getMembers(ids: MemberId[]): Promise<Membership[]> {
+  async getMembers(ids: MemberId[] | number[]): Promise<Membership[]> {
     return this._api.query.members.membershipById.multi(ids)
   }
 }

+ 10 - 0
cli/src/base/ContentDirectoryCommandBase.ts

@@ -6,6 +6,7 @@ import { CLIError } from '@oclif/errors'
 import { RolesCommandBase } from './WorkingGroupsCommandBase'
 import { createType, createTypeFromConstructor } from '@joystream/types'
 import { flags } from '@oclif/command'
+import { MemberId } from '@joystream/types/members'
 
 const CHANNEL_CREATION_CONTEXTS = ['Member', 'Curator'] as const
 const CATEGORIES_CONTEXTS = ['Lead', 'Curator'] as const
@@ -259,4 +260,13 @@ export default abstract class ContentDirectoryCommandBase extends RolesCommandBa
 
     throw new Error(`Unrecognized context: ${context}`)
   }
+
+  async validateCollaborators(collaborators: number[] | MemberId[]): Promise<void> {
+    const collaboratorMembers = await this.getApi().getMembers(collaborators)
+    if (collaboratorMembers.length < collaborators.length || collaboratorMembers.some((m) => m.isEmpty)) {
+      this.error(`Invalid collaborator set! All collaborators must be existing members.`, {
+        exit: ExitCodes.InvalidInput,
+      })
+    }
+  }
 }

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

@@ -36,6 +36,10 @@ export default class CreateChannelCommand extends UploadCommandBase {
     const channelInput = await getInputJson<ChannelInputParameters>(input, ChannelInputSchema)
     const meta = asValidatedMetadata(ChannelMetadata, channelInput)
 
+    if (channelInput.collaborators) {
+      await this.validateCollaborators(channelInput.collaborators)
+    }
+
     const { coverPhotoPath, avatarPhotoPath } = channelInput
     const assetsPaths = [coverPhotoPath, avatarPhotoPath].filter((v) => v !== undefined) as string[]
     const resolvedAssets = await this.resolveAndValidateAssets(assetsPaths, input)

+ 4 - 0
cli/src/commands/content/updateChannel.ts

@@ -97,6 +97,10 @@ export default class UpdateChannelCommand extends UploadCommandBase {
       this.error("Collaborators are not allowed to update channel's collaborators!", { exit: ExitCodes.AccessDenied })
     }
 
+    if (channelInput.collaborators) {
+      await this.validateCollaborators(channelInput.collaborators)
+    }
+
     const { coverPhotoPath, avatarPhotoPath, rewardAccount } = channelInput
     const inputPaths = [coverPhotoPath, avatarPhotoPath].filter((p) => p !== undefined) as string[]
     const resolvedAssets = await this.resolveAndValidateAssets(inputPaths, input)