video-channels.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /* eslint-disable @typescript-eslint/no-floating-promises */
  2. import * as request from 'supertest'
  3. import { VideoChannelUpdate } from '../../models/videos/channel/video-channel-update.model'
  4. import { VideoChannelCreate } from '../../models/videos/channel/video-channel-create.model'
  5. import { makeGetRequest, updateAvatarRequest } from '../requests/requests'
  6. import { ServerInfo } from '../server/servers'
  7. import { User } from '../../models/users/user.model'
  8. import { getMyUserInformation } from '../users/users'
  9. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  10. function getVideoChannelsList (url: string, start: number, count: number, sort?: string, withStats?: boolean) {
  11. const path = '/api/v1/video-channels'
  12. const req = request(url)
  13. .get(path)
  14. .query({ start: start })
  15. .query({ count: count })
  16. if (sort) req.query({ sort })
  17. if (withStats) req.query({ withStats })
  18. return req.set('Accept', 'application/json')
  19. .expect(HttpStatusCode.OK_200)
  20. .expect('Content-Type', /json/)
  21. }
  22. function getAccountVideoChannelsList (parameters: {
  23. url: string
  24. accountName: string
  25. start?: number
  26. count?: number
  27. sort?: string
  28. specialStatus?: HttpStatusCode
  29. withStats?: boolean
  30. search?: string
  31. }) {
  32. const {
  33. url,
  34. accountName,
  35. start,
  36. count,
  37. sort = 'createdAt',
  38. specialStatus = HttpStatusCode.OK_200,
  39. withStats = false,
  40. search
  41. } = parameters
  42. const path = '/api/v1/accounts/' + accountName + '/video-channels'
  43. return makeGetRequest({
  44. url,
  45. path,
  46. query: {
  47. start,
  48. count,
  49. sort,
  50. withStats,
  51. search
  52. },
  53. statusCodeExpected: specialStatus
  54. })
  55. }
  56. function addVideoChannel (
  57. url: string,
  58. token: string,
  59. videoChannelAttributesArg: VideoChannelCreate,
  60. expectedStatus = HttpStatusCode.OK_200
  61. ) {
  62. const path = '/api/v1/video-channels/'
  63. // Default attributes
  64. let attributes = {
  65. displayName: 'my super video channel',
  66. description: 'my super channel description',
  67. support: 'my super channel support'
  68. }
  69. attributes = Object.assign(attributes, videoChannelAttributesArg)
  70. return request(url)
  71. .post(path)
  72. .send(attributes)
  73. .set('Accept', 'application/json')
  74. .set('Authorization', 'Bearer ' + token)
  75. .expect(expectedStatus)
  76. }
  77. function updateVideoChannel (
  78. url: string,
  79. token: string,
  80. channelName: string,
  81. attributes: VideoChannelUpdate,
  82. expectedStatus = HttpStatusCode.NO_CONTENT_204
  83. ) {
  84. const body: any = {}
  85. const path = '/api/v1/video-channels/' + channelName
  86. if (attributes.displayName) body.displayName = attributes.displayName
  87. if (attributes.description) body.description = attributes.description
  88. if (attributes.support) body.support = attributes.support
  89. if (attributes.bulkVideosSupportUpdate) body.bulkVideosSupportUpdate = attributes.bulkVideosSupportUpdate
  90. return request(url)
  91. .put(path)
  92. .send(body)
  93. .set('Accept', 'application/json')
  94. .set('Authorization', 'Bearer ' + token)
  95. .expect(expectedStatus)
  96. }
  97. function deleteVideoChannel (url: string, token: string, channelName: string, expectedStatus = HttpStatusCode.NO_CONTENT_204) {
  98. const path = '/api/v1/video-channels/' + channelName
  99. return request(url)
  100. .delete(path)
  101. .set('Accept', 'application/json')
  102. .set('Authorization', 'Bearer ' + token)
  103. .expect(expectedStatus)
  104. }
  105. function getVideoChannel (url: string, channelName: string) {
  106. const path = '/api/v1/video-channels/' + channelName
  107. return request(url)
  108. .get(path)
  109. .set('Accept', 'application/json')
  110. .expect(HttpStatusCode.OK_200)
  111. .expect('Content-Type', /json/)
  112. }
  113. function updateVideoChannelAvatar (options: {
  114. url: string
  115. accessToken: string
  116. fixture: string
  117. videoChannelName: string | number
  118. }) {
  119. const path = '/api/v1/video-channels/' + options.videoChannelName + '/avatar/pick'
  120. return updateAvatarRequest(Object.assign(options, { path }))
  121. }
  122. function setDefaultVideoChannel (servers: ServerInfo[]) {
  123. const tasks: Promise<any>[] = []
  124. for (const server of servers) {
  125. const p = getMyUserInformation(server.url, server.accessToken)
  126. .then(res => { server.videoChannel = (res.body as User).videoChannels[0] })
  127. tasks.push(p)
  128. }
  129. return Promise.all(tasks)
  130. }
  131. // ---------------------------------------------------------------------------
  132. export {
  133. updateVideoChannelAvatar,
  134. getVideoChannelsList,
  135. getAccountVideoChannelsList,
  136. addVideoChannel,
  137. updateVideoChannel,
  138. deleteVideoChannel,
  139. getVideoChannel,
  140. setDefaultVideoChannel
  141. }