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