aboutsummaryrefslogtreecommitdiffhomepage
path: root/shared/extra-utils/videos/video-comments.ts
blob: 0b0df81dc5b48dea85bcc654b0496acd18fe8894 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* eslint-disable @typescript-eslint/no-floating-promises */

import * as request from 'supertest'
import { makeDeleteRequest, makeGetRequest } from '../requests/requests'

function getAdminVideoComments (options: {
  url: string
  token: string
  start: number
  count: number
  sort?: string
  isLocal?: boolean
  search?: string
  searchAccount?: string
  searchVideo?: string
}) {
  const { url, token, start, count, sort, isLocal, search, searchAccount, searchVideo } = options
  const path = '/api/v1/videos/comments'

  const query = {
    start,
    count,
    sort: sort || '-createdAt'
  }

  if (isLocal !== undefined) Object.assign(query, { isLocal })
  if (search !== undefined) Object.assign(query, { search })
  if (searchAccount !== undefined) Object.assign(query, { searchAccount })
  if (searchVideo !== undefined) Object.assign(query, { searchVideo })

  return makeGetRequest({
    url,
    path,
    token,
    query,
    statusCodeExpected: 200
  })
}

function getVideoCommentThreads (url: string, videoId: number | string, start: number, count: number, sort?: string, token?: string) {
  const path = '/api/v1/videos/' + videoId + '/comment-threads'

  const req = request(url)
    .get(path)
    .query({ start: start })
    .query({ count: count })

  if (sort) req.query({ sort })
  if (token) req.set('Authorization', 'Bearer ' + token)

  return req.set('Accept', 'application/json')
    .expect(200)
    .expect('Content-Type', /json/)
}

function getVideoThreadComments (url: string, videoId: number | string, threadId: number, token?: string) {
  const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId

  const req = request(url)
    .get(path)
    .set('Accept', 'application/json')

  if (token) req.set('Authorization', 'Bearer ' + token)

  return req.expect(200)
            .expect('Content-Type', /json/)
}

function addVideoCommentThread (url: string, token: string, videoId: number | string, text: string, expectedStatus = 200) {
  const path = '/api/v1/videos/' + videoId + '/comment-threads'

  return request(url)
    .post(path)
    .send({ text })
    .set('Accept', 'application/json')
    .set('Authorization', 'Bearer ' + token)
    .expect(expectedStatus)
}

function addVideoCommentReply (
  url: string,
  token: string,
  videoId: number | string,
  inReplyToCommentId: number,
  text: string,
  expectedStatus = 200
) {
  const path = '/api/v1/videos/' + videoId + '/comments/' + inReplyToCommentId

  return request(url)
    .post(path)
    .send({ text })
    .set('Accept', 'application/json')
    .set('Authorization', 'Bearer ' + token)
    .expect(expectedStatus)
}

async function findCommentId (url: string, videoId: number | string, text: string) {
  const res = await getVideoCommentThreads(url, videoId, 0, 25, '-createdAt')

  return res.body.data.find(c => c.text === text).id as number
}

function deleteVideoComment (
  url: string,
  token: string,
  videoId: number | string,
  commentId: number,
  statusCodeExpected = 204
) {
  const path = '/api/v1/videos/' + videoId + '/comments/' + commentId

  return makeDeleteRequest({
    url,
    path,
    token,
    statusCodeExpected
  })
}

// ---------------------------------------------------------------------------

export {
  getVideoCommentThreads,
  getAdminVideoComments,
  getVideoThreadComments,
  addVideoCommentThread,
  addVideoCommentReply,
  findCommentId,
  deleteVideoComment
}