]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
Use typescript standard and lint all files
[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 * as 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 { RateType, Video, VideoService } from '../shared'
15 import { WebTorrentService } from './webtorrent.service'
16
17 @Component({
18 selector: 'my-video-watch',
19 templateUrl: './video-watch.component.html',
20 styleUrls: [ './video-watch.component.scss' ]
21 })
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: RateType = 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: false
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, { autoplay: true }, (err) => {
131 if (err) {
132 this.notificationsService.error('Error', 'Cannot append the file in the video element.')
133 console.error(err)
134 }
135 })
136
137 this.runInProgress(torrent)
138 })
139 }
140
141 setLike () {
142 if (this.isUserLoggedIn() === false) return
143 // Already liked this video
144 if (this.userRating === 'like') return
145
146 this.videoService.setVideoLike(this.video.id)
147 .subscribe(
148 () => {
149 // Update the video like attribute
150 this.updateVideoRating(this.userRating, 'like')
151 this.userRating = 'like'
152 },
153
154 err => this.notificationsService.error('Error', err.text)
155 )
156 }
157
158 setDislike () {
159 if (this.isUserLoggedIn() === false) return
160 // Already disliked this video
161 if (this.userRating === 'dislike') return
162
163 this.videoService.setVideoDislike(this.video.id)
164 .subscribe(
165 () => {
166 // Update the video dislike attribute
167 this.updateVideoRating(this.userRating, 'dislike')
168 this.userRating = 'dislike'
169 },
170
171 err => this.notificationsService.error('Error', err.text)
172 )
173 }
174
175 removeVideo (event: Event) {
176 event.preventDefault()
177
178 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
179 res => {
180 if (res === false) return
181
182 this.videoService.removeVideo(this.video.id)
183 .subscribe(
184 status => {
185 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
186 // Go back to the video-list.
187 this.router.navigate(['/videos/list'])
188 },
189
190 error => this.notificationsService.error('Error', error.text)
191 )
192 }
193 )
194 }
195
196 blacklistVideo (event: Event) {
197 event.preventDefault()
198
199 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
200 res => {
201 if (res === false) return
202
203 this.videoService.blacklistVideo(this.video.id)
204 .subscribe(
205 status => {
206 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
207 this.router.navigate(['/videos/list'])
208 },
209
210 error => this.notificationsService.error('Error', error.text)
211 )
212 }
213 )
214 }
215
216 showReportModal (event: Event) {
217 event.preventDefault()
218 this.videoReportModal.show()
219 }
220
221 showShareModal () {
222 this.videoShareModal.show()
223 }
224
225 showMagnetUriModal (event: Event) {
226 event.preventDefault()
227 this.videoMagnetModal.show()
228 }
229
230 isUserLoggedIn () {
231 return this.authService.isLoggedIn()
232 }
233
234 canUserUpdateVideo () {
235 return this.video.isUpdatableBy(this.authService.getUser())
236 }
237
238 isVideoRemovable () {
239 return this.video.isRemovableBy(this.authService.getUser())
240 }
241
242 isVideoBlacklistable () {
243 return this.video.isBlackistableBy(this.authService.getUser())
244 }
245
246 private checkUserRating () {
247 // Unlogged users do not have ratings
248 if (this.isUserLoggedIn() === false) return
249
250 this.videoService.getUserVideoRating(this.video.id)
251 .subscribe(
252 ratingObject => {
253 if (ratingObject) {
254 this.userRating = ratingObject.rating
255 }
256 },
257
258 err => this.notificationsService.error('Error', err.text)
259 )
260 }
261
262 private onVideoFetched (video: Video) {
263 this.video = video
264
265 let observable
266 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
267 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
268 } else {
269 observable = Observable.of(true)
270 }
271
272 observable.subscribe(
273 res => {
274 if (res === false) {
275 return this.router.navigate([ '/videos/list' ])
276 }
277
278 this.setOpenGraphTags()
279 this.loadVideo()
280 this.checkUserRating()
281 }
282 )
283 }
284
285 private updateVideoRating (oldRating: RateType, newRating: RateType) {
286 let likesToIncrement = 0
287 let dislikesToIncrement = 0
288
289 if (oldRating) {
290 if (oldRating === 'like') likesToIncrement--
291 if (oldRating === 'dislike') dislikesToIncrement--
292 }
293
294 if (newRating === 'like') likesToIncrement++
295 if (newRating === 'dislike') dislikesToIncrement++
296
297 this.video.likes += likesToIncrement
298 this.video.dislikes += dislikesToIncrement
299 }
300
301 private loadTooLong () {
302 this.error = true
303 console.error('The video load seems to be abnormally long.')
304 }
305
306 private setOpenGraphTags () {
307 this.metaService.setTitle(this.video.name)
308
309 this.metaService.setTag('og:type', 'video')
310
311 this.metaService.setTag('og:title', this.video.name)
312 this.metaService.setTag('name', this.video.name)
313
314 this.metaService.setTag('og:description', this.video.description)
315 this.metaService.setTag('description', this.video.description)
316
317 this.metaService.setTag('og:image', this.video.thumbnailPath)
318
319 this.metaService.setTag('og:duration', this.video.duration.toString())
320
321 this.metaService.setTag('og:site_name', 'PeerTube')
322
323 this.metaService.setTag('og:url', window.location.href)
324 this.metaService.setTag('url', window.location.href)
325 }
326
327 private runInProgress (torrent: any) {
328 // Refresh each second
329 this.torrentInfosInterval = window.setInterval(() => {
330 this.ngZone.run(() => {
331 this.downloadSpeed = torrent.downloadSpeed
332 this.numPeers = torrent.numPeers
333 this.uploadSpeed = torrent.uploadSpeed
334 })
335 }, 1000)
336 }
337 }