2
1

video-playlists.ts 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
  2. import { VideoPlaylistCreate } from '../../models/videos/playlist/video-playlist-create.model'
  3. import { omit } from 'lodash'
  4. import { VideoPlaylistUpdate } from '../../models/videos/playlist/video-playlist-update.model'
  5. import { VideoPlaylistElementCreate } from '../../models/videos/playlist/video-playlist-element-create.model'
  6. import { VideoPlaylistElementUpdate } from '../../models/videos/playlist/video-playlist-element-update.model'
  7. import { videoUUIDToId } from './videos'
  8. import { join } from 'path'
  9. import { root } from '..'
  10. import { readdir } from 'fs-extra'
  11. import { expect } from 'chai'
  12. import { VideoPlaylistType } from '../../models/videos/playlist/video-playlist-type.model'
  13. import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
  14. function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) {
  15. const path = '/api/v1/video-playlists'
  16. const query = {
  17. start,
  18. count,
  19. sort
  20. }
  21. return makeGetRequest({
  22. url,
  23. path,
  24. query,
  25. statusCodeExpected: HttpStatusCode.OK_200
  26. })
  27. }
  28. function getVideoChannelPlaylistsList (url: string, videoChannelName: string, start: number, count: number, sort?: string) {
  29. const path = '/api/v1/video-channels/' + videoChannelName + '/video-playlists'
  30. const query = {
  31. start,
  32. count,
  33. sort
  34. }
  35. return makeGetRequest({
  36. url,
  37. path,
  38. query,
  39. statusCodeExpected: HttpStatusCode.OK_200
  40. })
  41. }
  42. function getAccountPlaylistsList (url: string, accountName: string, start: number, count: number, sort?: string, search?: string) {
  43. const path = '/api/v1/accounts/' + accountName + '/video-playlists'
  44. const query = {
  45. start,
  46. count,
  47. sort,
  48. search
  49. }
  50. return makeGetRequest({
  51. url,
  52. path,
  53. query,
  54. statusCodeExpected: HttpStatusCode.OK_200
  55. })
  56. }
  57. function getAccountPlaylistsListWithToken (
  58. url: string,
  59. token: string,
  60. accountName: string,
  61. start: number,
  62. count: number,
  63. playlistType?: VideoPlaylistType,
  64. sort?: string
  65. ) {
  66. const path = '/api/v1/accounts/' + accountName + '/video-playlists'
  67. const query = {
  68. start,
  69. count,
  70. playlistType,
  71. sort
  72. }
  73. return makeGetRequest({
  74. url,
  75. token,
  76. path,
  77. query,
  78. statusCodeExpected: HttpStatusCode.OK_200
  79. })
  80. }
  81. function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.OK_200) {
  82. const path = '/api/v1/video-playlists/' + playlistId
  83. return makeGetRequest({
  84. url,
  85. path,
  86. statusCodeExpected
  87. })
  88. }
  89. function getVideoPlaylistWithToken (url: string, token: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.OK_200) {
  90. const path = '/api/v1/video-playlists/' + playlistId
  91. return makeGetRequest({
  92. url,
  93. token,
  94. path,
  95. statusCodeExpected
  96. })
  97. }
  98. function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = HttpStatusCode.NO_CONTENT_204) {
  99. const path = '/api/v1/video-playlists/' + playlistId
  100. return makeDeleteRequest({
  101. url,
  102. path,
  103. token,
  104. statusCodeExpected
  105. })
  106. }
  107. function createVideoPlaylist (options: {
  108. url: string
  109. token: string
  110. playlistAttrs: VideoPlaylistCreate
  111. expectedStatus?: number
  112. }) {
  113. const path = '/api/v1/video-playlists'
  114. const fields = omit(options.playlistAttrs, 'thumbnailfile')
  115. const attaches = options.playlistAttrs.thumbnailfile
  116. ? { thumbnailfile: options.playlistAttrs.thumbnailfile }
  117. : {}
  118. return makeUploadRequest({
  119. method: 'POST',
  120. url: options.url,
  121. path,
  122. token: options.token,
  123. fields,
  124. attaches,
  125. statusCodeExpected: options.expectedStatus || HttpStatusCode.OK_200
  126. })
  127. }
  128. function updateVideoPlaylist (options: {
  129. url: string
  130. token: string
  131. playlistAttrs: VideoPlaylistUpdate
  132. playlistId: number | string
  133. expectedStatus?: number
  134. }) {
  135. const path = '/api/v1/video-playlists/' + options.playlistId
  136. const fields = omit(options.playlistAttrs, 'thumbnailfile')
  137. const attaches = options.playlistAttrs.thumbnailfile
  138. ? { thumbnailfile: options.playlistAttrs.thumbnailfile }
  139. : {}
  140. return makeUploadRequest({
  141. method: 'PUT',
  142. url: options.url,
  143. path,
  144. token: options.token,
  145. fields,
  146. attaches,
  147. statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
  148. })
  149. }
  150. async function addVideoInPlaylist (options: {
  151. url: string
  152. token: string
  153. playlistId: number | string
  154. elementAttrs: VideoPlaylistElementCreate | { videoId: string }
  155. expectedStatus?: number
  156. }) {
  157. options.elementAttrs.videoId = await videoUUIDToId(options.url, options.elementAttrs.videoId)
  158. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
  159. return makePostBodyRequest({
  160. url: options.url,
  161. path,
  162. token: options.token,
  163. fields: options.elementAttrs,
  164. statusCodeExpected: options.expectedStatus || HttpStatusCode.OK_200
  165. })
  166. }
  167. function updateVideoPlaylistElement (options: {
  168. url: string
  169. token: string
  170. playlistId: number | string
  171. playlistElementId: number | string
  172. elementAttrs: VideoPlaylistElementUpdate
  173. expectedStatus?: number
  174. }) {
  175. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
  176. return makePutBodyRequest({
  177. url: options.url,
  178. path,
  179. token: options.token,
  180. fields: options.elementAttrs,
  181. statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
  182. })
  183. }
  184. function removeVideoFromPlaylist (options: {
  185. url: string
  186. token: string
  187. playlistId: number | string
  188. playlistElementId: number
  189. expectedStatus?: number
  190. }) {
  191. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.playlistElementId
  192. return makeDeleteRequest({
  193. url: options.url,
  194. path,
  195. token: options.token,
  196. statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
  197. })
  198. }
  199. function reorderVideosPlaylist (options: {
  200. url: string
  201. token: string
  202. playlistId: number | string
  203. elementAttrs: {
  204. startPosition: number
  205. insertAfterPosition: number
  206. reorderLength?: number
  207. }
  208. expectedStatus?: number
  209. }) {
  210. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder'
  211. return makePostBodyRequest({
  212. url: options.url,
  213. path,
  214. token: options.token,
  215. fields: options.elementAttrs,
  216. statusCodeExpected: options.expectedStatus || HttpStatusCode.NO_CONTENT_204
  217. })
  218. }
  219. async function checkPlaylistFilesWereRemoved (
  220. playlistUUID: string,
  221. internalServerNumber: number,
  222. directories = [ 'thumbnails' ]
  223. ) {
  224. const testDirectory = 'test' + internalServerNumber
  225. for (const directory of directories) {
  226. const directoryPath = join(root(), testDirectory, directory)
  227. const files = await readdir(directoryPath)
  228. for (const file of files) {
  229. expect(file).to.not.contain(playlistUUID)
  230. }
  231. }
  232. }
  233. function getVideoPlaylistPrivacies (url: string) {
  234. const path = '/api/v1/video-playlists/privacies'
  235. return makeGetRequest({
  236. url,
  237. path,
  238. statusCodeExpected: HttpStatusCode.OK_200
  239. })
  240. }
  241. function doVideosExistInMyPlaylist (url: string, token: string, videoIds: number[]) {
  242. const path = '/api/v1/users/me/video-playlists/videos-exist'
  243. return makeGetRequest({
  244. url,
  245. token,
  246. path,
  247. query: { videoIds },
  248. statusCodeExpected: HttpStatusCode.OK_200
  249. })
  250. }
  251. // ---------------------------------------------------------------------------
  252. export {
  253. getVideoPlaylistPrivacies,
  254. getVideoPlaylistsList,
  255. getVideoChannelPlaylistsList,
  256. getAccountPlaylistsList,
  257. getAccountPlaylistsListWithToken,
  258. getVideoPlaylist,
  259. getVideoPlaylistWithToken,
  260. createVideoPlaylist,
  261. updateVideoPlaylist,
  262. deleteVideoPlaylist,
  263. addVideoInPlaylist,
  264. updateVideoPlaylistElement,
  265. removeVideoFromPlaylist,
  266. reorderVideosPlaylist,
  267. checkPlaylistFilesWereRemoved,
  268. doVideosExistInMyPlaylist
  269. }