]>
Commit | Line | Data |
---|---|---|
e972e046 | 1 | import { catchError } from 'rxjs/operators' |
3b492bff | 2 | import { ChangeDetectorRef, Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' |
df98563e | 3 | import { ActivatedRoute, Router } from '@angular/router' |
901637bb | 4 | import { RedirectService } from '@app/core/routing/redirect.service' |
0bd78bf3 | 5 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' |
07fa4c97 | 6 | import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component' |
1f3e9fec | 7 | import { MetaService } from '@ngx-meta/core' |
f8b2c1b4 | 8 | import { Notifier, ServerService } from '@app/core' |
16f7022b | 9 | import { forkJoin, Subscription } from 'rxjs' |
20d21199 | 10 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
e2f01c47 | 11 | import { UserVideoRateType, VideoCaption, VideoPlaylistPrivacy, VideoPrivacy, VideoState } from '../../../../../shared' |
df98563e | 12 | import { AuthService, ConfirmService } from '../../core' |
a51bad1a | 13 | import { RestExtractor, VideoBlacklistService } from '../../shared' |
ff249f49 | 14 | import { VideoDetails } from '../../shared/video/video-details.model' |
63c4db6d | 15 | import { VideoService } from '../../shared/video/video.service' |
4635f59d C |
16 | import { VideoDownloadComponent } from './modal/video-download.component' |
17 | import { VideoReportComponent } from './modal/video-report.component' | |
18 | import { VideoShareComponent } from './modal/video-share.component' | |
26b7305a | 19 | import { VideoBlacklistComponent } from './modal/video-blacklist.component' |
20d21199 | 20 | import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component' |
989e526a | 21 | import { I18n } from '@ngx-translate/i18n-polyfill' |
e945b184 | 22 | import { environment } from '../../../environments/environment' |
16f7022b | 23 | import { VideoCaptionService } from '@app/shared/video-caption' |
1506307f | 24 | import { MarkdownService } from '@app/shared/renderer' |
6ec0b75b C |
25 | import { |
26 | P2PMediaLoaderOptions, | |
27 | PeertubePlayerManager, | |
28 | PeertubePlayerManagerOptions, | |
597a9266 | 29 | PlayerMode |
6ec0b75b | 30 | } from '../../../assets/player/peertube-player-manager' |
e2f01c47 C |
31 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' |
32 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' | |
33 | import { ComponentPagination } from '@app/shared/rest/component-pagination.model' | |
34 | import { Video } from '@app/shared/video/video.model' | |
dc8bc31b | 35 | |
dc8bc31b C |
36 | @Component({ |
37 | selector: 'my-video-watch', | |
ec8d8440 C |
38 | templateUrl: './video-watch.component.html', |
39 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 40 | }) |
0629423c | 41 | export class VideoWatchComponent implements OnInit, OnDestroy { |
22b59e80 C |
42 | private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern' |
43 | ||
a96aed15 | 44 | @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent |
df98563e C |
45 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent |
46 | @ViewChild('videoReportModal') videoReportModal: VideoReportComponent | |
07fa4c97 | 47 | @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent |
26b7305a | 48 | @ViewChild('videoBlacklistModal') videoBlacklistModal: VideoBlacklistComponent |
20d21199 | 49 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
df98563e | 50 | |
2adfc7ea | 51 | player: any |
0826c92d | 52 | playerElement: HTMLVideoElement |
154898b0 | 53 | userRating: UserVideoRateType = null |
404b54e1 | 54 | video: VideoDetails = null |
80958c78 | 55 | descriptionLoading = false |
2de96f4d | 56 | |
e2f01c47 C |
57 | playlist: VideoPlaylist = null |
58 | playlistVideos: Video[] = [] | |
59 | playlistPagination: ComponentPagination = { | |
60 | currentPage: 1, | |
61 | itemsPerPage: 10, | |
62 | totalItems: null | |
63 | } | |
64 | noPlaylistVideos = false | |
65 | currentPlaylistPosition = 1 | |
66 | ||
2de96f4d C |
67 | completeDescriptionShown = false |
68 | completeVideoDescription: string | |
69 | shortVideoDescription: string | |
9d9597df | 70 | videoHTMLDescription = '' |
e9189001 | 71 | likesBarTooltipText = '' |
73e09f27 | 72 | hasAlreadyAcceptedPrivacyConcern = false |
6d88de72 | 73 | remoteServerDown = false |
20d21199 | 74 | hotkeys: Hotkey[] |
df98563e | 75 | |
f0a39880 | 76 | private currentTime: number |
df98563e | 77 | private paramsSub: Subscription |
e2f01c47 | 78 | private queryParamsSub: Subscription |
df98563e C |
79 | |
80 | constructor ( | |
4fd8aa32 | 81 | private elementRef: ElementRef, |
3b492bff | 82 | private changeDetector: ChangeDetectorRef, |
0629423c | 83 | private route: ActivatedRoute, |
92fb909c | 84 | private router: Router, |
d3ef341a | 85 | private videoService: VideoService, |
e2f01c47 | 86 | private playlistService: VideoPlaylistService, |
35bf0c83 | 87 | private videoBlacklistService: VideoBlacklistService, |
92fb909c | 88 | private confirmService: ConfirmService, |
3ec343a4 | 89 | private metaService: MetaService, |
7ddd02c9 | 90 | private authService: AuthService, |
0883b324 | 91 | private serverService: ServerService, |
a51bad1a | 92 | private restExtractor: RestExtractor, |
f8b2c1b4 | 93 | private notifier: Notifier, |
7ae71355 | 94 | private markdownService: MarkdownService, |
901637bb | 95 | private zone: NgZone, |
989e526a | 96 | private redirectService: RedirectService, |
16f7022b | 97 | private videoCaptionService: VideoCaptionService, |
e945b184 | 98 | private i18n: I18n, |
20d21199 | 99 | private hotkeysService: HotkeysService, |
e945b184 | 100 | @Inject(LOCALE_ID) private localeId: string |
d3ef341a | 101 | ) {} |
dc8bc31b | 102 | |
b2731bff C |
103 | get user () { |
104 | return this.authService.getUser() | |
105 | } | |
106 | ||
df98563e | 107 | ngOnInit () { |
0bd78bf3 | 108 | if ( |
c7ca4c8b | 109 | !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false || |
0bd78bf3 C |
110 | peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true' |
111 | ) { | |
2b3b76ab C |
112 | this.hasAlreadyAcceptedPrivacyConcern = true |
113 | } | |
114 | ||
13fc89f4 | 115 | this.paramsSub = this.route.params.subscribe(routeParams => { |
e2f01c47 C |
116 | const videoId = routeParams[ 'videoId' ] |
117 | if (videoId) this.loadVideo(videoId) | |
a51bad1a | 118 | |
e2f01c47 C |
119 | const playlistId = routeParams[ 'playlistId' ] |
120 | if (playlistId) this.loadPlaylist(playlistId) | |
121 | }) | |
bf079b7b | 122 | |
e2f01c47 C |
123 | this.queryParamsSub = this.route.queryParams.subscribe(queryParams => { |
124 | const videoId = queryParams[ 'videoId' ] | |
125 | if (videoId) this.loadVideo(videoId) | |
df98563e | 126 | }) |
20d21199 RK |
127 | |
128 | this.hotkeys = [ | |
a157b3a3 | 129 | new Hotkey('shift+l', (event: KeyboardEvent): boolean => { |
20d21199 RK |
130 | this.setLike() |
131 | return false | |
e33f888b | 132 | }, undefined, this.i18n('Like the video')), |
a157b3a3 | 133 | new Hotkey('shift+d', (event: KeyboardEvent): boolean => { |
20d21199 RK |
134 | this.setDislike() |
135 | return false | |
e33f888b | 136 | }, undefined, this.i18n('Dislike the video')), |
a157b3a3 | 137 | new Hotkey('shift+s', (event: KeyboardEvent): boolean => { |
20d21199 RK |
138 | this.subscribeButton.subscribed ? |
139 | this.subscribeButton.unsubscribe() : | |
140 | this.subscribeButton.subscribe() | |
141 | return false | |
e33f888b | 142 | }, undefined, this.i18n('Subscribe to the account')) |
20d21199 RK |
143 | ] |
144 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
d1992b93 C |
145 | } |
146 | ||
df98563e | 147 | ngOnDestroy () { |
09edde40 | 148 | this.flushPlayer() |
067e3f84 | 149 | |
13fc89f4 | 150 | // Unsubscribe subscriptions |
e2f01c47 C |
151 | if (this.paramsSub) this.paramsSub.unsubscribe() |
152 | if (this.queryParamsSub) this.queryParamsSub.unsubscribe() | |
20d21199 RK |
153 | |
154 | // Unbind hotkeys | |
155 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
dc8bc31b | 156 | } |
98b01bac | 157 | |
df98563e C |
158 | setLike () { |
159 | if (this.isUserLoggedIn() === false) return | |
57a49263 BB |
160 | if (this.userRating === 'like') { |
161 | // Already liked this video | |
162 | this.setRating('none') | |
163 | } else { | |
164 | this.setRating('like') | |
165 | } | |
d38b8281 C |
166 | } |
167 | ||
df98563e C |
168 | setDislike () { |
169 | if (this.isUserLoggedIn() === false) return | |
57a49263 BB |
170 | if (this.userRating === 'dislike') { |
171 | // Already disliked this video | |
172 | this.setRating('none') | |
173 | } else { | |
174 | this.setRating('dislike') | |
175 | } | |
d38b8281 C |
176 | } |
177 | ||
2de96f4d | 178 | showMoreDescription () { |
2de96f4d C |
179 | if (this.completeVideoDescription === undefined) { |
180 | return this.loadCompleteDescription() | |
181 | } | |
182 | ||
183 | this.updateVideoDescription(this.completeVideoDescription) | |
80958c78 | 184 | this.completeDescriptionShown = true |
2de96f4d C |
185 | } |
186 | ||
187 | showLessDescription () { | |
2de96f4d | 188 | this.updateVideoDescription(this.shortVideoDescription) |
80958c78 | 189 | this.completeDescriptionShown = false |
2de96f4d C |
190 | } |
191 | ||
192 | loadCompleteDescription () { | |
80958c78 C |
193 | this.descriptionLoading = true |
194 | ||
2de96f4d | 195 | this.videoService.loadCompleteDescription(this.video.descriptionPath) |
2186386c C |
196 | .subscribe( |
197 | description => { | |
198 | this.completeDescriptionShown = true | |
199 | this.descriptionLoading = false | |
200 | ||
201 | this.shortVideoDescription = this.video.description | |
202 | this.completeVideoDescription = description | |
203 | ||
204 | this.updateVideoDescription(this.completeVideoDescription) | |
205 | }, | |
206 | ||
207 | error => { | |
208 | this.descriptionLoading = false | |
f8b2c1b4 | 209 | this.notifier.error(error.message) |
2186386c C |
210 | } |
211 | ) | |
2de96f4d C |
212 | } |
213 | ||
df98563e C |
214 | showReportModal (event: Event) { |
215 | event.preventDefault() | |
216 | this.videoReportModal.show() | |
4f8c0eb0 C |
217 | } |
218 | ||
07fa4c97 C |
219 | showSupportModal () { |
220 | this.videoSupportModal.show() | |
221 | } | |
222 | ||
df98563e | 223 | showShareModal () { |
f0a39880 | 224 | this.videoShareModal.show(this.currentTime) |
99cc4f49 C |
225 | } |
226 | ||
a96aed15 | 227 | showDownloadModal (event: Event) { |
df98563e | 228 | event.preventDefault() |
a96aed15 | 229 | this.videoDownloadModal.show() |
99cc4f49 C |
230 | } |
231 | ||
26b7305a C |
232 | showBlacklistModal (event: Event) { |
233 | event.preventDefault() | |
234 | this.videoBlacklistModal.show() | |
235 | } | |
236 | ||
191764f3 C |
237 | async unblacklistVideo (event: Event) { |
238 | event.preventDefault() | |
239 | ||
240 | const confirmMessage = this.i18n( | |
241 | 'Do you really want to remove this video from the blacklist? It will be available again in the videos list.' | |
242 | ) | |
243 | ||
244 | const res = await this.confirmService.confirm(confirmMessage, this.i18n('Unblacklist')) | |
245 | if (res === false) return | |
246 | ||
247 | this.videoBlacklistService.removeVideoFromBlacklist(this.video.id).subscribe( | |
248 | () => { | |
f8b2c1b4 | 249 | this.notifier.success(this.i18n('Video {{name}} removed from the blacklist.', { name: this.video.name })) |
191764f3 C |
250 | |
251 | this.video.blacklisted = false | |
252 | this.video.blacklistedReason = null | |
253 | }, | |
254 | ||
f8b2c1b4 | 255 | err => this.notifier.error(err.message) |
191764f3 C |
256 | ) |
257 | } | |
258 | ||
df98563e C |
259 | isUserLoggedIn () { |
260 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
261 | } |
262 | ||
4635f59d C |
263 | isVideoUpdatable () { |
264 | return this.video.isUpdatableBy(this.authService.getUser()) | |
265 | } | |
266 | ||
df98563e | 267 | isVideoBlacklistable () { |
b2731bff | 268 | return this.video.isBlackistableBy(this.user) |
198b205c GS |
269 | } |
270 | ||
191764f3 C |
271 | isVideoUnblacklistable () { |
272 | return this.video.isUnblacklistableBy(this.user) | |
273 | } | |
274 | ||
b1fa3eba C |
275 | getVideoTags () { |
276 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
277 | ||
4278710d | 278 | return this.video.tags |
b1fa3eba C |
279 | } |
280 | ||
6725d05c C |
281 | isVideoRemovable () { |
282 | return this.video.isRemovableBy(this.authService.getUser()) | |
283 | } | |
284 | ||
1f30a185 | 285 | async removeVideo (event: Event) { |
6725d05c C |
286 | event.preventDefault() |
287 | ||
989e526a | 288 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete')) |
1f30a185 | 289 | if (res === false) return |
6725d05c | 290 | |
1f30a185 | 291 | this.videoService.removeVideo(this.video.id) |
2186386c | 292 | .subscribe( |
f8b2c1b4 C |
293 | () => { |
294 | this.notifier.success(this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name })) | |
2186386c C |
295 | |
296 | // Go back to the video-list. | |
297 | this.redirectService.redirectToHomepage() | |
298 | }, | |
299 | ||
f8b2c1b4 | 300 | error => this.notifier.error(error.message) |
2186386c | 301 | ) |
6725d05c C |
302 | } |
303 | ||
73e09f27 | 304 | acceptedPrivacyConcern () { |
0bd78bf3 | 305 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') |
73e09f27 C |
306 | this.hasAlreadyAcceptedPrivacyConcern = true |
307 | } | |
308 | ||
2186386c C |
309 | isVideoToTranscode () { |
310 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
311 | } | |
312 | ||
156c50af | 313 | isVideoDownloadable () { |
7f2cfe3a | 314 | return this.video && this.video.downloadEnabled |
156c50af LD |
315 | } |
316 | ||
516df59b C |
317 | isVideoToImport () { |
318 | return this.video && this.video.state.id === VideoState.TO_IMPORT | |
319 | } | |
320 | ||
bbe0f064 C |
321 | hasVideoScheduledPublication () { |
322 | return this.video && this.video.scheduledUpdate !== undefined | |
323 | } | |
324 | ||
e2f01c47 C |
325 | isVideoBlur (video: Video) { |
326 | return video.isVideoNSFWForUser(this.user, this.serverService.getConfig()) | |
327 | } | |
328 | ||
329 | isPlaylistOwned () { | |
330 | return this.playlist.isLocal === true && this.playlist.ownerAccount.name === this.user.username | |
331 | } | |
332 | ||
333 | isUnlistedPlaylist () { | |
334 | return this.playlist.privacy.id === VideoPlaylistPrivacy.UNLISTED | |
335 | } | |
336 | ||
337 | isPrivatePlaylist () { | |
338 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PRIVATE | |
339 | } | |
340 | ||
341 | isPublicPlaylist () { | |
342 | return this.playlist.privacy.id === VideoPlaylistPrivacy.PUBLIC | |
343 | } | |
344 | ||
345 | onPlaylistVideosNearOfBottom () { | |
346 | // Last page | |
347 | if (this.playlistPagination.totalItems <= (this.playlistPagination.currentPage * this.playlistPagination.itemsPerPage)) return | |
348 | ||
349 | this.playlistPagination.currentPage += 1 | |
350 | this.loadPlaylistElements(false) | |
351 | } | |
352 | ||
353 | onElementRemoved (video: Video) { | |
354 | this.playlistVideos = this.playlistVideos.filter(v => v.id !== video.id) | |
355 | ||
356 | this.playlistPagination.totalItems-- | |
357 | } | |
358 | ||
359 | private loadVideo (videoId: string) { | |
360 | // Video did not change | |
361 | if (this.video && this.video.uuid === videoId) return | |
362 | ||
363 | if (this.player) this.player.pause() | |
364 | ||
365 | // Video did change | |
366 | forkJoin( | |
367 | this.videoService.getVideo(videoId), | |
368 | this.videoCaptionService.listCaptions(videoId) | |
369 | ) | |
370 | .pipe( | |
371 | // If 401, the video is private or blacklisted so redirect to 404 | |
372 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
373 | ) | |
374 | .subscribe(([ video, captionsResult ]) => { | |
375 | const queryParams = this.route.snapshot.queryParams | |
376 | const startTime = queryParams.start | |
377 | const stopTime = queryParams.stop | |
378 | const subtitle = queryParams.subtitle | |
379 | const playerMode = queryParams.mode | |
380 | ||
381 | this.onVideoFetched(video, captionsResult.data, { startTime, stopTime, subtitle, playerMode }) | |
382 | .catch(err => this.handleError(err)) | |
383 | }) | |
384 | } | |
385 | ||
386 | private loadPlaylist (playlistId: string) { | |
387 | // Playlist did not change | |
388 | if (this.playlist && this.playlist.uuid === playlistId) return | |
389 | ||
390 | this.playlistService.getVideoPlaylist(playlistId) | |
391 | .pipe( | |
392 | // If 401, the video is private or blacklisted so redirect to 404 | |
393 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
394 | ) | |
395 | .subscribe(playlist => { | |
396 | this.playlist = playlist | |
397 | ||
398 | const videoId = this.route.snapshot.queryParams['videoId'] | |
399 | this.loadPlaylistElements(!videoId) | |
400 | }) | |
401 | } | |
402 | ||
403 | private loadPlaylistElements (redirectToFirst = false) { | |
404 | this.videoService.getPlaylistVideos(this.playlist.id, this.playlistPagination) | |
405 | .subscribe(({ totalVideos, videos }) => { | |
406 | this.playlistVideos = this.playlistVideos.concat(videos) | |
407 | this.playlistPagination.totalItems = totalVideos | |
408 | ||
409 | if (totalVideos === 0) { | |
410 | this.noPlaylistVideos = true | |
411 | return | |
412 | } | |
413 | ||
414 | this.updatePlaylistIndex() | |
415 | ||
416 | if (redirectToFirst) { | |
417 | const extras = { | |
418 | queryParams: { videoId: this.playlistVideos[ 0 ].uuid }, | |
419 | replaceUrl: true | |
420 | } | |
421 | this.router.navigate([], extras) | |
422 | } | |
423 | }) | |
424 | } | |
425 | ||
2de96f4d C |
426 | private updateVideoDescription (description: string) { |
427 | this.video.description = description | |
428 | this.setVideoDescriptionHTML() | |
429 | } | |
430 | ||
41d71344 C |
431 | private async setVideoDescriptionHTML () { |
432 | this.videoHTMLDescription = await this.markdownService.textMarkdownToHTML(this.video.description) | |
2de96f4d C |
433 | } |
434 | ||
e9189001 | 435 | private setVideoLikesBarTooltipText () { |
2186386c C |
436 | this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', { |
437 | likesNumber: this.video.likes, | |
438 | dislikesNumber: this.video.dislikes | |
439 | }) | |
e9189001 C |
440 | } |
441 | ||
0c31c33d C |
442 | private handleError (err: any) { |
443 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
444 | if (!errorMessage) return |
445 | ||
6d88de72 | 446 | // Display a message in the video player instead of a notification |
0f7fedc3 | 447 | if (errorMessage.indexOf('from xs param') !== -1) { |
6d88de72 C |
448 | this.flushPlayer() |
449 | this.remoteServerDown = true | |
3b492bff C |
450 | this.changeDetector.detectChanges() |
451 | ||
6d88de72 | 452 | return |
0c31c33d C |
453 | } |
454 | ||
f8b2c1b4 | 455 | this.notifier.error(errorMessage) |
0c31c33d C |
456 | } |
457 | ||
df98563e | 458 | private checkUserRating () { |
d38b8281 | 459 | // Unlogged users do not have ratings |
df98563e | 460 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
461 | |
462 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
463 | .subscribe( |
464 | ratingObject => { | |
465 | if (ratingObject) { | |
466 | this.userRating = ratingObject.rating | |
467 | } | |
468 | }, | |
469 | ||
f8b2c1b4 | 470 | err => this.notifier.error(err.message) |
2186386c | 471 | ) |
d38b8281 C |
472 | } |
473 | ||
597a9266 C |
474 | private async onVideoFetched ( |
475 | video: VideoDetails, | |
476 | videoCaptions: VideoCaption[], | |
f0a39880 | 477 | urlOptions: { startTime?: number, stopTime?: number, subtitle?: string, playerMode?: string } |
597a9266 | 478 | ) { |
df98563e | 479 | this.video = video |
92fb909c | 480 | |
c448d412 C |
481 | // Re init attributes |
482 | this.descriptionLoading = false | |
483 | this.completeDescriptionShown = false | |
6d88de72 | 484 | this.remoteServerDown = false |
f0a39880 | 485 | this.currentTime = undefined |
c448d412 | 486 | |
e2f01c47 C |
487 | this.updatePlaylistIndex() |
488 | ||
1b04f19c | 489 | let startTime = urlOptions.startTime || (this.video.userHistory ? this.video.userHistory.currentTime : 0) |
43483d12 | 490 | // If we are at the end of the video, reset the timer |
6e46de09 C |
491 | if (this.video.duration - startTime <= 1) startTime = 0 |
492 | ||
e2f01c47 | 493 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 494 | const res = await this.confirmService.confirm( |
989e526a C |
495 | this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'), |
496 | this.i18n('Mature or explicit content') | |
d6e32a2e | 497 | ) |
901637bb | 498 | if (res === false) return this.redirectService.redirectToHomepage() |
92fb909c C |
499 | } |
500 | ||
09edde40 C |
501 | // Flush old player if needed |
502 | this.flushPlayer() | |
b891f9bc C |
503 | |
504 | // Build video element, because videojs remove it on dispose | |
e2f01c47 | 505 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
506 | this.playerElement = document.createElement('video') |
507 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 508 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
509 | playerElementWrapper.appendChild(this.playerElement) |
510 | ||
16f7022b C |
511 | const playerCaptions = videoCaptions.map(c => ({ |
512 | label: c.language.label, | |
513 | language: c.language.id, | |
514 | src: environment.apiUrl + c.captionPath | |
515 | })) | |
516 | ||
6ec0b75b | 517 | const options: PeertubePlayerManagerOptions = { |
2adfc7ea C |
518 | common: { |
519 | autoplay: this.isAutoplay(), | |
6ec0b75b | 520 | |
2adfc7ea | 521 | playerElement: this.playerElement, |
6ec0b75b C |
522 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, |
523 | ||
2adfc7ea C |
524 | videoDuration: this.video.duration, |
525 | enableHotkeys: true, | |
526 | inactivityTimeout: 2500, | |
527 | poster: this.video.previewUrl, | |
528 | startTime, | |
f0a39880 | 529 | stopTime: urlOptions.stopTime, |
2adfc7ea C |
530 | |
531 | theaterMode: true, | |
532 | captions: videoCaptions.length !== 0, | |
533 | peertubeLink: false, | |
534 | ||
535 | videoViewUrl: this.video.privacy.id !== VideoPrivacy.PRIVATE ? this.videoService.getVideoViewUrl(this.video.uuid) : null, | |
536 | embedUrl: this.video.embedUrl, | |
537 | ||
538 | language: this.localeId, | |
539 | ||
540 | subtitle: urlOptions.subtitle, | |
aa8b6df4 | 541 | |
2adfc7ea C |
542 | userWatching: this.user && this.user.videosHistoryEnabled === true ? { |
543 | url: this.videoService.getUserWatchingVideoUrl(this.video.uuid), | |
544 | authorizationHeader: this.authService.getRequestHeaderValue() | |
545 | } : undefined, | |
aa8b6df4 | 546 | |
2adfc7ea C |
547 | serverUrl: environment.apiUrl, |
548 | ||
549 | videoCaptions: playerCaptions | |
6ec0b75b C |
550 | }, |
551 | ||
552 | webtorrent: { | |
553 | videoFiles: this.video.files | |
09209296 | 554 | } |
e945b184 C |
555 | } |
556 | ||
597a9266 C |
557 | const mode: PlayerMode = urlOptions.playerMode === 'p2p-media-loader' ? 'p2p-media-loader' : 'webtorrent' |
558 | ||
559 | if (mode === 'p2p-media-loader') { | |
560 | const hlsPlaylist = this.video.getHlsPlaylist() | |
e945b184 | 561 | |
09209296 C |
562 | const p2pMediaLoader = { |
563 | playlistUrl: hlsPlaylist.playlistUrl, | |
564 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
565 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
566 | trackerAnnounce: this.video.trackerUrls, | |
2adfc7ea | 567 | videoFiles: this.video.files |
09209296 C |
568 | } as P2PMediaLoaderOptions |
569 | ||
570 | Object.assign(options, { p2pMediaLoader }) | |
e945b184 C |
571 | } |
572 | ||
e945b184 | 573 | this.zone.runOutsideAngular(async () => { |
09209296 | 574 | this.player = await PeertubePlayerManager.initialize(mode, options) |
2adfc7ea | 575 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
576 | |
577 | this.player.on('timeupdate', () => { | |
578 | this.currentTime = Math.floor(this.player.currentTime()) | |
579 | }) | |
e2f01c47 C |
580 | |
581 | this.player.one('ended', () => { | |
582 | if (this.playlist) { | |
583 | this.zone.run(() => this.navigateToNextPlaylistVideo()) | |
584 | } | |
585 | }) | |
586 | ||
587 | this.player.one('stopped', () => { | |
588 | if (this.playlist) { | |
589 | this.zone.run(() => this.navigateToNextPlaylistVideo()) | |
590 | } | |
591 | }) | |
b891f9bc | 592 | }) |
22b59e80 C |
593 | |
594 | this.setVideoDescriptionHTML() | |
595 | this.setVideoLikesBarTooltipText() | |
596 | ||
597 | this.setOpenGraphTags() | |
598 | this.checkUserRating() | |
92fb909c C |
599 | } |
600 | ||
5c6d985f | 601 | private setRating (nextRating: UserVideoRateType) { |
57a49263 BB |
602 | let method |
603 | switch (nextRating) { | |
604 | case 'like': | |
605 | method = this.videoService.setVideoLike | |
606 | break | |
607 | case 'dislike': | |
608 | method = this.videoService.setVideoDislike | |
609 | break | |
610 | case 'none': | |
611 | method = this.videoService.unsetVideoLike | |
612 | break | |
613 | } | |
614 | ||
615 | method.call(this.videoService, this.video.id) | |
2186386c C |
616 | .subscribe( |
617 | () => { | |
618 | // Update the video like attribute | |
619 | this.updateVideoRating(this.userRating, nextRating) | |
620 | this.userRating = nextRating | |
621 | }, | |
622 | ||
f8b2c1b4 | 623 | (err: { message: string }) => this.notifier.error(err.message) |
2186386c | 624 | ) |
57a49263 BB |
625 | } |
626 | ||
5c6d985f | 627 | private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) { |
df98563e C |
628 | let likesToIncrement = 0 |
629 | let dislikesToIncrement = 0 | |
d38b8281 C |
630 | |
631 | if (oldRating) { | |
df98563e C |
632 | if (oldRating === 'like') likesToIncrement-- |
633 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
634 | } |
635 | ||
df98563e C |
636 | if (newRating === 'like') likesToIncrement++ |
637 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 638 | |
df98563e C |
639 | this.video.likes += likesToIncrement |
640 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 641 | |
22b59e80 | 642 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 643 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
644 | } |
645 | ||
e2f01c47 C |
646 | private updatePlaylistIndex () { |
647 | if (this.playlistVideos.length === 0 || !this.video) return | |
648 | ||
649 | for (const video of this.playlistVideos) { | |
650 | if (video.id === this.video.id) { | |
651 | this.currentPlaylistPosition = video.playlistElement.position | |
652 | return | |
653 | } | |
654 | } | |
655 | ||
656 | // Load more videos to find our video | |
657 | this.onPlaylistVideosNearOfBottom() | |
658 | } | |
659 | ||
df98563e C |
660 | private setOpenGraphTags () { |
661 | this.metaService.setTitle(this.video.name) | |
758b996d | 662 | |
df98563e | 663 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 664 | |
df98563e C |
665 | this.metaService.setTag('og:title', this.video.name) |
666 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 667 | |
df98563e C |
668 | this.metaService.setTag('og:description', this.video.description) |
669 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 670 | |
d38309c3 | 671 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 672 | |
df98563e | 673 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 674 | |
df98563e | 675 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 676 | |
df98563e C |
677 | this.metaService.setTag('og:url', window.location.href) |
678 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 679 | } |
1f3e9fec | 680 | |
d4c6a3b9 | 681 | private isAutoplay () { |
bf079b7b C |
682 | // We'll jump to the thread id, so do not play the video |
683 | if (this.route.snapshot.params['threadId']) return false | |
684 | ||
685 | // Otherwise true by default | |
d4c6a3b9 C |
686 | if (!this.user) return true |
687 | ||
688 | // Be sure the autoPlay is set to false | |
689 | return this.user.autoPlayVideo !== false | |
690 | } | |
09edde40 C |
691 | |
692 | private flushPlayer () { | |
693 | // Remove player if it exists | |
694 | if (this.player) { | |
695 | this.player.dispose() | |
696 | this.player = undefined | |
697 | } | |
698 | } | |
e2f01c47 C |
699 | |
700 | private navigateToNextPlaylistVideo () { | |
701 | if (this.currentPlaylistPosition < this.playlistPagination.totalItems) { | |
702 | const next = this.playlistVideos.find(v => v.playlistElement.position === this.currentPlaylistPosition + 1) | |
703 | ||
704 | const start = next.playlistElement.startTimestamp | |
705 | const stop = next.playlistElement.stopTimestamp | |
706 | this.router.navigate([],{ queryParams: { videoId: next.uuid, start, stop } }) | |
707 | } | |
708 | } | |
dc8bc31b | 709 | } |