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';
6 import * as videojs from 'video.js';
7 import { MetaService } from '@nglibs/meta';
8 import { NotificationsService } from 'angular2-notifications';
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';
18 selector: 'my-video-watch',
19 templateUrl: './video-watch.component.html',
20 styleUrls: [ './video-watch.component.scss' ]
23 export class VideoWatchComponent implements OnInit, OnDestroy {
24 private static LOADTIME_TOO_LONG: number = 20000;
26 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent;
27 @ViewChild('videoShareModal') videoShareModal: VideoShareComponent;
28 @ViewChild('videoReportModal') videoReportModal: VideoReportComponent;
30 downloadSpeed: number;
31 error: boolean = false;
32 loading: boolean = false;
34 player: VideoJSPlayer;
35 playerElement: Element;
37 userRating: RateType = null;
39 videoNotFound = false;
41 private errorTimer: number;
42 private paramsSub: Subscription;
43 private errorsSub: Subscription;
44 private warningsSub: Subscription;
45 private torrentInfosInterval: number;
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
61 this.paramsSub = this.route.params.subscribe(routeParams => {
62 let id = routeParams['id'];
63 this.videoService.getVideo(id).subscribe(
64 video => this.onVideoFetched(video),
66 error => this.videoNotFound = true
70 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container');
72 const videojsOptions = {
78 videojs(this.playerElement, videojsOptions, function () {
82 this.errorsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.error('Error', err.message));
83 this.warningsSub = this.webTorrentService.errors.subscribe(err => this.notificationsService.alert('Warning', err.message));
87 // Remove WebTorrent stuff
88 console.log('Removing video from webtorrent.');
89 window.clearInterval(this.torrentInfosInterval);
90 window.clearTimeout(this.errorTimer);
92 if (this.video !== null && this.webTorrentService.has(this.video.magnetUri)) {
93 this.webTorrentService.remove(this.video.magnetUri);
97 videojs(this.playerElement).dispose();
99 // Unsubscribe subscriptions
100 this.paramsSub.unsubscribe();
101 this.errorsSub.unsubscribe();
102 this.warningsSub.unsubscribe();
108 // We are loading the video
111 console.log('Adding ' + this.video.magnetUri + '.');
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
115 this.errorTimer = window.setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
117 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
118 // Clear the error timer
119 window.clearTimeout(this.errorTimer);
120 // Maybe the error was fired by the timer, so reset it
123 // We are not loading the video anymore
124 this.loading = false;
126 console.log('Added ' + this.video.magnetUri + '.');
127 torrent.files[0].renderTo(this.playerElement, { autoplay: true }, (err) => {
129 this.notificationsService.error('Error', 'Cannot append the file in the video element.');
134 this.runInProgress(torrent);
139 if (this.isUserLoggedIn() === false) return;
140 // Already liked this video
141 if (this.userRating === 'like') return;
143 this.videoService.setVideoLike(this.video.id)
146 // Update the video like attribute
147 this.updateVideoRating(this.userRating, 'like');
148 this.userRating = 'like';
151 err => this.notificationsService.error('Error', err.text)
156 if (this.isUserLoggedIn() === false) return;
157 // Already disliked this video
158 if (this.userRating === 'dislike') return;
160 this.videoService.setVideoDislike(this.video.id)
163 // Update the video dislike attribute
164 this.updateVideoRating(this.userRating, 'dislike');
165 this.userRating = 'dislike';
168 err => this.notificationsService.error('Error', err.text)
172 removeVideo(event: Event) {
173 event.preventDefault();
175 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
177 if (res === false) return;
179 this.videoService.removeVideo(this.video.id)
182 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`);
183 // Go back to the video-list.
184 this.router.navigate(['/videos/list']);
187 error => this.notificationsService.error('Error', error.text)
193 blacklistVideo(event: Event) {
194 event.preventDefault();
196 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
198 if (res === false) return;
200 this.videoService.blacklistVideo(this.video.id)
203 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`);
204 this.router.navigate(['/videos/list']);
207 error => this.notificationsService.error('Error', error.text)
213 showReportModal(event: Event) {
214 event.preventDefault();
215 this.videoReportModal.show();
219 this.videoShareModal.show();
222 showMagnetUriModal(event: Event) {
223 event.preventDefault();
224 this.videoMagnetModal.show();
228 return this.authService.isLoggedIn();
231 canUserUpdateVideo() {
232 return this.authService.getUser() !== null &&
233 this.authService.getUser().username === this.video.author;
237 return this.video.isRemovableBy(this.authService.getUser());
240 isVideoBlacklistable() {
241 return this.video.isBlackistableBy(this.authService.getUser());
244 private checkUserRating() {
245 // Unlogged users do not have ratings
246 if (this.isUserLoggedIn() === false) return;
248 this.videoService.getUserVideoRating(this.video.id)
252 this.userRating = ratingObject.rating;
256 err => this.notificationsService.error('Error', err.text)
260 private onVideoFetched(video: Video) {
264 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
265 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW');
267 observable = Observable.of(true);
270 observable.subscribe(
273 return this.router.navigate([ '/videos/list' ]);
276 this.setOpenGraphTags();
278 this.checkUserRating();
283 private updateVideoRating(oldRating: RateType, newRating: RateType) {
284 let likesToIncrement = 0;
285 let dislikesToIncrement = 0;
288 if (oldRating === 'like') likesToIncrement--;
289 if (oldRating === 'dislike') dislikesToIncrement--;
292 if (newRating === 'like') likesToIncrement++;
293 if (newRating === 'dislike') dislikesToIncrement++;
295 this.video.likes += likesToIncrement;
296 this.video.dislikes += dislikesToIncrement;
299 private loadTooLong() {
301 console.error('The video load seems to be abnormally long.');
304 private setOpenGraphTags() {
305 this.metaService.setTitle(this.video.name);
307 this.metaService.setTag('og:type', 'video');
309 this.metaService.setTag('og:title', this.video.name);
310 this.metaService.setTag('name', this.video.name);
312 this.metaService.setTag('og:description', this.video.description);
313 this.metaService.setTag('description', this.video.description);
315 this.metaService.setTag('og:image', this.video.thumbnailPath);
317 this.metaService.setTag('og:duration', this.video.duration);
319 this.metaService.setTag('og:site_name', 'PeerTube');
321 this.metaService.setTag('og:url', window.location.href);
322 this.metaService.setTag('url', window.location.href);
325 private runInProgress(torrent: any) {
326 // Refresh each second
327 this.torrentInfosInterval = window.setInterval(() => {
328 this.ngZone.run(() => {
329 this.downloadSpeed = torrent.downloadSpeed;
330 this.numPeers = torrent.numPeers;
331 this.uploadSpeed = torrent.uploadSpeed;