]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - shared/extra-utils/videos/video-comments.ts
replace numbers with typed http status codes (#3409)
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / video-comments.ts
CommitLineData
a1587156
C
1/* eslint-disable @typescript-eslint/no-floating-promises */
2
d3ea8975 3import * as request from 'supertest'
f1273314 4import { makeDeleteRequest, makeGetRequest } from '../requests/requests'
2d53be02 5import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
f1273314
C
6
7function getAdminVideoComments (options: {
8 url: string
9 token: string
10 start: number
11 count: number
12 sort?: string
13 isLocal?: boolean
14 search?: string
15 searchAccount?: string
16 searchVideo?: string
17}) {
18 const { url, token, start, count, sort, isLocal, search, searchAccount, searchVideo } = options
19 const path = '/api/v1/videos/comments'
20
21 const query = {
22 start,
23 count,
24 sort: sort || '-createdAt'
25 }
26
27 if (isLocal !== undefined) Object.assign(query, { isLocal })
28 if (search !== undefined) Object.assign(query, { search })
29 if (searchAccount !== undefined) Object.assign(query, { searchAccount })
30 if (searchVideo !== undefined) Object.assign(query, { searchVideo })
31
32 return makeGetRequest({
33 url,
34 path,
35 token,
36 query,
2d53be02 37 statusCodeExpected: HttpStatusCode.OK_200
f1273314
C
38 })
39}
d3ea8975 40
7ad9b984 41function getVideoCommentThreads (url: string, videoId: number | string, start: number, count: number, sort?: string, token?: string) {
d3ea8975
C
42 const path = '/api/v1/videos/' + videoId + '/comment-threads'
43
44 const req = request(url)
45 .get(path)
46 .query({ start: start })
47 .query({ count: count })
48
49 if (sort) req.query({ sort })
7ad9b984 50 if (token) req.set('Authorization', 'Bearer ' + token)
d3ea8975
C
51
52 return req.set('Accept', 'application/json')
2d53be02 53 .expect(HttpStatusCode.OK_200)
d3ea8975
C
54 .expect('Content-Type', /json/)
55}
56
7ad9b984 57function getVideoThreadComments (url: string, videoId: number | string, threadId: number, token?: string) {
d3ea8975
C
58 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
59
7ad9b984 60 const req = request(url)
d3ea8975
C
61 .get(path)
62 .set('Accept', 'application/json')
7ad9b984
C
63
64 if (token) req.set('Authorization', 'Bearer ' + token)
65
2d53be02 66 return req.expect(HttpStatusCode.OK_200)
7ad9b984 67 .expect('Content-Type', /json/)
d3ea8975
C
68}
69
2d53be02
RK
70function addVideoCommentThread (
71 url: string,
72 token: string,
73 videoId: number | string,
74 text: string,
75 expectedStatus = HttpStatusCode.OK_200
76) {
d3ea8975
C
77 const path = '/api/v1/videos/' + videoId + '/comment-threads'
78
79 return request(url)
80 .post(path)
81 .send({ text })
82 .set('Accept', 'application/json')
83 .set('Authorization', 'Bearer ' + token)
84 .expect(expectedStatus)
85}
86
87function addVideoCommentReply (
88 url: string,
89 token: string,
d50acfab 90 videoId: number | string,
d3ea8975
C
91 inReplyToCommentId: number,
92 text: string,
2d53be02 93 expectedStatus = HttpStatusCode.OK_200
d3ea8975
C
94) {
95 const path = '/api/v1/videos/' + videoId + '/comments/' + inReplyToCommentId
96
97 return request(url)
98 .post(path)
99 .send({ text })
100 .set('Accept', 'application/json')
101 .set('Authorization', 'Bearer ' + token)
102 .expect(expectedStatus)
103}
104
696d83fd
C
105async function findCommentId (url: string, videoId: number | string, text: string) {
106 const res = await getVideoCommentThreads(url, videoId, 0, 25, '-createdAt')
107
108 return res.body.data.find(c => c.text === text).id as number
109}
110
4cb6d457
C
111function deleteVideoComment (
112 url: string,
113 token: string,
114 videoId: number | string,
115 commentId: number,
2d53be02 116 statusCodeExpected = HttpStatusCode.NO_CONTENT_204
4cb6d457
C
117) {
118 const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
119
120 return makeDeleteRequest({
121 url,
122 path,
123 token,
124 statusCodeExpected
125 })
126}
127
d3ea8975
C
128// ---------------------------------------------------------------------------
129
130export {
131 getVideoCommentThreads,
f1273314 132 getAdminVideoComments,
d3ea8975
C
133 getVideoThreadComments,
134 addVideoCommentThread,
4cb6d457 135 addVideoCommentReply,
696d83fd 136 findCommentId,
4cb6d457 137 deleteVideoComment
d3ea8975 138}