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