aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/server-commands/videos/views-command.ts
blob: 01113f7982a035c1e43d1e086d89231624e66a15 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/no-floating-promises */
import { HttpStatusCode, VideoViewEvent } from '@shared/models'
import { AbstractCommand, OverrideCommandOptions } from '../shared'

export class ViewsCommand extends AbstractCommand {

  view (options: OverrideCommandOptions & {
    id: number | string
    currentTime?: number
    viewEvent?: VideoViewEvent
    xForwardedFor?: string
  }) {
    const { id, xForwardedFor, viewEvent, currentTime } = options
    const path = '/api/v1/videos/' + id + '/views'

    return this.postBodyRequest({
      ...options,

      path,
      xForwardedFor,
      fields: {
        currentTime: currentTime ?? 1,
        viewEvent
      },
      implicitToken: false,
      defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
    })
  }

  async simulateView (options: OverrideCommandOptions & {
    id: number | string
    xForwardedFor?: string
  }) {
    await this.view({ ...options, currentTime: 0 })
    await this.view({ ...options, currentTime: 5 })
  }

  async simulateViewer (options: OverrideCommandOptions & {
    id: number | string
    currentTimes: number[]
    xForwardedFor?: string
  }) {
    let viewEvent: VideoViewEvent = 'seek'

    for (const currentTime of options.currentTimes) {
      await this.view({ ...options, currentTime, viewEvent })

      viewEvent = undefined
    }
  }
}