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