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