video-playlists.ts 7.5 KB

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