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