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