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