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