]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - 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
1import { Component, ElementRef, OnDestroy, OnInit, ViewChild } from '@angular/core'
2import { ActivatedRoute, Router } from '@angular/router'
3import { MetaService } from '@ngx-meta/core'
4import { NotificationsService } from 'angular2-notifications'
5import { Observable } from 'rxjs/Observable'
6import { Subscription } from 'rxjs/Subscription'
7import videojs from 'video.js'
8import { UserVideoRateType, VideoRateType } from '../../../../../shared'
9import '../../../assets/player/peertube-videojs-plugin'
10import { AuthService, ConfirmService } from '../../core'
11import { VideoBlacklistService } from '../../shared'
12import { MarkdownService, VideoDetails, VideoService } from '../shared'
13import { VideoDownloadComponent } from './video-download.component'
14import { VideoReportComponent } from './video-report.component'
15import { VideoShareComponent } from './video-share.component'
16
17@Component({
18 selector: 'my-video-watch',
19 templateUrl: './video-watch.component.html',
20 styleUrls: [ './video-watch.component.scss' ]
21})
22export class VideoWatchComponent implements OnInit, OnDestroy {
23 @ViewChild('videoDownloadModal') videoDownloadModal: VideoDownloadComponent
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
32 playerElement: HTMLMediaElement
33 uploadSpeed: number
34 userRating: UserVideoRateType = null
35 video: VideoDetails = null
36 videoPlayerLoaded = false
37 videoNotFound = false
38 descriptionLoading = false
39
40 completeDescriptionShown = false
41 completeVideoDescription: string
42 shortVideoDescription: string
43 videoHTMLDescription = ''
44
45 private paramsSub: Subscription
46
47 constructor (
48 private elementRef: ElementRef,
49 private route: ActivatedRoute,
50 private router: Router,
51 private videoService: VideoService,
52 private videoBlacklistService: VideoBlacklistService,
53 private confirmService: ConfirmService,
54 private metaService: MetaService,
55 private authService: AuthService,
56 private notificationsService: NotificationsService,
57 private markdownService: MarkdownService
58 ) {}
59
60 ngOnInit () {
61 this.paramsSub = this.route.params.subscribe(routeParams => {
62 let uuid = routeParams['uuid']
63 this.videoService.getVideo(uuid).subscribe(
64 video => this.onVideoFetched(video),
65
66 error => {
67 this.videoNotFound = true
68 console.error(error)
69 }
70 )
71 })
72 }
73
74 ngOnDestroy () {
75 // Remove player if it exists
76 if (this.videoPlayerLoaded === true) {
77 videojs(this.playerElement).dispose()
78 }
79
80 // Unsubscribe subscriptions
81 this.paramsSub.unsubscribe()
82 }
83
84 setLike () {
85 if (this.isUserLoggedIn() === false) return
86 // Already liked this video
87 if (this.userRating === 'like') return
88
89 this.videoService.setVideoLike(this.video.id)
90 .subscribe(
91 () => {
92 // Update the video like attribute
93 this.updateVideoRating(this.userRating, 'like')
94 this.userRating = 'like'
95 },
96
97 err => this.notificationsService.error('Error', err.message)
98 )
99 }
100
101 setDislike () {
102 if (this.isUserLoggedIn() === false) return
103 // Already disliked this video
104 if (this.userRating === 'dislike') return
105
106 this.videoService.setVideoDislike(this.video.id)
107 .subscribe(
108 () => {
109 // Update the video dislike attribute
110 this.updateVideoRating(this.userRating, 'dislike')
111 this.userRating = 'dislike'
112 },
113
114 err => this.notificationsService.error('Error', err.message)
115 )
116 }
117
118 removeVideo (event: Event) {
119 event.preventDefault()
120
121 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
122 res => {
123 if (res === false) return
124
125 this.videoService.removeVideo(this.video.id)
126 .subscribe(
127 status => {
128 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
129 // Go back to the video-list.
130 this.router.navigate(['/videos/list'])
131 },
132
133 error => this.notificationsService.error('Error', error.text)
134 )
135 }
136 )
137 }
138
139 blacklistVideo (event: Event) {
140 event.preventDefault()
141
142 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
143 res => {
144 if (res === false) return
145
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 },
152
153 error => this.notificationsService.error('Error', error.text)
154 )
155 }
156 )
157 }
158
159 showMoreDescription () {
160 if (this.completeVideoDescription === undefined) {
161 return this.loadCompleteDescription()
162 }
163
164 this.updateVideoDescription(this.completeVideoDescription)
165 this.completeDescriptionShown = true
166 }
167
168 showLessDescription () {
169
170 this.updateVideoDescription(this.shortVideoDescription)
171 this.completeDescriptionShown = false
172 }
173
174 loadCompleteDescription () {
175 this.descriptionLoading = true
176
177 this.videoService.loadCompleteDescription(this.video.descriptionPath)
178 .subscribe(
179 description => {
180 this.completeDescriptionShown = true
181 this.descriptionLoading = false
182
183 this.shortVideoDescription = this.video.description
184 this.completeVideoDescription = description
185
186 this.updateVideoDescription(this.completeVideoDescription)
187 },
188
189 error => {
190 this.descriptionLoading = false
191 this.notificationsService.error('Error', error.text)
192 }
193 )
194 }
195
196 showReportModal (event: Event) {
197 event.preventDefault()
198 this.videoReportModal.show()
199 }
200
201 showShareModal () {
202 this.videoShareModal.show()
203 }
204
205 showDownloadModal (event: Event) {
206 event.preventDefault()
207 this.videoDownloadModal.show()
208 }
209
210 isUserLoggedIn () {
211 return this.authService.isLoggedIn()
212 }
213
214 canUserUpdateVideo () {
215 return this.video.isUpdatableBy(this.authService.getUser())
216 }
217
218 isVideoRemovable () {
219 return this.video.isRemovableBy(this.authService.getUser())
220 }
221
222 isVideoBlacklistable () {
223 return this.video.isBlackistableBy(this.authService.getUser())
224 }
225
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
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
248 private checkUserRating () {
249 // Unlogged users do not have ratings
250 if (this.isUserLoggedIn() === false) return
251
252 this.videoService.getUserVideoRating(this.video.id)
253 .subscribe(
254 ratingObject => {
255 if (ratingObject) {
256 this.userRating = ratingObject.rating
257 }
258 },
259
260 err => this.notificationsService.error('Error', err.message)
261 )
262 }
263
264 private onVideoFetched (video: VideoDetails) {
265 this.video = video
266
267 let observable
268 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
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 )
273 } else {
274 observable = Observable.of(true)
275 }
276
277 observable.subscribe(
278 res => {
279 if (res === false) {
280
281 return this.router.navigate([ '/videos/list' ])
282 }
283
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
299 this.videoPlayerLoaded = true
300
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
315 this.setVideoDescriptionHTML()
316
317 this.setOpenGraphTags()
318 this.checkUserRating()
319
320 this.prepareViewAdd()
321 }
322 )
323 }
324
325 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
326 let likesToIncrement = 0
327 let dislikesToIncrement = 0
328
329 if (oldRating) {
330 if (oldRating === 'like') likesToIncrement--
331 if (oldRating === 'dislike') dislikesToIncrement--
332 }
333
334 if (newRating === 'like') likesToIncrement++
335 if (newRating === 'dislike') dislikesToIncrement++
336
337 this.video.likes += likesToIncrement
338 this.video.dislikes += dislikesToIncrement
339 }
340
341 private setOpenGraphTags () {
342 this.metaService.setTitle(this.video.name)
343
344 this.metaService.setTag('og:type', 'video')
345
346 this.metaService.setTag('og:title', this.video.name)
347 this.metaService.setTag('name', this.video.name)
348
349 this.metaService.setTag('og:description', this.video.description)
350 this.metaService.setTag('description', this.video.description)
351
352 this.metaService.setTag('og:image', this.video.previewPath)
353
354 this.metaService.setTag('og:duration', this.video.duration.toString())
355
356 this.metaService.setTag('og:site_name', 'PeerTube')
357
358 this.metaService.setTag('og:url', window.location.href)
359 this.metaService.setTag('url', window.location.href)
360 }
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 }
374}