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