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