Browse Source

Content CLI: introduce new video/videos commands

iorveth 4 years ago
parent
commit
ede585a2e2

+ 37 - 0
cli/examples/content/updateVideo.json

@@ -0,0 +1,37 @@
+{
+  "assets": [
+    {
+      "Urls": ["https://joystream.org/WorkingGroupOpening.schema.json"]
+    }
+  ],
+  "meta": {
+    "title": "Title",
+    "description": "Description",
+    "video": 1,
+    "thumbnailPhoto": "1",
+    "duration": 10,
+    "mediaPixelHeight": 20,
+    "mediaPixelWidth": 50,
+    "language": "en",
+    "hasMarketing": true,
+    "isPublic": true,
+    "isExplicit": true,
+    "personsList": [1, 2, 5],
+    "category": 2,
+    "mediaType": {
+      "codecName": "mp4",
+      "container": "zip",
+      "mimeMediaType": "mime media type"
+    },
+    "licence": {
+      "code": 1001,
+      "attribution": "first",
+      "customText": "text"
+    },
+    "publishedBeforeJoystream": {
+      "isPublished": true,
+      "date": "2020.02.20"
+    }
+  }
+}
+

+ 6 - 5
cli/src/Api.ts

@@ -67,7 +67,6 @@ export const apiModuleByGroup: { [key in WorkingGroups]: string } = {
 // Api wrapper for handling most common api calls and allowing easy API implementation switch in the future
 export default class Api {
   private _api: ApiPromise
-  private _cdClassesCache: [ChannelId, Channel][] | null = null
 
   private constructor(originalApi: ApiPromise) {
     this._api = originalApi
@@ -495,10 +494,12 @@ export default class Api {
   }
 
   // Content directory
-  async availableChannels(useCache = true): Promise<[ChannelId, Channel][]> {
-    return useCache && this._cdClassesCache
-      ? this._cdClassesCache
-      : (this._cdClassesCache = await this.entriesByIds<ChannelId, Channel>(this._api.query.content.channelById))
+  async availableChannels(): Promise<[ChannelId, Channel][]> {
+    return await this.entriesByIds<ChannelId, Channel>(this._api.query.content.channelById)
+  }
+
+  async availableVideos(): Promise<[VideoId, Video][]> {
+    return await this.entriesByIds<VideoId, Video>(this._api.query.content.videoById)
   }
 
   availableCuratorGroups(): Promise<[CuratorGroupId, CuratorGroup][]> {

+ 18 - 9
cli/src/base/ContentDirectoryCommandBase.ts

@@ -83,15 +83,24 @@ export default abstract class ContentDirectoryCommandBase extends RolesCommandBa
     return selectedClass
   }
 
-  async channelEntryById(channelId: string): Promise<[ChannelId, Channel]> {
-    const channels = await this.getApi().availableChannels()
-    const foundClass = channels.find(([id, c]) => id.toString() === channelId)
-    if (!foundClass) {
-      this.error(`Class id not found by class name or id: "${channelId}"!`)
-    }
-
-    return foundClass
-  }
+  // async channelEntryById(channelId: number): Promise<[ChannelId, Channel]> {
+  //   const foundChannel = await this.getApi().channelById(channelId)
+  //   if (!foundChannel) {
+  //     this.error(`Channel not found by channel id: "${channelId}"!`)
+  //   }
+
+  //   return [channelId, foundChannel]
+  // }
+
+  // async videoEntryById(videoId: string): Promise<[ChannelId, Channel]> {
+  //   const videos = await this.getApi().availableVideos()
+  //   const foundVideo = channels.find(([id, ]) => id.toString() === channelId)
+  //   if (!foundChannel) {
+  //     this.error(`Channel not found by channel id: "${channelId}"!`)
+  //   }
+
+  //   return foundChannel
+  // }
 
   private async curatorGroupChoices(ids?: CuratorGroupId[]) {
     const groups = await this.getApi().availableCuratorGroups()

+ 27 - 24
cli/src/commands/content/channel.ts

@@ -13,29 +13,32 @@ export default class ChannelCommand extends ContentDirectoryCommandBase {
 
   async run() {
     const { channelId } = this.parse(ChannelCommand).args
-    const [id, aChannel] = await this.channelEntryById(channelId)
-
-    displayCollapsedRow({
-      'ID': id.toString(),
-      'Owner': JSON.stringify(aChannel.owner.toJSON()),
-      'IsCensored': aChannel.is_censored.toString(),
-      'RewardAccount': aChannel.reward_account? aChannel.reward_account.toString() : 'NONE'
-    })
-
-    displayHeader(`Media`)
-
-    displayCollapsedRow({
-      'NumberOfVideos': aChannel.videos.length,
-      'NumberOfPlaylists': aChannel.playlists.length,
-      'NumberOfSeries': aChannel.series.length,
-    })
-
-    displayHeader(`MediaData`)
-
-    displayCollapsedRow({
-      'Videos': JSON.stringify(aChannel.videos.toJSON()),
-      'Playlists': JSON.stringify(aChannel.playlists.toJSON()),
-      'Series': JSON.stringify(aChannel.series.toJSON()),
-    })
+    const aChannel = await this.getApi().channelById(channelId)
+    if (aChannel) {
+      displayCollapsedRow({
+        'ID': channelId.toString(),
+        'Owner': JSON.stringify(aChannel.owner.toJSON()),
+        'IsCensored': aChannel.is_censored.toString(),
+        'RewardAccount': aChannel.reward_account? aChannel.reward_account.toString() : 'NONE'
+      })
+  
+      displayHeader(`Media`)
+  
+      displayCollapsedRow({
+        'NumberOfVideos': aChannel.videos.length,
+        'NumberOfPlaylists': aChannel.playlists.length,
+        'NumberOfSeries': aChannel.series.length,
+      })
+  
+      displayHeader(`MediaData`)
+  
+      displayCollapsedRow({
+        'Videos': JSON.stringify(aChannel.videos.toJSON()),
+        'Playlists': JSON.stringify(aChannel.playlists.toJSON()),
+        'Series': JSON.stringify(aChannel.series.toJSON()),
+      })
+    } else {
+      this.error(`Channel not found by channel id: "${channelId}"!`)
+    }
   }
 }

+ 28 - 0
cli/src/commands/content/video.ts

@@ -0,0 +1,28 @@
+import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
+import { displayCollapsedRow } from '../../helpers/display'
+
+export default class VideoCommand extends ContentDirectoryCommandBase {
+  static description = 'Show Video details by id.'
+  static args = [
+    {
+      name: 'videoId',
+      required: true,
+      description: 'ID of the Video',
+    },
+  ]
+
+  async run() {
+    const { videoId } = this.parse(VideoCommand).args
+    const aVideo = await this.getApi().videoById(videoId)
+    if (aVideo) {
+      displayCollapsedRow({
+        'ID': videoId.toString(),
+        'InChannel': aVideo.in_channel.toString(),
+        'InSeries': aVideo.in_series.toString(),
+        'IsCensored': aVideo.is_censored.toString(),
+      })
+    } else {
+      this.error(`Video not found by channel id: "${videoId}"!`)
+    }
+  }
+}

+ 39 - 0
cli/src/commands/content/videos.ts

@@ -0,0 +1,39 @@
+import ContentDirectoryCommandBase from '../../base/ContentDirectoryCommandBase'
+// import chalk from 'chalk'
+import { displayTable } from '../../helpers/display'
+
+export default class VideosCommand extends ContentDirectoryCommandBase {
+  static description = 'List existing content directory videos.'
+
+  static args = [
+    {
+      name: 'channelId',
+      required: true,
+      description: 'ID of the Channel',
+    },
+  ]
+
+  async run() {
+    const { channelId } = this.parse(VideosCommand).args
+
+    let videos = await this.getApi().availableVideos()
+
+    if (channelId) {
+        videos = videos.filter(([, /* id */ video]) => video.in_channel.eqn(channelId))
+    }
+
+    if (videos.length > 0) {
+      displayTable(
+        videos.map(([id, v]) => ({
+            'ID': id.toString(),
+            'InChannel': v.in_channel.toString(),
+            'InSeries': v.in_series.toString(),
+            'IsCensored': v.is_censored.toString(),
+        })),
+        3
+      )
+    } else {
+        this.log('There are no videos yet')
+    }
+  }
+}