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