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