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