]>
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' |
a96aed15 | 17 | import { VideoDownloadComponent } from './video-download.component' |
df98563e | 18 | import { VideoReportComponent } from './video-report.component' |
1f3e9fec | 19 | import { VideoShareComponent } from './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 C |
31 | otherVideos: Video[] = [] |
32 | ||
df98563e C |
33 | error = false |
34 | loading = false | |
df98563e | 35 | player: videojs.Player |
33c4972d | 36 | playerElement: HTMLMediaElement |
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 = '' |
df98563e | 47 | |
df98563e | 48 | private paramsSub: Subscription |
df98563e C |
49 | |
50 | constructor ( | |
4fd8aa32 | 51 | private elementRef: ElementRef, |
0629423c | 52 | private route: ActivatedRoute, |
92fb909c | 53 | private router: Router, |
d3ef341a | 54 | private videoService: VideoService, |
35bf0c83 | 55 | private videoBlacklistService: VideoBlacklistService, |
92fb909c | 56 | private confirmService: ConfirmService, |
3ec343a4 | 57 | private metaService: MetaService, |
7ddd02c9 | 58 | private authService: AuthService, |
9d9597df C |
59 | private notificationsService: NotificationsService, |
60 | private markdownService: MarkdownService | |
d3ef341a | 61 | ) {} |
dc8bc31b | 62 | |
b2731bff C |
63 | get user () { |
64 | return this.authService.getUser() | |
65 | } | |
66 | ||
df98563e | 67 | ngOnInit () { |
b1fa3eba C |
68 | this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt') |
69 | .subscribe( | |
70 | data => this.otherVideos = data.videos, | |
71 | ||
72 | err => console.error(err) | |
73 | ) | |
74 | ||
13fc89f4 | 75 | this.paramsSub = this.route.params.subscribe(routeParams => { |
ed9f9f5f C |
76 | if (this.videoPlayerLoaded) { |
77 | this.player.pause() | |
78 | } | |
79 | ||
0a6658fd C |
80 | let uuid = routeParams['uuid'] |
81 | this.videoService.getVideo(uuid).subscribe( | |
92fb909c C |
82 | video => this.onVideoFetched(video), |
83 | ||
f1013131 C |
84 | error => { |
85 | this.videoNotFound = true | |
86 | console.error(error) | |
87 | } | |
df98563e C |
88 | ) |
89 | }) | |
d1992b93 C |
90 | } |
91 | ||
df98563e | 92 | ngOnDestroy () { |
2ed6a0ae | 93 | // Remove player if it exists |
efee3505 | 94 | if (this.videoPlayerLoaded === true) { |
2ed6a0ae C |
95 | videojs(this.playerElement).dispose() |
96 | } | |
067e3f84 | 97 | |
13fc89f4 | 98 | // Unsubscribe subscriptions |
df98563e | 99 | this.paramsSub.unsubscribe() |
dc8bc31b | 100 | } |
98b01bac | 101 | |
df98563e C |
102 | setLike () { |
103 | if (this.isUserLoggedIn() === false) return | |
d38b8281 | 104 | // Already liked this video |
df98563e | 105 | if (this.userRating === 'like') return |
d38b8281 C |
106 | |
107 | this.videoService.setVideoLike(this.video.id) | |
108 | .subscribe( | |
109 | () => { | |
110 | // Update the video like attribute | |
df98563e C |
111 | this.updateVideoRating(this.userRating, 'like') |
112 | this.userRating = 'like' | |
d38b8281 C |
113 | }, |
114 | ||
bfb3a98f | 115 | err => this.notificationsService.error('Error', err.message) |
df98563e | 116 | ) |
d38b8281 C |
117 | } |
118 | ||
df98563e C |
119 | setDislike () { |
120 | if (this.isUserLoggedIn() === false) return | |
d38b8281 | 121 | // Already disliked this video |
df98563e | 122 | if (this.userRating === 'dislike') return |
d38b8281 C |
123 | |
124 | this.videoService.setVideoDislike(this.video.id) | |
125 | .subscribe( | |
126 | () => { | |
127 | // Update the video dislike attribute | |
df98563e C |
128 | this.updateVideoRating(this.userRating, 'dislike') |
129 | this.userRating = 'dislike' | |
d38b8281 C |
130 | }, |
131 | ||
bfb3a98f | 132 | err => this.notificationsService.error('Error', err.message) |
df98563e | 133 | ) |
d38b8281 C |
134 | } |
135 | ||
df98563e C |
136 | blacklistVideo (event: Event) { |
137 | event.preventDefault() | |
ab683a8e | 138 | |
198b205c GS |
139 | this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe( |
140 | res => { | |
df98563e | 141 | if (res === false) return |
198b205c | 142 | |
35bf0c83 C |
143 | this.videoBlacklistService.blacklistVideo(this.video.id) |
144 | .subscribe( | |
145 | status => { | |
146 | this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`) | |
147 | this.router.navigate(['/videos/list']) | |
148 | }, | |
198b205c | 149 | |
35bf0c83 C |
150 | error => this.notificationsService.error('Error', error.text) |
151 | ) | |
198b205c | 152 | } |
df98563e | 153 | ) |
198b205c GS |
154 | } |
155 | ||
2de96f4d | 156 | showMoreDescription () { |
2de96f4d C |
157 | if (this.completeVideoDescription === undefined) { |
158 | return this.loadCompleteDescription() | |
159 | } | |
160 | ||
161 | this.updateVideoDescription(this.completeVideoDescription) | |
80958c78 | 162 | this.completeDescriptionShown = true |
2de96f4d C |
163 | } |
164 | ||
165 | showLessDescription () { | |
2de96f4d | 166 | this.updateVideoDescription(this.shortVideoDescription) |
80958c78 | 167 | this.completeDescriptionShown = false |
2de96f4d C |
168 | } |
169 | ||
170 | loadCompleteDescription () { | |
80958c78 C |
171 | this.descriptionLoading = true |
172 | ||
2de96f4d C |
173 | this.videoService.loadCompleteDescription(this.video.descriptionPath) |
174 | .subscribe( | |
175 | description => { | |
80958c78 C |
176 | this.completeDescriptionShown = true |
177 | this.descriptionLoading = false | |
178 | ||
2de96f4d C |
179 | this.shortVideoDescription = this.video.description |
180 | this.completeVideoDescription = description | |
181 | ||
182 | this.updateVideoDescription(this.completeVideoDescription) | |
183 | }, | |
184 | ||
80958c78 C |
185 | error => { |
186 | this.descriptionLoading = false | |
187 | this.notificationsService.error('Error', error.text) | |
188 | } | |
2de96f4d C |
189 | ) |
190 | } | |
191 | ||
df98563e C |
192 | showReportModal (event: Event) { |
193 | event.preventDefault() | |
194 | this.videoReportModal.show() | |
4f8c0eb0 C |
195 | } |
196 | ||
df98563e C |
197 | showShareModal () { |
198 | this.videoShareModal.show() | |
99cc4f49 C |
199 | } |
200 | ||
a96aed15 | 201 | showDownloadModal (event: Event) { |
df98563e | 202 | event.preventDefault() |
a96aed15 | 203 | this.videoDownloadModal.show() |
99cc4f49 C |
204 | } |
205 | ||
df98563e C |
206 | isUserLoggedIn () { |
207 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
208 | } |
209 | ||
df98563e | 210 | isVideoBlacklistable () { |
b2731bff | 211 | return this.video.isBlackistableBy(this.user) |
198b205c GS |
212 | } |
213 | ||
b1fa3eba C |
214 | getAvatarPath () { |
215 | return Account.GET_ACCOUNT_AVATAR_PATH(this.video.account) | |
216 | } | |
217 | ||
218 | getVideoTags () { | |
219 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
220 | ||
221 | return this.video.tags.join(', ') | |
222 | } | |
223 | ||
2de96f4d C |
224 | private updateVideoDescription (description: string) { |
225 | this.video.description = description | |
226 | this.setVideoDescriptionHTML() | |
227 | } | |
228 | ||
229 | private setVideoDescriptionHTML () { | |
cadb46d8 C |
230 | if (!this.video.description) { |
231 | this.videoHTMLDescription = '' | |
232 | return | |
233 | } | |
234 | ||
2de96f4d C |
235 | this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description) |
236 | } | |
237 | ||
0c31c33d C |
238 | private handleError (err: any) { |
239 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
240 | let message = '' | |
241 | ||
242 | if (errorMessage.indexOf('http error') !== -1) { | |
243 | message = 'Cannot fetch video from server, maybe down.' | |
244 | } else { | |
245 | message = errorMessage | |
246 | } | |
247 | ||
248 | this.notificationsService.error('Error', message) | |
249 | } | |
250 | ||
df98563e | 251 | private checkUserRating () { |
d38b8281 | 252 | // Unlogged users do not have ratings |
df98563e | 253 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
254 | |
255 | this.videoService.getUserVideoRating(this.video.id) | |
256 | .subscribe( | |
b632e904 | 257 | ratingObject => { |
d38b8281 | 258 | if (ratingObject) { |
df98563e | 259 | this.userRating = ratingObject.rating |
d38b8281 C |
260 | } |
261 | }, | |
262 | ||
bfb3a98f | 263 | err => this.notificationsService.error('Error', err.message) |
df98563e | 264 | ) |
d38b8281 C |
265 | } |
266 | ||
404b54e1 | 267 | private onVideoFetched (video: VideoDetails) { |
df98563e | 268 | this.video = video |
92fb909c | 269 | |
df98563e | 270 | let observable |
b2731bff | 271 | if (this.video.isVideoNSFWForUser(this.user)) { |
d6e32a2e C |
272 | observable = this.confirmService.confirm( |
273 | 'This video contains mature or explicit content. Are you sure you want to watch it?', | |
274 | 'Mature or explicit content' | |
275 | ) | |
92fb909c | 276 | } else { |
df98563e | 277 | observable = Observable.of(true) |
92fb909c C |
278 | } |
279 | ||
280 | observable.subscribe( | |
281 | res => { | |
282 | if (res === false) { | |
efee3505 | 283 | |
df98563e | 284 | return this.router.navigate([ '/videos/list' ]) |
92fb909c C |
285 | } |
286 | ||
ed9f9f5f C |
287 | // Player was already loaded |
288 | if (this.videoPlayerLoaded !== true) { | |
289 | this.playerElement = this.elementRef.nativeElement.querySelector('#video-element') | |
290 | ||
291 | const videojsOptions = { | |
292 | controls: true, | |
d4c6a3b9 | 293 | autoplay: this.isAutoplay(), |
ed9f9f5f C |
294 | plugins: { |
295 | peertube: { | |
296 | videoFiles: this.video.files, | |
297 | playerElement: this.playerElement, | |
d4c6a3b9 | 298 | autoplay: this.isAutoplay(), |
ed9f9f5f C |
299 | peerTubeLink: false |
300 | } | |
aa8b6df4 C |
301 | } |
302 | } | |
aa8b6df4 | 303 | |
ed9f9f5f | 304 | this.videoPlayerLoaded = true |
efee3505 | 305 | |
ed9f9f5f C |
306 | const self = this |
307 | videojs(this.playerElement, videojsOptions, function () { | |
308 | self.player = this | |
309 | this.on('customError', (event, data) => { | |
310 | self.handleError(data.err) | |
311 | }) | |
aa8b6df4 | 312 | }) |
ed9f9f5f C |
313 | } else { |
314 | (this.player as any).setVideoFiles(this.video.files) | |
315 | } | |
aa8b6df4 | 316 | |
2de96f4d | 317 | this.setVideoDescriptionHTML() |
9d9597df | 318 | |
df98563e | 319 | this.setOpenGraphTags() |
df98563e | 320 | this.checkUserRating() |
1f3e9fec C |
321 | |
322 | this.prepareViewAdd() | |
92fb909c | 323 | } |
df98563e | 324 | ) |
92fb909c C |
325 | } |
326 | ||
154898b0 | 327 | private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) { |
df98563e C |
328 | let likesToIncrement = 0 |
329 | let dislikesToIncrement = 0 | |
d38b8281 C |
330 | |
331 | if (oldRating) { | |
df98563e C |
332 | if (oldRating === 'like') likesToIncrement-- |
333 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
334 | } |
335 | ||
df98563e C |
336 | if (newRating === 'like') likesToIncrement++ |
337 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 338 | |
df98563e C |
339 | this.video.likes += likesToIncrement |
340 | this.video.dislikes += dislikesToIncrement | |
d38b8281 C |
341 | } |
342 | ||
df98563e C |
343 | private setOpenGraphTags () { |
344 | this.metaService.setTitle(this.video.name) | |
758b996d | 345 | |
df98563e | 346 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 347 | |
df98563e C |
348 | this.metaService.setTag('og:title', this.video.name) |
349 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 350 | |
df98563e C |
351 | this.metaService.setTag('og:description', this.video.description) |
352 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 353 | |
d38309c3 | 354 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 355 | |
df98563e | 356 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 357 | |
df98563e | 358 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 359 | |
df98563e C |
360 | this.metaService.setTag('og:url', window.location.href) |
361 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 362 | } |
1f3e9fec C |
363 | |
364 | private prepareViewAdd () { | |
365 | // After 30 seconds (or 3/4 of the video), increment add a view | |
366 | let viewTimeoutSeconds = 30 | |
367 | if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4 | |
368 | ||
369 | setTimeout(() => { | |
370 | this.videoService | |
371 | .viewVideo(this.video.uuid) | |
372 | .subscribe() | |
373 | ||
374 | }, viewTimeoutSeconds * 1000) | |
375 | } | |
d4c6a3b9 C |
376 | |
377 | private isAutoplay () { | |
378 | // True by default | |
379 | if (!this.user) return true | |
380 | ||
381 | // Be sure the autoPlay is set to false | |
382 | return this.user.autoPlayVideo !== false | |
383 | } | |
dc8bc31b | 384 | } |