]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
Better skin for videojs
[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 { Observable } from 'rxjs/Observable'
4 import { Subscription } from 'rxjs/Subscription'
5
6 import videojs from 'video.js'
7 import { MetaService } from '@nglibs/meta'
8 import { NotificationsService } from 'angular2-notifications'
9
10 import { AuthService, ConfirmService } from '../../core'
11 import { VideoMagnetComponent } from './video-magnet.component'
12 import { VideoShareComponent } from './video-share.component'
13 import { VideoReportComponent } from './video-report.component'
14 import { Video, VideoService } from '../shared'
15 import { WebTorrentService } from './webtorrent.service'
16 import { UserVideoRateType, VideoRateType, UserVideoRate } from '../../../../../shared'
17
18 @Component({
19 selector: 'my-video-watch',
20 templateUrl: './video-watch.component.html',
21 styleUrls: [ './video-watch.component.scss' ]
22 })
23 export class VideoWatchComponent implements OnInit, OnDestroy {
24 private static LOADTIME_TOO_LONG = 20000
25
26 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent
27 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent
28 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent
29
30 downloadSpeed: number
31 error = false
32 loading = false
33 numPeers: number
34 player: videojs.Player
35 playerElement: Element
36 uploadSpeed: number
37 userRating: UserVideoRateType = null
38 video: Video = null
39 videoNotFound = false
40
41 private errorTimer: number
42 private paramsSub: Subscription
43 private errorsSub: Subscription
44 private warningsSub: Subscription
45 private torrentInfosInterval: number
46
47 constructor (
48 private elementRef: ElementRef,
49 private ngZone: NgZone,
50 private route: ActivatedRoute,
51 private router: Router,
52 private videoService: VideoService,
53 private confirmService: ConfirmService,
54 private metaService: MetaService,
55 private webTorrentService: WebTorrentService,
56 private authService: AuthService,
57 private notificationsService: NotificationsService
58 ) {}
59
60 ngOnInit () {
61 this.paramsSub = this.route.params.subscribe(routeParams => {
62 let id = routeParams['id']
63 this.videoService.getVideo(id).subscribe(
64 video => this.onVideoFetched(video),
65
66 error => {
67 console.error(error)
68 this.videoNotFound = true
69 }
70 )
71 })
72
73 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
74
75 const videojsOptions = {
76 controls: true,
77 autoplay: true
78 }
79
80 const self = this
81 videojs(this.playerElement, videojsOptions, function () {
82 self.player = this
83 })
84
85 this.errorsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.error('Error', err.message))
86 this.warningsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.alert('Warning', err.message))
87 }
88
89 ngOnDestroy () {
90 // Remove WebTorrent stuff
91 console.log('Removing video from webtorrent.')
92 window.clearInterval(this.torrentInfosInterval)
93 window.clearTimeout(this.errorTimer)
94
95 if (this.video !== null && this.webTorrentService.has(this.video.magnetUri)) {
96 this.webTorrentService.remove(this.video.magnetUri)
97 }
98
99 // Remove player
100 videojs(this.playerElement).dispose()
101
102 // Unsubscribe subscriptions
103 this.paramsSub.unsubscribe()
104 this.errorsSub.unsubscribe()
105 this.warningsSub.unsubscribe()
106 }
107
108 loadVideo () {
109 // Reset the error
110 this.error = false
111 // We are loading the video
112 this.loading = true
113
114 console.log('Adding ' + this.video.magnetUri + '.')
115
116 // The callback might never return if there are network issues
117 // So we create a timer to inform the user the load is abnormally long
118 this.errorTimer = window.setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG)
119
120 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
121 // Clear the error timer
122 window.clearTimeout(this.errorTimer)
123 // Maybe the error was fired by the timer, so reset it
124 this.error = false
125
126 // We are not loading the video anymore
127 this.loading = false
128
129 console.log('Added ' + this.video.magnetUri + '.')
130 torrent.files[0].renderTo(this.playerElement, (err) => {
131 if (err) {
132 this.notificationsService.error('Error', 'Cannot append the file in the video element.')
133 console.error(err)
134 }
135
136 // Hack to "simulate" src link in video.js >= 6
137 // If no, we can't play the video after pausing it
138 // https://github.com/videojs/video.js/blob/master/src/js/player.js#L1633
139 (this.player as any).src = () => true
140
141 this.player.play()
142 })
143
144 this.runInProgress(torrent)
145 })
146 }
147
148 setLike () {
149 if (this.isUserLoggedIn() === false) return
150 // Already liked this video
151 if (this.userRating === 'like') return
152
153 this.videoService.setVideoLike(this.video.id)
154 .subscribe(
155 () => {
156 // Update the video like attribute
157 this.updateVideoRating(this.userRating, 'like')
158 this.userRating = 'like'
159 },
160
161 err => this.notificationsService.error('Error', err.text)
162 )
163 }
164
165 setDislike () {
166 if (this.isUserLoggedIn() === false) return
167 // Already disliked this video
168 if (this.userRating === 'dislike') return
169
170 this.videoService.setVideoDislike(this.video.id)
171 .subscribe(
172 () => {
173 // Update the video dislike attribute
174 this.updateVideoRating(this.userRating, 'dislike')
175 this.userRating = 'dislike'
176 },
177
178 err => this.notificationsService.error('Error', err.text)
179 )
180 }
181
182 removeVideo (event: Event) {
183 event.preventDefault()
184
185 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
186 res => {
187 if (res === false) return
188
189 this.videoService.removeVideo(this.video.id)
190 .subscribe(
191 status => {
192 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
193 // Go back to the video-list.
194 this.router.navigate(['/videos/list'])
195 },
196
197 error => this.notificationsService.error('Error', error.text)
198 )
199 }
200 )
201 }
202
203 blacklistVideo (event: Event) {
204 event.preventDefault()
205
206 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
207 res => {
208 if (res === false) return
209
210 this.videoService.blacklistVideo(this.video.id)
211 .subscribe(
212 status => {
213 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
214 this.router.navigate(['/videos/list'])
215 },
216
217 error => this.notificationsService.error('Error', error.text)
218 )
219 }
220 )
221 }
222
223 showReportModal (event: Event) {
224 event.preventDefault()
225 this.videoReportModal.show()
226 }
227
228 showShareModal () {
229 this.videoShareModal.show()
230 }
231
232 showMagnetUriModal (event: Event) {
233 event.preventDefault()
234 this.videoMagnetModal.show()
235 }
236
237 isUserLoggedIn () {
238 return this.authService.isLoggedIn()
239 }
240
241 canUserUpdateVideo () {
242 return this.video.isUpdatableBy(this.authService.getUser())
243 }
244
245 isVideoRemovable () {
246 return this.video.isRemovableBy(this.authService.getUser())
247 }
248
249 isVideoBlacklistable () {
250 return this.video.isBlackistableBy(this.authService.getUser())
251 }
252
253 private checkUserRating () {
254 // Unlogged users do not have ratings
255 if (this.isUserLoggedIn() === false) return
256
257 this.videoService.getUserVideoRating(this.video.id)
258 .subscribe(
259 ratingObject => {
260 if (ratingObject) {
261 this.userRating = ratingObject.rating
262 }
263 },
264
265 err => this.notificationsService.error('Error', err.text)
266 )
267 }
268
269 private onVideoFetched (video: Video) {
270 this.video = video
271
272 let observable
273 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
274 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
275 } else {
276 observable = Observable.of(true)
277 }
278
279 observable.subscribe(
280 res => {
281 if (res === false) {
282 return this.router.navigate([ '/videos/list' ])
283 }
284
285 this.setOpenGraphTags()
286 this.loadVideo()
287 this.checkUserRating()
288 }
289 )
290 }
291
292 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
293 let likesToIncrement = 0
294 let dislikesToIncrement = 0
295
296 if (oldRating) {
297 if (oldRating === 'like') likesToIncrement--
298 if (oldRating === 'dislike') dislikesToIncrement--
299 }
300
301 if (newRating === 'like') likesToIncrement++
302 if (newRating === 'dislike') dislikesToIncrement++
303
304 this.video.likes += likesToIncrement
305 this.video.dislikes += dislikesToIncrement
306 }
307
308 private loadTooLong () {
309 this.error = true
310 console.error('The video load seems to be abnormally long.')
311 }
312
313 private setOpenGraphTags () {
314 this.metaService.setTitle(this.video.name)
315
316 this.metaService.setTag('og:type', 'video')
317
318 this.metaService.setTag('og:title', this.video.name)
319 this.metaService.setTag('name', this.video.name)
320
321 this.metaService.setTag('og:description', this.video.description)
322 this.metaService.setTag('description', this.video.description)
323
324 this.metaService.setTag('og:image', this.video.thumbnailPath)
325
326 this.metaService.setTag('og:duration', this.video.duration.toString())
327
328 this.metaService.setTag('og:site_name', 'PeerTube')
329
330 this.metaService.setTag('og:url', window.location.href)
331 this.metaService.setTag('url', window.location.href)
332 }
333
334 private runInProgress (torrent: any) {
335 // Refresh each second
336 this.torrentInfosInterval = window.setInterval(() => {
337 this.ngZone.run(() => {
338 this.downloadSpeed = torrent.downloadSpeed
339 this.numPeers = torrent.numPeers
340 this.uploadSpeed = torrent.uploadSpeed
341 })
342 }, 1000)
343 }
344 }