]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - shared/extra-utils/videos/video-comments.ts
Add admin view to manage comments
[github/Chocobozzz/PeerTube.git] / shared / extra-utils / videos / video-comments.ts
1 /* eslint-disable @typescript-eslint/no-floating-promises */
2
3 import * as request from 'supertest'
4 import { makeDeleteRequest, makeGetRequest } from '../requests/requests'
5
6 function 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 }
39
40 function getVideoCommentThreads (url: string, videoId: number | string, start: number, count: number, sort?: string, token?: string) {
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 })
49 if (token) req.set('Authorization', 'Bearer ' + token)
50
51 return req.set('Accept', 'application/json')
52 .expect(200)
53 .expect('Content-Type', /json/)
54 }
55
56 function getVideoThreadComments (url: string, videoId: number | string, threadId: number, token?: string) {
57 const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
58
59 const req = request(url)
60 .get(path)
61 .set('Accept', 'application/json')
62
63 if (token) req.set('Authorization', 'Bearer ' + token)
64
65 return req.expect(200)
66 .expect('Content-Type', /json/)
67 }
68
69 function addVideoCommentThread (url: string, token: string, videoId: number | string, text: string, expectedStatus = 200) {
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
80 function addVideoCommentReply (
81 url: string,
82 token: string,
83 videoId: number | string,
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
98 async 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
104 function 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
121 // ---------------------------------------------------------------------------
122
123 export {
124 getVideoCommentThreads,
125 getAdminVideoComments,
126 getVideoThreadComments,
127 addVideoCommentThread,
128 addVideoCommentReply,
129 findCommentId,
130 deleteVideoComment
131 }