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