PeerTube/client/src/app/shared/video/video.service.ts

281 lines
9.5 KiB
TypeScript
Raw Normal View History

2017-10-31 16:37:37 +01:00
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
2017-12-01 18:56:26 +01:00
import { Injectable } from '@angular/core'
import 'rxjs/add/operator/catch'
import 'rxjs/add/operator/map'
2017-12-01 18:56:26 +01:00
import { Observable } from 'rxjs/Observable'
import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
import { ResultList } from '../../../../../shared/models/result-list.model'
import { UserVideoRateUpdate } from '../../../../../shared/models/videos/user-video-rate-update.model'
import { UserVideoRate } from '../../../../../shared/models/videos/user-video-rate.model'
2018-03-13 10:24:28 +01:00
import { VideoFilter } from '../../../../../shared/models/videos/video-query.type'
import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
2017-12-01 18:56:26 +01:00
import { VideoRateType } from '../../../../../shared/models/videos/video-rate.type'
import { VideoUpdate } from '../../../../../shared/models/videos/video-update.model'
2017-12-11 17:36:46 +01:00
import { environment } from '../../../environments/environment'
2017-12-27 16:11:53 +01:00
import { ComponentPagination } from '../rest/component-pagination.model'
2017-12-01 18:56:26 +01:00
import { RestExtractor } from '../rest/rest-extractor.service'
import { RestService } from '../rest/rest.service'
import { UserService } from '../users/user.service'
2018-04-17 10:56:27 +02:00
import { VideoSortField } from './sort-field.type'
2017-10-25 16:43:19 +02:00
import { VideoDetails } from './video-details.model'
import { VideoEdit } from './video-edit.model'
2017-12-01 18:56:26 +01:00
import { Video } from './video.model'
import { objectToFormData } from '@app/shared/misc/utils'
2018-04-24 15:10:54 +02:00
import { Account } from '@app/shared/account/account.model'
import { AccountService } from '@app/shared/account/account.service'
2018-04-25 16:56:13 +02:00
import { VideoChannel } from '../../../../../shared/models/videos'
import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
2016-03-14 13:50:19 +01:00
@Injectable()
export class VideoService {
2017-12-11 17:36:46 +01:00
private static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
private static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
2016-03-14 13:50:19 +01:00
constructor (
private authHttp: HttpClient,
private restExtractor: RestExtractor,
private restService: RestService
2016-05-27 17:49:18 +02:00
) {}
getVideoViewUrl (uuid: string) {
return VideoService.BASE_VIDEO_URL + uuid + '/views'
}
getVideo (uuid: string): Observable<VideoDetails> {
2017-10-25 16:43:19 +02:00
return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
.map(videoHash => new VideoDetails(videoHash))
.catch((res) => this.restExtractor.handleError(res))
2016-05-27 17:49:18 +02:00
}
2016-03-14 13:50:19 +01:00
2017-11-30 09:21:11 +01:00
viewVideo (uuid: string): Observable<VideoDetails> {
return this.authHttp.post(this.getVideoViewUrl(uuid), {})
2017-11-30 09:21:11 +01:00
.map(this.restExtractor.extractDataBool)
.catch(this.restExtractor.handleError)
}
2017-10-25 16:43:19 +02:00
updateVideo (video: VideoEdit) {
const language = video.language || null
const licence = video.licence || null
const category = video.category || null
const description = video.description || null
const support = video.support || null
const body: VideoUpdate = {
name: video.name,
2017-12-08 08:39:15 +01:00
category,
licence,
language,
2018-02-21 08:49:05 +01:00
support,
2017-12-08 08:39:15 +01:00
description,
2018-05-11 15:10:13 +02:00
channelId: video.channelId,
2017-10-31 11:52:52 +01:00
privacy: video.privacy,
tags: video.tags,
2018-01-03 10:12:36 +01:00
nsfw: video.nsfw,
commentsEnabled: video.commentsEnabled,
thumbnailfile: video.thumbnailfile,
previewfile: video.previewfile
}
const data = objectToFormData(body)
return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
.map(this.restExtractor.extractDataBool)
.catch(this.restExtractor.handleError)
}
uploadVideo (video: FormData) {
const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
2017-10-31 11:52:52 +01:00
return this.authHttp
.request(req)
.catch(this.restExtractor.handleError)
}
2018-04-17 10:56:27 +02:00
getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number}> {
2017-12-27 16:11:53 +01:00
const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
2016-05-23 11:07:42 +02:00
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
2016-03-14 13:50:19 +01:00
2017-10-31 11:52:52 +01:00
return this.authHttp.get(UserService.BASE_USERS_URL + '/me/videos', { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
2016-03-14 13:50:19 +01:00
}
2018-04-24 15:10:54 +02:00
getAccountVideos (
account: Account,
videoPagination: ComponentPagination,
sort: VideoSortField
): Observable<{ videos: Video[], totalVideos: number}> {
const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
return this.authHttp
.get(AccountService.BASE_ACCOUNT_URL + account.id + '/videos', { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
}
2018-04-25 16:56:13 +02:00
getVideoChannelVideos (
videoChannel: VideoChannel,
videoPagination: ComponentPagination,
sort: VideoSortField
): Observable<{ videos: Video[], totalVideos: number}> {
const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
return this.authHttp
.get(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.uuid + '/videos', { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
}
2018-03-13 10:24:28 +01:00
getVideos (
videoPagination: ComponentPagination,
2018-04-17 10:56:27 +02:00
sort: VideoSortField,
2018-03-13 10:24:28 +01:00
filter?: VideoFilter
): Observable<{ videos: Video[], totalVideos: number}> {
2017-12-27 16:11:53 +01:00
const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
2017-10-31 11:52:52 +01:00
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
2018-03-13 10:24:28 +01:00
if (filter) {
params = params.set('filter', filter)
}
2017-10-31 11:52:52 +01:00
return this.authHttp
.get(VideoService.BASE_VIDEO_URL, { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
}
2018-04-17 10:56:27 +02:00
buildBaseFeedUrls (params: HttpParams) {
2018-04-17 10:35:08 +02:00
const feeds = [
{
label: 'rss 2.0',
url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
},
{
label: 'atom 1.0',
url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
},
{
label: 'json 1.0',
url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
}
]
2018-04-17 10:56:27 +02:00
if (params && params.keys().length !== 0) {
for (const feed of feeds) {
feed.url += '?' + params.toString()
}
}
2018-04-17 10:35:08 +02:00
return feeds
}
2018-04-17 10:56:27 +02:00
getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter) {
let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
2018-04-17 10:35:08 +02:00
if (filter) params = params.set('filter', filter)
2018-04-17 10:56:27 +02:00
return this.buildBaseFeedUrls(params)
}
2018-04-17 10:35:08 +02:00
getAccountFeedUrls (accountId: number) {
let params = this.restService.addRestGetParams(new HttpParams())
params = params.set('accountId', accountId.toString())
2018-04-17 10:35:08 +02:00
2018-04-17 10:56:27 +02:00
return this.buildBaseFeedUrls(params)
}
2018-04-25 16:56:13 +02:00
getVideoChannelFeedUrls (videoChannelId: number) {
let params = this.restService.addRestGetParams(new HttpParams())
params = params.set('videoChannelId', videoChannelId.toString())
return this.buildBaseFeedUrls(params)
}
2017-12-27 16:11:53 +01:00
searchVideos (
search: string,
videoPagination: ComponentPagination,
2018-04-17 10:56:27 +02:00
sort: VideoSortField
2017-12-27 16:11:53 +01:00
): Observable<{ videos: Video[], totalVideos: number}> {
2017-12-05 17:46:33 +01:00
const url = VideoService.BASE_VIDEO_URL + 'search'
2017-12-27 16:11:53 +01:00
const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
2017-12-05 17:46:33 +01:00
params = params.append('search', search)
2016-05-23 11:07:42 +02:00
2017-10-31 11:52:52 +01:00
return this.authHttp
.get<ResultList<VideoServerModel>>(url, { params })
.map(this.extractVideos)
.catch((res) => this.restExtractor.handleError(res))
}
removeVideo (id: number) {
2017-10-31 11:52:52 +01:00
return this.authHttp
.delete(VideoService.BASE_VIDEO_URL + id)
.map(this.restExtractor.extractDataBool)
.catch((res) => this.restExtractor.handleError(res))
2016-05-27 17:49:18 +02:00
}
loadCompleteDescription (descriptionPath: string) {
return this.authHttp
2017-12-11 17:36:46 +01:00
.get(environment.apiUrl + descriptionPath)
.map(res => res['description'])
.catch((res) => this.restExtractor.handleError(res))
2017-03-08 21:35:43 +01:00
}
setVideoLike (id: number) {
return this.setVideoRate(id, 'like')
2017-03-08 21:35:43 +01:00
}
setVideoDislike (id: number) {
return this.setVideoRate(id, 'dislike')
2017-03-08 21:35:43 +01:00
}
unsetVideoLike (id: number) {
return this.setVideoRate(id, 'none')
}
getUserVideoRating (id: number): Observable<UserVideoRate> {
const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
2017-03-08 21:35:43 +01:00
2017-10-31 11:52:52 +01:00
return this.authHttp
.get(url)
.catch(res => this.restExtractor.handleError(res))
2017-03-08 21:35:43 +01:00
}
private setVideoRate (id: number, rateType: VideoRateType) {
const url = VideoService.BASE_VIDEO_URL + id + '/rate'
const body: UserVideoRateUpdate = {
2017-03-08 21:35:43 +01:00
rating: rateType
}
2017-03-08 21:35:43 +01:00
2017-10-31 11:52:52 +01:00
return this.authHttp
.put(url, body)
.map(this.restExtractor.extractDataBool)
.catch(res => this.restExtractor.handleError(res))
2017-01-20 19:22:15 +01:00
}
private extractVideos (result: ResultList<VideoServerModel>) {
const videosJson = result.data
const totalVideos = result.total
const videos = []
for (const videoJson of videosJson) {
videos.push(new Video(videoJson))
}
return { videos, totalVideos }
}
2016-03-14 13:50:19 +01:00
}