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