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