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