]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/videos/+video-watch/video-watch.component.ts
Move video form inside a component
[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'
202f6b6c 5import { VideoService } from 'app/shared/video/video.service'
df98563e
C
6import { Observable } from 'rxjs/Observable'
7import { Subscription } from 'rxjs/Subscription'
80624154 8import videojs from 'video.js'
1f3e9fec 9import { UserVideoRateType, VideoRateType } from '../../../../../shared'
aa8b6df4 10import '../../../assets/player/peertube-videojs-plugin'
df98563e 11import { AuthService, ConfirmService } from '../../core'
1f3e9fec 12import { VideoBlacklistService } from '../../shared'
b1fa3eba 13import { Account } from '../../shared/account/account.model'
ff249f49 14import { VideoDetails } from '../../shared/video/video-details.model'
b1fa3eba 15import { Video } from '../../shared/video/video.model'
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
33c4972d 36 playerElement: HTMLMediaElement
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
df98563e 63 ngOnInit () {
b1fa3eba
C
64 this.videoService.getVideos({ currentPage: 1, itemsPerPage: 5 }, '-createdAt')
65 .subscribe(
66 data => this.otherVideos = data.videos,
67
68 err => console.error(err)
69 )
70
13fc89f4 71 this.paramsSub = this.route.params.subscribe(routeParams => {
0a6658fd
C
72 let uuid = routeParams['uuid']
73 this.videoService.getVideo(uuid).subscribe(
92fb909c
C
74 video => this.onVideoFetched(video),
75
f1013131
C
76 error => {
77 this.videoNotFound = true
78 console.error(error)
79 }
df98563e
C
80 )
81 })
d1992b93
C
82 }
83
df98563e 84 ngOnDestroy () {
2ed6a0ae 85 // Remove player if it exists
efee3505 86 if (this.videoPlayerLoaded === true) {
2ed6a0ae
C
87 videojs(this.playerElement).dispose()
88 }
067e3f84 89
13fc89f4 90 // Unsubscribe subscriptions
df98563e 91 this.paramsSub.unsubscribe()
dc8bc31b 92 }
98b01bac 93
df98563e
C
94 setLike () {
95 if (this.isUserLoggedIn() === false) return
d38b8281 96 // Already liked this video
df98563e 97 if (this.userRating === 'like') return
d38b8281
C
98
99 this.videoService.setVideoLike(this.video.id)
100 .subscribe(
101 () => {
102 // Update the video like attribute
df98563e
C
103 this.updateVideoRating(this.userRating, 'like')
104 this.userRating = 'like'
d38b8281
C
105 },
106
bfb3a98f 107 err => this.notificationsService.error('Error', err.message)
df98563e 108 )
d38b8281
C
109 }
110
df98563e
C
111 setDislike () {
112 if (this.isUserLoggedIn() === false) return
d38b8281 113 // Already disliked this video
df98563e 114 if (this.userRating === 'dislike') return
d38b8281
C
115
116 this.videoService.setVideoDislike(this.video.id)
117 .subscribe(
118 () => {
119 // Update the video dislike attribute
df98563e
C
120 this.updateVideoRating(this.userRating, 'dislike')
121 this.userRating = 'dislike'
d38b8281
C
122 },
123
bfb3a98f 124 err => this.notificationsService.error('Error', err.message)
df98563e 125 )
d38b8281
C
126 }
127
df98563e
C
128 blacklistVideo (event: Event) {
129 event.preventDefault()
ab683a8e 130
198b205c
GS
131 this.confirmService.confirm('Do you really want to blacklist this video ?', 'Blacklist').subscribe(
132 res => {
df98563e 133 if (res === false) return
198b205c 134
35bf0c83
C
135 this.videoBlacklistService.blacklistVideo(this.video.id)
136 .subscribe(
137 status => {
138 this.notificationsService.success('Success', `Video ${this.video.name} had been blacklisted.`)
139 this.router.navigate(['/videos/list'])
140 },
198b205c 141
35bf0c83
C
142 error => this.notificationsService.error('Error', error.text)
143 )
198b205c 144 }
df98563e 145 )
198b205c
GS
146 }
147
2de96f4d 148 showMoreDescription () {
2de96f4d
C
149 if (this.completeVideoDescription === undefined) {
150 return this.loadCompleteDescription()
151 }
152
153 this.updateVideoDescription(this.completeVideoDescription)
80958c78 154 this.completeDescriptionShown = true
2de96f4d
C
155 }
156
157 showLessDescription () {
2de96f4d 158 this.updateVideoDescription(this.shortVideoDescription)
80958c78 159 this.completeDescriptionShown = false
2de96f4d
C
160 }
161
162 loadCompleteDescription () {
80958c78
C
163 this.descriptionLoading = true
164
2de96f4d
C
165 this.videoService.loadCompleteDescription(this.video.descriptionPath)
166 .subscribe(
167 description => {
80958c78
C
168 this.completeDescriptionShown = true
169 this.descriptionLoading = false
170
2de96f4d
C
171 this.shortVideoDescription = this.video.description
172 this.completeVideoDescription = description
173
174 this.updateVideoDescription(this.completeVideoDescription)
175 },
176
80958c78
C
177 error => {
178 this.descriptionLoading = false
179 this.notificationsService.error('Error', error.text)
180 }
2de96f4d
C
181 )
182 }
183
df98563e
C
184 showReportModal (event: Event) {
185 event.preventDefault()
186 this.videoReportModal.show()
4f8c0eb0
C
187 }
188
df98563e
C
189 showShareModal () {
190 this.videoShareModal.show()
99cc4f49
C
191 }
192
a96aed15 193 showDownloadModal (event: Event) {
df98563e 194 event.preventDefault()
a96aed15 195 this.videoDownloadModal.show()
99cc4f49
C
196 }
197
df98563e
C
198 isUserLoggedIn () {
199 return this.authService.isLoggedIn()
4f8c0eb0
C
200 }
201
df98563e
C
202 isVideoBlacklistable () {
203 return this.video.isBlackistableBy(this.authService.getUser())
198b205c
GS
204 }
205
b1fa3eba
C
206 getAvatarPath () {
207 return Account.GET_ACCOUNT_AVATAR_PATH(this.video.account)
208 }
209
210 getVideoTags () {
211 if (!this.video || Array.isArray(this.video.tags) === false) return []
212
213 return this.video.tags.join(', ')
214 }
215
2de96f4d
C
216 private updateVideoDescription (description: string) {
217 this.video.description = description
218 this.setVideoDescriptionHTML()
219 }
220
221 private setVideoDescriptionHTML () {
222 this.videoHTMLDescription = this.markdownService.markdownToHTML(this.video.description)
223 }
224
0c31c33d
C
225 private handleError (err: any) {
226 const errorMessage: string = typeof err === 'string' ? err : err.message
227 let message = ''
228
229 if (errorMessage.indexOf('http error') !== -1) {
230 message = 'Cannot fetch video from server, maybe down.'
231 } else {
232 message = errorMessage
233 }
234
235 this.notificationsService.error('Error', message)
236 }
237
df98563e 238 private checkUserRating () {
d38b8281 239 // Unlogged users do not have ratings
df98563e 240 if (this.isUserLoggedIn() === false) return
d38b8281
C
241
242 this.videoService.getUserVideoRating(this.video.id)
243 .subscribe(
b632e904 244 ratingObject => {
d38b8281 245 if (ratingObject) {
df98563e 246 this.userRating = ratingObject.rating
d38b8281
C
247 }
248 },
249
bfb3a98f 250 err => this.notificationsService.error('Error', err.message)
df98563e 251 )
d38b8281
C
252 }
253
404b54e1 254 private onVideoFetched (video: VideoDetails) {
df98563e 255 this.video = video
92fb909c 256
df98563e 257 let observable
92fb909c 258 if (this.video.isVideoNSFWForUser(this.authService.getUser())) {
d6e32a2e
C
259 observable = this.confirmService.confirm(
260 'This video contains mature or explicit content. Are you sure you want to watch it?',
261 'Mature or explicit content'
262 )
92fb909c 263 } else {
df98563e 264 observable = Observable.of(true)
92fb909c
C
265 }
266
267 observable.subscribe(
268 res => {
269 if (res === false) {
efee3505 270
df98563e 271 return this.router.navigate([ '/videos/list' ])
92fb909c
C
272 }
273
be6a4802 274 this.playerElement = this.elementRef.nativeElement.querySelector('#video-element')
aa8b6df4
C
275
276 const videojsOptions = {
277 controls: true,
278 autoplay: true,
279 plugins: {
280 peertube: {
281 videoFiles: this.video.files,
282 playerElement: this.playerElement,
283 autoplay: true,
284 peerTubeLink: false
285 }
286 }
287 }
288
efee3505
C
289 this.videoPlayerLoaded = true
290
aa8b6df4
C
291 const self = this
292 videojs(this.playerElement, videojsOptions, function () {
293 self.player = this
294 this.on('customError', (event, data) => {
295 self.handleError(data.err)
296 })
aa8b6df4
C
297 })
298
2de96f4d 299 this.setVideoDescriptionHTML()
9d9597df 300
df98563e 301 this.setOpenGraphTags()
df98563e 302 this.checkUserRating()
1f3e9fec
C
303
304 this.prepareViewAdd()
92fb909c 305 }
df98563e 306 )
92fb909c
C
307 }
308
154898b0 309 private updateVideoRating (oldRating: UserVideoRateType, newRating: VideoRateType) {
df98563e
C
310 let likesToIncrement = 0
311 let dislikesToIncrement = 0
d38b8281
C
312
313 if (oldRating) {
df98563e
C
314 if (oldRating === 'like') likesToIncrement--
315 if (oldRating === 'dislike') dislikesToIncrement--
d38b8281
C
316 }
317
df98563e
C
318 if (newRating === 'like') likesToIncrement++
319 if (newRating === 'dislike') dislikesToIncrement++
d38b8281 320
df98563e
C
321 this.video.likes += likesToIncrement
322 this.video.dislikes += dislikesToIncrement
d38b8281
C
323 }
324
df98563e
C
325 private setOpenGraphTags () {
326 this.metaService.setTitle(this.video.name)
758b996d 327
df98563e 328 this.metaService.setTag('og:type', 'video')
3ec343a4 329
df98563e
C
330 this.metaService.setTag('og:title', this.video.name)
331 this.metaService.setTag('name', this.video.name)
3ec343a4 332
df98563e
C
333 this.metaService.setTag('og:description', this.video.description)
334 this.metaService.setTag('description', this.video.description)
3ec343a4 335
d38309c3 336 this.metaService.setTag('og:image', this.video.previewPath)
3ec343a4 337
df98563e 338 this.metaService.setTag('og:duration', this.video.duration.toString())
3ec343a4 339
df98563e 340 this.metaService.setTag('og:site_name', 'PeerTube')
3ec343a4 341
df98563e
C
342 this.metaService.setTag('og:url', window.location.href)
343 this.metaService.setTag('url', window.location.href)
3ec343a4 344 }
1f3e9fec
C
345
346 private prepareViewAdd () {
347 // After 30 seconds (or 3/4 of the video), increment add a view
348 let viewTimeoutSeconds = 30
349 if (this.video.duration < viewTimeoutSeconds) viewTimeoutSeconds = (this.video.duration * 3) / 4
350
351 setTimeout(() => {
352 this.videoService
353 .viewVideo(this.video.uuid)
354 .subscribe()
355
356 }, viewTimeoutSeconds * 1000)
357 }
dc8bc31b 358}