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