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