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