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