]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/+video-watch/video-watch.component.ts
Add confirm when admin use custom js/css
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
1 import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core'
2 import { ActivatedRoute, Router } from '@angular/router'
3 import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component'
4 import { MetaService } from '@ngx-meta/core'
5 import { NotificationsService } from 'angular2-notifications'
6 import { Observable } from 'rxjs/Observable'
7 import { Subscription } from 'rxjs/Subscription'
8 import * as videojs from 'video.js'
9 import 'videojs-hotkeys'
10 import { UserVideoRateType, VideoRateType } from '../../../../../shared'
11 import '../../../assets/player/peertube-videojs-plugin'
12 import { AuthService, ConfirmService } from '../../core'
13 import { VideoBlacklistService } from '../../shared'
14 import { Account } from '../../shared/account/account.model'
15 import { VideoDetails } from '../../shared/video/video-details.model'
16 import { Video } from '../../shared/video/video.model'
17 import { VideoService } from '../../shared/video/video.service'
18 import { MarkdownService } from '../shared'
19 import { VideoDownloadComponent } from './modal/video-download.component'
20 import { VideoReportComponent } from './modal/video-report.component'
21 import { VideoShareComponent } from './modal/video-share.component'
22
23 @Component({
24 selector: 'my-video-watch',
25 templateUrl: './video-watch.component.html',
26 styleUrls: [ './video-watch.component.scss' ]
27 })
28 export class VideoWatchComponent implements OnInit, OnDestroy {
29 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
30 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
31 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
32 @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent
33
34 otherVideosDisplayed: Video[] = []
35
36 error = false
37 player: videojs.Player
38 playerElement: HTMLVideoElement
39 userRating: UserVideoRateType = null
40 video: VideoDetails = null
41 videoPlayerLoaded = false
42 videoNotFound = false
43 descriptionLoading = false
44
45 completeDescriptionShown = false
46 completeVideoDescription: string
47 shortVideoDescription: string
48 videoHTMLDescription = ''
49 likesBarTooltipText = ''
50
51 private otherVideos: Video[] = []
52 private paramsSub: Subscription
53
54 constructor (
55 private elementRef: ElementRef,
56 private route: ActivatedRoute,
57 private router: Router,
58 private videoService: VideoService,
59 private videoBlacklistService: VideoBlacklistService,
60 private confirmService: ConfirmService,
61 private metaService: MetaService,
62 private authService: AuthService,
63 private notificationsService: NotificationsService,
64 private markdownService: MarkdownService,
65 private zone: NgZone
66 ) {}
67
68 get user () {
69 return this.authService.getUser()
70 }
71
72 ngOnInit () {
73 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
74 .subscribe(
75 data => {
76 this.otherVideos = data.videos
77 this.updateOtherVideosDisplayed()
78 },
79
80 err => console.error(err)
81 )
82
83 this.paramsSub = this.route.params.subscribe(routeParams => {
84 if (this.videoPlayerLoaded) {
85 this.player.pause()
86 }
87
88 const uuid = routeParams['uuid']
89 // Video did not changed
90 if (this.video && this.video.uuid === uuid) return
91
92 this.videoService.getVideo(uuid).subscribe(
93 video => this.onVideoFetched(video),
94
95 error => {
96 this.videoNotFound = true
97 console.error(error)
98 }
99 )
100 })
101 }
102
103 ngOnDestroy () {
104 // Remove player if it exists
105 if (this.videoPlayerLoaded === true) {
106 videojs(this.playerElement).dispose()
107 }
108
109 // Unsubscribe subscriptions
110 this.paramsSub.unsubscribe()
111 }
112
113 setLike () {
114 if (this.isUserLoggedIn() === false) return
115 if (this.userRating === 'like') {
116 // Already liked this video
117 this.setRating('none')
118 } else {
119 this.setRating('like')
120 }
121 }
122
123 setDislike () {
124 if (this.isUserLoggedIn() === false) return
125 if (this.userRating === 'dislike') {
126 // Already disliked this video
127 this.setRating('none')
128 } else {
129 this.setRating('dislike')
130 }
131 }
132
133 async blacklistVideo (event: Event) {
134 event.preventDefault()
135
136 const res = await this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist')
137 if (res === false) return
138
139 this.videoBlacklistService.blacklistVideo(this.video.id)
140 .subscribe(
141 status => {
142 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
143 this.router.navigate(['/videos/list'])
144 },
145
146 error => this.notificationsService.error('Error', error.message)
147 )
148 }
149
150 showMoreDescription () {
151 if (this.completeVideoDescription === undefined) {
152 return this.loadCompleteDescription()
153 }
154
155 this.updateVideoDescription(this.completeVideoDescription)
156 this.completeDescriptionShown = true
157 }
158
159 showLessDescription () {
160 this.updateVideoDescription(this.shortVideoDescription)
161 this.completeDescriptionShown = false
162 }
163
164 loadCompleteDescription () {
165 this.descriptionLoading = true
166
167 this.videoService.loadCompleteDescription(this.video.descriptionPath)
168 .subscribe(
169 description => {
170 this.completeDescriptionShown = true
171 this.descriptionLoading = false
172
173 this.shortVideoDescription = this.video.description
174 this.completeVideoDescription = description
175
176 this.updateVideoDescription(this.completeVideoDescription)
177 },
178
179 error => {
180 this.descriptionLoading = false
181 this.notificationsService.error('Error', error.message)
182 }
183 )
184 }
185
186 showReportModal (event: Event) {
187 event.preventDefault()
188 this.videoReportModal.show()
189 }
190
191 showSupportModal () {
192 this.videoSupportModal.show()
193 }
194
195 showShareModal () {
196 this.videoShareModal.show()
197 }
198
199 showDownloadModal (event: Event) {
200 event.preventDefault()
201 this.videoDownloadModal.show()
202 }
203
204 isUserLoggedIn () {
205 return this.authService.isLoggedIn()
206 }
207
208 isVideoUpdatable () {
209 return this.video.isUpdatableBy(this.authService.getUser())
210 }
211
212 isVideoBlacklistable () {
213 return this.video.isBlackistableBy(this.user)
214 }
215
216 getAvatarPath () {
217 return Account.GET_ACCOUNT_AVATAR_URL(this.video.account)
218 }
219
220 getVideoPoster () {
221 if (!this.video) return ''
222
223 return this.video.previewUrl
224 }
225
226 getVideoTags () {
227 if (!this.video || Array.isArray(this.video.tags) === false) return []
228
229 return this.video.tags.join(', ')
230 }
231
232 isVideoRemovable () {
233 return this.video.isRemovableBy(this.authService.getUser())
234 }
235
236 async removeVideo (event: Event) {
237 event.preventDefault()
238
239 const res = await this.confirmService.confirm('Do you really want to delete this video?', 'Delete')
240 if (res === false) return
241
242 this.videoService.removeVideo(this.video.id)
243 .subscribe(
244 status => {
245 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
246
247 // Go back to the video-list.
248 this.router.navigate([ '/videos/list' ])
249 },
250
251 error => this.notificationsService.error('Error', error.message)
252 )
253 }
254
255 private updateVideoDescription (description: string) {
256 this.video.description = description
257 this.setVideoDescriptionHTML()
258 }
259
260 private setVideoDescriptionHTML () {
261 if (!this.video.description) {
262 this.videoHTMLDescription = ''
263 return
264 }
265
266 this.videoHTMLDescription = this.markdownService.textMarkdownToHTML(this.video.description)
267 }
268
269 private setVideoLikesBarTooltipText () {
270 this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes`
271 }
272
273 private handleError (err: any) {
274 const errorMessage: string = typeof err === 'string' ? err : err.message
275 let message = ''
276
277 if (errorMessage.indexOf('http error') !== -1) {
278 message = 'Cannot fetch video from server, maybe down.'
279 } else {
280 message = errorMessage
281 }
282
283 this.notificationsService.error('Error', message)
284 }
285
286 private checkUserRating () {
287 // Unlogged users do not have ratings
288 if (this.isUserLoggedIn() === false) return
289
290 this.videoService.getUserVideoRating(this.video.id)
291 .subscribe(
292 ratingObject => {
293 if (ratingObject) {
294 this.userRating = ratingObject.rating
295 }
296 },
297
298 err => this.notificationsService.error('Error', err.message)
299 )
300 }
301
302 private onVideoFetched (video: VideoDetails) {
303 this.video = video
304
305 this.updateOtherVideosDisplayed()
306
307 let observable
308 if (this.video.isVideoNSFWForUser(this.user)) {
309 observable = this.confirmService.confirm(
310 'This video contains mature or explicit content. Are you sure you want to watch it?',
311 'Mature or explicit content'
312 )
313 } else {
314 observable = Observable.of(true)
315 }
316
317 observable.subscribe(
318 res => {
319 if (res === false) {
320
321 return this.router.navigate([ '/videos/list' ])
322 }
323
324 // Player was already loaded
325 if (this.videoPlayerLoaded !== true) {
326 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
327
328 // If autoplay is true, we don't really need a poster
329 if (this.isAutoplay() === false) {
330 this.playerElement.poster = this.video.previewUrl
331 }
332
333 const videojsOptions = {
334 controls: true,
335 autoplay: this.isAutoplay(),
336 plugins: {
337 peertube: {
338 videoFiles: this.video.files,
339 playerElement: this.playerElement,
340 peerTubeLink: false,
341 videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid),
342 videoDuration: this.video.duration
343 },
344 hotkeys: {
345 enableVolumeScroll: false
346 }
347 }
348 }
349
350 this.videoPlayerLoaded = true
351
352 const self = this
353 this.zone.runOutsideAngular(() => {
354 videojs(this.playerElement, videojsOptions, function () {
355 self.player = this
356 this.on('customError', (event, data) => {
357 self.handleError(data.err)
358 })
359 })
360 })
361 } else {
362 const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid)
363 this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration)
364 }
365
366 this.setVideoDescriptionHTML()
367 this.setVideoLikesBarTooltipText()
368
369 this.setOpenGraphTags()
370 this.checkUserRating()
371 }
372 )
373 }
374
375 private setRating (nextRating) {
376 let method
377 switch (nextRating) {
378 case 'like':
379 method = this.videoService.setVideoLike
380 break
381 case 'dislike':
382 method = this.videoService.setVideoDislike
383 break
384 case 'none':
385 method = this.videoService.unsetVideoLike
386 break
387 }
388
389 method.call(this.videoService, this.video.id)
390 .subscribe(
391 () => {
392 // Update the video like attribute
393 this.updateVideoRating(this.userRating, nextRating)
394 this.userRating = nextRating
395 },
396 err => this.notificationsService.error('Error', err.message)
397 )
398 }
399
400 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
401 let likesToIncrement = 0
402 let dislikesToIncrement = 0
403
404 if (oldRating) {
405 if (oldRating === 'like') likesToIncrement--
406 if (oldRating === 'dislike') dislikesToIncrement--
407 }
408
409 if (newRating === 'like') likesToIncrement++
410 if (newRating === 'dislike') dislikesToIncrement++
411
412 this.video.likes += likesToIncrement
413 this.video.dislikes += dislikesToIncrement
414 }
415
416 private updateOtherVideosDisplayed () {
417 if (this.video && this.otherVideos && this.otherVideos.length > 0) {
418 this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid)
419 }
420 }
421
422 private setOpenGraphTags () {
423 this.metaService.setTitle(this.video.name)
424
425 this.metaService.setTag('og:type', 'video')
426
427 this.metaService.setTag('og:title', this.video.name)
428 this.metaService.setTag('name', this.video.name)
429
430 this.metaService.setTag('og:description', this.video.description)
431 this.metaService.setTag('description', this.video.description)
432
433 this.metaService.setTag('og:image', this.video.previewPath)
434
435 this.metaService.setTag('og:duration', this.video.duration.toString())
436
437 this.metaService.setTag('og:site_name', 'PeerTube')
438
439 this.metaService.setTag('og:url', window.location.href)
440 this.metaService.setTag('url', window.location.href)
441 }
442
443 private isAutoplay () {
444 // True by default
445 if (!this.user) return true
446
447 // Be sure the autoPlay is set to false
448 return this.user.autoPlayVideo !== false
449 }
450 }