]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/shared/video/video.service.ts
Add internal privacy mode
[github/Chocobozzz/PeerTube.git] / client / src / app / shared / video / video.service.ts
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'
7 import {
8 UserVideoRate,
9 UserVideoRateType,
10 UserVideoRateUpdate,
11 VideoConstant,
12 VideoFilter,
13 VideoPrivacy,
14 VideoUpdate
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'
34
35 export interface VideosProvider {
36 getVideos (parameters: {
37 videoPagination: ComponentPagination,
38 sort: VideoSortField,
39 filter?: VideoFilter,
40 categoryOneOf?: number,
41 languageOneOf?: string[]
42 }): Observable<ResultList<Video>>
43 }
44
45 @Injectable()
46 export class VideoService implements VideosProvider {
47 static BASE_VIDEO_URL = environment.apiUrl + '/api/v1/videos/'
48 static BASE_FEEDS_URL = environment.apiUrl + '/feeds/videos.'
49
50 constructor (
51 private authHttp: HttpClient,
52 private restExtractor: RestExtractor,
53 private restService: RestService,
54 private serverService: ServerService,
55 private i18n: I18n
56 ) {}
57
58 getVideoViewUrl (uuid: string) {
59 return VideoService.BASE_VIDEO_URL + uuid + '/views'
60 }
61
62 getUserWatchingVideoUrl (uuid: string) {
63 return VideoService.BASE_VIDEO_URL + uuid + '/watching'
64 }
65
66 getVideo (options: { videoId: string }): Observable<VideoDetails> {
67 return this.serverService.localeObservable
68 .pipe(
69 switchMap(translations => {
70 return this.authHttp.get<VideoDetailsServerModel>(VideoService.BASE_VIDEO_URL + options.videoId)
71 .pipe(map(videoHash => ({ videoHash, translations })))
72 }),
73 map(({ videoHash, translations }) => new VideoDetails(videoHash, translations)),
74 catchError(err => this.restExtractor.handleError(err))
75 )
76 }
77
78 updateVideo (video: VideoEdit) {
79 const language = video.language || null
80 const licence = video.licence || null
81 const category = video.category || null
82 const description = video.description || null
83 const support = video.support || null
84 const scheduleUpdate = video.scheduleUpdate || null
85 const originallyPublishedAt = video.originallyPublishedAt || null
86
87 const body: VideoUpdate = {
88 name: video.name,
89 category,
90 licence,
91 language,
92 support,
93 description,
94 channelId: video.channelId,
95 privacy: video.privacy,
96 tags: video.tags,
97 nsfw: video.nsfw,
98 waitTranscoding: video.waitTranscoding,
99 commentsEnabled: video.commentsEnabled,
100 downloadEnabled: video.downloadEnabled,
101 thumbnailfile: video.thumbnailfile,
102 previewfile: video.previewfile,
103 scheduleUpdate,
104 originallyPublishedAt
105 }
106
107 const data = objectToFormData(body)
108
109 return this.authHttp.put(VideoService.BASE_VIDEO_URL + video.id, data)
110 .pipe(
111 map(this.restExtractor.extractDataBool),
112 catchError(err => this.restExtractor.handleError(err))
113 )
114 }
115
116 uploadVideo (video: FormData) {
117 const req = new HttpRequest('POST', VideoService.BASE_VIDEO_URL + 'upload', video, { reportProgress: true })
118
119 return this.authHttp
120 .request<{ video: { id: number, uuid: string } }>(req)
121 .pipe(catchError(err => this.restExtractor.handleError(err)))
122 }
123
124 getMyVideos (videoPagination: ComponentPagination, sort: VideoSortField): Observable<ResultList<Video>> {
125 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
126
127 let params = new HttpParams()
128 params = this.restService.addRestGetParams(params, pagination, sort)
129
130 return this.authHttp
131 .get<ResultList<Video>>(UserService.BASE_USERS_URL + '/me/videos', { params })
132 .pipe(
133 switchMap(res => this.extractVideos(res)),
134 catchError(err => this.restExtractor.handleError(err))
135 )
136 }
137
138 getAccountVideos (
139 account: Account,
140 videoPagination: ComponentPagination,
141 sort: VideoSortField
142 ): Observable<ResultList<Video>> {
143 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
144
145 let params = new HttpParams()
146 params = this.restService.addRestGetParams(params, pagination, sort)
147
148 return this.authHttp
149 .get<ResultList<Video>>(AccountService.BASE_ACCOUNT_URL + account.nameWithHost + '/videos', { params })
150 .pipe(
151 switchMap(res => this.extractVideos(res)),
152 catchError(err => this.restExtractor.handleError(err))
153 )
154 }
155
156 getVideoChannelVideos (
157 videoChannel: VideoChannel,
158 videoPagination: ComponentPagination,
159 sort: VideoSortField
160 ): Observable<ResultList<Video>> {
161 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
162
163 let params = new HttpParams()
164 params = this.restService.addRestGetParams(params, pagination, sort)
165
166 return this.authHttp
167 .get<ResultList<Video>>(VideoChannelService.BASE_VIDEO_CHANNEL_URL + videoChannel.nameWithHost + '/videos', { params })
168 .pipe(
169 switchMap(res => this.extractVideos(res)),
170 catchError(err => this.restExtractor.handleError(err))
171 )
172 }
173
174 getUserSubscriptionVideos (parameters: {
175 videoPagination: ComponentPagination,
176 sort: VideoSortField
177 }): Observable<ResultList<Video>> {
178 const { videoPagination, sort } = parameters
179 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
180
181 let params = new HttpParams()
182 params = this.restService.addRestGetParams(params, pagination, sort)
183
184 return this.authHttp
185 .get<ResultList<Video>>(UserSubscriptionService.BASE_USER_SUBSCRIPTIONS_URL + '/videos', { params })
186 .pipe(
187 switchMap(res => this.extractVideos(res)),
188 catchError(err => this.restExtractor.handleError(err))
189 )
190 }
191
192 getVideos (parameters: {
193 videoPagination: ComponentPagination,
194 sort: VideoSortField,
195 filter?: VideoFilter,
196 categoryOneOf?: number,
197 languageOneOf?: string[]
198 }): Observable<ResultList<Video>> {
199 const { videoPagination, sort, filter, categoryOneOf, languageOneOf } = parameters
200
201 const pagination = this.restService.componentPaginationToRestPagination(videoPagination)
202
203 let params = new HttpParams()
204 params = this.restService.addRestGetParams(params, pagination, sort)
205
206 if (filter) {
207 params = params.set('filter', filter)
208 }
209
210 if (categoryOneOf) {
211 params = params.set('categoryOneOf', categoryOneOf + '')
212 }
213
214 if (languageOneOf) {
215 for (const l of languageOneOf) {
216 params = params.append('languageOneOf[]', l)
217 }
218 }
219
220 return this.authHttp
221 .get<ResultList<Video>>(VideoService.BASE_VIDEO_URL, { params })
222 .pipe(
223 switchMap(res => this.extractVideos(res)),
224 catchError(err => this.restExtractor.handleError(err))
225 )
226 }
227
228 buildBaseFeedUrls (params: HttpParams) {
229 const feeds = [
230 {
231 format: FeedFormat.RSS,
232 label: 'rss 2.0',
233 url: VideoService.BASE_FEEDS_URL + FeedFormat.RSS.toLowerCase()
234 },
235 {
236 format: FeedFormat.ATOM,
237 label: 'atom 1.0',
238 url: VideoService.BASE_FEEDS_URL + FeedFormat.ATOM.toLowerCase()
239 },
240 {
241 format: FeedFormat.JSON,
242 label: 'json 1.0',
243 url: VideoService.BASE_FEEDS_URL + FeedFormat.JSON.toLowerCase()
244 }
245 ]
246
247 if (params && params.keys().length !== 0) {
248 for (const feed of feeds) {
249 feed.url += '?' + params.toString()
250 }
251 }
252
253 return feeds
254 }
255
256 getVideoFeedUrls (sort: VideoSortField, filter?: VideoFilter, categoryOneOf?: number) {
257 let params = this.restService.addRestGetParams(new HttpParams(), undefined, sort)
258
259 if (filter) params = params.set('filter', filter)
260
261 if (categoryOneOf) params = params.set('categoryOneOf', categoryOneOf + '')
262
263 return this.buildBaseFeedUrls(params)
264 }
265
266 getAccountFeedUrls (accountId: number) {
267 let params = this.restService.addRestGetParams(new HttpParams())
268 params = params.set('accountId', accountId.toString())
269
270 return this.buildBaseFeedUrls(params)
271 }
272
273 getVideoChannelFeedUrls (videoChannelId: number) {
274 let params = this.restService.addRestGetParams(new HttpParams())
275 params = params.set('videoChannelId', videoChannelId.toString())
276
277 return this.buildBaseFeedUrls(params)
278 }
279
280 removeVideo (id: number) {
281 return this.authHttp
282 .delete(VideoService.BASE_VIDEO_URL + id)
283 .pipe(
284 map(this.restExtractor.extractDataBool),
285 catchError(err => this.restExtractor.handleError(err))
286 )
287 }
288
289 loadCompleteDescription (descriptionPath: string) {
290 return this.authHttp
291 .get<{ description: string }>(environment.apiUrl + descriptionPath)
292 .pipe(
293 map(res => res.description),
294 catchError(err => this.restExtractor.handleError(err))
295 )
296 }
297
298 setVideoLike (id: number) {
299 return this.setVideoRate(id, 'like')
300 }
301
302 setVideoDislike (id: number) {
303 return this.setVideoRate(id, 'dislike')
304 }
305
306 unsetVideoLike (id: number) {
307 return this.setVideoRate(id, 'none')
308 }
309
310 getUserVideoRating (id: number) {
311 const url = UserService.BASE_USERS_URL + 'me/videos/' + id + '/rating'
312
313 return this.authHttp.get<UserVideoRate>(url)
314 .pipe(catchError(err => this.restExtractor.handleError(err)))
315 }
316
317 extractVideos (result: ResultList<VideoServerModel>) {
318 return this.serverService.localeObservable
319 .pipe(
320 map(translations => {
321 const videosJson = result.data
322 const totalVideos = result.total
323 const videos: Video[] = []
324
325 for (const videoJson of videosJson) {
326 videos.push(new Video(videoJson, translations))
327 }
328
329 return { total: totalVideos, data: videos }
330 })
331 )
332 }
333
334 explainedPrivacyLabels (privacies: VideoConstant<VideoPrivacy>[]) {
335 const base = [
336 {
337 id: VideoPrivacy.PRIVATE,
338 label: this.i18n('Only I can see this video')
339 },
340 {
341 id: VideoPrivacy.UNLISTED,
342 label: this.i18n('Only people with the private link can see this video')
343 },
344 {
345 id: VideoPrivacy.PUBLIC,
346 label: this.i18n('Anyone can see this video')
347 },
348 {
349 id: VideoPrivacy.INTERNAL,
350 label: this.i18n('Only users of this instance can see this video')
351 }
352 ]
353
354 return base.filter(o => !!privacies.find(p => p.id === o.id))
355 }
356
357 private setVideoRate (id: number, rateType: UserVideoRateType) {
358 const url = VideoService.BASE_VIDEO_URL + id + '/rate'
359 const body: UserVideoRateUpdate = {
360 rating: rateType
361 }
362
363 return this.authHttp
364 .put(url, body)
365 .pipe(
366 map(this.restExtractor.extractDataBool),
367 catchError(err => this.restExtractor.handleError(err))
368 )
369 }
370 }