]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/videos/video-watch/video-watch.component.ts
Client: add opengraph tags
[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 } from '@angular/router';
3
4 import { ModalDirective } from 'ng2-bootstrap/components/modal';
5 import { MetaService } from 'ng2-meta';
6
7 import { Video, VideoService } from '../shared';
8 import { WebTorrentService } from './webtorrent.service';
9
10 @Component({
11 selector: 'my-video-watch',
12 templateUrl: './video-watch.component.html',
13 styleUrls: [ './video-watch.component.scss' ]
14 })
15
16 export class VideoWatchComponent implements OnInit, OnDestroy {
17 private static LOADTIME_TOO_LONG: number = 30000;
18
19 @ViewChild('magnetUriModal') magnetUriModal: ModalDirective;
20
21 downloadSpeed: number;
22 error: boolean = false;
23 loading: boolean = false;
24 numPeers: number;
25 uploadSpeed: number;
26 video: Video = null;
27
28 private errorTimer: NodeJS.Timer;
29 private sub: any;
30 private torrentInfosInterval: NodeJS.Timer;
31
32 constructor(
33 private elementRef: ElementRef,
34 private ngZone: NgZone,
35 private route: ActivatedRoute,
36 private videoService: VideoService,
37 private metaService: MetaService,
38 private webTorrentService: WebTorrentService
39 ) {}
40
41 ngOnInit() {
42 this.sub = this.route.params.subscribe(routeParams => {
43 let id = routeParams['id'];
44 this.videoService.getVideo(id).subscribe(
45 video => {
46 this.video = video;
47 this.setOpenGraphTags();
48 this.loadVideo();
49 },
50 error => alert(error.text)
51 );
52 });
53 }
54
55 ngOnDestroy() {
56 console.log('Removing video from webtorrent.');
57 clearInterval(this.torrentInfosInterval);
58 clearTimeout(this.errorTimer);
59 this.webTorrentService.remove(this.video.magnetUri);
60
61 this.sub.unsubscribe();
62 }
63
64 loadVideo() {
65 // Reset the error
66 this.error = false;
67 // We are loading the video
68 this.loading = true;
69
70 console.log('Adding ' + this.video.magnetUri + '.');
71
72 // The callback might never return if there are network issues
73 // So we create a timer to inform the user the load is abnormally long
74 this.errorTimer = setTimeout(() => this.loadTooLong(), VideoWatchComponent.LOADTIME_TOO_LONG);
75
76 this.webTorrentService.add(this.video.magnetUri, (torrent) => {
77 // Clear the error timer
78 clearTimeout(this.errorTimer);
79 // Maybe the error was fired by the timer, so reset it
80 this.error = false;
81
82 // We are not loading the video anymore
83 this.loading = false;
84
85 console.log('Added ' + this.video.magnetUri + '.');
86 torrent.files[0].appendTo(this.elementRef.nativeElement.querySelector('.embed-responsive'), (err) => {
87 if (err) {
88 alert('Cannot append the file.');
89 console.error(err);
90 }
91 });
92
93 this.runInProgress(torrent);
94 });
95 }
96
97 showMagnetUriModal() {
98 this.magnetUriModal.show();
99 }
100
101 hideMagnetUriModal() {
102 this.magnetUriModal.hide();
103 }
104
105 private loadTooLong() {
106 this.error = true;
107 console.error('The video load seems to be abnormally long.');
108 }
109
110 private setOpenGraphTags() {
111 this.metaService.setTag('og:type', 'video');
112
113 this.metaService.setTag('og:title', this.video.name);
114 this.metaService.setTag('name', this.video.name);
115
116 this.metaService.setTag('og:description', this.video.description);
117 this.metaService.setTag('description', this.video.description);
118
119 this.metaService.setTag('og:image', this.video.thumbnailPath);
120
121 this.metaService.setTag('og:duration', this.video.duration);
122
123 this.metaService.setTag('og:site_name', 'PeerTube');
124
125 this.metaService.setTag('og:url', window.location.href);
126 this.metaService.setTag('url', window.location.href);
127 }
128
129 private runInProgress(torrent: any) {
130 // Refresh each second
131 this.torrentInfosInterval = setInterval(() => {
132 this.ngZone.run(() => {
133 this.downloadSpeed = torrent.downloadSpeed;
134 this.numPeers = torrent.numPeers;
135 this.uploadSpeed = torrent.uploadSpeed;
136 });
137 }, 1000);
138 }
139 }