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