playlists-command.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. import { omit, pick } from '@shared/core-utils'
  2. import {
  3. BooleanBothQuery,
  4. HttpStatusCode,
  5. ResultList,
  6. VideoExistInPlaylist,
  7. VideoPlaylist,
  8. VideoPlaylistCreate,
  9. VideoPlaylistCreateResult,
  10. VideoPlaylistElement,
  11. VideoPlaylistElementCreate,
  12. VideoPlaylistElementCreateResult,
  13. VideoPlaylistElementUpdate,
  14. VideoPlaylistReorder,
  15. VideoPlaylistType,
  16. VideoPlaylistUpdate
  17. } from '@shared/models'
  18. import { unwrapBody } from '../requests'
  19. import { AbstractCommand, OverrideCommandOptions } from '../shared'
  20. export class PlaylistsCommand extends AbstractCommand {
  21. list (options: OverrideCommandOptions & {
  22. start?: number
  23. count?: number
  24. sort?: string
  25. }) {
  26. const path = '/api/v1/video-playlists'
  27. const query = pick(options, [ 'start', 'count', 'sort' ])
  28. return this.getRequestBody<ResultList<VideoPlaylist>>({
  29. ...options,
  30. path,
  31. query,
  32. implicitToken: false,
  33. defaultExpectedStatus: HttpStatusCode.OK_200
  34. })
  35. }
  36. listByChannel (options: OverrideCommandOptions & {
  37. handle: string
  38. start?: number
  39. count?: number
  40. sort?: string
  41. }) {
  42. const path = '/api/v1/video-channels/' + options.handle + '/video-playlists'
  43. const query = pick(options, [ 'start', 'count', 'sort' ])
  44. return this.getRequestBody<ResultList<VideoPlaylist>>({
  45. ...options,
  46. path,
  47. query,
  48. implicitToken: false,
  49. defaultExpectedStatus: HttpStatusCode.OK_200
  50. })
  51. }
  52. listByAccount (options: OverrideCommandOptions & {
  53. handle: string
  54. start?: number
  55. count?: number
  56. sort?: string
  57. search?: string
  58. playlistType?: VideoPlaylistType
  59. }) {
  60. const path = '/api/v1/accounts/' + options.handle + '/video-playlists'
  61. const query = pick(options, [ 'start', 'count', 'sort', 'search', 'playlistType' ])
  62. return this.getRequestBody<ResultList<VideoPlaylist>>({
  63. ...options,
  64. path,
  65. query,
  66. implicitToken: false,
  67. defaultExpectedStatus: HttpStatusCode.OK_200
  68. })
  69. }
  70. get (options: OverrideCommandOptions & {
  71. playlistId: number | string
  72. }) {
  73. const { playlistId } = options
  74. const path = '/api/v1/video-playlists/' + playlistId
  75. return this.getRequestBody<VideoPlaylist>({
  76. ...options,
  77. path,
  78. implicitToken: false,
  79. defaultExpectedStatus: HttpStatusCode.OK_200
  80. })
  81. }
  82. listVideos (options: OverrideCommandOptions & {
  83. playlistId: number | string
  84. start?: number
  85. count?: number
  86. query?: { nsfw?: BooleanBothQuery }
  87. }) {
  88. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
  89. const query = options.query ?? {}
  90. return this.getRequestBody<ResultList<VideoPlaylistElement>>({
  91. ...options,
  92. path,
  93. query: {
  94. ...query,
  95. start: options.start,
  96. count: options.count
  97. },
  98. implicitToken: true,
  99. defaultExpectedStatus: HttpStatusCode.OK_200
  100. })
  101. }
  102. delete (options: OverrideCommandOptions & {
  103. playlistId: number | string
  104. }) {
  105. const path = '/api/v1/video-playlists/' + options.playlistId
  106. return this.deleteRequest({
  107. ...options,
  108. path,
  109. implicitToken: true,
  110. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  111. })
  112. }
  113. async create (options: OverrideCommandOptions & {
  114. attributes: VideoPlaylistCreate
  115. }) {
  116. const path = '/api/v1/video-playlists'
  117. const fields = omit(options.attributes, [ 'thumbnailfile' ])
  118. const attaches = options.attributes.thumbnailfile
  119. ? { thumbnailfile: options.attributes.thumbnailfile }
  120. : {}
  121. const body = await unwrapBody<{ videoPlaylist: VideoPlaylistCreateResult }>(this.postUploadRequest({
  122. ...options,
  123. path,
  124. fields,
  125. attaches,
  126. implicitToken: true,
  127. defaultExpectedStatus: HttpStatusCode.OK_200
  128. }))
  129. return body.videoPlaylist
  130. }
  131. update (options: OverrideCommandOptions & {
  132. attributes: VideoPlaylistUpdate
  133. playlistId: number | string
  134. }) {
  135. const path = '/api/v1/video-playlists/' + options.playlistId
  136. const fields = omit(options.attributes, [ 'thumbnailfile' ])
  137. const attaches = options.attributes.thumbnailfile
  138. ? { thumbnailfile: options.attributes.thumbnailfile }
  139. : {}
  140. return this.putUploadRequest({
  141. ...options,
  142. path,
  143. fields,
  144. attaches,
  145. implicitToken: true,
  146. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  147. })
  148. }
  149. async addElement (options: OverrideCommandOptions & {
  150. playlistId: number | string
  151. attributes: VideoPlaylistElementCreate | { videoId: string }
  152. }) {
  153. const attributes = {
  154. ...options.attributes,
  155. videoId: await this.server.videos.getId({ ...options, uuid: options.attributes.videoId })
  156. }
  157. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
  158. const body = await unwrapBody<{ videoPlaylistElement: VideoPlaylistElementCreateResult }>(this.postBodyRequest({
  159. ...options,
  160. path,
  161. fields: attributes,
  162. implicitToken: true,
  163. defaultExpectedStatus: HttpStatusCode.OK_200
  164. }))
  165. return body.videoPlaylistElement
  166. }
  167. updateElement (options: OverrideCommandOptions & {
  168. playlistId: number | string
  169. elementId: number | string
  170. attributes: VideoPlaylistElementUpdate
  171. }) {
  172. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.elementId
  173. return this.putBodyRequest({
  174. ...options,
  175. path,
  176. fields: options.attributes,
  177. implicitToken: true,
  178. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  179. })
  180. }
  181. removeElement (options: OverrideCommandOptions & {
  182. playlistId: number | string
  183. elementId: number
  184. }) {
  185. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.elementId
  186. return this.deleteRequest({
  187. ...options,
  188. path,
  189. implicitToken: true,
  190. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  191. })
  192. }
  193. reorderElements (options: OverrideCommandOptions & {
  194. playlistId: number | string
  195. attributes: VideoPlaylistReorder
  196. }) {
  197. const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/reorder'
  198. return this.postBodyRequest({
  199. ...options,
  200. path,
  201. fields: options.attributes,
  202. implicitToken: true,
  203. defaultExpectedStatus: HttpStatusCode.NO_CONTENT_204
  204. })
  205. }
  206. getPrivacies (options: OverrideCommandOptions = {}) {
  207. const path = '/api/v1/video-playlists/privacies'
  208. return this.getRequestBody<{ [ id: number ]: string }>({
  209. ...options,
  210. path,
  211. implicitToken: false,
  212. defaultExpectedStatus: HttpStatusCode.OK_200
  213. })
  214. }
  215. videosExist (options: OverrideCommandOptions & {
  216. videoIds: number[]
  217. }) {
  218. const { videoIds } = options
  219. const path = '/api/v1/users/me/video-playlists/videos-exist'
  220. return this.getRequestBody<VideoExistInPlaylist>({
  221. ...options,
  222. path,
  223. query: { videoIds },
  224. implicitToken: true,
  225. defaultExpectedStatus: HttpStatusCode.OK_200
  226. })
  227. }
  228. }