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