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