]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Fix changing video we are watching
[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 => {
ed9f9f5f
C
72 if (this.videoPlayerLoaded) {
73 this.player.pause()
74 }
75
0a6658fd
C
76 let uuid = routeParams['uuid']
77 this.videoService.getVideo(uuid).subscribe(
92fb909c
C
78 video => this.onVideoFetched(video),
79
f1013131
C
80 error => {
81 this.videoNotFound = true
82 console.error(error)
83 }
df98563e
C
84 )
85 })
d1992b93
C
86 }
87
df98563e 88 ngOnDestroy () {
2ed6a0ae 89 // Remove player if it exists
efee3505 90 if (this.videoPlayerLoaded === true) {
2ed6a0ae
C
91 videojs(this.playerElement).dispose()
92 }
067e3f84 93
13fc89f4 94 // Unsubscribe subscriptions
df98563e 95 this.paramsSub.unsubscribe()
dc8bc31b 96 }
98b01bac 97
df98563e
C
98 setLike () {
99 if (this.isUserLoggedIn() === false) return
d38b8281 100 // Already liked this video
df98563e 101 if (this.userRating === 'like') return
d38b8281
C
102
103 this.videoService.setVideoLike(this.video.id)
104 .subscribe(
105 () => {
106 // Update the video like attribute
df98563e
C
107 this.updateVideoRating(this.userRating, 'like')
108 this.userRating = 'like'
d38b8281
C
109 },
110
bfb3a98f 111 err => this.notificationsService.error('Error', err.message)
df98563e 112 )
d38b8281
C
113 }
114
df98563e
C
115 setDislike () {
116 if (this.isUserLoggedIn() === false) return
d38b8281 117 // Already disliked this video
df98563e 118 if (this.userRating === 'dislike') return
d38b8281
C
119
120 this.videoService.setVideoDislike(this.video.id)
121 .subscribe(
122 () => {
123 // Update the video dislike attribute
df98563e
C
124 this.updateVideoRating(this.userRating, 'dislike')
125 this.userRating = 'dislike'
d38b8281
C
126 },
127
bfb3a98f 128 err => this.notificationsService.error('Error', err.message)
df98563e 129 )
d38b8281
C
130 }
131
df98563e
C
132 blacklistVideo (event: Event) {
133 event.preventDefault()
ab683a8e 134
198b205c
GS
135 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
136 res => {
df98563e 137 if (res === false) return
198b205c 138
35bf0c83
C
139 this.videoBlacklistService.blacklistVideo(this.video.id)
140 .subscribe(
141 status => {
142 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
143 this.router.navigate(['/videos/list'])
144 },
198b205c 145
35bf0c83
C
146 error => this.notificationsService.error('Error', error.text)
147 )
198b205c 148 }
df98563e 149 )
198b205c
GS
150 }
151
2de96f4d 152 showMoreDescription () {
2de96f4d
C
153 if (this.completeVideoDescription === undefined) {
154 return this.loadCompleteDescription()
155 }
156
157 this.updateVideoDescription(this.completeVideoDescription)
80958c78 158 this.completeDescriptionShown = true
2de96f4d
C
159 }
160
161 showLessDescription () {
2de96f4d 162 this.updateVideoDescription(this.shortVideoDescription)
80958c78 163 this.completeDescriptionShown = false
2de96f4d
C
164 }
165
166 loadCompleteDescription () {
80958c78
C
167 this.descriptionLoading = true
168
2de96f4d
C
169 this.videoService.loadCompleteDescription(this.video.descriptionPath)
170 .subscribe(
171 description => {
80958c78
C
172 this.completeDescriptionShown = true
173 this.descriptionLoading = false
174
2de96f4d
C
175 this.shortVideoDescription = this.video.description
176 this.completeVideoDescription = description
177
178 this.updateVideoDescription(this.completeVideoDescription)
179 },
180
80958c78
C
181 error => {
182 this.descriptionLoading = false
183 this.notificationsService.error('Error', error.text)
184 }
2de96f4d
C
185 )
186 }
187
df98563e
C
188 showReportModal (event: Event) {
189 event.preventDefault()
190 this.videoReportModal.show()
4f8c0eb0
C
191 }
192
df98563e
C
193 showShareModal () {
194 this.videoShareModal.show()
99cc4f49
C
195 }
196
a96aed15 197 showDownloadModal (event: Event) {
df98563e 198 event.preventDefault()
a96aed15 199 this.videoDownloadModal.show()
99cc4f49
C
200 }
201
df98563e
C
202 isUserLoggedIn () {
203 return this.authService.isLoggedIn()
4f8c0eb0
C
204 }
205
df98563e
C
206 isVideoBlacklistable () {
207 return this.video.isBlackistableBy(this.authService.getUser())
198b205c
GS
208 }
209
b1fa3eba
C
210 getAvatarPath () {
211 return Account.GET_ACCOUNT_AVATAR_PATH(this.video.account)
212 }
213
214 getVideoTags () {
215 if (!this.video || Array.isArray(this.video.tags) === false) return []
216
217 return this.video.tags.join(', ')
218 }
219
2de96f4d
C
220 private updateVideoDescription (description: string) {
221 this.video.description = description
222 this.setVideoDescriptionHTML()
223 }
224
225 private setVideoDescriptionHTML () {
cadb46d8
C
226 if (!this.video.description) {
227 this.videoHTMLDescription = ''
228 return
229 }
230
2de96f4d
C
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
ed9f9f5f
C
283 // Player was already loaded
284 if (this.videoPlayerLoaded !== true) {
285 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
286
287 const videojsOptions = {
288 controls: true,
289 autoplay: true,
290 plugins: {
291 peertube: {
292 videoFiles: this.video.files,
293 playerElement: this.playerElement,
294 autoplay: true,
295 peerTubeLink: false
296 }
aa8b6df4
C
297 }
298 }
aa8b6df4 299
ed9f9f5f 300 this.videoPlayerLoaded = true
efee3505 301
ed9f9f5f
C
302 const self = this
303 videojs(this.playerElement, videojsOptions, function () {
304 self.player = this
305 this.on('customError', (event, data) => {
306 self.handleError(data.err)
307 })
aa8b6df4 308 })
ed9f9f5f
C
309 } else {
310 (this.player as any).setVideoFiles(this.video.files)
311 }
aa8b6df4 312
2de96f4d 313 this.setVideoDescriptionHTML()
9d9597df 314
df98563e 315 this.setOpenGraphTags()
df98563e 316 this.checkUserRating()
1f3e9fec
C
317
318 this.prepareViewAdd()
92fb909c 319 }
df98563e 320 )
92fb909c
C
321 }
322
154898b0 323 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
324 let likesToIncrement = 0
325 let dislikesToIncrement = 0
d38b8281
C
326
327 if (oldRating) {
df98563e
C
328 if (oldRating === 'like') likesToIncrement--
329 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
330 }
331
df98563e
C
332 if (newRating === 'like') likesToIncrement++
333 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 334
df98563e
C
335 this.video.likes += likesToIncrement
336 this.video.dislikes += dislikesToIncrement
d38b8281
C
337 }
338
df98563e
C
339 private setOpenGraphTags () {
340 this.metaService.setTitle(this.video.name)
758b996d 341
df98563e 342 this.metaService.setTag('og:type', 'video')
3ec343a4 343
df98563e
C
344 this.metaService.setTag('og:title', this.video.name)
345 this.metaService.setTag('name', this.video.name)
3ec343a4 346
df98563e
C
347 this.metaService.setTag('og:description', this.video.description)
348 this.metaService.setTag('description', this.video.description)
3ec343a4 349
d38309c3 350 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 351
df98563e 352 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 353
df98563e 354 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 355
df98563e
C
356 this.metaService.setTag('og:url', window.location.href)
357 this.metaService.setTag('url', window.location.href)
3ec343a4 358 }
1f3e9fec
C
359
360 private prepareViewAdd () {
361 // After 30 seconds (or 3/4 of the video), increment add a view
362 let viewTimeoutSeconds = 30
363 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
364
365 setTimeout(() => {
366 this.videoService
367 .viewVideo(this.video.uuid)
368 .subscribe()
369
370 }, viewTimeoutSeconds * 1000)
371 }
dc8bc31b 372}