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