channels-command.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import { pick } from '@shared/core-utils'
  2. import {
  3. ActorFollow,
  4. HttpStatusCode,
  5. ResultList,
  6. VideoChannel,
  7. VideoChannelCreate,
  8. VideoChannelCreateResult,
  9. VideoChannelUpdate,
  10. VideosImportInChannelCreate
  11. } from '@shared/models'
  12. import { unwrapBody } from '../requests'
  13. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  14. export class ChannelsCommand extends AbstractCommand {
  15. list (options: OverrideCommandOptions & {
  16. start?: number
  17. count?: number
  18. sort?: string
  19. withStats?: boolean
  20. } = {}) {
  21. const path = '/api/v1/video-channels'
  22. return this.getRequestBody<ResultList<VideoChannel>>({
  23. ...options,
  24. path,
  25. query: pick(options, [ 'start', 'count', 'sort', 'withStats' ]),
  26. implicitToken: false,
  27. defaultExpectedStatus: HttpStatusCode.OK_200
  28. })
  29. }
  30. listByAccount (options: OverrideCommandOptions & {
  31. accountName: string
  32. start?: number
  33. count?: number
  34. sort?: string
  35. withStats?: boolean
  36. search?: string
  37. }) {
  38. const { accountName, sort = 'createdAt' } = options
  39. const path = '/api/v1/accounts/' + accountName + '/video-channels'
  40. return this.getRequestBody<ResultList<VideoChannel>>({
  41. ...options,
  42. path,
  43. query: { sort, ...pick(options, [ 'start', 'count', 'withStats', 'search' ]) },
  44. implicitToken: false,
  45. defaultExpectedStatus: HttpStatusCode.OK_200
  46. })
  47. }
  48. async create (options: OverrideCommandOptions & {
  49. attributes: Partial<VideoChannelCreate>
  50. }) {
  51. const path = '/api/v1/video-channels/'
  52. // Default attributes
  53. const defaultAttributes = {
  54. displayName: 'my super video channel',
  55. description: 'my super channel description',
  56. support: 'my super channel support'
  57. }
  58. const attributes = { ...defaultAttributes, ...options.attributes }
  59. const body = await unwrapBody<{ videoChannel: VideoChannelCreateResult }>(this.postBodyRequest({
  60. ...options,
  61. path,
  62. fields: attributes,
  63. implicitToken: true,
  64. defaultExpectedStatus: HttpStatusCode.OK_200
  65. }))
  66. return body.videoChannel
  67. }
  68. update (options: OverrideCommandOptions & {
  69. channelName: string
  70. attributes: VideoChannelUpdate
  71. }) {
  72. const { channelName, attributes } = options
  73. const path = '/api/v1/video-channels/' + channelName
  74. return this.putBodyRequest({
  75. ...options,
  76. path,
  77. fields: attributes,
  78. implicitToken: true,
  79. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  80. })
  81. }
  82. delete (options: OverrideCommandOptions & {
  83. channelName: string
  84. }) {
  85. const path = '/api/v1/video-channels/' + options.channelName
  86. return this.deleteRequest({
  87. ...options,
  88. path,
  89. implicitToken: true,
  90. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  91. })
  92. }
  93. get (options: OverrideCommandOptions & {
  94. channelName: string
  95. }) {
  96. const path = '/api/v1/video-channels/' + options.channelName
  97. return this.getRequestBody<VideoChannel>({
  98. ...options,
  99. path,
  100. implicitToken: false,
  101. defaultExpectedStatus: HttpStatusCode.OK_200
  102. })
  103. }
  104. updateImage (options: OverrideCommandOptions & {
  105. fixture: string
  106. channelName: string | number
  107. type: 'avatar' | 'banner'
  108. }) {
  109. const { channelName, fixture, type } = options
  110. const path = `/api/v1/video-channels/${channelName}/${type}/pick`
  111. return this.updateImageRequest({
  112. ...options,
  113. path,
  114. fixture,
  115. fieldname: type + 'file',
  116. implicitToken: true,
  117. defaultExpectedStatus: HttpStatusCode.OK_200
  118. })
  119. }
  120. deleteImage (options: OverrideCommandOptions & {
  121. channelName: string | number
  122. type: 'avatar' | 'banner'
  123. }) {
  124. const { channelName, type } = options
  125. const path = `/api/v1/video-channels/${channelName}/${type}`
  126. return this.deleteRequest({
  127. ...options,
  128. path,
  129. implicitToken: true,
  130. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  131. })
  132. }
  133. listFollowers (options: OverrideCommandOptions & {
  134. channelName: string
  135. start?: number
  136. count?: number
  137. sort?: string
  138. search?: string
  139. }) {
  140. const { channelName, start, count, sort, search } = options
  141. const path = '/api/v1/video-channels/' + channelName + '/followers'
  142. const query = { start, count, sort, search }
  143. return this.getRequestBody<ResultList<ActorFollow>>({
  144. ...options,
  145. path,
  146. query,
  147. implicitToken: true,
  148. defaultExpectedStatus: HttpStatusCode.OK_200
  149. })
  150. }
  151. importVideos (options: OverrideCommandOptions & VideosImportInChannelCreate & {
  152. channelName: string
  153. }) {
  154. const { channelName, externalChannelUrl, videoChannelSyncId } = options
  155. const path = `/api/v1/video-channels/${channelName}/import-videos`
  156. return this.postBodyRequest({
  157. ...options,
  158. path,
  159. fields: { externalChannelUrl, videoChannelSyncId },
  160. implicitToken: true,
  161. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  162. })
  163. }
  164. }