]>
Commit | Line | Data |
---|---|---|
e972e046 | 1 | import { catchError } from 'rxjs/operators' |
3b492bff | 2 | import { ChangeDetectorRef, Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' |
df98563e | 3 | import { ActivatedRoute, Router } from '@angular/router' |
901637bb | 4 | import { RedirectService } from '@app/core/routing/redirect.service' |
0bd78bf3 | 5 | import { peertubeLocalStorage } from '@app/shared/misc/peertube-local-storage' |
07fa4c97 | 6 | import { VideoSupportComponent } from '@app/videos/+video-watch/modal/video-support.component' |
1f3e9fec | 7 | import { MetaService } from '@ngx-meta/core' |
f8b2c1b4 | 8 | import { Notifier, ServerService } from '@app/core' |
4c72c1cd | 9 | import { forkJoin, Observable, Subscription } from 'rxjs' |
20d21199 | 10 | import { Hotkey, HotkeysService } from 'angular2-hotkeys' |
72675ebe | 11 | import { UserVideoRateType, VideoCaption, VideoPrivacy, VideoState } from '../../../../../shared' |
df98563e | 12 | import { AuthService, ConfirmService } from '../../core' |
a51bad1a | 13 | import { RestExtractor, VideoBlacklistService } from '../../shared' |
ff249f49 | 14 | import { VideoDetails } from '../../shared/video/video-details.model' |
63c4db6d | 15 | import { VideoService } from '../../shared/video/video.service' |
4635f59d | 16 | import { VideoShareComponent } from './modal/video-share.component' |
20d21199 | 17 | import { SubscribeButtonComponent } from '@app/shared/user-subscription/subscribe-button.component' |
989e526a | 18 | import { I18n } from '@ngx-translate/i18n-polyfill' |
e945b184 | 19 | import { environment } from '../../../environments/environment' |
16f7022b | 20 | import { VideoCaptionService } from '@app/shared/video-caption' |
1506307f | 21 | import { MarkdownService } from '@app/shared/renderer' |
6ec0b75b C |
22 | import { |
23 | P2PMediaLoaderOptions, | |
24 | PeertubePlayerManager, | |
25 | PeertubePlayerManagerOptions, | |
597a9266 | 26 | PlayerMode |
6ec0b75b | 27 | } from '../../../assets/player/peertube-player-manager' |
e2f01c47 C |
28 | import { VideoPlaylist } from '@app/shared/video-playlist/video-playlist.model' |
29 | import { VideoPlaylistService } from '@app/shared/video-playlist/video-playlist.service' | |
e2f01c47 | 30 | import { Video } from '@app/shared/video/video.model' |
31b6ddf8 | 31 | import { isWebRTCDisabled } from '../../../assets/player/utils' |
72675ebe | 32 | import { VideoWatchPlaylistComponent } from '@app/videos/+video-watch/video-watch-playlist.component' |
dc8bc31b | 33 | |
dc8bc31b C |
34 | @Component({ |
35 | selector: 'my-video-watch', | |
ec8d8440 C |
36 | templateUrl: './video-watch.component.html', |
37 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 38 | }) |
0629423c | 39 | export class VideoWatchComponent implements OnInit, OnDestroy { |
22b59e80 C |
40 | private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern' |
41 | ||
72675ebe | 42 | @ViewChild('videoWatchPlaylist') videoWatchPlaylist: VideoWatchPlaylistComponent |
df98563e | 43 | @ViewChild('videoShareModal') videoShareModal: VideoShareComponent |
07fa4c97 | 44 | @ViewChild('videoSupportModal') videoSupportModal: VideoSupportComponent |
20d21199 | 45 | @ViewChild('subscribeButton') subscribeButton: SubscribeButtonComponent |
df98563e | 46 | |
2adfc7ea | 47 | player: any |
0826c92d | 48 | playerElement: HTMLVideoElement |
9a18a625 | 49 | theaterEnabled = false |
154898b0 | 50 | userRating: UserVideoRateType = null |
404b54e1 | 51 | video: VideoDetails = null |
80958c78 | 52 | descriptionLoading = false |
2de96f4d | 53 | |
e2f01c47 | 54 | playlist: VideoPlaylist = null |
e2f01c47 | 55 | |
2de96f4d C |
56 | completeDescriptionShown = false |
57 | completeVideoDescription: string | |
58 | shortVideoDescription: string | |
9d9597df | 59 | videoHTMLDescription = '' |
e9189001 | 60 | likesBarTooltipText = '' |
73e09f27 | 61 | hasAlreadyAcceptedPrivacyConcern = false |
6d88de72 | 62 | remoteServerDown = false |
20d21199 | 63 | hotkeys: Hotkey[] |
df98563e | 64 | |
f0a39880 | 65 | private currentTime: number |
df98563e | 66 | private paramsSub: Subscription |
e2f01c47 | 67 | private queryParamsSub: Subscription |
31b6ddf8 | 68 | private configSub: Subscription |
df98563e C |
69 | |
70 | constructor ( | |
4fd8aa32 | 71 | private elementRef: ElementRef, |
3b492bff | 72 | private changeDetector: ChangeDetectorRef, |
0629423c | 73 | private route: ActivatedRoute, |
92fb909c | 74 | private router: Router, |
d3ef341a | 75 | private videoService: VideoService, |
e2f01c47 | 76 | private playlistService: VideoPlaylistService, |
35bf0c83 | 77 | private videoBlacklistService: VideoBlacklistService, |
92fb909c | 78 | private confirmService: ConfirmService, |
3ec343a4 | 79 | private metaService: MetaService, |
7ddd02c9 | 80 | private authService: AuthService, |
0883b324 | 81 | private serverService: ServerService, |
a51bad1a | 82 | private restExtractor: RestExtractor, |
f8b2c1b4 | 83 | private notifier: Notifier, |
7ae71355 | 84 | private markdownService: MarkdownService, |
901637bb | 85 | private zone: NgZone, |
989e526a | 86 | private redirectService: RedirectService, |
16f7022b | 87 | private videoCaptionService: VideoCaptionService, |
e945b184 | 88 | private i18n: I18n, |
20d21199 | 89 | private hotkeysService: HotkeysService, |
e945b184 | 90 | @Inject(LOCALE_ID) private localeId: string |
d3ef341a | 91 | ) {} |
dc8bc31b | 92 | |
b2731bff C |
93 | get user () { |
94 | return this.authService.getUser() | |
95 | } | |
96 | ||
df98563e | 97 | ngOnInit () { |
31b6ddf8 C |
98 | this.configSub = this.serverService.configLoaded |
99 | .subscribe(() => { | |
100 | if ( | |
101 | isWebRTCDisabled() || | |
102 | this.serverService.getConfig().tracker.enabled === false || | |
103 | peertubeLocalStorage.getItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY) === 'true' | |
104 | ) { | |
105 | this.hasAlreadyAcceptedPrivacyConcern = true | |
106 | } | |
107 | }) | |
2b3b76ab | 108 | |
13fc89f4 | 109 | this.paramsSub = this.route.params.subscribe(routeParams => { |
e2f01c47 C |
110 | const videoId = routeParams[ 'videoId' ] |
111 | if (videoId) this.loadVideo(videoId) | |
a51bad1a | 112 | |
e2f01c47 C |
113 | const playlistId = routeParams[ 'playlistId' ] |
114 | if (playlistId) this.loadPlaylist(playlistId) | |
115 | }) | |
bf079b7b | 116 | |
e2f01c47 C |
117 | this.queryParamsSub = this.route.queryParams.subscribe(queryParams => { |
118 | const videoId = queryParams[ 'videoId' ] | |
119 | if (videoId) this.loadVideo(videoId) | |
df98563e | 120 | }) |
20d21199 | 121 | |
1c8ddbfa | 122 | this.initHotkeys() |
d1992b93 C |
123 | } |
124 | ||
df98563e | 125 | ngOnDestroy () { |
09edde40 | 126 | this.flushPlayer() |
067e3f84 | 127 | |
13fc89f4 | 128 | // Unsubscribe subscriptions |
e2f01c47 C |
129 | if (this.paramsSub) this.paramsSub.unsubscribe() |
130 | if (this.queryParamsSub) this.queryParamsSub.unsubscribe() | |
20d21199 RK |
131 | |
132 | // Unbind hotkeys | |
133 | if (this.isUserLoggedIn()) this.hotkeysService.remove(this.hotkeys) | |
dc8bc31b | 134 | } |
98b01bac | 135 | |
df98563e C |
136 | setLike () { |
137 | if (this.isUserLoggedIn() === false) return | |
4c72c1cd C |
138 | |
139 | // Already liked this video | |
140 | if (this.userRating === 'like') this.setRating('none') | |
141 | else this.setRating('like') | |
d38b8281 C |
142 | } |
143 | ||
df98563e C |
144 | setDislike () { |
145 | if (this.isUserLoggedIn() === false) return | |
4c72c1cd C |
146 | |
147 | // Already disliked this video | |
148 | if (this.userRating === 'dislike') this.setRating('none') | |
149 | else this.setRating('dislike') | |
d38b8281 C |
150 | } |
151 | ||
2de96f4d | 152 | showMoreDescription () { |
2de96f4d C |
153 | if (this.completeVideoDescription === undefined) { |
154 | return this.loadCompleteDescription() | |
155 | } | |
156 | ||
157 | this.updateVideoDescription(this.completeVideoDescription) | |
80958c78 | 158 | this.completeDescriptionShown = true |
2de96f4d C |
159 | } |
160 | ||
161 | showLessDescription () { | |
2de96f4d | 162 | this.updateVideoDescription(this.shortVideoDescription) |
80958c78 | 163 | this.completeDescriptionShown = false |
2de96f4d C |
164 | } |
165 | ||
166 | loadCompleteDescription () { | |
80958c78 C |
167 | this.descriptionLoading = true |
168 | ||
2de96f4d | 169 | this.videoService.loadCompleteDescription(this.video.descriptionPath) |
2186386c C |
170 | .subscribe( |
171 | description => { | |
172 | this.completeDescriptionShown = true | |
173 | this.descriptionLoading = false | |
174 | ||
175 | this.shortVideoDescription = this.video.description | |
176 | this.completeVideoDescription = description | |
177 | ||
178 | this.updateVideoDescription(this.completeVideoDescription) | |
179 | }, | |
180 | ||
181 | error => { | |
182 | this.descriptionLoading = false | |
f8b2c1b4 | 183 | this.notifier.error(error.message) |
2186386c C |
184 | } |
185 | ) | |
2de96f4d C |
186 | } |
187 | ||
07fa4c97 C |
188 | showSupportModal () { |
189 | this.videoSupportModal.show() | |
190 | } | |
191 | ||
df98563e | 192 | showShareModal () { |
f0a39880 | 193 | this.videoShareModal.show(this.currentTime) |
99cc4f49 C |
194 | } |
195 | ||
df98563e C |
196 | isUserLoggedIn () { |
197 | return this.authService.isLoggedIn() | |
4f8c0eb0 C |
198 | } |
199 | ||
b1fa3eba C |
200 | getVideoTags () { |
201 | if (!this.video || Array.isArray(this.video.tags) === false) return [] | |
202 | ||
4278710d | 203 | return this.video.tags |
b1fa3eba C |
204 | } |
205 | ||
3a0fb65c C |
206 | onVideoRemoved () { |
207 | this.redirectService.redirectToHomepage() | |
6725d05c C |
208 | } |
209 | ||
73e09f27 | 210 | acceptedPrivacyConcern () { |
0bd78bf3 | 211 | peertubeLocalStorage.setItem(VideoWatchComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') |
73e09f27 C |
212 | this.hasAlreadyAcceptedPrivacyConcern = true |
213 | } | |
214 | ||
2186386c C |
215 | isVideoToTranscode () { |
216 | return this.video && this.video.state.id === VideoState.TO_TRANSCODE | |
217 | } | |
218 | ||
516df59b C |
219 | isVideoToImport () { |
220 | return this.video && this.video.state.id === VideoState.TO_IMPORT | |
221 | } | |
222 | ||
bbe0f064 C |
223 | hasVideoScheduledPublication () { |
224 | return this.video && this.video.scheduledUpdate !== undefined | |
225 | } | |
226 | ||
e2f01c47 C |
227 | isVideoBlur (video: Video) { |
228 | return video.isVideoNSFWForUser(this.user, this.serverService.getConfig()) | |
229 | } | |
230 | ||
e2f01c47 C |
231 | private loadVideo (videoId: string) { |
232 | // Video did not change | |
233 | if (this.video && this.video.uuid === videoId) return | |
234 | ||
235 | if (this.player) this.player.pause() | |
236 | ||
237 | // Video did change | |
238 | forkJoin( | |
239 | this.videoService.getVideo(videoId), | |
240 | this.videoCaptionService.listCaptions(videoId) | |
241 | ) | |
242 | .pipe( | |
243 | // If 401, the video is private or blacklisted so redirect to 404 | |
244 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
245 | ) | |
246 | .subscribe(([ video, captionsResult ]) => { | |
247 | const queryParams = this.route.snapshot.queryParams | |
e2f01c47 | 248 | |
4c72c1cd C |
249 | const urlOptions = { |
250 | startTime: queryParams.start, | |
251 | stopTime: queryParams.stop, | |
252 | subtitle: queryParams.subtitle, | |
253 | playerMode: queryParams.mode | |
254 | } | |
255 | ||
256 | this.onVideoFetched(video, captionsResult.data, urlOptions) | |
e2f01c47 C |
257 | .catch(err => this.handleError(err)) |
258 | }) | |
259 | } | |
260 | ||
261 | private loadPlaylist (playlistId: string) { | |
262 | // Playlist did not change | |
263 | if (this.playlist && this.playlist.uuid === playlistId) return | |
264 | ||
265 | this.playlistService.getVideoPlaylist(playlistId) | |
266 | .pipe( | |
267 | // If 401, the video is private or blacklisted so redirect to 404 | |
268 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
269 | ) | |
270 | .subscribe(playlist => { | |
271 | this.playlist = playlist | |
272 | ||
273 | const videoId = this.route.snapshot.queryParams['videoId'] | |
72675ebe | 274 | this.videoWatchPlaylist.loadPlaylistElements(playlist, !videoId) |
e2f01c47 C |
275 | }) |
276 | } | |
277 | ||
2de96f4d C |
278 | private updateVideoDescription (description: string) { |
279 | this.video.description = description | |
280 | this.setVideoDescriptionHTML() | |
4c72c1cd | 281 | .catch(err => console.error(err)) |
2de96f4d C |
282 | } |
283 | ||
41d71344 C |
284 | private async setVideoDescriptionHTML () { |
285 | this.videoHTMLDescription = await this.markdownService.textMarkdownToHTML(this.video.description) | |
2de96f4d C |
286 | } |
287 | ||
e9189001 | 288 | private setVideoLikesBarTooltipText () { |
2186386c C |
289 | this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', { |
290 | likesNumber: this.video.likes, | |
291 | dislikesNumber: this.video.dislikes | |
292 | }) | |
e9189001 C |
293 | } |
294 | ||
0c31c33d C |
295 | private handleError (err: any) { |
296 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
297 | if (!errorMessage) return |
298 | ||
6d88de72 | 299 | // Display a message in the video player instead of a notification |
0f7fedc3 | 300 | if (errorMessage.indexOf('from xs param') !== -1) { |
6d88de72 C |
301 | this.flushPlayer() |
302 | this.remoteServerDown = true | |
3b492bff C |
303 | this.changeDetector.detectChanges() |
304 | ||
6d88de72 | 305 | return |
0c31c33d C |
306 | } |
307 | ||
f8b2c1b4 | 308 | this.notifier.error(errorMessage) |
0c31c33d C |
309 | } |
310 | ||
df98563e | 311 | private checkUserRating () { |
d38b8281 | 312 | // Unlogged users do not have ratings |
df98563e | 313 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
314 | |
315 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
316 | .subscribe( |
317 | ratingObject => { | |
318 | if (ratingObject) { | |
319 | this.userRating = ratingObject.rating | |
320 | } | |
321 | }, | |
322 | ||
f8b2c1b4 | 323 | err => this.notifier.error(err.message) |
2186386c | 324 | ) |
d38b8281 C |
325 | } |
326 | ||
597a9266 C |
327 | private async onVideoFetched ( |
328 | video: VideoDetails, | |
329 | videoCaptions: VideoCaption[], | |
f0a39880 | 330 | urlOptions: { startTime?: number, stopTime?: number, subtitle?: string, playerMode?: string } |
597a9266 | 331 | ) { |
df98563e | 332 | this.video = video |
92fb909c | 333 | |
c448d412 C |
334 | // Re init attributes |
335 | this.descriptionLoading = false | |
336 | this.completeDescriptionShown = false | |
6d88de72 | 337 | this.remoteServerDown = false |
f0a39880 | 338 | this.currentTime = undefined |
c448d412 | 339 | |
72675ebe | 340 | this.videoWatchPlaylist.updatePlaylistIndex(video) |
e2f01c47 | 341 | |
1b04f19c | 342 | let startTime = urlOptions.startTime || (this.video.userHistory ? this.video.userHistory.currentTime : 0) |
43483d12 | 343 | // If we are at the end of the video, reset the timer |
6e46de09 C |
344 | if (this.video.duration - startTime <= 1) startTime = 0 |
345 | ||
e2f01c47 | 346 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 347 | const res = await this.confirmService.confirm( |
989e526a C |
348 | this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'), |
349 | this.i18n('Mature or explicit content') | |
d6e32a2e | 350 | ) |
901637bb | 351 | if (res === false) return this.redirectService.redirectToHomepage() |
92fb909c C |
352 | } |
353 | ||
09edde40 C |
354 | // Flush old player if needed |
355 | this.flushPlayer() | |
b891f9bc C |
356 | |
357 | // Build video element, because videojs remove it on dispose | |
e2f01c47 | 358 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
359 | this.playerElement = document.createElement('video') |
360 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 361 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
362 | playerElementWrapper.appendChild(this.playerElement) |
363 | ||
16f7022b C |
364 | const playerCaptions = videoCaptions.map(c => ({ |
365 | label: c.language.label, | |
366 | language: c.language.id, | |
367 | src: environment.apiUrl + c.captionPath | |
368 | })) | |
369 | ||
6ec0b75b | 370 | const options: PeertubePlayerManagerOptions = { |
2adfc7ea C |
371 | common: { |
372 | autoplay: this.isAutoplay(), | |
6ec0b75b | 373 | |
2adfc7ea | 374 | playerElement: this.playerElement, |
6ec0b75b C |
375 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, |
376 | ||
2adfc7ea C |
377 | videoDuration: this.video.duration, |
378 | enableHotkeys: true, | |
379 | inactivityTimeout: 2500, | |
380 | poster: this.video.previewUrl, | |
381 | startTime, | |
f0a39880 | 382 | stopTime: urlOptions.stopTime, |
2adfc7ea C |
383 | |
384 | theaterMode: true, | |
385 | captions: videoCaptions.length !== 0, | |
386 | peertubeLink: false, | |
387 | ||
4c72c1cd C |
388 | videoViewUrl: this.video.privacy.id !== VideoPrivacy.PRIVATE |
389 | ? this.videoService.getVideoViewUrl(this.video.uuid) | |
390 | : null, | |
2adfc7ea C |
391 | embedUrl: this.video.embedUrl, |
392 | ||
393 | language: this.localeId, | |
394 | ||
395 | subtitle: urlOptions.subtitle, | |
aa8b6df4 | 396 | |
2adfc7ea C |
397 | userWatching: this.user && this.user.videosHistoryEnabled === true ? { |
398 | url: this.videoService.getUserWatchingVideoUrl(this.video.uuid), | |
399 | authorizationHeader: this.authService.getRequestHeaderValue() | |
400 | } : undefined, | |
aa8b6df4 | 401 | |
2adfc7ea C |
402 | serverUrl: environment.apiUrl, |
403 | ||
404 | videoCaptions: playerCaptions | |
6ec0b75b C |
405 | }, |
406 | ||
407 | webtorrent: { | |
408 | videoFiles: this.video.files | |
09209296 | 409 | } |
e945b184 C |
410 | } |
411 | ||
65659166 C |
412 | let mode: PlayerMode |
413 | ||
414 | if (urlOptions.playerMode) { | |
415 | if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader' | |
416 | else mode = 'webtorrent' | |
417 | } else { | |
418 | if (this.video.hasHlsPlaylist()) mode = 'p2p-media-loader' | |
419 | else mode = 'webtorrent' | |
420 | } | |
597a9266 C |
421 | |
422 | if (mode === 'p2p-media-loader') { | |
423 | const hlsPlaylist = this.video.getHlsPlaylist() | |
e945b184 | 424 | |
09209296 C |
425 | const p2pMediaLoader = { |
426 | playlistUrl: hlsPlaylist.playlistUrl, | |
427 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
428 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
429 | trackerAnnounce: this.video.trackerUrls, | |
2adfc7ea | 430 | videoFiles: this.video.files |
09209296 C |
431 | } as P2PMediaLoaderOptions |
432 | ||
433 | Object.assign(options, { p2pMediaLoader }) | |
e945b184 C |
434 | } |
435 | ||
e945b184 | 436 | this.zone.runOutsideAngular(async () => { |
09209296 | 437 | this.player = await PeertubePlayerManager.initialize(mode, options) |
9a18a625 C |
438 | this.theaterEnabled = this.player.theaterEnabled |
439 | ||
2adfc7ea | 440 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
441 | |
442 | this.player.on('timeupdate', () => { | |
443 | this.currentTime = Math.floor(this.player.currentTime()) | |
444 | }) | |
e2f01c47 C |
445 | |
446 | this.player.one('ended', () => { | |
447 | if (this.playlist) { | |
72675ebe | 448 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
449 | } |
450 | }) | |
451 | ||
452 | this.player.one('stopped', () => { | |
453 | if (this.playlist) { | |
72675ebe | 454 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
455 | } |
456 | }) | |
9a18a625 C |
457 | |
458 | this.player.on('theaterChange', (_: any, enabled: boolean) => { | |
459 | this.zone.run(() => this.theaterEnabled = enabled) | |
460 | }) | |
b891f9bc | 461 | }) |
22b59e80 C |
462 | |
463 | this.setVideoDescriptionHTML() | |
464 | this.setVideoLikesBarTooltipText() | |
465 | ||
466 | this.setOpenGraphTags() | |
467 | this.checkUserRating() | |
92fb909c C |
468 | } |
469 | ||
5c6d985f | 470 | private setRating (nextRating: UserVideoRateType) { |
4c72c1cd C |
471 | const ratingMethods: { [id in UserVideoRateType]: (id: number) => Observable<any> } = { |
472 | like: this.videoService.setVideoLike, | |
473 | dislike: this.videoService.setVideoDislike, | |
474 | none: this.videoService.unsetVideoLike | |
57a49263 BB |
475 | } |
476 | ||
4c72c1cd | 477 | ratingMethods[nextRating].call(this.videoService, this.video.id) |
2186386c C |
478 | .subscribe( |
479 | () => { | |
480 | // Update the video like attribute | |
481 | this.updateVideoRating(this.userRating, nextRating) | |
482 | this.userRating = nextRating | |
483 | }, | |
484 | ||
f8b2c1b4 | 485 | (err: { message: string }) => this.notifier.error(err.message) |
2186386c | 486 | ) |
57a49263 BB |
487 | } |
488 | ||
5c6d985f | 489 | private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) { |
df98563e C |
490 | let likesToIncrement = 0 |
491 | let dislikesToIncrement = 0 | |
d38b8281 C |
492 | |
493 | if (oldRating) { | |
df98563e C |
494 | if (oldRating === 'like') likesToIncrement-- |
495 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
496 | } |
497 | ||
df98563e C |
498 | if (newRating === 'like') likesToIncrement++ |
499 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 500 | |
df98563e C |
501 | this.video.likes += likesToIncrement |
502 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 503 | |
22b59e80 | 504 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 505 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
506 | } |
507 | ||
df98563e C |
508 | private setOpenGraphTags () { |
509 | this.metaService.setTitle(this.video.name) | |
758b996d | 510 | |
df98563e | 511 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 512 | |
df98563e C |
513 | this.metaService.setTag('og:title', this.video.name) |
514 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 515 | |
df98563e C |
516 | this.metaService.setTag('og:description', this.video.description) |
517 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 518 | |
d38309c3 | 519 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 520 | |
df98563e | 521 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 522 | |
df98563e | 523 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 524 | |
df98563e C |
525 | this.metaService.setTag('og:url', window.location.href) |
526 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 527 | } |
1f3e9fec | 528 | |
d4c6a3b9 | 529 | private isAutoplay () { |
bf079b7b C |
530 | // We'll jump to the thread id, so do not play the video |
531 | if (this.route.snapshot.params['threadId']) return false | |
532 | ||
533 | // Otherwise true by default | |
d4c6a3b9 C |
534 | if (!this.user) return true |
535 | ||
536 | // Be sure the autoPlay is set to false | |
537 | return this.user.autoPlayVideo !== false | |
538 | } | |
09edde40 C |
539 | |
540 | private flushPlayer () { | |
541 | // Remove player if it exists | |
542 | if (this.player) { | |
536598cf C |
543 | try { |
544 | this.player.dispose() | |
545 | this.player = undefined | |
546 | } catch (err) { | |
547 | console.error('Cannot dispose player.', err) | |
548 | } | |
09edde40 C |
549 | } |
550 | } | |
1c8ddbfa C |
551 | |
552 | private initHotkeys () { | |
553 | this.hotkeys = [ | |
4c72c1cd | 554 | new Hotkey('shift+l', () => { |
1c8ddbfa C |
555 | this.setLike() |
556 | return false | |
557 | }, undefined, this.i18n('Like the video')), | |
4c72c1cd C |
558 | |
559 | new Hotkey('shift+d', () => { | |
1c8ddbfa C |
560 | this.setDislike() |
561 | return false | |
562 | }, undefined, this.i18n('Dislike the video')), | |
4c72c1cd C |
563 | |
564 | new Hotkey('shift+s', () => { | |
565 | this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe() | |
1c8ddbfa C |
566 | return false |
567 | }, undefined, this.i18n('Subscribe to the account')) | |
568 | ] | |
569 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
570 | } | |
dc8bc31b | 571 | } |