video-playlists.ts 7.4 KB

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