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