]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Video blacklist refractoring
[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'
13import { VideoMagnetComponent } from './video-magnet.component'
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 {
df98563e
C
26 @ViewChild('videoMagnetModal') videoMagnetModal: VideoMagnetComponent
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
C
38 video: Video = null
39 videoNotFound = false
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
df98563e
C
61 error => {
62 console.error(error)
63 this.videoNotFound = true
64 }
65 )
66 })
d1992b93
C
67 }
68
df98563e 69 ngOnDestroy () {
2ed6a0ae
C
70 // Remove player if it exists
71 if (this.videoNotFound === false) {
72 videojs(this.playerElement).dispose()
73 }
067e3f84 74
13fc89f4 75 // Unsubscribe subscriptions
df98563e 76 this.paramsSub.unsubscribe()
dc8bc31b 77 }
98b01bac 78
df98563e
C
79 setLike () {
80 if (this.isUserLoggedIn() === false) return
d38b8281 81 // Already liked this video
df98563e 82 if (this.userRating === 'like') return
d38b8281
C
83
84 this.videoService.setVideoLike(this.video.id)
85 .subscribe(
86 () => {
87 // Update the video like attribute
df98563e
C
88 this.updateVideoRating(this.userRating, 'like')
89 this.userRating = 'like'
d38b8281
C
90 },
91
bfb3a98f 92 err => this.notificationsService.error('Error', err.message)
df98563e 93 )
d38b8281
C
94 }
95
df98563e
C
96 setDislike () {
97 if (this.isUserLoggedIn() === false) return
d38b8281 98 // Already disliked this video
df98563e 99 if (this.userRating === 'dislike') return
d38b8281
C
100
101 this.videoService.setVideoDislike(this.video.id)
102 .subscribe(
103 () => {
104 // Update the video dislike attribute
df98563e
C
105 this.updateVideoRating(this.userRating, 'dislike')
106 this.userRating = 'dislike'
d38b8281
C
107 },
108
bfb3a98f 109 err => this.notificationsService.error('Error', err.message)
df98563e 110 )
d38b8281
C
111 }
112
df98563e
C
113 removeVideo (event: Event) {
114 event.preventDefault()
ab683a8e 115
198b205c
GS
116 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
117 res => {
df98563e 118 if (res === false) return
198b205c
GS
119
120 this.videoService.removeVideo(this.video.id)
ab683a8e
C
121 .subscribe(
122 status => {
df98563e 123 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
ab683a8e 124 // Go back to the video-list.
df98563e 125 this.router.navigate(['/videos/list'])
ab683a8e
C
126 },
127
128 error => this.notificationsService.error('Error', error.text)
df98563e 129 )
198b205c 130 }
df98563e 131 )
198b205c
GS
132 }
133
df98563e
C
134 blacklistVideo (event: Event) {
135 event.preventDefault()
ab683a8e 136
198b205c
GS
137 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
138 res => {
df98563e 139 if (res === false) return
198b205c 140
35bf0c83
C
141 this.videoBlacklistService.blacklistVideo(this.video.id)
142 .subscribe(
143 status => {
144 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
145 this.router.navigate(['/videos/list'])
146 },
198b205c 147
35bf0c83
C
148 error => this.notificationsService.error('Error', error.text)
149 )
198b205c 150 }
df98563e 151 )
198b205c
GS
152 }
153
df98563e
C
154 showReportModal (event: Event) {
155 event.preventDefault()
156 this.videoReportModal.show()
4f8c0eb0
C
157 }
158
df98563e
C
159 showShareModal () {
160 this.videoShareModal.show()
99cc4f49
C
161 }
162
df98563e
C
163 showMagnetUriModal (event: Event) {
164 event.preventDefault()
165 this.videoMagnetModal.show()
99cc4f49
C
166 }
167
df98563e
C
168 isUserLoggedIn () {
169 return this.authService.isLoggedIn()
4f8c0eb0
C
170 }
171
df98563e
C
172 canUserUpdateVideo () {
173 return this.video.isUpdatableBy(this.authService.getUser())
d8e689b8
C
174 }
175
df98563e
C
176 isVideoRemovable () {
177 return this.video.isRemovableBy(this.authService.getUser())
198b205c
GS
178 }
179
df98563e
C
180 isVideoBlacklistable () {
181 return this.video.isBlackistableBy(this.authService.getUser())
198b205c
GS
182 }
183
0c31c33d
C
184 private handleError (err: any) {
185 const errorMessage: string = typeof err === 'string' ? err : err.message
186 let message = ''
187
188 if (errorMessage.indexOf('http error') !== -1) {
189 message = 'Cannot fetch video from server, maybe down.'
190 } else {
191 message = errorMessage
192 }
193
194 this.notificationsService.error('Error', message)
195 }
196
df98563e 197 private checkUserRating () {
d38b8281 198 // Unlogged users do not have ratings
df98563e 199 if (this.isUserLoggedIn() === false) return
d38b8281
C
200
201 this.videoService.getUserVideoRating(this.video.id)
202 .subscribe(
b632e904 203 ratingObject => {
d38b8281 204 if (ratingObject) {
df98563e 205 this.userRating = ratingObject.rating
d38b8281
C
206 }
207 },
208
bfb3a98f 209 err => this.notificationsService.error('Error', err.message)
df98563e 210 )
d38b8281
C
211 }
212
df98563e
C
213 private onVideoFetched (video: Video) {
214 this.video = video
92fb909c 215
df98563e 216 let observable
92fb909c 217 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
df98563e 218 observable = this.confirmService.confirm('This video is not safe for work. Are you sure you want to watch it?', 'NSFW')
92fb909c 219 } else {
df98563e 220 observable = Observable.of(true)
92fb909c
C
221 }
222
223 observable.subscribe(
224 res => {
225 if (res === false) {
df98563e 226 return this.router.navigate([ '/videos/list' ])
92fb909c
C
227 }
228
aa8b6df4
C
229 this.playerElement = this.elementRef.nativeElement.querySelector('#video-container')
230
231 const videojsOptions = {
232 controls: true,
233 autoplay: true,
234 plugins: {
235 peertube: {
236 videoFiles: this.video.files,
237 playerElement: this.playerElement,
238 autoplay: true,
239 peerTubeLink: false
240 }
241 }
242 }
243
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}