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