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