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