video-comments.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* eslint-disable @typescript-eslint/no-floating-promises */
  2. import * as request from 'supertest'
  3. import { makeDeleteRequest, makeGetRequest } from '../requests/requests'
  4. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  5. function getAdminVideoComments (options: {
  6. url: string
  7. token: string
  8. start: number
  9. count: number
  10. sort?: string
  11. isLocal?: boolean
  12. search?: string
  13. searchAccount?: string
  14. searchVideo?: string
  15. }) {
  16. const { url, token, start, count, sort, isLocal, search, searchAccount, searchVideo } = options
  17. const path = '/api/v1/videos/comments'
  18. const query = {
  19. start,
  20. count,
  21. sort: sort || '-createdAt'
  22. }
  23. if (isLocal !== undefined) Object.assign(query, { isLocal })
  24. if (search !== undefined) Object.assign(query, { search })
  25. if (searchAccount !== undefined) Object.assign(query, { searchAccount })
  26. if (searchVideo !== undefined) Object.assign(query, { searchVideo })
  27. return makeGetRequest({
  28. url,
  29. path,
  30. token,
  31. query,
  32. statusCodeExpected: HttpStatusCode.OK_200
  33. })
  34. }
  35. function getVideoCommentThreads (url: string, videoId: number | string, start: number, count: number, sort?: string, token?: string) {
  36. const path = '/api/v1/videos/' + videoId + '/comment-threads'
  37. const req = request(url)
  38. .get(path)
  39. .query({ start: start })
  40. .query({ count: count })
  41. if (sort) req.query({ sort })
  42. if (token) req.set('Authorization', 'Bearer ' + token)
  43. return req.set('Accept', 'application/json')
  44. .expect(HttpStatusCode.OK_200)
  45. .expect('Content-Type', /json/)
  46. }
  47. function getVideoThreadComments (url: string, videoId: number | string, threadId: number, token?: string) {
  48. const path = '/api/v1/videos/' + videoId + '/comment-threads/' + threadId
  49. const req = request(url)
  50. .get(path)
  51. .set('Accept', 'application/json')
  52. if (token) req.set('Authorization', 'Bearer ' + token)
  53. return req.expect(HttpStatusCode.OK_200)
  54. .expect('Content-Type', /json/)
  55. }
  56. function addVideoCommentThread (
  57. url: string,
  58. token: string,
  59. videoId: number | string,
  60. text: string,
  61. expectedStatus = HttpStatusCode.OK_200
  62. ) {
  63. const path = '/api/v1/videos/' + videoId + '/comment-threads'
  64. return request(url)
  65. .post(path)
  66. .send({ text })
  67. .set('Accept', 'application/json')
  68. .set('Authorization', 'Bearer ' + token)
  69. .expect(expectedStatus)
  70. }
  71. function addVideoCommentReply (
  72. url: string,
  73. token: string,
  74. videoId: number | string,
  75. inReplyToCommentId: number,
  76. text: string,
  77. expectedStatus = HttpStatusCode.OK_200
  78. ) {
  79. const path = '/api/v1/videos/' + videoId + '/comments/' + inReplyToCommentId
  80. return request(url)
  81. .post(path)
  82. .send({ text })
  83. .set('Accept', 'application/json')
  84. .set('Authorization', 'Bearer ' + token)
  85. .expect(expectedStatus)
  86. }
  87. async function findCommentId (url: string, videoId: number | string, text: string) {
  88. const res = await getVideoCommentThreads(url, videoId, 0, 25, '-createdAt')
  89. return res.body.data.find(c => c.text === text).id as number
  90. }
  91. function deleteVideoComment (
  92. url: string,
  93. token: string,
  94. videoId: number | string,
  95. commentId: number,
  96. statusCodeExpected = HttpStatusCode.NO_CONTENT_204
  97. ) {
  98. const path = '/api/v1/videos/' + videoId + '/comments/' + commentId
  99. return makeDeleteRequest({
  100. url,
  101. path,
  102. token,
  103. statusCodeExpected
  104. })
  105. }
  106. // ---------------------------------------------------------------------------
  107. export {
  108. getVideoCommentThreads,
  109. getAdminVideoComments,
  110. getVideoThreadComments,
  111. addVideoCommentThread,
  112. addVideoCommentReply,
  113. findCommentId,
  114. deleteVideoComment
  115. }