aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/videos/channels-command.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-07-09 11:21:30 +0200
committerChocobozzz <me@florianbigard.com>2021-07-20 15:27:18 +0200
commita54618880c394ad7571f3f3222dc96ec2dd10d9a (patch)
treec9f7b05e578abc2383bccd707c11438c61857c72 /shared/extra-utils/videos/channels-command.ts
parent57f879a540551c3b958b0991c8e1e3657a4481d8 (diff)
downloadPeerTube-a54618880c394ad7571f3f3222dc96ec2dd10d9a.tar.gz
PeerTube-a54618880c394ad7571f3f3222dc96ec2dd10d9a.tar.zst
PeerTube-a54618880c394ad7571f3f3222dc96ec2dd10d9a.zip
Introduce channels command
Diffstat (limited to 'shared/extra-utils/videos/channels-command.ts')
-rw-r--r--shared/extra-utils/videos/channels-command.ts157
1 files changed, 157 insertions, 0 deletions
diff --git a/shared/extra-utils/videos/channels-command.ts b/shared/extra-utils/videos/channels-command.ts
new file mode 100644
index 000000000..a98c5cc93
--- /dev/null
+++ b/shared/extra-utils/videos/channels-command.ts
@@ -0,0 +1,157 @@
1import { pick } from 'lodash'
2import { ResultList, VideoChannel, VideoChannelCreateResult } from '@shared/models'
3import { HttpStatusCode } from '../../core-utils/miscs/http-error-codes'
4import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
5import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
6import { unwrapBody } from '../requests'
7import { AbstractCommand, OverrideCommandOptions } from '../shared'
8
9export 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}