]>
Commit | Line | Data |
---|---|---|
1 | import { Component, ElementRef, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' | |
2 | import { ActivatedRoute, Router } from '@angular/router' | |
3 | import { MetaService } from '@ngx-meta/core' | |
4 | import { NotificationsService } from 'angular2-notifications' | |
5 | import { Observable } from 'rxjs/Observable' | |
6 | import { Subscription } from 'rxjs/Subscription' | |
7 | import * as videojs from 'video.js' | |
8 | import 'videojs-hotkeys' | |
9 | import { UserVideoRateType, VideoRateType } from '../../../../../shared' | |
10 | import '../../../assets/player/peertube-videojs-plugin' | |
11 | import { AuthService, ConfirmService } from '../../core' | |
12 | import { VideoBlacklistService } from '../../shared' | |
13 | import { Account } from '../../shared/account/account.model' | |
14 | import { VideoDetails } from '../../shared/video/video-details.model' | |
15 | import { Video } from '../../shared/video/video.model' | |
16 | import { VideoService } from '../../shared/video/video.service' | |
17 | import { MarkdownService } from '../shared' | |
18 | import { VideoDownloadComponent } from './modal/video-download.component' | |
19 | import { VideoReportComponent } from './modal/video-report.component' | |
20 | import { VideoShareComponent } from './modal/video-share.component' | |
21 | ||
22 | @Component({ | |
23 | selector: 'my-video-watch', | |
24 | templateUrl: './video-watch.component.html', | |
25 | styleUrls: [ './video-watch.component.scss' ] | |
26 | }) | |
27 | export class VideoWatchComponent implements OnInit, OnDestroy { | |
28 | @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent | |
29 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent | |
30 | @ViewChild('videoReportModal') videoReportModal: VideoReportComponent | |
31 | ||
32 | otherVideosDisplayed: Video[] = [] | |
33 | ||
34 | error = false | |
35 | player: videojs.Player | |
36 | playerElement: HTMLVideoElement | |
37 | userRating: UserVideoRateType = null | |
38 | video: VideoDetails = null | |
39 | videoPlayerLoaded = false | |
40 | videoNotFound = false | |
41 | descriptionLoading = false | |
42 | ||
43 | completeDescriptionShown = false | |
44 | completeVideoDescription: string | |
45 | shortVideoDescription: string | |
46 | videoHTMLDescription = '' | |
47 | likesBarTooltipText = '' | |
48 | ||
49 | private otherVideos: Video[] = [] | |
50 | private paramsSub: Subscription | |
51 | ||
52 | constructor ( | |
53 | private elementRef: ElementRef, | |
54 | private route: ActivatedRoute, | |
55 | private router: Router, | |
56 | private videoService: VideoService, | |
57 | private videoBlacklistService: VideoBlacklistService, | |
58 | private confirmService: ConfirmService, | |
59 | private metaService: MetaService, | |
60 | private authService: AuthService, | |
61 | private notificationsService: NotificationsService, | |
62 | private markdownService: MarkdownService, | |
63 | private zone: NgZone | |
64 | ) {} | |
65 | ||
66 | get user () { | |
67 | return this.authService.getUser() | |
68 | } | |
69 | ||
70 | ngOnInit () { | |
71 | this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt') | |
72 | .subscribe( | |
73 | data => { | |
74 | this.otherVideos = data.videos | |
75 | this.updateOtherVideosDisplayed() | |
76 | }, | |
77 | ||
78 | err => console.error(err) | |
79 | ) | |
80 | ||
81 | this.paramsSub = this.route.params.subscribe(routeParams => { | |
82 | if (this.videoPlayerLoaded) { | |
83 | this.player.pause() | |
84 | } | |
85 | ||
86 | const uuid = routeParams['uuid'] | |
87 | // Video did not changed | |
88 | if (this.video && this.video.uuid === uuid) return | |
89 | ||
90 | this.videoService.getVideo(uuid).subscribe( | |
91 | video => this.onVideoFetched(video), | |
92 | ||
93 | error => { | |
94 | this.videoNotFound = true | |
95 | console.error(error) | |
96 | } | |
97 | ) | |
98 | }) | |
99 | } | |
100 | ||
101 | ngOnDestroy () { | |
102 | // Remove player if it exists | |
103 | if (this.videoPlayerLoaded === true) { | |
104 | videojs(this.playerElement).dispose() | |
105 | } | |
106 | ||
107 | // Unsubscribe subscriptions | |
108 | this.paramsSub.unsubscribe() | |
109 | } | |
110 | ||
111 | setLike () { | |
112 | if (this.isUserLoggedIn() === false) return | |
113 | if (this.userRating === 'like') { | |
114 | // Already liked this video | |
115 | this.setRating('none') | |
116 | } else { | |
117 | this.setRating('like') | |
118 | } | |
119 | } | |
120 | ||
121 | setDislike () { | |
122 | if (this.isUserLoggedIn() === false) return | |
123 | if (this.userRating === 'dislike') { | |
124 | // Already disliked this video | |
125 | this.setRating('none') | |
126 | } else { | |
127 | this.setRating('dislike') | |
128 | } | |
129 | } | |
130 | ||
131 | blacklistVideo (event: Event) { | |
132 | event.preventDefault() | |
133 | ||
134 | this.confirmService.confirm('Do you really want to blacklist this video?', 'Blacklist').subscribe( | |
135 | res => { | |
136 | if (res === false) return | |
137 | ||
138 | this.videoBlacklistService.blacklistVideo(this.video.id) | |
139 | .subscribe( | |
140 | status => { | |
141 | this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`) | |
142 | this.router.navigate(['/videos/list']) | |
143 | }, | |
144 | ||
145 | error => this.notificationsService.error('Error', error.message) | |
146 | ) | |
147 | } | |
148 | ) | |
149 | } | |
150 | ||
151 | showMoreDescription () { | |
152 | if (this.completeVideoDescription === undefined) { | |
153 | return this.loadCompleteDescription() | |
154 | } | |
155 | ||
156 | this.updateVideoDescription(this.completeVideoDescription) | |
157 | this.completeDescriptionShown = true | |
158 | } | |
159 | ||
160 | showLessDescription () { | |
161 | this.updateVideoDescription(this.shortVideoDescription) | |
162 | this.completeDescriptionShown = false | |
163 | } | |
164 | ||
165 | loadCompleteDescription () { | |
166 | this.descriptionLoading = true | |
167 | ||
168 | this.videoService.loadCompleteDescription(this.video.descriptionPath) | |
169 | .subscribe( | |
170 | description => { | |
171 | this.completeDescriptionShown = true | |
172 | this.descriptionLoading = false | |
173 | ||
174 | this.shortVideoDescription = this.video.description | |
175 | this.completeVideoDescription = description | |
176 | ||
177 | this.updateVideoDescription(this.completeVideoDescription) | |
178 | }, | |
179 | ||
180 | error => { | |
181 | this.descriptionLoading = false | |
182 | this.notificationsService.error('Error', error.message) | |
183 | } | |
184 | ) | |
185 | } | |
186 | ||
187 | showReportModal (event: Event) { | |
188 | event.preventDefault() | |
189 | this.videoReportModal.show() | |
190 | } | |
191 | ||
192 | showShareModal () { | |
193 | this.videoShareModal.show() | |
194 | } | |
195 | ||
196 | showDownloadModal (event: Event) { | |
197 | event.preventDefault() | |
198 | this.videoDownloadModal.show() | |
199 | } | |
200 | ||
201 | isUserLoggedIn () { | |
202 | return this.authService.isLoggedIn() | |
203 | } | |
204 | ||
205 | isVideoUpdatable () { | |
206 | return this.video.isUpdatableBy(this.authService.getUser()) | |
207 | } | |
208 | ||
209 | isVideoBlacklistable () { | |
210 | return this.video.isBlackistableBy(this.user) | |
211 | } | |
212 | ||
213 | getAvatarPath () { | |
214 | return Account.GET_ACCOUNT_AVATAR_URL(this.video.account) | |
215 | } | |
216 | ||
217 | getVideoPoster () { | |
218 | if (!this.video) return '' | |
219 | ||
220 | return this.video.previewUrl | |
221 | } | |
222 | ||
223 | getVideoTags () { | |
224 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
225 | ||
226 | return this.video.tags.join(', ') | |
227 | } | |
228 | ||
229 | isVideoRemovable () { | |
230 | return this.video.isRemovableBy(this.authService.getUser()) | |
231 | } | |
232 | ||
233 | removeVideo (event: Event) { | |
234 | event.preventDefault() | |
235 | ||
236 | this.confirmService.confirm('Do you really want to delete this video?', 'Delete') | |
237 | .subscribe( | |
238 | res => { | |
239 | if (res === false) return | |
240 | ||
241 | this.videoService.removeVideo(this.video.id) | |
242 | .subscribe( | |
243 | status => { | |
244 | this.notificationsService.success('Success', `Video ${this.video.name} deleted.`) | |
245 | ||
246 | // Go back to the video-list. | |
247 | this.router.navigate([ '/videos/list' ]) | |
248 | }, | |
249 | ||
250 | error => this.notificationsService.error('Error', error.message) | |
251 | ) | |
252 | } | |
253 | ) | |
254 | } | |
255 | ||
256 | private updateVideoDescription (description: string) { | |
257 | this.video.description = description | |
258 | this.setVideoDescriptionHTML() | |
259 | } | |
260 | ||
261 | private setVideoDescriptionHTML () { | |
262 | if (!this.video.description) { | |
263 | this.videoHTMLDescription = '' | |
264 | return | |
265 | } | |
266 | ||
267 | this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description) | |
268 | } | |
269 | ||
270 | private setVideoLikesBarTooltipText () { | |
271 | this.likesBarTooltipText = `${this.video.likes} likes / ${this.video.dislikes} dislikes` | |
272 | } | |
273 | ||
274 | private handleError (err: any) { | |
275 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
276 | let message = '' | |
277 | ||
278 | if (errorMessage.indexOf('http error') !== -1) { | |
279 | message = 'Cannot fetch video from server, maybe down.' | |
280 | } else { | |
281 | message = errorMessage | |
282 | } | |
283 | ||
284 | this.notificationsService.error('Error', message) | |
285 | } | |
286 | ||
287 | private checkUserRating () { | |
288 | // Unlogged users do not have ratings | |
289 | if (this.isUserLoggedIn() === false) return | |
290 | ||
291 | this.videoService.getUserVideoRating(this.video.id) | |
292 | .subscribe( | |
293 | ratingObject => { | |
294 | if (ratingObject) { | |
295 | this.userRating = ratingObject.rating | |
296 | } | |
297 | }, | |
298 | ||
299 | err => this.notificationsService.error('Error', err.message) | |
300 | ) | |
301 | } | |
302 | ||
303 | private onVideoFetched (video: VideoDetails) { | |
304 | this.video = video | |
305 | ||
306 | this.updateOtherVideosDisplayed() | |
307 | ||
308 | let observable | |
309 | if (this.video.isVideoNSFWForUser(this.user)) { | |
310 | observable = this.confirmService.confirm( | |
311 | 'This video contains mature or explicit content. Are you sure you want to watch it?', | |
312 | 'Mature or explicit content' | |
313 | ) | |
314 | } else { | |
315 | observable = Observable.of(true) | |
316 | } | |
317 | ||
318 | observable.subscribe( | |
319 | res => { | |
320 | if (res === false) { | |
321 | ||
322 | return this.router.navigate([ '/videos/list' ]) | |
323 | } | |
324 | ||
325 | // Player was already loaded | |
326 | if (this.videoPlayerLoaded !== true) { | |
327 | this.playerElement = this.elementRef.nativeElement.querySelector('#video-element') | |
328 | ||
329 | // If autoplay is true, we don't really need a poster | |
330 | if (this.isAutoplay() === false) { | |
331 | this.playerElement.poster = this.video.previewUrl | |
332 | } | |
333 | ||
334 | const videojsOptions = { | |
335 | controls: true, | |
336 | autoplay: this.isAutoplay(), | |
337 | plugins: { | |
338 | peertube: { | |
339 | videoFiles: this.video.files, | |
340 | playerElement: this.playerElement, | |
341 | peerTubeLink: false, | |
342 | videoViewUrl: this.videoService.getVideoViewUrl(this.video.uuid), | |
343 | videoDuration: this.video.duration | |
344 | }, | |
345 | hotkeys: { | |
346 | enableVolumeScroll: false | |
347 | } | |
348 | } | |
349 | } | |
350 | ||
351 | this.videoPlayerLoaded = true | |
352 | ||
353 | const self = this | |
354 | this.zone.runOutsideAngular(() => { | |
355 | videojs(this.playerElement, videojsOptions, function () { | |
356 | self.player = this | |
357 | this.on('customError', (event, data) => { | |
358 | self.handleError(data.err) | |
359 | }) | |
360 | }) | |
361 | }) | |
362 | } else { | |
363 | const videoViewUrl = this.videoService.getVideoViewUrl(this.video.uuid) | |
364 | this.player.peertube().setVideoFiles(this.video.files, videoViewUrl, this.video.duration) | |
365 | } | |
366 | ||
367 | this.setVideoDescriptionHTML() | |
368 | this.setVideoLikesBarTooltipText() | |
369 | ||
370 | this.setOpenGraphTags() | |
371 | this.checkUserRating() | |
372 | } | |
373 | ) | |
374 | } | |
375 | ||
376 | private setRating (nextRating) { | |
377 | let method | |
378 | switch (nextRating) { | |
379 | case 'like': | |
380 | method = this.videoService.setVideoLike | |
381 | break | |
382 | case 'dislike': | |
383 | method = this.videoService.setVideoDislike | |
384 | break | |
385 | case 'none': | |
386 | method = this.videoService.unsetVideoLike | |
387 | break | |
388 | } | |
389 | ||
390 | method.call(this.videoService, this.video.id) | |
391 | .subscribe( | |
392 | () => { | |
393 | // Update the video like attribute | |
394 | this.updateVideoRating(this.userRating, nextRating) | |
395 | this.userRating = nextRating | |
396 | }, | |
397 | err => this.notificationsService.error('Error', err.message) | |
398 | ) | |
399 | } | |
400 | ||
401 | private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) { | |
402 | let likesToIncrement = 0 | |
403 | let dislikesToIncrement = 0 | |
404 | ||
405 | if (oldRating) { | |
406 | if (oldRating === 'like') likesToIncrement-- | |
407 | if (oldRating === 'dislike') dislikesToIncrement-- | |
408 | } | |
409 | ||
410 | if (newRating === 'like') likesToIncrement++ | |
411 | if (newRating === 'dislike') dislikesToIncrement++ | |
412 | ||
413 | this.video.likes += likesToIncrement | |
414 | this.video.dislikes += dislikesToIncrement | |
415 | } | |
416 | ||
417 | private updateOtherVideosDisplayed () { | |
418 | if (this.video && this.otherVideos && this.otherVideos.length > 0) { | |
419 | this.otherVideosDisplayed = this.otherVideos.filter(v => v.uuid !== this.video.uuid) | |
420 | } | |
421 | } | |
422 | ||
423 | private setOpenGraphTags () { | |
424 | this.metaService.setTitle(this.video.name) | |
425 | ||
426 | this.metaService.setTag('og:type', 'video') | |
427 | ||
428 | this.metaService.setTag('og:title', this.video.name) | |
429 | this.metaService.setTag('name', this.video.name) | |
430 | ||
431 | this.metaService.setTag('og:description', this.video.description) | |
432 | this.metaService.setTag('description', this.video.description) | |
433 | ||
434 | this.metaService.setTag('og:image', this.video.previewPath) | |
435 | ||
436 | this.metaService.setTag('og:duration', this.video.duration.toString()) | |
437 | ||
438 | this.metaService.setTag('og:site_name', 'PeerTube') | |
439 | ||
440 | this.metaService.setTag('og:url', window.location.href) | |
441 | this.metaService.setTag('url', window.location.href) | |
442 | } | |
443 | ||
444 | private isAutoplay () { | |
445 | // True by default | |
446 | if (!this.user) return true | |
447 | ||
448 | // Be sure the autoPlay is set to false | |
449 | return this.user.autoPlayVideo !== false | |
450 | } | |
451 | } |