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