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