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