]>
Commit | Line | Data |
---|---|---|
a51bad1a | 1 | import { catchError } from 'rxjs/operators' |
2186386c | 2 | import { 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 C |
7 | import { MetaService } from '@ngx-meta/core' |
8 | import { NotificationsService } from 'angular2-notifications' | |
db400f44 | 9 | import { Subscription } from 'rxjs' |
63c4db6d | 10 | import * as videojs from 'video.js' |
d7701449 | 11 | import 'videojs-hotkeys' |
caae7a06 | 12 | import * as WebTorrent from 'webtorrent' |
2186386c | 13 | import { UserVideoRateType, VideoRateType, VideoState } from '../../../../../shared' |
aa8b6df4 | 14 | import '../../../assets/player/peertube-videojs-plugin' |
df98563e | 15 | import { AuthService, ConfirmService } from '../../core' |
a51bad1a | 16 | import { RestExtractor, VideoBlacklistService } from '../../shared' |
ff249f49 | 17 | import { VideoDetails } from '../../shared/video/video-details.model' |
b1fa3eba | 18 | import { Video } from '../../shared/video/video.model' |
63c4db6d | 19 | import { VideoService } from '../../shared/video/video.service' |
202f6b6c | 20 | import { MarkdownService } from '../shared' |
4635f59d C |
21 | import { VideoDownloadComponent } from './modal/video-download.component' |
22 | import { VideoReportComponent } from './modal/video-report.component' | |
23 | import { VideoShareComponent } from './modal/video-share.component' | |
2186386c | 24 | import { addContextMenu, getVideojsOptions, loadLocale } from '../../../assets/player/peertube-player' |
0883b324 | 25 | import { ServerService } from '@app/core' |
989e526a | 26 | import { I18n } from '@ngx-translate/i18n-polyfill' |
e945b184 | 27 | import { environment } from '../../../environments/environment' |
74b7c6d4 | 28 | import { getDevLocale, isOnDevLocale } from '@app/shared/i18n/i18n-utils' |
dc8bc31b | 29 | |
dc8bc31b C |
30 | @Component({ |
31 | selector: 'my-video-watch', | |
ec8d8440 C |
32 | templateUrl: './video-watch.component.html', |
33 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 34 | }) |
0629423c | 35 | export class VideoWatchComponent implements OnInit, OnDestroy { |
22b59e80 C |
36 | private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern' |
37 | ||
a96aed15 | 38 | @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent |
df98563e C |
39 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent |
40 | @ViewChild('videoReportModal') videoReportModal: VideoReportComponent | |
07fa4c97 | 41 | @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent |
df98563e | 42 | |
57a49263 | 43 | otherVideosDisplayed: Video[] = [] |
b1fa3eba | 44 | |
df98563e | 45 | player: videojs.Player |
0826c92d | 46 | playerElement: HTMLVideoElement |
154898b0 | 47 | userRating: UserVideoRateType = null |
404b54e1 | 48 | video: VideoDetails = null |
80958c78 | 49 | descriptionLoading = false |
2de96f4d C |
50 | |
51 | completeDescriptionShown = false | |
52 | completeVideoDescription: string | |
53 | shortVideoDescription: string | |
9d9597df | 54 | videoHTMLDescription = '' |
e9189001 | 55 | likesBarTooltipText = '' |
73e09f27 | 56 | hasAlreadyAcceptedPrivacyConcern = false |
df98563e | 57 | |
e945b184 | 58 | private videojsLocaleLoaded = false |
28832412 | 59 | private otherVideos: Video[] = [] |
df98563e | 60 | private paramsSub: Subscription |
df98563e C |
61 | |
62 | constructor ( | |
4fd8aa32 | 63 | private elementRef: ElementRef, |
0629423c | 64 | private route: ActivatedRoute, |
92fb909c | 65 | private router: Router, |
d3ef341a | 66 | private videoService: VideoService, |
35bf0c83 | 67 | private videoBlacklistService: VideoBlacklistService, |
92fb909c | 68 | private confirmService: ConfirmService, |
3ec343a4 | 69 | private metaService: MetaService, |
7ddd02c9 | 70 | private authService: AuthService, |
0883b324 | 71 | private serverService: ServerService, |
a51bad1a | 72 | private restExtractor: RestExtractor, |
9d9597df | 73 | private notificationsService: NotificationsService, |
7ae71355 | 74 | private markdownService: MarkdownService, |
901637bb | 75 | private zone: NgZone, |
989e526a | 76 | private redirectService: RedirectService, |
e945b184 C |
77 | private i18n: I18n, |
78 | @Inject(LOCALE_ID) private localeId: string | |
d3ef341a | 79 | ) {} |
dc8bc31b | 80 | |
b2731bff C |
81 | get user () { |
82 | return this.authService.getUser() | |
83 | } | |
84 | ||
df98563e | 85 | ngOnInit () { |
0bd78bf3 C |
86 | if ( |
87 | WebTorrent.WEBRTC_SUPPORT === false || | |
88 | peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true' | |
89 | ) { | |
2b3b76ab C |
90 | this.hasAlreadyAcceptedPrivacyConcern = true |
91 | } | |
92 | ||
b1fa3eba | 93 | this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt') |
2186386c C |
94 | .subscribe( |
95 | data => { | |
96 | this.otherVideos = data.videos | |
97 | this.updateOtherVideosDisplayed() | |
98 | }, | |
649fb082 | 99 | |
2186386c C |
100 | err => console.error(err) |
101 | ) | |
b1fa3eba | 102 | |
13fc89f4 | 103 | this.paramsSub = this.route.params.subscribe(routeParams => { |
09edde40 | 104 | if (this.player) { |
ed9f9f5f C |
105 | this.player.pause() |
106 | } | |
107 | ||
2186386c | 108 | const uuid = routeParams[ 'uuid' ] |
a51bad1a | 109 | |
244e76a5 | 110 | // Video did not change |
1263fc4e | 111 | if (this.video && this.video.uuid === uuid) return |
244e76a5 | 112 | // Video did change |
a51bad1a C |
113 | this.videoService |
114 | .getVideo(uuid) | |
115 | .pipe(catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ]))) | |
2186386c C |
116 | .subscribe(video => { |
117 | const startTime = this.route.snapshot.queryParams.start | |
118 | this.onVideoFetched(video, startTime) | |
119 | .catch(err => this.handleError(err)) | |
120 | }) | |
df98563e | 121 | }) |
d1992b93 C |
122 | } |
123 | ||
df98563e | 124 | ngOnDestroy () { |
09edde40 | 125 | this.flushPlayer() |
067e3f84 | 126 | |
13fc89f4 | 127 | // Unsubscribe subscriptions |
df98563e | 128 | this.paramsSub.unsubscribe() |
dc8bc31b | 129 | } |
98b01bac | 130 | |
df98563e C |
131 | setLike () { |
132 | if (this.isUserLoggedIn() === false) return | |
57a49263 BB |
133 | if (this.userRating === 'like') { |
134 | // Already liked this video | |
135 | this.setRating('none') | |
136 | } else { | |
137 | this.setRating('like') | |
138 | } | |
d38b8281 C |
139 | } |
140 | ||
df98563e C |
141 | setDislike () { |
142 | if (this.isUserLoggedIn() === false) return | |
57a49263 BB |
143 | if (this.userRating === 'dislike') { |
144 | // Already disliked this video | |
145 | this.setRating('none') | |
146 | } else { | |
147 | this.setRating('dislike') | |
148 | } | |
d38b8281 C |
149 | } |
150 | ||
1f30a185 | 151 | async blacklistVideo (event: Event) { |
df98563e | 152 | event.preventDefault() |
ab683a8e | 153 | |
989e526a | 154 | const res = await this.confirmService.confirm(this.i18n('Do you really want to blacklist this video?'), this.i18n('Blacklist')) |
1f30a185 | 155 | if (res === false) return |
198b205c | 156 | |
1f30a185 | 157 | this.videoBlacklistService.blacklistVideo(this.video.id) |
2186386c C |
158 | .subscribe( |
159 | () => { | |
160 | this.notificationsService.success( | |
161 | this.i18n('Success'), | |
162 | this.i18n('Video {{videoName}} had been blacklisted.', { videoName: this.video.name }) | |
163 | ) | |
164 | this.redirectService.redirectToHomepage() | |
165 | }, | |
166 | ||
167 | error => this.notificationsService.error(this.i18n('Error'), error.message) | |
168 | ) | |
198b205c GS |
169 | } |
170 | ||
2de96f4d | 171 | showMoreDescription () { |
2de96f4d C |
172 | if (this.completeVideoDescription === undefined) { |
173 | return this.loadCompleteDescription() | |
174 | } | |
175 | ||
176 | this.updateVideoDescription(this.completeVideoDescription) | |
80958c78 | 177 | this.completeDescriptionShown = true |
2de96f4d C |
178 | } |
179 | ||
180 | showLessDescription () { | |
2de96f4d | 181 | this.updateVideoDescription(this.shortVideoDescription) |
80958c78 | 182 | this.completeDescriptionShown = false |
2de96f4d C |
183 | } |
184 | ||
185 | loadCompleteDescription () { | |
80958c78 C |
186 | this.descriptionLoading = true |
187 | ||
2de96f4d | 188 | this.videoService.loadCompleteDescription(this.video.descriptionPath) |
2186386c C |
189 | .subscribe( |
190 | description => { | |
191 | this.completeDescriptionShown = true | |
192 | this.descriptionLoading = false | |
193 | ||
194 | this.shortVideoDescription = this.video.description | |
195 | this.completeVideoDescription = description | |
196 | ||
197 | this.updateVideoDescription(this.completeVideoDescription) | |
198 | }, | |
199 | ||
200 | error => { | |
201 | this.descriptionLoading = false | |
202 | this.notificationsService.error(this.i18n('Error'), error.message) | |
203 | } | |
204 | ) | |
2de96f4d C |
205 | } |
206 | ||
df98563e C |
207 | showReportModal (event: Event) { |
208 | event.preventDefault() | |
209 | this.videoReportModal.show() | |
4f8c0eb0 C |
210 | } |
211 | ||
07fa4c97 C |
212 | showSupportModal () { |
213 | this.videoSupportModal.show() | |
214 | } | |
215 | ||
df98563e C |
216 | showShareModal () { |
217 | this.videoShareModal.show() | |
99cc4f49 C |
218 | } |
219 | ||
a96aed15 | 220 | showDownloadModal (event: Event) { |
df98563e | 221 | event.preventDefault() |
a96aed15 | 222 | this.videoDownloadModal.show() |
99cc4f49 C |
223 | } |
224 | ||
df98563e C |
225 | isUserLoggedIn () { |
226 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
227 | } |
228 | ||
4635f59d C |
229 | isVideoUpdatable () { |
230 | return this.video.isUpdatableBy(this.authService.getUser()) | |
231 | } | |
232 | ||
df98563e | 233 | isVideoBlacklistable () { |
b2731bff | 234 | return this.video.isBlackistableBy(this.user) |
198b205c GS |
235 | } |
236 | ||
6de36768 C |
237 | getVideoPoster () { |
238 | if (!this.video) return '' | |
239 | ||
240 | return this.video.previewUrl | |
241 | } | |
242 | ||
b1fa3eba C |
243 | getVideoTags () { |
244 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
245 | ||
246 | return this.video.tags.join(', ') | |
247 | } | |
248 | ||
6725d05c C |
249 | isVideoRemovable () { |
250 | return this.video.isRemovableBy(this.authService.getUser()) | |
251 | } | |
252 | ||
1f30a185 | 253 | async removeVideo (event: Event) { |
6725d05c C |
254 | event.preventDefault() |
255 | ||
989e526a | 256 | const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete')) |
1f30a185 | 257 | if (res === false) return |
6725d05c | 258 | |
1f30a185 | 259 | this.videoService.removeVideo(this.video.id) |
2186386c C |
260 | .subscribe( |
261 | status => { | |
262 | this.notificationsService.success( | |
263 | this.i18n('Success'), | |
264 | this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name }) | |
265 | ) | |
266 | ||
267 | // Go back to the video-list. | |
268 | this.redirectService.redirectToHomepage() | |
269 | }, | |
270 | ||
271 | error => this.notificationsService.error(this.i18n('Error'), error.message) | |
272 | ) | |
6725d05c C |
273 | } |
274 | ||
73e09f27 | 275 | acceptedPrivacyConcern () { |
0bd78bf3 | 276 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') |
73e09f27 C |
277 | this.hasAlreadyAcceptedPrivacyConcern = true |
278 | } | |
279 | ||
2186386c C |
280 | isVideoToTranscode () { |
281 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
282 | } | |
283 | ||
2de96f4d C |
284 | private updateVideoDescription (description: string) { |
285 | this.video.description = description | |
286 | this.setVideoDescriptionHTML() | |
287 | } | |
288 | ||
289 | private setVideoDescriptionHTML () { | |
cadb46d8 C |
290 | if (!this.video.description) { |
291 | this.videoHTMLDescription = '' | |
292 | return | |
293 | } | |
294 | ||
07fa4c97 | 295 | this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description) |
2de96f4d C |
296 | } |
297 | ||
e9189001 | 298 | private setVideoLikesBarTooltipText () { |
2186386c C |
299 | this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', { |
300 | likesNumber: this.video.likes, | |
301 | dislikesNumber: this.video.dislikes | |
302 | }) | |
e9189001 C |
303 | } |
304 | ||
0c31c33d C |
305 | private handleError (err: any) { |
306 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
307 | if (!errorMessage) return |
308 | ||
0c31c33d C |
309 | let message = '' |
310 | ||
311 | if (errorMessage.indexOf('http error') !== -1) { | |
989e526a | 312 | message = this.i18n('Cannot fetch video from server, maybe down.') |
0c31c33d C |
313 | } else { |
314 | message = errorMessage | |
315 | } | |
316 | ||
989e526a | 317 | this.notificationsService.error(this.i18n('Error'), message) |
0c31c33d C |
318 | } |
319 | ||
df98563e | 320 | private checkUserRating () { |
d38b8281 | 321 | // Unlogged users do not have ratings |
df98563e | 322 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
323 | |
324 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
325 | .subscribe( |
326 | ratingObject => { | |
327 | if (ratingObject) { | |
328 | this.userRating = ratingObject.rating | |
329 | } | |
330 | }, | |
331 | ||
332 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
333 | ) | |
d38b8281 C |
334 | } |
335 | ||
f37bad63 | 336 | private async onVideoFetched (video: VideoDetails, startTime = 0) { |
df98563e | 337 | this.video = video |
92fb909c | 338 | |
c448d412 C |
339 | // Re init attributes |
340 | this.descriptionLoading = false | |
341 | this.completeDescriptionShown = false | |
342 | ||
649fb082 | 343 | this.updateOtherVideosDisplayed() |
57a49263 | 344 | |
0883b324 | 345 | if (this.video.isVideoNSFWForUser(this.user, this.serverService.getConfig())) { |
22b59e80 | 346 | const res = await this.confirmService.confirm( |
989e526a C |
347 | this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'), |
348 | this.i18n('Mature or explicit content') | |
d6e32a2e | 349 | ) |
901637bb | 350 | if (res === false) return this.redirectService.redirectToHomepage() |
92fb909c C |
351 | } |
352 | ||
09edde40 C |
353 | // Flush old player if needed |
354 | this.flushPlayer() | |
b891f9bc C |
355 | |
356 | // Build video element, because videojs remove it on dispose | |
357 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper') | |
358 | this.playerElement = document.createElement('video') | |
359 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 360 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
361 | playerElementWrapper.appendChild(this.playerElement) |
362 | ||
363 | const videojsOptions = getVideojsOptions({ | |
364 | autoplay: this.isAutoplay(), | |
09edde40 | 365 | inactivityTimeout: 2500, |
b891f9bc C |
366 | videoFiles: this.video.files, |
367 | playerElement: this.playerElement, | |
368 | videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid), | |
369 | videoDuration: this.video.duration, | |
370 | enableHotkeys: true, | |
371 | peertubeLink: false, | |
f37bad63 | 372 | poster: this.video.previewUrl, |
054a103b C |
373 | startTime, |
374 | theaterMode: true | |
b891f9bc | 375 | }) |
aa8b6df4 | 376 | |
e945b184 | 377 | if (this.videojsLocaleLoaded === false) { |
74b7c6d4 | 378 | await loadLocale(environment.apiUrl, videojs, isOnDevLocale() ? getDevLocale() : this.localeId) |
e945b184 C |
379 | this.videojsLocaleLoaded = true |
380 | } | |
381 | ||
b891f9bc | 382 | const self = this |
e945b184 | 383 | this.zone.runOutsideAngular(async () => { |
09edde40 | 384 | videojs(this.playerElement, videojsOptions, function () { |
b891f9bc C |
385 | self.player = this |
386 | this.on('customError', (event, data) => self.handleError(data.err)) | |
e945b184 C |
387 | |
388 | addContextMenu(self.player, self.video.embedUrl) | |
22b59e80 | 389 | }) |
b891f9bc | 390 | }) |
22b59e80 C |
391 | |
392 | this.setVideoDescriptionHTML() | |
393 | this.setVideoLikesBarTooltipText() | |
394 | ||
395 | this.setOpenGraphTags() | |
396 | this.checkUserRating() | |
92fb909c C |
397 | } |
398 | ||
57a49263 BB |
399 | private setRating (nextRating) { |
400 | let method | |
401 | switch (nextRating) { | |
402 | case 'like': | |
403 | method = this.videoService.setVideoLike | |
404 | break | |
405 | case 'dislike': | |
406 | method = this.videoService.setVideoDislike | |
407 | break | |
408 | case 'none': | |
409 | method = this.videoService.unsetVideoLike | |
410 | break | |
411 | } | |
412 | ||
413 | method.call(this.videoService, this.video.id) | |
2186386c C |
414 | .subscribe( |
415 | () => { | |
416 | // Update the video like attribute | |
417 | this.updateVideoRating(this.userRating, nextRating) | |
418 | this.userRating = nextRating | |
419 | }, | |
420 | ||
421 | err => this.notificationsService.error(this.i18n('Error'), err.message) | |
422 | ) | |
57a49263 BB |
423 | } |
424 | ||
154898b0 | 425 | private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) { |
df98563e C |
426 | let likesToIncrement = 0 |
427 | let dislikesToIncrement = 0 | |
d38b8281 C |
428 | |
429 | if (oldRating) { | |
df98563e C |
430 | if (oldRating === 'like') likesToIncrement-- |
431 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
432 | } |
433 | ||
df98563e C |
434 | if (newRating === 'like') likesToIncrement++ |
435 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 436 | |
df98563e C |
437 | this.video.likes += likesToIncrement |
438 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 439 | |
22b59e80 | 440 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 441 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
442 | } |
443 | ||
649fb082 | 444 | private updateOtherVideosDisplayed () { |
f6dc2fff | 445 | if (this.video && this.otherVideos && this.otherVideos.length > 0) { |
649fb082 C |
446 | this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid) |
447 | } | |
448 | } | |
449 | ||
df98563e C |
450 | private setOpenGraphTags () { |
451 | this.metaService.setTitle(this.video.name) | |
758b996d | 452 | |
df98563e | 453 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 454 | |
df98563e C |
455 | this.metaService.setTag('og:title', this.video.name) |
456 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 457 | |
df98563e C |
458 | this.metaService.setTag('og:description', this.video.description) |
459 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 460 | |
d38309c3 | 461 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 462 | |
df98563e | 463 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 464 | |
df98563e | 465 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 466 | |
df98563e C |
467 | this.metaService.setTag('og:url', window.location.href) |
468 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 469 | } |
1f3e9fec | 470 | |
d4c6a3b9 C |
471 | private isAutoplay () { |
472 | // True by default | |
473 | if (!this.user) return true | |
474 | ||
475 | // Be sure the autoPlay is set to false | |
476 | return this.user.autoPlayVideo !== false | |
477 | } | |
09edde40 C |
478 | |
479 | private flushPlayer () { | |
480 | // Remove player if it exists | |
481 | if (this.player) { | |
482 | this.player.dispose() | |
483 | this.player = undefined | |
484 | } | |
485 | } | |
dc8bc31b | 486 | } |