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

431 lines
14 KiB
TypeScript
Raw Normal View History

import { Observable } from 'rxjs'
import { catchError, map, switchMap } from 'rxjs/operators'
import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
2017-12-01 18:56:26 +01:00
import { Injectable } from '@angular/core'
import { ComponentPaginationLight, RestExtractor, RestService, ServerService, UserService } from '@app/core'
2020-06-23 14:10:17 +02:00
import { objectToFormData } from '@app/helpers'
2018-09-04 16:21:07 +02:00
import {
2020-06-23 14:10:17 +02:00
FeedFormat,
NSFWPolicyType,
ResultList,
2018-09-04 16:21:07 +02:00
UserVideoRate,
2018-11-14 15:01:28 +01:00
UserVideoRateType,
2018-09-04 16:21:07 +02:00
UserVideoRateUpdate,
2020-06-23 14:10:17 +02:00
Video as VideoServerModel,
2018-09-04 16:21:07 +02:00
VideoConstant,
2020-06-23 14:10:17 +02:00
VideoDetails as VideoDetailsServerModel,
VideoFileMetadata,
2018-09-04 16:21:07 +02:00
VideoFilter,
VideoPrivacy,
2020-06-23 14:10:17 +02:00
VideoSortField,
2020-11-09 16:25:27 +01:00
VideoUpdate
2020-06-23 14:10:17 +02:00
} from '@shared/models'
import { environment } from '../../../../environments/environment'
2020-08-14 17:28:54 +02:00
import { Account } from '../account/account.model'
import { AccountService } from '../account/account.service'
2020-06-23 14:10:17 +02:00
import { VideoChannel, VideoChannelService } from '../video-channel'
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'
2016-03-14 13:50:19 +01:00
export interface VideosProvider {
getVideos (parameters: {
videoPagination: ComponentPaginationLight,
sort: VideoSortField,
filter?: VideoFilter,
2020-06-16 11:00:35 +02:00
categoryOneOf?: number[],
languageOneOf?: string[]
2020-06-16 11:00:35 +02:00
nsfwPolicy: NSFWPolicyType
2019-07-22 15:40:13 +02:00
}): Observable<ResultList<Video>>
}
2016-03-14 13:50:19 +01:00
@Injectable()
export class VideoService implements VideosProvider {
2018-07-12 19:02:00 +02:00
static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
2020-11-09 16:25:27 +01:00
static BASE_SUBSCRIPTION_FEEDS_URL = environment.apiUrl + '/feeds/subscriptions.'
2016-03-14 13:50:19 +01:00
constructor (
private authHttp: HttpClient,
private restExtractor: RestExtractor,
2018-06-06 16:46:42 +02:00
private restService: RestService,
2020-11-09 16:25:27 +01:00
private serverService: ServerService
2016-05-27 17:49:18 +02:00
) {}
getVideoViewUrl (uuid: string) {
return VideoService.BASE_VIDEO_URL + uuid + '/views'
}
2018-10-05 11:15:06 +02:00
getUserWatchingVideoUrl (uuid: string) {
return VideoService.BASE_VIDEO_URL + uuid + '/watching'
}
2019-07-22 15:40:13 +02:00
getVideo (options: { videoId: string }): Observable<VideoDetails> {
2019-12-18 15:31:54 +01:00
return this.serverService.getServerLocale()
2018-05-15 11:55:51 +02:00
.pipe(
2018-06-06 16:46:42 +02:00
switchMap(translations => {
2019-07-22 15:40:13 +02:00
return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
2018-06-06 17:37:13 +02:00
.pipe(map(videoHash => ({ videoHash, translations })))
2018-06-06 16:46:42 +02:00
}),
map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2016-05-27 17:49:18 +02:00
}
2016-03-14 13:50:19 +01:00
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
2018-06-18 10:24:53 +02:00
const scheduleUpdate = video.scheduleUpdate || null
2019-01-12 14:45:23 +01:00
const originallyPublishedAt = video.originallyPublishedAt || 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,
waitTranscoding: video.waitTranscoding,
commentsEnabled: video.commentsEnabled,
downloadEnabled: video.downloadEnabled,
thumbnailfile: video.thumbnailfile,
previewfile: video.previewfile,
pluginData: video.pluginData,
2019-01-12 14:45:23 +01:00
scheduleUpdate,
originallyPublishedAt
}
const data = objectToFormData(body)
return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
2018-05-15 11:55:51 +02:00
.pipe(
map(this.restExtractor.extractDataBool),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
}
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<{ video: { id: number, uuid: string } }>(req)
2018-07-09 15:56:02 +02:00
.pipe(catchError(err => this.restExtractor.handleError(err)))
}
getMyVideos (videoPagination: ComponentPaginationLight, sort: VideoSortField, search?: string): Observable<ResultList<Video>> {
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)
if (search) {
const filters = this.restService.parseQueryStringFilter(search, {
isLive: {
prefix: 'isLive:',
isBoolean: true
}
})
params = this.restService.addObjectParams(params, filters)
}
2016-03-14 13:50:19 +01:00
2018-06-06 16:46:42 +02:00
return this.authHttp
.get<ResultList<Video>>(UserService.BASE_USERS_URL + 'me/videos', { params })
2018-05-15 11:55:51 +02:00
.pipe(
2018-06-06 16:46:42 +02:00
switchMap(res => this.extractVideos(res)),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2016-03-14 13:50:19 +01:00
}
getAccountVideos (parameters: {
account: Pick<Account, 'nameWithHost'>,
videoPagination: ComponentPaginationLight,
2018-04-24 15:10:54 +02:00
sort: VideoSortField
nsfwPolicy?: NSFWPolicyType
videoFilter?: VideoFilter
search?: string
}): Observable<ResultList<Video>> {
const { account, videoPagination, sort, videoFilter, nsfwPolicy, search } = parameters
2018-04-24 15:10:54 +02:00
const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
if (nsfwPolicy) {
params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
}
if (videoFilter) {
params = params.set('filter', videoFilter)
}
if (search) {
params = params.set('search', search)
}
2018-04-24 15:10:54 +02:00
return this.authHttp
2018-06-06 16:46:42 +02:00
.get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
2018-05-15 11:55:51 +02:00
.pipe(
2018-06-06 16:46:42 +02:00
switchMap(res => this.extractVideos(res)),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2018-04-24 15:10:54 +02:00
}
getVideoChannelVideos (parameters: {
videoChannel: Pick<VideoChannel, 'nameWithHost'>,
videoPagination: ComponentPaginationLight,
2020-06-16 11:00:35 +02:00
sort: VideoSortField,
nsfwPolicy?: NSFWPolicyType
videoFilter?: VideoFilter
}): Observable<ResultList<Video>> {
const { videoChannel, videoPagination, sort, nsfwPolicy, videoFilter } = parameters
2018-04-25 16:56:13 +02:00
const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
let params = new HttpParams()
params = this.restService.addRestGetParams(params, pagination, sort)
2020-06-16 11:00:35 +02:00
if (nsfwPolicy) {
params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
}
if (videoFilter) {
params = params.set('filter', videoFilter)
}
2018-04-25 16:56:13 +02:00
return this.authHttp
2018-08-24 11:04:02 +02:00
.get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
2018-05-15 11:55:51 +02:00
.pipe(
2018-06-06 16:46:42 +02:00
switchMap(res => this.extractVideos(res)),
2018-08-21 16:18:59 +02:00
catchError(err => this.restExtractor.handleError(err))
)
}
getVideos (parameters: {
videoPagination: ComponentPaginationLight,
2018-04-17 10:56:27 +02:00
sort: VideoSortField,
filter?: VideoFilter,
2020-06-16 11:00:35 +02:00
categoryOneOf?: number[],
languageOneOf?: string[],
skipCount?: boolean,
2020-06-16 11:00:35 +02:00
nsfwPolicy?: NSFWPolicyType
2019-07-22 15:40:13 +02:00
}): Observable<ResultList<Video>> {
2020-06-16 11:00:35 +02:00
const { videoPagination, sort, filter, categoryOneOf, languageOneOf, skipCount, nsfwPolicy } = parameters
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)
if (filter) params = params.set('filter', filter)
if (skipCount) params = params.set('skipCount', skipCount + '')
2020-06-16 11:00:35 +02:00
if (nsfwPolicy) {
params = params.set('nsfw', this.nsfwPolicyToParam(nsfwPolicy))
}
if (languageOneOf) {
for (const l of languageOneOf) {
params = params.append('languageOneOf[]', l)
}
}
2020-06-16 11:00:35 +02:00
if (categoryOneOf) {
for (const c of categoryOneOf) {
params = params.append('categoryOneOf[]', c + '')
}
}
2017-10-31 11:52:52 +01:00
return this.authHttp
2018-06-06 16:46:42 +02:00
.get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
2018-05-15 11:55:51 +02:00
.pipe(
2018-06-06 16:46:42 +02:00
switchMap(res => this.extractVideos(res)),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2017-10-31 11:52:52 +01:00
}
2020-11-09 16:25:27 +01:00
buildBaseFeedUrls (params: HttpParams, base = VideoService.BASE_FEEDS_URL) {
2018-04-17 10:35:08 +02:00
const feeds = [
{
2018-09-26 09:39:41 +02:00
format: FeedFormat.RSS,
2020-07-02 16:25:16 +02:00
label: 'media rss 2.0',
2020-11-09 16:25:27 +01:00
url: base + FeedFormat.RSS.toLowerCase()
2018-04-17 10:35:08 +02:00
},
{
2018-09-26 09:39:41 +02:00
format: FeedFormat.ATOM,
2018-04-17 10:35:08 +02:00
label: 'atom 1.0',
2020-11-09 16:25:27 +01:00
url: base + FeedFormat.ATOM.toLowerCase()
2018-04-17 10:35:08 +02:00
},
{
2018-09-26 09:39:41 +02:00
format: FeedFormat.JSON,
2018-04-17 10:35:08 +02:00
label: 'json 1.0',
2020-11-09 16:25:27 +01:00
url: base + FeedFormat.JSON.toLowerCase()
2018-04-17 10:35:08 +02:00
}
]
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
}
2020-06-16 11:00:35 +02:00
getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number[]) {
2018-04-17 10:56:27 +02:00
let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
2018-04-17 10:35:08 +02:00
if (filter) params = params.set('filter', filter)
2020-06-16 11:00:35 +02:00
if (categoryOneOf) {
for (const c of categoryOneOf) {
params = params.append('categoryOneOf[]', c + '')
}
}
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)
}
2020-11-09 16:25:27 +01:00
getVideoSubscriptionFeedUrls (accountId: number, feedToken: string) {
let params = this.restService.addRestGetParams(new HttpParams())
params = params.set('accountId', accountId.toString())
params = params.set('token', feedToken)
2020-11-09 16:25:27 +01:00
return this.buildBaseFeedUrls(params, VideoService.BASE_SUBSCRIPTION_FEEDS_URL)
}
getVideoFileMetadata (metadataUrl: string) {
return this.authHttp
2020-06-26 08:37:26 +02:00
.get<VideoFileMetadata>(metadataUrl)
.pipe(
catchError(err => this.restExtractor.handleError(err))
)
}
removeVideo (id: number) {
2017-10-31 11:52:52 +01:00
return this.authHttp
2018-05-15 11:55:51 +02:00
.delete(VideoService.BASE_VIDEO_URL + id)
.pipe(
map(this.restExtractor.extractDataBool),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
2016-05-27 17:49:18 +02:00
}
loadCompleteDescription (descriptionPath: string) {
return this.authHttp
2018-10-18 14:35:31 +02:00
.get<{ description: string }>(environment.apiUrl + descriptionPath)
2018-05-15 11:55:51 +02:00
.pipe(
2018-10-18 14:35:31 +02:00
map(res => res.description),
2018-07-09 15:56:02 +02:00
catchError(err => this.restExtractor.handleError(err))
2018-05-15 11:55:51 +02:00
)
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')
}
2018-05-16 11:00:57 +02:00
getUserVideoRating (id: number) {
const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
2017-03-08 21:35:43 +01:00
2018-05-16 11:00:57 +02:00
return this.authHttp.get<UserVideoRate>(url)
2018-07-09 15:56:02 +02:00
.pipe(catchError(err => this.restExtractor.handleError(err)))
2017-03-08 21:35:43 +01:00
}
2018-07-19 16:17:54 +02:00
extractVideos (result: ResultList<VideoServerModel>) {
2019-12-18 15:31:54 +01:00
return this.serverService.getServerLocale()
.pipe(
map(translations => {
const videosJson = result.data
const totalVideos = result.total
const videos: Video[] = []
for (const videoJson of videosJson) {
videos.push(new Video(videoJson, translations))
}
2019-07-22 15:40:13 +02:00
return { total: totalVideos, data: videos }
})
)
}
2018-07-19 16:17:54 +02:00
explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[], defaultPrivacyId = VideoPrivacy.PUBLIC) {
2019-12-12 15:47:47 +01:00
const base = [
{
id: VideoPrivacy.PRIVATE,
description: $localize`Only I can see this video`
2019-12-12 15:47:47 +01:00
},
{
id: VideoPrivacy.UNLISTED,
description: $localize`Only shareable via a private link`
2019-12-12 15:47:47 +01:00
},
{
id: VideoPrivacy.PUBLIC,
description: $localize`Anyone can see this video`
2019-12-12 15:47:47 +01:00
},
{
id: VideoPrivacy.INTERNAL,
description: $localize`Only users of this instance can see this video`
2019-12-12 15:47:47 +01:00
}
]
2018-09-04 16:21:07 +02:00
const videoPrivacies = base
.filter(o => !!privacies.find(p => p.id === o.id)) // filter down to privacies that where in the input
.map(o => ({ ...privacies[o.id - 1], ...o })) // merge the input privacies that contain a label, and extend them with a description
return {
defaultPrivacyId: videoPrivacies.find(p => p.id === defaultPrivacyId)?.id || videoPrivacies[0].id,
videoPrivacies
}
2018-09-04 16:21:07 +02:00
}
2020-06-16 11:00:35 +02:00
nsfwPolicyToParam (nsfwPolicy: NSFWPolicyType) {
return nsfwPolicy === 'do_not_list'
? 'false'
: 'both'
}
2018-11-14 15:01:28 +01:00
private setVideoRate (id: number, rateType: UserVideoRateType) {
2018-07-19 16:17:54 +02:00
const url = VideoService.BASE_VIDEO_URL + id + '/rate'
const body: UserVideoRateUpdate = {
rating: rateType
}
return this.authHttp
.put(url, body)
.pipe(
map(this.restExtractor.extractDataBool),
catchError(err => this.restExtractor.handleError(err))
)
}
2016-03-14 13:50:19 +01:00
}