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