PeerTube/shared/utils/videos/video-playlists.ts

186 lines
4.4 KiB
TypeScript
Raw Normal View History

2019-02-26 10:55:40 +01:00
import { makeDeleteRequest, makeGetRequest, makePostBodyRequest, makePutBodyRequest, makeUploadRequest } from '../requests/requests'
import { VideoPlaylistCreate } from '../../models/videos/playlist/video-playlist-create.model'
import { omit } from 'lodash'
import { VideoPlaylistUpdate } from '../../models/videos/playlist/video-playlist-update.model'
import { VideoPlaylistElementCreate } from '../../models/videos/playlist/video-playlist-element-create.model'
import { VideoPlaylistElementUpdate } from '../../models/videos/playlist/video-playlist-element-update.model'
2019-01-29 08:37:25 +01:00
2019-02-26 10:55:40 +01:00
function getVideoPlaylistsList (url: string, start: number, count: number, sort?: string) {
const path = '/api/v1/video-playlists'
const query = {
start,
count,
sort
}
return makeGetRequest({
url,
path,
query
})
2019-01-29 08:37:25 +01:00
}
2019-02-26 10:55:40 +01:00
function getVideoPlaylist (url: string, playlistId: number | string, statusCodeExpected = 200) {
const path = '/api/v1/video-playlists/' + playlistId
return makeGetRequest({
url,
path,
statusCodeExpected
})
2019-01-29 08:37:25 +01:00
}
2019-02-26 10:55:40 +01:00
function deleteVideoPlaylist (url: string, token: string, playlistId: number | string, statusCodeExpected = 200) {
const path = '/api/v1/video-playlists/' + playlistId
return makeDeleteRequest({
url,
path,
token,
statusCodeExpected
})
2019-01-29 08:37:25 +01:00
}
2019-02-26 10:55:40 +01:00
function createVideoPlaylist (options: {
url: string,
token: string,
playlistAttrs: VideoPlaylistCreate,
expectedStatus: number
}) {
const path = '/api/v1/video-playlists/'
const fields = omit(options.playlistAttrs, 'thumbnailfile')
2019-02-26 10:55:40 +01:00
const attaches = options.playlistAttrs.thumbnailfile
? { thumbnailfile: options.playlistAttrs.thumbnailfile }
: {}
2019-02-26 10:55:40 +01:00
return makeUploadRequest({
method: 'POST',
url: options.url,
path,
token: options.token,
fields,
attaches,
statusCodeExpected: options.expectedStatus
})
}
2019-02-26 10:55:40 +01:00
function updateVideoPlaylist (options: {
url: string,
token: string,
playlistAttrs: VideoPlaylistUpdate,
expectedStatus: number
}) {
const path = '/api/v1/video-playlists/'
2019-02-26 10:55:40 +01:00
const fields = omit(options.playlistAttrs, 'thumbnailfile')
2019-02-26 10:55:40 +01:00
const attaches = options.playlistAttrs.thumbnailfile
? { thumbnailfile: options.playlistAttrs.thumbnailfile }
: {}
2019-02-26 10:55:40 +01:00
return makeUploadRequest({
method: 'PUT',
url: options.url,
path,
token: options.token,
fields,
attaches,
statusCodeExpected: options.expectedStatus
})
}
function addVideoInPlaylist (options: {
url: string,
token: string,
playlistId: number | string,
elementAttrs: VideoPlaylistElementCreate
expectedStatus: number
}) {
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
return makePostBodyRequest({
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
statusCodeExpected: options.expectedStatus
})
}
function updateVideoPlaylistElement (options: {
url: string,
token: string,
playlistId: number | string,
videoId: number | string,
elementAttrs: VideoPlaylistElementUpdate,
expectedStatus: number
}) {
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId
return makePutBodyRequest({
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
statusCodeExpected: options.expectedStatus
})
}
function removeVideoFromPlaylist (options: {
url: string,
token: string,
playlistId: number | string,
videoId: number | string,
expectedStatus: number
}) {
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos/' + options.videoId
return makeDeleteRequest({
url: options.url,
path,
token: options.token,
statusCodeExpected: options.expectedStatus
})
}
function reorderVideosPlaylist (options: {
url: string,
token: string,
playlistId: number | string,
elementAttrs: {
startPosition: number,
insertAfter: number,
reorderLength?: number
},
expectedStatus: number
}) {
const path = '/api/v1/video-playlists/' + options.playlistId + '/videos'
return makePutBodyRequest({
url: options.url,
path,
token: options.token,
fields: options.elementAttrs,
statusCodeExpected: options.expectedStatus
})
}
2019-01-29 08:37:25 +01:00
// ---------------------------------------------------------------------------
export {
2019-02-26 10:55:40 +01:00
getVideoPlaylistsList,
getVideoPlaylist,
createVideoPlaylist,
updateVideoPlaylist,
deleteVideoPlaylist,
addVideoInPlaylist,
removeVideoFromPlaylist,
reorderVideosPlaylist
2019-01-29 08:37:25 +01:00
}