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