]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Hide big play button on autoplay
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
CommitLineData
244e76a5 1import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild, OnChanges } from '@angular/core'
df98563e 2import { ActivatedRoute, Router } from '@angular/router'
901637bb 3import { RedirectService } from '@app/core/routing/redirect.service'
0bd78bf3 4import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage'
07fa4c97 5import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
1f3e9fec
C
6import { MetaService } from '@ngx-meta/core'
7import { NotificationsService } from 'angular2-notifications'
df98563e 8import { Subscription } from 'rxjs/Subscription'
63c4db6d 9import * as videojs from 'video.js'
d7701449 10import 'videojs-hotkeys'
caae7a06 11import * as WebTorrent from 'webtorrent'
244e76a5 12import { UserVideoRateType, VideoRateType, FeedFormat } from '../../../../../shared'
aa8b6df4 13import '../../../assets/player/peertube-videojs-plugin'
df98563e 14import { AuthService, ConfirmService } from '../../core'
1f3e9fec 15import { VideoBlacklistService } from '../../shared'
b1fa3eba 16import { Account } from '../../shared/account/account.model'
ff249f49 17import { VideoDetails } from '../../shared/video/video-details.model'
244e76a5 18import { VideoFeedComponent } from '../../shared/video/video-feed.component'
b1fa3eba 19import { Video } from '../../shared/video/video.model'
63c4db6d 20import { VideoService } from '../../shared/video/video.service'
202f6b6c 21import { MarkdownService } from '../shared'
4635f59d
C
22import { VideoDownloadComponent } from './modal/video-download.component'
23import { VideoReportComponent } from './modal/video-report.component'
24import { VideoShareComponent } from './modal/video-share.component'
244e76a5 25import { environment } from '../../../environments/environment'
c6352f2c 26import { getVideojsOptions } from '../../../assets/player/peertube-player'
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
244e76a5
RK
43 syndicationItems = {}
44
df98563e 45 player: videojs.Player
0826c92d 46 playerElement: HTMLVideoElement
154898b0 47 userRating: UserVideoRateType = null
404b54e1 48 video: VideoDetails = null
f1013131 49 videoNotFound = false
80958c78 50 descriptionLoading = false
2de96f4d
C
51
52 completeDescriptionShown = false
53 completeVideoDescription: string
54 shortVideoDescription: string
9d9597df 55 videoHTMLDescription = ''
e9189001 56 likesBarTooltipText = ''
73e09f27 57 hasAlreadyAcceptedPrivacyConcern = false
df98563e 58
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,
9d9597df 71 private notificationsService: NotificationsService,
7ae71355 72 private markdownService: MarkdownService,
901637bb
C
73 private zone: NgZone,
74 private redirectService: RedirectService
d3ef341a 75 ) {}
dc8bc31b 76
b2731bff
C
77 get user () {
78 return this.authService.getUser()
79 }
80
df98563e 81 ngOnInit () {
0bd78bf3
C
82 if (
83 WebTorrent.WEBRTC_SUPPORT === false ||
84 peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true'
85 ) {
2b3b76ab
C
86 this.hasAlreadyAcceptedPrivacyConcern = true
87 }
88
b1fa3eba
C
89 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
90 .subscribe(
649fb082
C
91 data => {
92 this.otherVideos = data.videos
93 this.updateOtherVideosDisplayed()
94 },
95
57a49263 96 err => console.error(err)
b1fa3eba
C
97 )
98
13fc89f4 99 this.paramsSub = this.route.params.subscribe(routeParams => {
09edde40 100 if (this.player) {
ed9f9f5f
C
101 this.player.pause()
102 }
103
1263fc4e 104 const uuid = routeParams['uuid']
244e76a5 105 // Video did not change
1263fc4e 106 if (this.video && this.video.uuid === uuid) return
244e76a5 107 // Video did change
0a6658fd 108 this.videoService.getVideo(uuid).subscribe(
f37bad63
C
109 video => {
110 const startTime = this.route.snapshot.queryParams.start
111 this.onVideoFetched(video, startTime)
112 .catch(err => this.handleError(err))
244e76a5 113 this.generateSyndicationList()
f37bad63 114 },
92fb909c 115
f1013131
C
116 error => {
117 this.videoNotFound = true
118 console.error(error)
119 }
df98563e
C
120 )
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
1f30a185
C
154 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
155 if (res === false) return
198b205c 156
1f30a185
C
157 this.videoBlacklistService.blacklistVideo(this.video.id)
158 .subscribe(
159 status => {
160 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
901637bb 161 this.redirectService.redirectToHomepage()
1f30a185 162 },
198b205c 163
1f30a185
C
164 error => this.notificationsService.error('Error', error.message)
165 )
198b205c
GS
166 }
167
2de96f4d 168 showMoreDescription () {
2de96f4d
C
169 if (this.completeVideoDescription === undefined) {
170 return this.loadCompleteDescription()
171 }
172
173 this.updateVideoDescription(this.completeVideoDescription)
80958c78 174 this.completeDescriptionShown = true
2de96f4d
C
175 }
176
177 showLessDescription () {
2de96f4d 178 this.updateVideoDescription(this.shortVideoDescription)
80958c78 179 this.completeDescriptionShown = false
2de96f4d
C
180 }
181
182 loadCompleteDescription () {
80958c78
C
183 this.descriptionLoading = true
184
2de96f4d
C
185 this.videoService.loadCompleteDescription(this.video.descriptionPath)
186 .subscribe(
187 description => {
80958c78
C
188 this.completeDescriptionShown = true
189 this.descriptionLoading = false
190
2de96f4d
C
191 this.shortVideoDescription = this.video.description
192 this.completeVideoDescription = description
193
194 this.updateVideoDescription(this.completeVideoDescription)
195 },
196
80958c78
C
197 error => {
198 this.descriptionLoading = false
c5911fd3 199 this.notificationsService.error('Error', error.message)
80958c78 200 }
2de96f4d
C
201 )
202 }
203
df98563e
C
204 showReportModal (event: Event) {
205 event.preventDefault()
206 this.videoReportModal.show()
4f8c0eb0
C
207 }
208
07fa4c97
C
209 showSupportModal () {
210 this.videoSupportModal.show()
211 }
212
df98563e
C
213 showShareModal () {
214 this.videoShareModal.show()
99cc4f49
C
215 }
216
a96aed15 217 showDownloadModal (event: Event) {
df98563e 218 event.preventDefault()
a96aed15 219 this.videoDownloadModal.show()
99cc4f49
C
220 }
221
df98563e
C
222 isUserLoggedIn () {
223 return this.authService.isLoggedIn()
4f8c0eb0
C
224 }
225
4635f59d
C
226 isVideoUpdatable () {
227 return this.video.isUpdatableBy(this.authService.getUser())
228 }
229
df98563e 230 isVideoBlacklistable () {
b2731bff 231 return this.video.isBlackistableBy(this.user)
198b205c
GS
232 }
233
b1fa3eba 234 getAvatarPath () {
c5911fd3 235 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
b1fa3eba
C
236 }
237
6de36768
C
238 getVideoPoster () {
239 if (!this.video) return ''
240
241 return this.video.previewUrl
242 }
243
b1fa3eba
C
244 getVideoTags () {
245 if (!this.video || Array.isArray(this.video.tags) === false) return []
246
247 return this.video.tags.join(', ')
248 }
249
244e76a5
RK
250 generateSyndicationList () {
251 const feeds = this.videoService.getAccountFeed(
252 this.video.account.id,
253 (this.video.isLocal) ? environment.apiUrl : this.video.account.host
254 )
255 this.syndicationItems['rss 2.0'] = feeds[FeedFormat.RSS]
256 this.syndicationItems['atom 1.0'] = feeds[FeedFormat.ATOM]
257 this.syndicationItems['json 1.0'] = feeds[FeedFormat.JSON]
258 }
259
6725d05c
C
260 isVideoRemovable () {
261 return this.video.isRemovableBy(this.authService.getUser())
262 }
263
1f30a185 264 async removeVideo (event: Event) {
6725d05c
C
265 event.preventDefault()
266
1f30a185
C
267 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
268 if (res === false) return
6725d05c 269
1f30a185
C
270 this.videoService.removeVideo(this.video.id)
271 .subscribe(
272 status => {
273 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
6725d05c 274
1f30a185 275 // Go back to the video-list.
901637bb 276 this.redirectService.redirectToHomepage()
1f30a185 277 },
6725d05c 278
1f30a185 279 error => this.notificationsService.error('Error', error.message)
e9189001 280 )
6725d05c
C
281 }
282
73e09f27 283 acceptedPrivacyConcern () {
0bd78bf3 284 peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true')
73e09f27
C
285 this.hasAlreadyAcceptedPrivacyConcern = true
286 }
287
2de96f4d
C
288 private updateVideoDescription (description: string) {
289 this.video.description = description
290 this.setVideoDescriptionHTML()
291 }
292
293 private setVideoDescriptionHTML () {
cadb46d8
C
294 if (!this.video.description) {
295 this.videoHTMLDescription = ''
296 return
297 }
298
07fa4c97 299 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
2de96f4d
C
300 }
301
e9189001
C
302 private setVideoLikesBarTooltipText () {
303 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
304 }
305
0c31c33d
C
306 private handleError (err: any) {
307 const errorMessage: string = typeof err === 'string' ? err : err.message
bf5685f0
C
308 if (!errorMessage) return
309
0c31c33d
C
310 let message = ''
311
312 if (errorMessage.indexOf('http error') !== -1) {
313 message = 'Cannot fetch video from server, maybe down.'
314 } else {
315 message = errorMessage
316 }
317
318 this.notificationsService.error('Error', message)
319 }
320
df98563e 321 private checkUserRating () {
d38b8281 322 // Unlogged users do not have ratings
df98563e 323 if (this.isUserLoggedIn() === false) return
d38b8281
C
324
325 this.videoService.getUserVideoRating(this.video.id)
326 .subscribe(
b632e904 327 ratingObject => {
d38b8281 328 if (ratingObject) {
df98563e 329 this.userRating = ratingObject.rating
d38b8281
C
330 }
331 },
332
bfb3a98f 333 err => this.notificationsService.error('Error', err.message)
df98563e 334 )
d38b8281
C
335 }
336
f37bad63 337 private async onVideoFetched (video: VideoDetails, startTime = 0) {
df98563e 338 this.video = video
92fb909c 339
c448d412
C
340 // Re init attributes
341 this.descriptionLoading = false
342 this.completeDescriptionShown = false
343
649fb082 344 this.updateOtherVideosDisplayed()
57a49263 345
b2731bff 346 if (this.video.isVideoNSFWForUser(this.user)) {
22b59e80 347 const res = await this.confirmService.confirm(
d6e32a2e
C
348 'This video contains mature or explicit content. Are you sure you want to watch it?',
349 'Mature or explicit content'
350 )
901637bb 351 if (res === false) return this.redirectService.redirectToHomepage()
92fb909c
C
352 }
353
09edde40
C
354 // Flush old player if needed
355 this.flushPlayer()
b891f9bc
C
356
357 // Build video element, because videojs remove it on dispose
358 const playerElementWrapper = this.elementRef.nativeElement.querySelector('#video-element-wrapper')
359 this.playerElement = document.createElement('video')
360 this.playerElement.className = 'video-js vjs-peertube-skin'
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
C
372 poster: this.video.previewUrl,
373 startTime
b891f9bc 374 })
aa8b6df4 375
b891f9bc
C
376 const self = this
377 this.zone.runOutsideAngular(() => {
09edde40 378 videojs(this.playerElement, videojsOptions, function () {
b891f9bc
C
379 self.player = this
380 this.on('customError', (event, data) => self.handleError(data.err))
22b59e80 381 })
b891f9bc 382 })
22b59e80
C
383
384 this.setVideoDescriptionHTML()
385 this.setVideoLikesBarTooltipText()
386
387 this.setOpenGraphTags()
388 this.checkUserRating()
92fb909c
C
389 }
390
57a49263
BB
391 private setRating (nextRating) {
392 let method
393 switch (nextRating) {
394 case 'like':
395 method = this.videoService.setVideoLike
396 break
397 case 'dislike':
398 method = this.videoService.setVideoDislike
399 break
400 case 'none':
401 method = this.videoService.unsetVideoLike
402 break
403 }
404
405 method.call(this.videoService, this.video.id)
406 .subscribe(
407 () => {
408 // Update the video like attribute
409 this.updateVideoRating(this.userRating, nextRating)
410 this.userRating = nextRating
411 },
412 err => this.notificationsService.error('Error', err.message)
413 )
414 }
415
154898b0 416 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
417 let likesToIncrement = 0
418 let dislikesToIncrement = 0
d38b8281
C
419
420 if (oldRating) {
df98563e
C
421 if (oldRating === 'like') likesToIncrement--
422 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
423 }
424
df98563e
C
425 if (newRating === 'like') likesToIncrement++
426 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 427
df98563e
C
428 this.video.likes += likesToIncrement
429 this.video.dislikes += dislikesToIncrement
20b40b19 430
22b59e80 431 this.video.buildLikeAndDislikePercents()
20b40b19 432 this.setVideoLikesBarTooltipText()
d38b8281
C
433 }
434
649fb082 435 private updateOtherVideosDisplayed () {
f6dc2fff 436 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
649fb082
C
437 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
438 }
439 }
440
df98563e
C
441 private setOpenGraphTags () {
442 this.metaService.setTitle(this.video.name)
758b996d 443
df98563e 444 this.metaService.setTag('og:type', 'video')
3ec343a4 445
df98563e
C
446 this.metaService.setTag('og:title', this.video.name)
447 this.metaService.setTag('name', this.video.name)
3ec343a4 448
df98563e
C
449 this.metaService.setTag('og:description', this.video.description)
450 this.metaService.setTag('description', this.video.description)
3ec343a4 451
d38309c3 452 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 453
df98563e 454 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 455
df98563e 456 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 457
df98563e
C
458 this.metaService.setTag('og:url', window.location.href)
459 this.metaService.setTag('url', window.location.href)
3ec343a4 460 }
1f3e9fec 461
d4c6a3b9
C
462 private isAutoplay () {
463 // True by default
464 if (!this.user) return true
465
466 // Be sure the autoPlay is set to false
467 return this.user.autoPlayVideo !== false
468 }
09edde40
C
469
470 private flushPlayer () {
471 // Remove player if it exists
472 if (this.player) {
473 this.player.dispose()
474 this.player = undefined
475 }
476 }
dc8bc31b 477}