]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - shared/server-commands/videos/videos-command.ts
Try to have more robust live tests
[github/Chocobozzz/PeerTube.git] / shared / server-commands / videos / videos-command.ts
index 21753ddc46a71beedf36856a32ab96f9aa0e4371..b5df9c32534a05a00849d691c0a7e71938962505 100644 (file)
@@ -3,9 +3,8 @@
 import { expect } from 'chai'
 import { createReadStream, stat } from 'fs-extra'
 import got, { Response as GotResponse } from 'got'
-import { omit } from 'lodash'
 import validator from 'validator'
-import { buildAbsoluteFixturePath, pick, wait } from '@shared/core-utils'
+import { buildAbsoluteFixturePath, getAllPrivacies, omit, pick, wait } from '@shared/core-utils'
 import { buildUUID } from '@shared/extra-utils'
 import {
   HttpStatusCode,
@@ -16,10 +15,12 @@ import {
   VideoCreateResult,
   VideoDetails,
   VideoFileMetadata,
+  VideoInclude,
   VideoPrivacy,
   VideosCommonQuery,
   VideoTranscodingCreate
 } from '@shared/models'
+import { VideoSource } from '@shared/models/videos/video-source'
 import { unwrapBody } from '../requests'
 import { waitJobs } from '../server'
 import { AbstractCommand, OverrideCommandOptions } from '../shared'
@@ -107,23 +108,6 @@ export class VideosCommand extends AbstractCommand {
 
   // ---------------------------------------------------------------------------
 
-  view (options: OverrideCommandOptions & {
-    id: number | string
-    xForwardedFor?: string
-  }) {
-    const { id, xForwardedFor } = options
-    const path = '/api/v1/videos/' + id + '/views'
-
-    return this.postBodyRequest({
-      ...options,
-
-      path,
-      xForwardedFor,
-      implicitToken: false,
-      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
-    })
-  }
-
   rate (options: OverrideCommandOptions & {
     id: number | string
     rating: UserVideoRateType
@@ -167,6 +151,20 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
+  getSource (options: OverrideCommandOptions & {
+    id: number | string
+  }) {
+    const path = '/api/v1/videos/' + options.id + '/source'
+
+    return this.getRequestBody<VideoSource>({
+      ...options,
+
+      path,
+      implicitToken: true,
+      defaultExpectedStatus: HttpStatusCode.OK_200
+    })
+  }
+
   async getId (options: OverrideCommandOptions & {
     uuid: number | string
   }) {
@@ -212,6 +210,20 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
+  listMySubscriptionVideos (options: OverrideCommandOptions & VideosCommonQuery = {}) {
+    const { sort = '-createdAt' } = options
+    const path = '/api/v1/users/me/subscriptions/videos'
+
+    return this.getRequestBody<ResultList<Video>>({
+      ...options,
+
+      path,
+      query: { sort, ...this.buildListQuery(options) },
+      implicitToken: true,
+      defaultExpectedStatus: HttpStatusCode.OK_200
+    })
+  }
+
   // ---------------------------------------------------------------------------
 
   list (options: OverrideCommandOptions & VideosCommonQuery = {}) {
@@ -237,6 +249,22 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
+  listAllForAdmin (options: OverrideCommandOptions & VideosCommonQuery = {}) {
+    const include = VideoInclude.NOT_PUBLISHED_STATE | VideoInclude.BLACKLISTED | VideoInclude.BLOCKED_OWNER
+    const nsfw = 'both'
+    const privacyOneOf = getAllPrivacies()
+
+    return this.list({
+      ...options,
+
+      include,
+      nsfw,
+      privacyOneOf,
+
+      token: this.buildCommonRequestToken({ ...options, implicitToken: true })
+    })
+  }
+
   listByAccount (options: OverrideCommandOptions & VideosCommonQuery & {
     handle: string
   }) {
@@ -345,8 +373,9 @@ export class VideosCommand extends AbstractCommand {
   async upload (options: OverrideCommandOptions & {
     attributes?: VideoEdit
     mode?: 'legacy' | 'resumable' // default legacy
+    waitTorrentGeneration?: boolean // default true
   } = {}) {
-    const { mode = 'legacy' } = options
+    const { mode = 'legacy', waitTorrentGeneration = true } = options
     let defaultChannelId = 1
 
     try {
@@ -380,7 +409,7 @@ export class VideosCommand extends AbstractCommand {
 
     // Wait torrent generation
     const expectedStatus = this.buildExpectedStatus({ ...options, defaultExpectedStatus: HttpStatusCode.OK_200 })
-    if (expectedStatus === HttpStatusCode.OK_200) {
+    if (expectedStatus === HttpStatusCode.OK_200 && waitTorrentGeneration) {
       let video: VideoDetails
 
       do {
@@ -486,7 +515,7 @@ export class VideosCommand extends AbstractCommand {
       },
 
       // Fixture will be sent later
-      attaches: this.buildUploadAttaches(omit(options.attributes, 'fixture')),
+      attaches: this.buildUploadAttaches(omit(options.attributes, [ 'fixture' ])),
       implicitToken: true,
 
       defaultExpectedStatus: null
@@ -499,8 +528,17 @@ export class VideosCommand extends AbstractCommand {
     size: number
     contentLength?: number
     contentRangeBuilder?: (start: number, chunk: any) => string
+    digestBuilder?: (chunk: any) => string
   }) {
-    const { pathUploadId, videoFilePath, size, contentLength, contentRangeBuilder, expectedStatus = HttpStatusCode.OK_200 } = options
+    const {
+      pathUploadId,
+      videoFilePath,
+      size,
+      contentLength,
+      contentRangeBuilder,
+      digestBuilder,
+      expectedStatus = HttpStatusCode.OK_200
+    } = options
 
     const path = '/api/v1/videos/upload-resumable'
     let start = 0
@@ -522,6 +560,10 @@ export class VideosCommand extends AbstractCommand {
           'Content-Length': contentLength ? contentLength + '' : chunk.length + ''
         }
 
+        if (digestBuilder) {
+          Object.assign(headers, { digest: digestBuilder(chunk) })
+        }
+
         const res = await got<{ video: VideoCreateResult }>({
           url,
           method: 'put',
@@ -594,7 +636,7 @@ export class VideosCommand extends AbstractCommand {
 
   // ---------------------------------------------------------------------------
 
-  removeHLSFiles (options: OverrideCommandOptions & {
+  removeHLSPlaylist (options: OverrideCommandOptions & {
     videoId: number | string
   }) {
     const path = '/api/v1/videos/' + options.videoId + '/hls'
@@ -608,7 +650,22 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
-  removeWebTorrentFiles (options: OverrideCommandOptions & {
+  removeHLSFile (options: OverrideCommandOptions & {
+    videoId: number | string
+    fileId: number
+  }) {
+    const path = '/api/v1/videos/' + options.videoId + '/hls/' + options.fileId
+
+    return this.deleteRequest({
+      ...options,
+
+      path,
+      implicitToken: true,
+      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
+    })
+  }
+
+  removeAllWebTorrentFiles (options: OverrideCommandOptions & {
     videoId: number | string
   }) {
     const path = '/api/v1/videos/' + options.videoId + '/webtorrent'
@@ -622,6 +679,21 @@ export class VideosCommand extends AbstractCommand {
     })
   }
 
+  removeWebTorrentFile (options: OverrideCommandOptions & {
+    videoId: number | string
+    fileId: number
+  }) {
+    const path = '/api/v1/videos/' + options.videoId + '/webtorrent/' + options.fileId
+
+    return this.deleteRequest({
+      ...options,
+
+      path,
+      implicitToken: true,
+      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
+    })
+  }
+
   runTranscoding (options: OverrideCommandOptions & {
     videoId: number | string
     transcodingType: 'hls' | 'webtorrent'
@@ -652,6 +724,7 @@ export class VideosCommand extends AbstractCommand {
       'categoryOneOf',
       'licenceOneOf',
       'languageOneOf',
+      'privacyOneOf',
       'tagsOneOf',
       'tagsAllOf',
       'isLocal',