]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Fetch remote AP objects
[github/Chocobozzz/PeerTube.git] / client / src / app / videos / +video-watch / video-watch.component.ts
CommitLineData
a685e25c 1import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core'
df98563e 2import { ActivatedRoute, Router } from '@angular/router'
1f3e9fec
C
3import { MetaService } from '@ngx-meta/core'
4import { NotificationsService } from 'angular2-notifications'
df98563e
C
5import { Observable } from 'rxjs/Observable'
6import { Subscription } from 'rxjs/Subscription'
63c4db6d 7import * as videojs from 'video.js'
d7701449 8import 'videojs-hotkeys'
1f3e9fec 9import { UserVideoRateType, VideoRateType } from '../../../../../shared'
aa8b6df4 10import '../../../assets/player/peertube-videojs-plugin'
df98563e 11import { AuthService, ConfirmService } from '../../core'
1f3e9fec 12import { VideoBlacklistService } from '../../shared'
b1fa3eba 13import { Account } from '../../shared/account/account.model'
ff249f49 14import { VideoDetails } from '../../shared/video/video-details.model'
b1fa3eba 15import { Video } from '../../shared/video/video.model'
63c4db6d 16import { VideoService } from '../../shared/video/video.service'
202f6b6c 17import { MarkdownService } from '../shared'
4635f59d
C
18import { VideoDownloadComponent } from './modal/video-download.component'
19import { VideoReportComponent } from './modal/video-report.component'
20import { 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 27export 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
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
28832412 50 private otherVideos: Video[] = []
df98563e 51 private paramsSub: Subscription
df98563e
C
52
53 constructor (
4fd8aa32 54 private elementRef: ElementRef,
0629423c 55 private route: ActivatedRoute,
92fb909c 56 private router: Router,
d3ef341a 57 private videoService: VideoService,
35bf0c83 58 private videoBlacklistService: VideoBlacklistService,
92fb909c 59 private confirmService: ConfirmService,
3ec343a4 60 private metaService: MetaService,
7ddd02c9 61 private authService: AuthService,
9d9597df
C
62 private notificationsService: NotificationsService,
63 private markdownService: MarkdownService
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
C
340 const self = this
341 videojs(this.playerElement, videojsOptions, function () {
342 self.player = this
343 this.on('customError', (event, data) => {
344 self.handleError(data.err)
345 })
aa8b6df4 346 })
ed9f9f5f
C
347 } else {
348 (this.player as any).setVideoFiles(this.video.files)
349 }
aa8b6df4 350
2de96f4d 351 this.setVideoDescriptionHTML()
e9189001 352 this.setVideoLikesBarTooltipText()
9d9597df 353
df98563e 354 this.setOpenGraphTags()
df98563e 355 this.checkUserRating()
1f3e9fec
C
356
357 this.prepareViewAdd()
92fb909c 358 }
df98563e 359 )
92fb909c
C
360 }
361
57a49263
BB
362 private setRating (nextRating) {
363 let method
364 switch (nextRating) {
365 case 'like':
366 method = this.videoService.setVideoLike
367 break
368 case 'dislike':
369 method = this.videoService.setVideoDislike
370 break
371 case 'none':
372 method = this.videoService.unsetVideoLike
373 break
374 }
375
376 method.call(this.videoService, this.video.id)
377 .subscribe(
378 () => {
379 // Update the video like attribute
380 this.updateVideoRating(this.userRating, nextRating)
381 this.userRating = nextRating
382 },
383 err => this.notificationsService.error('Error', err.message)
384 )
385 }
386
154898b0 387 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
388 let likesToIncrement = 0
389 let dislikesToIncrement = 0
d38b8281
C
390
391 if (oldRating) {
df98563e
C
392 if (oldRating === 'like') likesToIncrement--
393 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
394 }
395
df98563e
C
396 if (newRating === 'like') likesToIncrement++
397 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 398
df98563e
C
399 this.video.likes += likesToIncrement
400 this.video.dislikes += dislikesToIncrement
d38b8281
C
401 }
402
df98563e
C
403 private setOpenGraphTags () {
404 this.metaService.setTitle(this.video.name)
758b996d 405
df98563e 406 this.metaService.setTag('og:type', 'video')
3ec343a4 407
df98563e
C
408 this.metaService.setTag('og:title', this.video.name)
409 this.metaService.setTag('name', this.video.name)
3ec343a4 410
df98563e
C
411 this.metaService.setTag('og:description', this.video.description)
412 this.metaService.setTag('description', this.video.description)
3ec343a4 413
d38309c3 414 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 415
df98563e 416 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 417
df98563e 418 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 419
df98563e
C
420 this.metaService.setTag('og:url', window.location.href)
421 this.metaService.setTag('url', window.location.href)
3ec343a4 422 }
1f3e9fec
C
423
424 private prepareViewAdd () {
425 // After 30 seconds (or 3/4 of the video), increment add a view
426 let viewTimeoutSeconds = 30
427 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
428
429 setTimeout(() => {
430 this.videoService
431 .viewVideo(this.video.uuid)
432 .subscribe()
433
434 }, viewTimeoutSeconds * 1000)
435 }
d4c6a3b9
C
436
437 private isAutoplay () {
438 // True by default
439 if (!this.user) return true
440
441 // Be sure the autoPlay is set to false
442 return this.user.autoPlayVideo !== false
443 }
dc8bc31b 444}