1 import { catchError, map, switchMap } from 'rxjs/operators'
2 import { HttpClient, HttpParams, HttpRequest } from '@angular/common/http'
3 import { Injectable } from '@angular/core'
4 import { Observable } from 'rxjs'
5 import { Video as VideoServerModel, VideoDetails as VideoDetailsServerModel } from '../../../../../shared'
6 import { ResultList } from '../../../../../shared/models/result-list.model'
15 } from '../../../../../shared/models/videos'
16 import { FeedFormat } from '../../../../../shared/models/feeds/feed-format.enum'
17 import { environment } from '../../../environments/environment'
18 import { ComponentPagination } from '../rest/component-pagination.model'
19 import { RestExtractor } from '../rest/rest-extractor.service'
20 import { RestService } from '../rest/rest.service'
21 import { UserService } from '../users/user.service'
22 import { VideoSortField } from './sort-field.type'
23 import { VideoDetails } from './video-details.model'
24 import { VideoEdit } from './video-edit.model'
25 import { Video } from './video.model'
26 import { objectToFormData } from '@app/shared/misc/utils'
27 import { Account } from '@app/shared/account/account.model'
28 import { AccountService } from '@app/shared/account/account.service'
29 import { VideoChannelService } from '@app/shared/video-channel/video-channel.service'
30 import { ServerService } from '@app/core'
31 import { UserSubscriptionService } from '@app/shared/user-subscription/user-subscription.service'
32 import { VideoChannel } from '@app/shared/video-channel/video-channel.model'
33 import { I18n } from '@ngx-translate/i18n-polyfill'
35 export interface VideosProvider {
37 videoPagination: ComponentPagination,
40 categoryOneOf?: number
41 ): Observable<{ videos: Video[], totalVideos: number }>
45 export class VideoService implements VideosProvider {
46 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
47 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
50 private authHttp: HttpClient,
51 private restExtractor: RestExtractor,
52 private restService: RestService,
53 private serverService: ServerService,
57 getVideoViewUrl (uuid: string) {
58 return VideoService.BASE_VIDEO_URL + uuid + '/views'
61 getUserWatchingVideoUrl (uuid: string) {
62 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
65 getVideo (uuid: string): Observable<VideoDetails> {
66 return this.serverService.localeObservable
68 switchMap(translations => {
69 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + uuid)
70 .pipe(map(videoHash => ({ videoHash, translations })))
72 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
73 catchError(err => this.restExtractor.handleError(err))
77 updateVideo (video: VideoEdit) {
78 const language = video.language || null
79 const licence = video.licence || null
80 const category = video.category || null
81 const description = video.description || null
82 const support = video.support || null
83 const scheduleUpdate = video.scheduleUpdate || null
85 const body: VideoUpdate = {
92 channelId: video.channelId,
93 privacy: video.privacy,
96 waitTranscoding: video.waitTranscoding,
97 commentsEnabled: video.commentsEnabled,
98 thumbnailfile: video.thumbnailfile,
99 previewfile: video.previewfile,
103 const data = objectToFormData(body)
105 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
107 map(this.restExtractor.extractDataBool),
108 catchError(err => this.restExtractor.handleError(err))
112 uploadVideo (video: FormData) {
113 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
116 .request<{ video: { id: number, uuid: string } }>(req)
117 .pipe(catchError(err => this.restExtractor.handleError(err)))
120 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<{ videos: Video[], totalVideos: number }> {
121 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
123 let params = new HttpParams()
124 params = this.restService.addRestGetParams(params, pagination, sort)
127 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
129 switchMap(res => this.extractVideos(res)),
130 catchError(err => this.restExtractor.handleError(err))
136 videoPagination: ComponentPagination,
138 ): Observable<{ videos: Video[], totalVideos: number }> {
139 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
141 let params = new HttpParams()
142 params = this.restService.addRestGetParams(params, pagination, sort)
145 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
147 switchMap(res => this.extractVideos(res)),
148 catchError(err => this.restExtractor.handleError(err))
152 getVideoChannelVideos (
153 videoChannel: VideoChannel,
154 videoPagination: ComponentPagination,
156 ): Observable<{ videos: Video[], totalVideos: number }> {
157 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
159 let params = new HttpParams()
160 params = this.restService.addRestGetParams(params, pagination, sort)
163 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
165 switchMap(res => this.extractVideos(res)),
166 catchError(err => this.restExtractor.handleError(err))
170 getUserSubscriptionVideos (
171 videoPagination: ComponentPagination,
173 ): Observable<{ videos: Video[], totalVideos: number }> {
174 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
176 let params = new HttpParams()
177 params = this.restService.addRestGetParams(params, pagination, sort)
180 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
182 switchMap(res => this.extractVideos(res)),
183 catchError(err => this.restExtractor.handleError(err))
188 videoPagination: ComponentPagination,
189 sort: VideoSortField,
190 filter?: VideoFilter,
191 categoryOneOf?: number
192 ): Observable<{ videos: Video[], totalVideos: number }> {
193 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
195 let params = new HttpParams()
196 params = this.restService.addRestGetParams(params, pagination, sort)
199 params = params.set('filter', filter)
203 params = params.set('categoryOneOf', categoryOneOf + '')
207 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
209 switchMap(res => this.extractVideos(res)),
210 catchError(err => this.restExtractor.handleError(err))
214 buildBaseFeedUrls (params: HttpParams) {
217 format: FeedFormat.RSS,
219 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
222 format: FeedFormat.ATOM,
224 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
227 format: FeedFormat.JSON,
229 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
233 if (params && params.keys().length !== 0) {
234 for (const feed of feeds) {
235 feed.url += '?' + params.toString()
242 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
243 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
245 if (filter) params = params.set('filter', filter)
247 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
249 return this.buildBaseFeedUrls(params)
252 getAccountFeedUrls (accountId: number) {
253 let params = this.restService.addRestGetParams(new HttpParams())
254 params = params.set('accountId', accountId.toString())
256 return this.buildBaseFeedUrls(params)
259 getVideoChannelFeedUrls (videoChannelId: number) {
260 let params = this.restService.addRestGetParams(new HttpParams())
261 params = params.set('videoChannelId', videoChannelId.toString())
263 return this.buildBaseFeedUrls(params)
266 removeVideo (id: number) {
268 .delete(VideoService.BASE_VIDEO_URL + id)
270 map(this.restExtractor.extractDataBool),
271 catchError(err => this.restExtractor.handleError(err))
275 loadCompleteDescription (descriptionPath: string) {
277 .get(environment.apiUrl + descriptionPath)
279 map(res => res[ 'description' ]),
280 catchError(err => this.restExtractor.handleError(err))
284 setVideoLike (id: number) {
285 return this.setVideoRate(id, 'like')
288 setVideoDislike (id: number) {
289 return this.setVideoRate(id, 'dislike')
292 unsetVideoLike (id: number) {
293 return this.setVideoRate(id, 'none')
296 getUserVideoRating (id: number) {
297 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
299 return this.authHttp.get<UserVideoRate>(url)
300 .pipe(catchError(err => this.restExtractor.handleError(err)))
303 extractVideos (result: ResultList<VideoServerModel>) {
304 return this.serverService.localeObservable
306 map(translations => {
307 const videosJson = result.data
308 const totalVideos = result.total
309 const videos: Video[] = []
311 for (const videoJson of videosJson) {
312 videos.push(new Video(videoJson, translations))
315 return { videos, totalVideos }
320 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
321 const newPrivacies = privacies.slice()
323 const privatePrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PRIVATE)
324 if (privatePrivacy) privatePrivacy.label = this.i18n('Only I can see this video')
326 const unlistedPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.UNLISTED)
327 if (unlistedPrivacy) unlistedPrivacy.label = this.i18n('Only people with the private link can see this video')
329 const publicPrivacy = newPrivacies.find(p => p.id === VideoPrivacy.PUBLIC)
330 if (publicPrivacy) publicPrivacy.label = this.i18n('Anyone can see this video')
335 private setVideoRate (id: number, rateType: VideoRateType) {
336 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
337 const body: UserVideoRateUpdate = {
344 map(this.restExtractor.extractDataBool),
345 catchError(err => this.restExtractor.handleError(err))