]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/channels-command.ts
Introduce channels command
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / channels-command.ts
1 import { pick } from 'lodash'
2 import { ResultList, VideoChannel, VideoChannelCreateResult } from '@shared/models'
3 import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
4 import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
5 import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
6 import { unwrapBody } from '../requests'
7 import { AbstractCommand, OverrideCommandOptions } from '../shared'
8
9 export class ChannelsCommand extends AbstractCommand {
10
11 list (options: OverrideCommandOptions & {
12 start?: number
13 count?: number
14 sort?: string
15 withStats?: boolean
16 } = {}) {
17 const path = '/api/v1/video-channels'
18
19 return this.getRequestBody<ResultList<VideoChannel>>({
20 ...options,
21
22 path,
23 query: pick(options, [ 'start', 'count', 'sort', 'withStats' ]),
24 implicitToken: false,
25 defaultExpectedStatus: HttpStatusCode.OK_200
26 })
27 }
28
29 listByAccount (options: OverrideCommandOptions & {
30 accountName: string
31 start?: number
32 count?: number
33 sort?: string
34 withStats?: boolean
35 search?: string
36 }) {
37 const { accountName, sort = 'createdAt' } = options
38 const path = '/api/v1/accounts/' + accountName + '/video-channels'
39
40 return this.getRequestBody<ResultList<VideoChannel>>({
41 ...options,
42
43 path,
44 query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'search' ]) },
45 implicitToken: false,
46 defaultExpectedStatus: HttpStatusCode.OK_200
47 })
48 }
49
50 async create (options: OverrideCommandOptions & {
51 attributes: VideoChannelCreate
52 }) {
53 const path = '/api/v1/video-channels/'
54
55 // Default attributes
56 const defaultAttributes = {
57 displayName: 'my super video channel',
58 description: 'my super channel description',
59 support: 'my super channel support'
60 }
61 const attributes = { ...defaultAttributes, ...options.attributes }
62
63 const body = await unwrapBody<{ videoChannel: VideoChannelCreateResult }>(this.postBodyRequest({
64 ...options,
65
66 path,
67 fields: attributes,
68 implicitToken: true,
69 defaultExpectedStatus: HttpStatusCode.OK_200
70 }))
71
72 return body.videoChannel
73 }
74
75 update (options: OverrideCommandOptions & {
76 channelName: string
77 attributes: VideoChannelUpdate
78 }) {
79 const { channelName, attributes } = options
80 const path = '/api/v1/video-channels/' + channelName
81
82 return this.putBodyRequest({
83 ...options,
84
85 path,
86 fields: attributes,
87 implicitToken: true,
88 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
89 })
90 }
91
92 delete (options: OverrideCommandOptions & {
93 channelName: string
94 }) {
95 const path = '/api/v1/video-channels/' + options.channelName
96
97 return this.deleteRequest({
98 ...options,
99
100 path,
101 implicitToken: true,
102 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
103 })
104 }
105
106 get (options: OverrideCommandOptions & {
107 channelName: string
108 }) {
109 const path = '/api/v1/video-channels/' + options.channelName
110
111 return this.getRequestBody<VideoChannel>({
112 ...options,
113
114 path,
115 implicitToken: false,
116 defaultExpectedStatus: HttpStatusCode.OK_200
117 })
118 }
119
120 updateImage (options: OverrideCommandOptions & {
121 fixture: string
122 channelName: string | number
123 type: 'avatar' | 'banner'
124 }) {
125 const { channelName, fixture, type } = options
126
127 const path = `/api/v1/video-channels/${channelName}/${type}/pick`
128
129 return this.updateImageRequest({
130 ...options,
131
132 path,
133 fixture,
134 fieldname: type + 'file',
135
136 implicitToken: true,
137 defaultExpectedStatus: HttpStatusCode.OK_200
138 })
139 }
140
141 deleteImage (options: OverrideCommandOptions & {
142 channelName: string | number
143 type: 'avatar' | 'banner'
144 }) {
145 const { channelName, type } = options
146
147 const path = `/api/v1/video-channels/${channelName}/${type}`
148
149 return this.deleteRequest({
150 ...options,
151
152 path,
153 implicitToken: true,
154 defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
155 })
156 }
157 }