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