]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
(doc) note about PeerTube for YunoHost (#624)
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
CommitLineData
a51bad1a 1import { catchError } from 'rxjs/operators'
cc1561f9 2import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } 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'
c6352f2c 24import { getVideojsOptions } from '../../../assets/player/peertube-player'
0883b324 25import { ServerService } from '@app/core'
989e526a 26import { I18n } from '@ngx-translate/i18n-polyfill'
dc8bc31b 27
dc8bc31b
C
28@Component({
29 selector: 'my-video-watch',
ec8d8440
C
30 templateUrl: './video-watch.component.html',
31 styleUrls: [ './video-watch.component.scss' ]
dc8bc31b 32})
0629423c 33export class VideoWatchComponent implements OnInit, OnDestroy {
22b59e80
C
34 private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern'
35
a96aed15 36 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
df98563e
C
37 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
38 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
07fa4c97 39 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
df98563e 40
57a49263 41 otherVideosDisplayed: Video[] = []
b1fa3eba 42
df98563e 43 player: videojs.Player
0826c92d 44 playerElement: HTMLVideoElement
154898b0 45 userRating: UserVideoRateType = null
404b54e1 46 video: VideoDetails = null
f1013131 47 videoNotFound = false
80958c78 48 descriptionLoading = false
2de96f4d
C
49
50 completeDescriptionShown = false
51 completeVideoDescription: string
52 shortVideoDescription: string
9d9597df 53 videoHTMLDescription = ''
e9189001 54 likesBarTooltipText = ''
73e09f27 55 hasAlreadyAcceptedPrivacyConcern = false
df98563e 56
28832412 57 private otherVideos: Video[] = []
df98563e 58 private paramsSub: Subscription
df98563e
C
59
60 constructor (
4fd8aa32 61 private elementRef: ElementRef,
0629423c 62 private route: ActivatedRoute,
92fb909c 63 private router: Router,
d3ef341a 64 private videoService: VideoService,
35bf0c83 65 private videoBlacklistService: VideoBlacklistService,
92fb909c 66 private confirmService: ConfirmService,
3ec343a4 67 private metaService: MetaService,
7ddd02c9 68 private authService: AuthService,
0883b324 69 private serverService: ServerService,
a51bad1a 70 private restExtractor: RestExtractor,
9d9597df 71 private notificationsService: NotificationsService,
7ae71355 72 private markdownService: MarkdownService,
901637bb 73 private zone: NgZone,
989e526a
C
74 private redirectService: RedirectService,
75 private i18n: I18n
d3ef341a 76 ) {}
dc8bc31b 77
b2731bff
C
78 get user () {
79 return this.authService.getUser()
80 }
81
df98563e 82 ngOnInit () {
0bd78bf3
C
83 if (
84 WebTorrent.WEBRTC_SUPPORT === false ||
85 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
86 ) {
2b3b76ab
C
87 this.hasAlreadyAcceptedPrivacyConcern = true
88 }
89
b1fa3eba
C
90 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
91 .subscribe(
649fb082
C
92 data => {
93 this.otherVideos = data.videos
94 this.updateOtherVideosDisplayed()
95 },
96
57a49263 97 err => console.error(err)
b1fa3eba
C
98 )
99
13fc89f4 100 this.paramsSub = this.route.params.subscribe(routeParams => {
09edde40 101 if (this.player) {
ed9f9f5f
C
102 this.player.pause()
103 }
104
1263fc4e 105 const uuid = routeParams['uuid']
a51bad1a 106
244e76a5 107 // Video did not change
1263fc4e 108 if (this.video && this.video.uuid === uuid) return
244e76a5 109 // Video did change
a51bad1a
C
110 this.videoService
111 .getVideo(uuid)
112 .pipe(catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 404 ])))
113 .subscribe(
114 video => {
115 const startTime = this.route.snapshot.queryParams.start
116 this.onVideoFetched(video, startTime)
117 .catch(err => this.handleError(err))
118 },
119
120 error => {
121 this.videoNotFound = true
122 console.error(error)
123 }
124 )
df98563e 125 })
d1992b93
C
126 }
127
df98563e 128 ngOnDestroy () {
09edde40 129 this.flushPlayer()
067e3f84 130
13fc89f4 131 // Unsubscribe subscriptions
df98563e 132 this.paramsSub.unsubscribe()
dc8bc31b 133 }
98b01bac 134
df98563e
C
135 setLike () {
136 if (this.isUserLoggedIn() === false) return
57a49263
BB
137 if (this.userRating === 'like') {
138 // Already liked this video
139 this.setRating('none')
140 } else {
141 this.setRating('like')
142 }
d38b8281
C
143 }
144
df98563e
C
145 setDislike () {
146 if (this.isUserLoggedIn() === false) return
57a49263
BB
147 if (this.userRating === 'dislike') {
148 // Already disliked this video
149 this.setRating('none')
150 } else {
151 this.setRating('dislike')
152 }
d38b8281
C
153 }
154
1f30a185 155 async blacklistVideo (event: Event) {
df98563e 156 event.preventDefault()
ab683a8e 157
989e526a 158 const res = await this.confirmService.confirm(this.i18n('Do you really want to blacklist this video?'), this.i18n('Blacklist'))
1f30a185 159 if (res === false) return
198b205c 160
1f30a185
C
161 this.videoBlacklistService.blacklistVideo(this.video.id)
162 .subscribe(
163 status => {
989e526a
C
164 this.notificationsService.success(
165 this.i18n('Success'),
25acef90 166 this.i18n('Video {{videoName}} had been blacklisted.', { videoName: this.video.name })
989e526a 167 )
901637bb 168 this.redirectService.redirectToHomepage()
1f30a185 169 },
198b205c 170
989e526a 171 error => this.notificationsService.error(this.i18n('Error'), error.message)
1f30a185 172 )
198b205c
GS
173 }
174
2de96f4d 175 showMoreDescription () {
2de96f4d
C
176 if (this.completeVideoDescription === undefined) {
177 return this.loadCompleteDescription()
178 }
179
180 this.updateVideoDescription(this.completeVideoDescription)
80958c78 181 this.completeDescriptionShown = true
2de96f4d
C
182 }
183
184 showLessDescription () {
2de96f4d 185 this.updateVideoDescription(this.shortVideoDescription)
80958c78 186 this.completeDescriptionShown = false
2de96f4d
C
187 }
188
189 loadCompleteDescription () {
80958c78
C
190 this.descriptionLoading = true
191
2de96f4d
C
192 this.videoService.loadCompleteDescription(this.video.descriptionPath)
193 .subscribe(
194 description => {
80958c78
C
195 this.completeDescriptionShown = true
196 this.descriptionLoading = false
197
2de96f4d
C
198 this.shortVideoDescription = this.video.description
199 this.completeVideoDescription = description
200
201 this.updateVideoDescription(this.completeVideoDescription)
202 },
203
80958c78
C
204 error => {
205 this.descriptionLoading = false
989e526a 206 this.notificationsService.error(this.i18n('Error'), error.message)
80958c78 207 }
2de96f4d
C
208 )
209 }
210
df98563e
C
211 showReportModal (event: Event) {
212 event.preventDefault()
213 this.videoReportModal.show()
4f8c0eb0
C
214 }
215
07fa4c97
C
216 showSupportModal () {
217 this.videoSupportModal.show()
218 }
219
df98563e
C
220 showShareModal () {
221 this.videoShareModal.show()
99cc4f49
C
222 }
223
a96aed15 224 showDownloadModal (event: Event) {
df98563e 225 event.preventDefault()
a96aed15 226 this.videoDownloadModal.show()
99cc4f49
C
227 }
228
df98563e
C
229 isUserLoggedIn () {
230 return this.authService.isLoggedIn()
4f8c0eb0
C
231 }
232
4635f59d
C
233 isVideoUpdatable () {
234 return this.video.isUpdatableBy(this.authService.getUser())
235 }
236
df98563e 237 isVideoBlacklistable () {
b2731bff 238 return this.video.isBlackistableBy(this.user)
198b205c
GS
239 }
240
6de36768
C
241 getVideoPoster () {
242 if (!this.video) return ''
243
244 return this.video.previewUrl
245 }
246
b1fa3eba
C
247 getVideoTags () {
248 if (!this.video || Array.isArray(this.video.tags) === false) return []
249
250 return this.video.tags.join(', ')
251 }
252
6725d05c
C
253 isVideoRemovable () {
254 return this.video.isRemovableBy(this.authService.getUser())
255 }
256
1f30a185 257 async removeVideo (event: Event) {
6725d05c
C
258 event.preventDefault()
259
989e526a 260 const res = await this.confirmService.confirm(this.i18n('Do you really want to delete this video?'), this.i18n('Delete'))
1f30a185 261 if (res === false) return
6725d05c 262
1f30a185
C
263 this.videoService.removeVideo(this.video.id)
264 .subscribe(
265 status => {
989e526a
C
266 this.notificationsService.success(
267 this.i18n('Success'),
25acef90 268 this.i18n('Video {{videoName}} deleted.', { videoName: this.video.name })
989e526a 269 )
6725d05c 270
1f30a185 271 // Go back to the video-list.
901637bb 272 this.redirectService.redirectToHomepage()
1f30a185 273 },
6725d05c 274
989e526a 275 error => this.notificationsService.error(this.i18n('Error'), error.message)
e9189001 276 )
6725d05c
C
277 }
278
73e09f27 279 acceptedPrivacyConcern () {
0bd78bf3 280 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
73e09f27
C
281 this.hasAlreadyAcceptedPrivacyConcern = true
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 () {
989e526a 299 this.likesBarTooltipText = this.i18n(
25acef90 300 '{{likesNumber}} likes / {{dislikesNumber}} dislikes',
989e526a
C
301 { likesNumber: this.video.likes, dislikes: 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)
325 .subscribe(
b632e904 326 ratingObject => {
d38b8281 327 if (ratingObject) {
df98563e 328 this.userRating = ratingObject.rating
d38b8281
C
329 }
330 },
331
989e526a 332 err => this.notificationsService.error(this.i18n('Error'), err.message)
df98563e 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,
960a11e8 368 videoEmbedUrl: this.video.embedUrl,
b891f9bc
C
369 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
370 videoDuration: this.video.duration,
371 enableHotkeys: true,
372 peertubeLink: false,
f37bad63
C
373 poster: this.video.previewUrl,
374 startTime
b891f9bc 375 })
aa8b6df4 376
b891f9bc
C
377 const self = this
378 this.zone.runOutsideAngular(() => {
09edde40 379 videojs(this.playerElement, videojsOptions, function () {
b891f9bc
C
380 self.player = this
381 this.on('customError', (event, data) => self.handleError(data.err))
22b59e80 382 })
b891f9bc 383 })
22b59e80
C
384
385 this.setVideoDescriptionHTML()
386 this.setVideoLikesBarTooltipText()
387
388 this.setOpenGraphTags()
389 this.checkUserRating()
92fb909c
C
390 }
391
57a49263
BB
392 private setRating (nextRating) {
393 let method
394 switch (nextRating) {
395 case 'like':
396 method = this.videoService.setVideoLike
397 break
398 case 'dislike':
399 method = this.videoService.setVideoDislike
400 break
401 case 'none':
402 method = this.videoService.unsetVideoLike
403 break
404 }
405
406 method.call(this.videoService, this.video.id)
407 .subscribe(
408 () => {
409 // Update the video like attribute
410 this.updateVideoRating(this.userRating, nextRating)
411 this.userRating = nextRating
412 },
989e526a 413 err => this.notificationsService.error(this.i18n('Error'), err.message)
57a49263
BB
414 )
415 }
416
154898b0 417 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
418 let likesToIncrement = 0
419 let dislikesToIncrement = 0
d38b8281
C
420
421 if (oldRating) {
df98563e
C
422 if (oldRating === 'like') likesToIncrement--
423 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
424 }
425
df98563e
C
426 if (newRating === 'like') likesToIncrement++
427 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 428
df98563e
C
429 this.video.likes += likesToIncrement
430 this.video.dislikes += dislikesToIncrement
20b40b19 431
22b59e80 432 this.video.buildLikeAndDislikePercents()
20b40b19 433 this.setVideoLikesBarTooltipText()
d38b8281
C
434 }
435
649fb082 436 private updateOtherVideosDisplayed () {
f6dc2fff 437 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
649fb082
C
438 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
439 }
440 }
441
df98563e
C
442 private setOpenGraphTags () {
443 this.metaService.setTitle(this.video.name)
758b996d 444
df98563e 445 this.metaService.setTag('og:type', 'video')
3ec343a4 446
df98563e
C
447 this.metaService.setTag('og:title', this.video.name)
448 this.metaService.setTag('name', this.video.name)
3ec343a4 449
df98563e
C
450 this.metaService.setTag('og:description', this.video.description)
451 this.metaService.setTag('description', this.video.description)
3ec343a4 452
d38309c3 453 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 454
df98563e 455 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 456
df98563e 457 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 458
df98563e
C
459 this.metaService.setTag('og:url', window.location.href)
460 this.metaService.setTag('url', window.location.href)
3ec343a4 461 }
1f3e9fec 462
d4c6a3b9
C
463 private isAutoplay () {
464 // True by default
465 if (!this.user) return true
466
467 // Be sure the autoPlay is set to false
468 return this.user.autoPlayVideo !== false
469 }
09edde40
C
470
471 private flushPlayer () {
472 // Remove player if it exists
473 if (this.player) {
474 this.player.dispose()
475 this.player = undefined
476 }
477 }
dc8bc31b 478}