]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Fix bug when quitting NSFW video
[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'
154898b0 16import { Video, VideoService } 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
df98563e 38 video: Video = null
efee3505 39 videoPlayerLoaded = false
df98563e 40
df98563e 41 private paramsSub: Subscription
df98563e
C
42
43 constructor (
4fd8aa32 44 private elementRef: ElementRef,
0629423c 45 private route: ActivatedRoute,
92fb909c 46 private router: Router,
d3ef341a 47 private videoService: VideoService,
35bf0c83 48 private videoBlacklistService: VideoBlacklistService,
92fb909c 49 private confirmService: ConfirmService,
3ec343a4 50 private metaService: MetaService,
7ddd02c9
C
51 private authService: AuthService,
52 private notificationsService: NotificationsService
d3ef341a 53 ) {}
dc8bc31b 54
df98563e 55 ngOnInit () {
13fc89f4 56 this.paramsSub = this.route.params.subscribe(routeParams => {
0a6658fd
C
57 let uuid = routeParams['uuid']
58 this.videoService.getVideo(uuid).subscribe(
92fb909c
C
59 video => this.onVideoFetched(video),
60
efee3505 61 error => console.error(error)
df98563e
C
62 )
63 })
d1992b93
C
64 }
65
df98563e 66 ngOnDestroy () {
2ed6a0ae 67 // Remove player if it exists
efee3505 68 if (this.videoPlayerLoaded === true) {
2ed6a0ae
C
69 videojs(this.playerElement).dispose()
70 }
067e3f84 71
13fc89f4 72 // Unsubscribe subscriptions
df98563e 73 this.paramsSub.unsubscribe()
dc8bc31b 74 }
98b01bac 75
df98563e
C
76 setLike () {
77 if (this.isUserLoggedIn() === false) return
d38b8281 78 // Already liked this video
df98563e 79 if (this.userRating === 'like') return
d38b8281
C
80
81 this.videoService.setVideoLike(this.video.id)
82 .subscribe(
83 () => {
84 // Update the video like attribute
df98563e
C
85 this.updateVideoRating(this.userRating, 'like')
86 this.userRating = 'like'
d38b8281
C
87 },
88
bfb3a98f 89 err => this.notificationsService.error('Error', err.message)
df98563e 90 )
d38b8281
C
91 }
92
df98563e
C
93 setDislike () {
94 if (this.isUserLoggedIn() === false) return
d38b8281 95 // Already disliked this video
df98563e 96 if (this.userRating === 'dislike') return
d38b8281
C
97
98 this.videoService.setVideoDislike(this.video.id)
99 .subscribe(
100 () => {
101 // Update the video dislike attribute
df98563e
C
102 this.updateVideoRating(this.userRating, 'dislike')
103 this.userRating = 'dislike'
d38b8281
C
104 },
105
bfb3a98f 106 err => this.notificationsService.error('Error', err.message)
df98563e 107 )
d38b8281
C
108 }
109
df98563e
C
110 removeVideo (event: Event) {
111 event.preventDefault()
ab683a8e 112
198b205c
GS
113 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
114 res => {
df98563e 115 if (res === false) return
198b205c
GS
116
117 this.videoService.removeVideo(this.video.id)
ab683a8e
C
118 .subscribe(
119 status => {
df98563e 120 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
ab683a8e 121 // Go back to the video-list.
df98563e 122 this.router.navigate(['/videos/list'])
ab683a8e
C
123 },
124
125 error => this.notificationsService.error('Error', error.text)
df98563e 126 )
198b205c 127 }
df98563e 128 )
198b205c
GS
129 }
130
df98563e
C
131 blacklistVideo (event: Event) {
132 event.preventDefault()
ab683a8e 133
198b205c
GS
134 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
135 res => {
df98563e 136 if (res === false) return
198b205c 137
35bf0c83
C
138 this.videoBlacklistService.blacklistVideo(this.video.id)
139 .subscribe(
140 status => {
141 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
142 this.router.navigate(['/videos/list'])
143 },
198b205c 144
35bf0c83
C
145 error => this.notificationsService.error('Error', error.text)
146 )
198b205c 147 }
df98563e 148 )
198b205c
GS
149 }
150
df98563e
C
151 showReportModal (event: Event) {
152 event.preventDefault()
153 this.videoReportModal.show()
4f8c0eb0
C
154 }
155
df98563e
C
156 showShareModal () {
157 this.videoShareModal.show()
99cc4f49
C
158 }
159
a96aed15 160 showDownloadModal (event: Event) {
df98563e 161 event.preventDefault()
a96aed15 162 this.videoDownloadModal.show()
99cc4f49
C
163 }
164
df98563e
C
165 isUserLoggedIn () {
166 return this.authService.isLoggedIn()
4f8c0eb0
C
167 }
168
df98563e
C
169 canUserUpdateVideo () {
170 return this.video.isUpdatableBy(this.authService.getUser())
d8e689b8
C
171 }
172
df98563e
C
173 isVideoRemovable () {
174 return this.video.isRemovableBy(this.authService.getUser())
198b205c
GS
175 }
176
df98563e
C
177 isVideoBlacklistable () {
178 return this.video.isBlackistableBy(this.authService.getUser())
198b205c
GS
179 }
180
0c31c33d
C
181 private handleError (err: any) {
182 const errorMessage: string = typeof err === 'string' ? err : err.message
183 let message = ''
184
185 if (errorMessage.indexOf('http error') !== -1) {
186 message = 'Cannot fetch video from server, maybe down.'
187 } else {
188 message = errorMessage
189 }
190
191 this.notificationsService.error('Error', message)
192 }
193
df98563e 194 private checkUserRating () {
d38b8281 195 // Unlogged users do not have ratings
df98563e 196 if (this.isUserLoggedIn() === false) return
d38b8281
C
197
198 this.videoService.getUserVideoRating(this.video.id)
199 .subscribe(
b632e904 200 ratingObject => {
d38b8281 201 if (ratingObject) {
df98563e 202 this.userRating = ratingObject.rating
d38b8281
C
203 }
204 },
205
bfb3a98f 206 err => this.notificationsService.error('Error', err.message)
df98563e 207 )
d38b8281
C
208 }
209
df98563e
C
210 private onVideoFetched (video: Video) {
211 this.video = video
92fb909c 212
df98563e 213 let observable
92fb909c 214 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
df98563e 215 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
92fb909c 216 } else {
df98563e 217 observable = Observable.of(true)
92fb909c
C
218 }
219
220 observable.subscribe(
221 res => {
222 if (res === false) {
efee3505 223
df98563e 224 return this.router.navigate([ '/videos/list' ])
92fb909c
C
225 }
226
aa8b6df4
C
227 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
228
229 const videojsOptions = {
230 controls: true,
231 autoplay: true,
232 plugins: {
233 peertube: {
234 videoFiles: this.video.files,
235 playerElement: this.playerElement,
236 autoplay: true,
237 peerTubeLink: false
238 }
239 }
240 }
241
efee3505
C
242 this.videoPlayerLoaded = true
243
aa8b6df4
C
244 const self = this
245 videojs(this.playerElement, videojsOptions, function () {
246 self.player = this
247 this.on('customError', (event, data) => {
248 self.handleError(data.err)
249 })
250
251 this.on('torrentInfo', (event, data) => {
252 self.downloadSpeed = data.downloadSpeed
253 self.numPeers = data.numPeers
254 self.uploadSpeed = data.uploadSpeed
255 })
256 })
257
df98563e 258 this.setOpenGraphTags()
df98563e 259 this.checkUserRating()
92fb909c 260 }
df98563e 261 )
92fb909c
C
262 }
263
154898b0 264 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
265 let likesToIncrement = 0
266 let dislikesToIncrement = 0
d38b8281
C
267
268 if (oldRating) {
df98563e
C
269 if (oldRating === 'like') likesToIncrement--
270 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
271 }
272
df98563e
C
273 if (newRating === 'like') likesToIncrement++
274 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 275
df98563e
C
276 this.video.likes += likesToIncrement
277 this.video.dislikes += dislikesToIncrement
d38b8281
C
278 }
279
df98563e
C
280 private setOpenGraphTags () {
281 this.metaService.setTitle(this.video.name)
758b996d 282
df98563e 283 this.metaService.setTag('og:type', 'video')
3ec343a4 284
df98563e
C
285 this.metaService.setTag('og:title', this.video.name)
286 this.metaService.setTag('name', this.video.name)
3ec343a4 287
df98563e
C
288 this.metaService.setTag('og:description', this.video.description)
289 this.metaService.setTag('description', this.video.description)
3ec343a4 290
d38309c3 291 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 292
df98563e 293 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 294
df98563e 295 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 296
df98563e
C
297 this.metaService.setTag('og:url', window.location.href)
298 this.metaService.setTag('url', window.location.href)
3ec343a4 299 }
dc8bc31b 300}