]>
Commit | Line | Data |
---|---|---|
df98563e C |
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 | ||
80624154 | 6 | import videojs from 'video.js' |
8b13c289 | 7 | import { MetaService } from '@ngx-meta/core' |
df98563e C |
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' | |
154898b0 | 14 | import { Video, VideoService } from '../shared' |
df98563e | 15 | import { WebTorrentService } from './webtorrent.service' |
154898b0 | 16 | import { 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 | 23 | export 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 | |
33c4972d C |
84 | this.errorsSub = this.webTorrentService.errors.subscribe(err => { |
85 | const message = typeof err === 'string' ? err : err.message | |
86 | this.notificationsService.error('Error', message) | |
87 | }) | |
d1992b93 C |
88 | } |
89 | ||
df98563e | 90 | ngOnDestroy () { |
067e3f84 | 91 | // Remove WebTorrent stuff |
df98563e C |
92 | console.log('Removing video from webtorrent.') |
93 | window.clearInterval(this.torrentInfosInterval) | |
94 | window.clearTimeout(this.errorTimer) | |
9c89a45c | 95 | |
92fb909c | 96 | if (this.video !== null && this.webTorrentService.has(this.video.magnetUri)) { |
df98563e | 97 | this.webTorrentService.remove(this.video.magnetUri) |
9c89a45c | 98 | } |
d1992b93 | 99 | |
067e3f84 | 100 | // Remove player |
df98563e | 101 | videojs(this.playerElement).dispose() |
067e3f84 | 102 | |
13fc89f4 | 103 | // Unsubscribe subscriptions |
df98563e C |
104 | this.paramsSub.unsubscribe() |
105 | this.errorsSub.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 | |
33c4972d | 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 + '.') |
e14852b4 | 130 | torrent.files[0].renderTo(this.playerElement, (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 | } |
e14852b4 C |
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() | |
df98563e | 142 | }) |
8cfecb2a | 143 | |
df98563e C |
144 | this.runInProgress(torrent) |
145 | }) | |
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 | ||
161 | err => this.notificationsService.error('Error', err.text) | |
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 | ||
178 | err => this.notificationsService.error('Error', err.text) | |
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 | ||
df98563e | 253 | private checkUserRating () { |
d38b8281 | 254 | // Unlogged users do not have ratings |
df98563e | 255 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
256 | |
257 | this.videoService.getUserVideoRating(this.video.id) | |
258 | .subscribe( | |
b632e904 | 259 | ratingObject => { |
d38b8281 | 260 | if (ratingObject) { |
df98563e | 261 | this.userRating = ratingObject.rating |
d38b8281 C |
262 | } |
263 | }, | |
264 | ||
265 | err => this.notificationsService.error('Error', err.text) | |
df98563e | 266 | ) |
d38b8281 C |
267 | } |
268 | ||
df98563e C |
269 | private onVideoFetched (video: Video) { |
270 | this.video = video | |
92fb909c | 271 | |
df98563e | 272 | let observable |
92fb909c | 273 | if (this.video.isVideoNSFWForUser(this.authService.getUser())) { |
df98563e | 274 | observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW') |
92fb909c | 275 | } else { |
df98563e | 276 | observable = Observable.of(true) |
92fb909c C |
277 | } |
278 | ||
279 | observable.subscribe( | |
280 | res => { | |
281 | if (res === false) { | |
df98563e | 282 | return this.router.navigate([ '/videos/list' ]) |
92fb909c C |
283 | } |
284 | ||
df98563e C |
285 | this.setOpenGraphTags() |
286 | this.loadVideo() | |
287 | this.checkUserRating() | |
92fb909c | 288 | } |
df98563e | 289 | ) |
92fb909c C |
290 | } |
291 | ||
154898b0 | 292 | private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) { |
df98563e C |
293 | let likesToIncrement = 0 |
294 | let dislikesToIncrement = 0 | |
d38b8281 C |
295 | |
296 | if (oldRating) { | |
df98563e C |
297 | if (oldRating === 'like') likesToIncrement-- |
298 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
299 | } |
300 | ||
df98563e C |
301 | if (newRating === 'like') likesToIncrement++ |
302 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 303 | |
df98563e C |
304 | this.video.likes += likesToIncrement |
305 | this.video.dislikes += dislikesToIncrement | |
d38b8281 C |
306 | } |
307 | ||
df98563e C |
308 | private loadTooLong () { |
309 | this.error = true | |
310 | console.error('The video load seems to be abnormally long.') | |
3ad109e4 | 311 | } |
c323efb9 | 312 | |
df98563e C |
313 | private setOpenGraphTags () { |
314 | this.metaService.setTitle(this.video.name) | |
758b996d | 315 | |
df98563e | 316 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 317 | |
df98563e C |
318 | this.metaService.setTag('og:title', this.video.name) |
319 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 320 | |
df98563e C |
321 | this.metaService.setTag('og:description', this.video.description) |
322 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 323 | |
df98563e | 324 | this.metaService.setTag('og:image', this.video.thumbnailPath) |
3ec343a4 | 325 | |
df98563e | 326 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 327 | |
df98563e | 328 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 329 | |
df98563e C |
330 | this.metaService.setTag('og:url', window.location.href) |
331 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 C |
332 | } |
333 | ||
df98563e | 334 | private runInProgress (torrent: any) { |
c323efb9 | 335 | // Refresh each second |
0d4fb7e6 | 336 | this.torrentInfosInterval = window.setInterval(() => { |
c323efb9 | 337 | this.ngZone.run(() => { |
df98563e C |
338 | this.downloadSpeed = torrent.downloadSpeed |
339 | this.numPeers = torrent.numPeers | |
340 | this.uploadSpeed = torrent.uploadSpeed | |
341 | }) | |
342 | }, 1000) | |
c323efb9 | 343 | } |
dc8bc31b | 344 | } |