]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Add ability to remove a video on watch page
[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 2import { ActivatedRoute, Router } from '@angular/router'
1f3e9fec
C
3import { MetaService } from '@ngx-meta/core'
4import { NotificationsService } from 'angular2-notifications'
df98563e
C
5import { Observable } from 'rxjs/Observable'
6import { Subscription } from 'rxjs/Subscription'
63c4db6d 7import * as videojs from 'video.js'
1f3e9fec 8import { UserVideoRateType, VideoRateType } from '../../../../../shared'
aa8b6df4 9import '../../../assets/player/peertube-videojs-plugin'
df98563e 10import { AuthService, ConfirmService } from '../../core'
1f3e9fec 11import { VideoBlacklistService } from '../../shared'
b1fa3eba 12import { Account } from '../../shared/account/account.model'
ff249f49 13import { VideoDetails } from '../../shared/video/video-details.model'
b1fa3eba 14import { Video } from '../../shared/video/video.model'
63c4db6d 15import { VideoService } from '../../shared/video/video.service'
202f6b6c 16import { MarkdownService } from '../shared'
a96aed15 17import { VideoDownloadComponent } from './video-download.component'
df98563e 18import { VideoReportComponent } from './video-report.component'
1f3e9fec 19import { VideoShareComponent } from './video-share.component'
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
b1fa3eba
C
31 otherVideos: Video[] = []
32
df98563e
C
33 error = false
34 loading = false
df98563e 35 player: videojs.Player
0826c92d 36 playerElement: HTMLVideoElement
154898b0 37 userRating: UserVideoRateType = null
404b54e1 38 video: VideoDetails = null
efee3505 39 videoPlayerLoaded = false
f1013131 40 videoNotFound = false
80958c78 41 descriptionLoading = false
2de96f4d
C
42
43 completeDescriptionShown = false
44 completeVideoDescription: string
45 shortVideoDescription: string
9d9597df 46 videoHTMLDescription = ''
df98563e 47
df98563e 48 private paramsSub: Subscription
df98563e
C
49
50 constructor (
4fd8aa32 51 private elementRef: ElementRef,
0629423c 52 private route: ActivatedRoute,
92fb909c 53 private router: Router,
d3ef341a 54 private videoService: VideoService,
35bf0c83 55 private videoBlacklistService: VideoBlacklistService,
92fb909c 56 private confirmService: ConfirmService,
3ec343a4 57 private metaService: MetaService,
7ddd02c9 58 private authService: AuthService,
9d9597df
C
59 private notificationsService: NotificationsService,
60 private markdownService: MarkdownService
d3ef341a 61 ) {}
dc8bc31b 62
b2731bff
C
63 get user () {
64 return this.authService.getUser()
65 }
66
df98563e 67 ngOnInit () {
b1fa3eba
C
68 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
69 .subscribe(
70 data => this.otherVideos = data.videos,
71
72 err => console.error(err)
73 )
74
13fc89f4 75 this.paramsSub = this.route.params.subscribe(routeParams => {
ed9f9f5f
C
76 if (this.videoPlayerLoaded) {
77 this.player.pause()
78 }
79
0a6658fd
C
80 let uuid = routeParams['uuid']
81 this.videoService.getVideo(uuid).subscribe(
92fb909c
C
82 video => this.onVideoFetched(video),
83
f1013131
C
84 error => {
85 this.videoNotFound = true
86 console.error(error)
87 }
df98563e
C
88 )
89 })
d1992b93
C
90 }
91
df98563e 92 ngOnDestroy () {
2ed6a0ae 93 // Remove player if it exists
efee3505 94 if (this.videoPlayerLoaded === true) {
2ed6a0ae
C
95 videojs(this.playerElement).dispose()
96 }
067e3f84 97
13fc89f4 98 // Unsubscribe subscriptions
df98563e 99 this.paramsSub.unsubscribe()
dc8bc31b 100 }
98b01bac 101
df98563e
C
102 setLike () {
103 if (this.isUserLoggedIn() === false) return
d38b8281 104 // Already liked this video
df98563e 105 if (this.userRating === 'like') return
d38b8281
C
106
107 this.videoService.setVideoLike(this.video.id)
108 .subscribe(
109 () => {
110 // Update the video like attribute
df98563e
C
111 this.updateVideoRating(this.userRating, 'like')
112 this.userRating = 'like'
d38b8281
C
113 },
114
bfb3a98f 115 err => this.notificationsService.error('Error', err.message)
df98563e 116 )
d38b8281
C
117 }
118
df98563e
C
119 setDislike () {
120 if (this.isUserLoggedIn() === false) return
d38b8281 121 // Already disliked this video
df98563e 122 if (this.userRating === 'dislike') return
d38b8281
C
123
124 this.videoService.setVideoDislike(this.video.id)
125 .subscribe(
126 () => {
127 // Update the video dislike attribute
df98563e
C
128 this.updateVideoRating(this.userRating, 'dislike')
129 this.userRating = 'dislike'
d38b8281
C
130 },
131
bfb3a98f 132 err => this.notificationsService.error('Error', err.message)
df98563e 133 )
d38b8281
C
134 }
135
df98563e
C
136 blacklistVideo (event: Event) {
137 event.preventDefault()
ab683a8e 138
198b205c
GS
139 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
140 res => {
df98563e 141 if (res === false) return
198b205c 142
35bf0c83
C
143 this.videoBlacklistService.blacklistVideo(this.video.id)
144 .subscribe(
145 status => {
146 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
147 this.router.navigate(['/videos/list'])
148 },
198b205c 149
35bf0c83
C
150 error => this.notificationsService.error('Error', error.text)
151 )
198b205c 152 }
df98563e 153 )
198b205c
GS
154 }
155
2de96f4d 156 showMoreDescription () {
2de96f4d
C
157 if (this.completeVideoDescription === undefined) {
158 return this.loadCompleteDescription()
159 }
160
161 this.updateVideoDescription(this.completeVideoDescription)
80958c78 162 this.completeDescriptionShown = true
2de96f4d
C
163 }
164
165 showLessDescription () {
2de96f4d 166 this.updateVideoDescription(this.shortVideoDescription)
80958c78 167 this.completeDescriptionShown = false
2de96f4d
C
168 }
169
170 loadCompleteDescription () {
80958c78
C
171 this.descriptionLoading = true
172
2de96f4d
C
173 this.videoService.loadCompleteDescription(this.video.descriptionPath)
174 .subscribe(
175 description => {
80958c78
C
176 this.completeDescriptionShown = true
177 this.descriptionLoading = false
178
2de96f4d
C
179 this.shortVideoDescription = this.video.description
180 this.completeVideoDescription = description
181
182 this.updateVideoDescription(this.completeVideoDescription)
183 },
184
80958c78
C
185 error => {
186 this.descriptionLoading = false
187 this.notificationsService.error('Error', error.text)
188 }
2de96f4d
C
189 )
190 }
191
df98563e
C
192 showReportModal (event: Event) {
193 event.preventDefault()
194 this.videoReportModal.show()
4f8c0eb0
C
195 }
196
df98563e
C
197 showShareModal () {
198 this.videoShareModal.show()
99cc4f49
C
199 }
200
a96aed15 201 showDownloadModal (event: Event) {
df98563e 202 event.preventDefault()
a96aed15 203 this.videoDownloadModal.show()
99cc4f49
C
204 }
205
df98563e
C
206 isUserLoggedIn () {
207 return this.authService.isLoggedIn()
4f8c0eb0
C
208 }
209
df98563e 210 isVideoBlacklistable () {
b2731bff 211 return this.video.isBlackistableBy(this.user)
198b205c
GS
212 }
213
b1fa3eba
C
214 getAvatarPath () {
215 return Account.GET_ACCOUNT_AVATAR_PATH(this.video.account)
216 }
217
218 getVideoTags () {
219 if (!this.video || Array.isArray(this.video.tags) === false) return []
220
221 return this.video.tags.join(', ')
222 }
223
6725d05c
C
224 isVideoRemovable () {
225 return this.video.isRemovableBy(this.authService.getUser())
226 }
227
228 removeVideo (event: Event) {
229 event.preventDefault()
230
231 this.confirmService.confirm('Do you really want to delete this video?', 'Delete').subscribe(
232 res => {
233 if (res === false) return
234
235 this.videoService.removeVideo(this.video.id)
236 .subscribe(
237 status => {
238 this.notificationsService.success('Success', `Video ${this.video.name} deleted.`)
239
240 // Go back to the video-list.
241 this.router.navigate([ '/videos/list' ])
242 },
243
244 error => this.notificationsService.error('Error', error.text)
245 )
246 }
247 )
248 }
249
2de96f4d
C
250 private updateVideoDescription (description: string) {
251 this.video.description = description
252 this.setVideoDescriptionHTML()
253 }
254
255 private setVideoDescriptionHTML () {
cadb46d8
C
256 if (!this.video.description) {
257 this.videoHTMLDescription = ''
258 return
259 }
260
2de96f4d
C
261 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
262 }
263
0c31c33d
C
264 private handleError (err: any) {
265 const errorMessage: string = typeof err === 'string' ? err : err.message
266 let message = ''
267
268 if (errorMessage.indexOf('http error') !== -1) {
269 message = 'Cannot fetch video from server, maybe down.'
270 } else {
271 message = errorMessage
272 }
273
274 this.notificationsService.error('Error', message)
275 }
276
df98563e 277 private checkUserRating () {
d38b8281 278 // Unlogged users do not have ratings
df98563e 279 if (this.isUserLoggedIn() === false) return
d38b8281
C
280
281 this.videoService.getUserVideoRating(this.video.id)
282 .subscribe(
b632e904 283 ratingObject => {
d38b8281 284 if (ratingObject) {
df98563e 285 this.userRating = ratingObject.rating
d38b8281
C
286 }
287 },
288
bfb3a98f 289 err => this.notificationsService.error('Error', err.message)
df98563e 290 )
d38b8281
C
291 }
292
404b54e1 293 private onVideoFetched (video: VideoDetails) {
df98563e 294 this.video = video
92fb909c 295
df98563e 296 let observable
b2731bff 297 if (this.video.isVideoNSFWForUser(this.user)) {
d6e32a2e
C
298 observable = this.confirmService.confirm(
299 'This video contains mature or explicit content. Are you sure you want to watch it?',
300 'Mature or explicit content'
301 )
92fb909c 302 } else {
df98563e 303 observable = Observable.of(true)
92fb909c
C
304 }
305
306 observable.subscribe(
307 res => {
308 if (res === false) {
efee3505 309
df98563e 310 return this.router.navigate([ '/videos/list' ])
92fb909c
C
311 }
312
ed9f9f5f
C
313 // Player was already loaded
314 if (this.videoPlayerLoaded !== true) {
315 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
316
0826c92d
C
317 // If autoplay is true, we don't really need a poster
318 if (this.isAutoplay() === false) {
0826c92d
C
319 this.playerElement.poster = this.video.previewUrl
320 }
321
ed9f9f5f
C
322 const videojsOptions = {
323 controls: true,
d4c6a3b9 324 autoplay: this.isAutoplay(),
ed9f9f5f
C
325 plugins: {
326 peertube: {
327 videoFiles: this.video.files,
328 playerElement: this.playerElement,
d4c6a3b9 329 autoplay: this.isAutoplay(),
ed9f9f5f
C
330 peerTubeLink: false
331 }
aa8b6df4
C
332 }
333 }
aa8b6df4 334
ed9f9f5f 335 this.videoPlayerLoaded = true
efee3505 336
ed9f9f5f
C
337 const self = this
338 videojs(this.playerElement, videojsOptions, function () {
339 self.player = this
340 this.on('customError', (event, data) => {
341 self.handleError(data.err)
342 })
aa8b6df4 343 })
ed9f9f5f
C
344 } else {
345 (this.player as any).setVideoFiles(this.video.files)
346 }
aa8b6df4 347
2de96f4d 348 this.setVideoDescriptionHTML()
9d9597df 349
df98563e 350 this.setOpenGraphTags()
df98563e 351 this.checkUserRating()
1f3e9fec
C
352
353 this.prepareViewAdd()
92fb909c 354 }
df98563e 355 )
92fb909c
C
356 }
357
154898b0 358 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
359 let likesToIncrement = 0
360 let dislikesToIncrement = 0
d38b8281
C
361
362 if (oldRating) {
df98563e
C
363 if (oldRating === 'like') likesToIncrement--
364 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
365 }
366
df98563e
C
367 if (newRating === 'like') likesToIncrement++
368 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 369
df98563e
C
370 this.video.likes += likesToIncrement
371 this.video.dislikes += dislikesToIncrement
d38b8281
C
372 }
373
df98563e
C
374 private setOpenGraphTags () {
375 this.metaService.setTitle(this.video.name)
758b996d 376
df98563e 377 this.metaService.setTag('og:type', 'video')
3ec343a4 378
df98563e
C
379 this.metaService.setTag('og:title', this.video.name)
380 this.metaService.setTag('name', this.video.name)
3ec343a4 381
df98563e
C
382 this.metaService.setTag('og:description', this.video.description)
383 this.metaService.setTag('description', this.video.description)
3ec343a4 384
d38309c3 385 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 386
df98563e 387 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 388
df98563e 389 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 390
df98563e
C
391 this.metaService.setTag('og:url', window.location.href)
392 this.metaService.setTag('url', window.location.href)
3ec343a4 393 }
1f3e9fec
C
394
395 private prepareViewAdd () {
396 // After 30 seconds (or 3/4 of the video), increment add a view
397 let viewTimeoutSeconds = 30
398 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
399
400 setTimeout(() => {
401 this.videoService
402 .viewVideo(this.video.uuid)
403 .subscribe()
404
405 }, viewTimeoutSeconds * 1000)
406 }
d4c6a3b9
C
407
408 private isAutoplay () {
409 // True by default
410 if (!this.user) return true
411
412 // Be sure the autoPlay is set to false
413 return this.user.autoPlayVideo !== false
414 }
dc8bc31b 415}