]>
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' |
93cae479 | 36 | import { HooksService } from '@app/core/plugins/hooks.service' |
dc8bc31b | 37 | |
dc8bc31b C |
38 | @Component({ |
39 | selector: 'my-video-watch', | |
ec8d8440 C |
40 | templateUrl: './video-watch.component.html', |
41 | styleUrls: [ './video-watch.component.scss' ] | |
dc8bc31b | 42 | }) |
0629423c | 43 | export class VideoWatchComponent implements OnInit, OnDestroy { |
22b59e80 C |
44 | private static LOCAL_STORAGE_PRIVACY_CONCERN_KEY = 'video-watch-privacy-concern' |
45 | ||
f36da21e C |
46 | @ViewChild('videoWatchPlaylist', { static: true }) videoWatchPlaylist: VideoWatchPlaylistComponent |
47 | @ViewChild('videoShareModal', { static: false }) videoShareModal: VideoShareComponent | |
48 | @ViewChild('videoSupportModal', { static: false }) videoSupportModal: VideoSupportComponent | |
49 | @ViewChild('subscribeButton', { static: false }) subscribeButton: SubscribeButtonComponent | |
df98563e | 50 | |
2adfc7ea | 51 | player: any |
0826c92d | 52 | playerElement: HTMLVideoElement |
9a18a625 | 53 | theaterEnabled = false |
154898b0 | 54 | userRating: UserVideoRateType = null |
80958c78 | 55 | descriptionLoading = false |
2de96f4d | 56 | |
2f4c784a C |
57 | video: VideoDetails = null |
58 | videoCaptions: VideoCaption[] = [] | |
59 | ||
e2f01c47 | 60 | playlist: VideoPlaylist = null |
e2f01c47 | 61 | |
2de96f4d C |
62 | completeDescriptionShown = false |
63 | completeVideoDescription: string | |
64 | shortVideoDescription: string | |
9d9597df | 65 | videoHTMLDescription = '' |
e9189001 | 66 | likesBarTooltipText = '' |
73e09f27 | 67 | hasAlreadyAcceptedPrivacyConcern = false |
6d88de72 | 68 | remoteServerDown = false |
20d21199 | 69 | hotkeys: Hotkey[] |
df98563e | 70 | |
f0a39880 | 71 | private currentTime: number |
df98563e | 72 | private paramsSub: Subscription |
e2f01c47 | 73 | private queryParamsSub: Subscription |
31b6ddf8 | 74 | private configSub: Subscription |
df98563e C |
75 | |
76 | constructor ( | |
4fd8aa32 | 77 | private elementRef: ElementRef, |
3b492bff | 78 | private changeDetector: ChangeDetectorRef, |
0629423c | 79 | private route: ActivatedRoute, |
92fb909c | 80 | private router: Router, |
d3ef341a | 81 | private videoService: VideoService, |
e2f01c47 | 82 | private playlistService: VideoPlaylistService, |
35bf0c83 | 83 | private videoBlacklistService: VideoBlacklistService, |
92fb909c | 84 | private confirmService: ConfirmService, |
3ec343a4 | 85 | private metaService: MetaService, |
7ddd02c9 | 86 | private authService: AuthService, |
0883b324 | 87 | private serverService: ServerService, |
a51bad1a | 88 | private restExtractor: RestExtractor, |
f8b2c1b4 | 89 | private notifier: Notifier, |
18a6f04c | 90 | private pluginService: PluginService, |
7ae71355 | 91 | private markdownService: MarkdownService, |
901637bb | 92 | private zone: NgZone, |
989e526a | 93 | private redirectService: RedirectService, |
16f7022b | 94 | private videoCaptionService: VideoCaptionService, |
e945b184 | 95 | private i18n: I18n, |
20d21199 | 96 | private hotkeysService: HotkeysService, |
93cae479 | 97 | private hooks: HooksService, |
e945b184 | 98 | @Inject(LOCALE_ID) private localeId: string |
d3ef341a | 99 | ) {} |
dc8bc31b | 100 | |
b2731bff C |
101 | get user () { |
102 | return this.authService.getUser() | |
103 | } | |
104 | ||
18a6f04c | 105 | async ngOnInit () { |
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 | 133 | |
c9e3eeed | 134 | this.hooks.runAction('action:video-watch.init', 'video-watch') |
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 | ||
93cae479 C |
249 | const videoObs = this.hooks.wrapObsFun( |
250 | this.videoService.getVideo.bind(this.videoService), | |
251 | { videoId }, | |
252 | 'video-watch', | |
253 | 'filter:api.video-watch.video.get.params', | |
254 | 'filter:api.video-watch.video.get.result' | |
255 | ) | |
256 | ||
e2f01c47 | 257 | // Video did change |
c8861d5d | 258 | forkJoin([ |
93cae479 | 259 | videoObs, |
e2f01c47 | 260 | this.videoCaptionService.listCaptions(videoId) |
c8861d5d | 261 | ]) |
e2f01c47 C |
262 | .pipe( |
263 | // If 401, the video is private or blacklisted so redirect to 404 | |
264 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
265 | ) | |
266 | .subscribe(([ video, captionsResult ]) => { | |
267 | const queryParams = this.route.snapshot.queryParams | |
e2f01c47 | 268 | |
4c72c1cd C |
269 | const urlOptions = { |
270 | startTime: queryParams.start, | |
271 | stopTime: queryParams.stop, | |
5efab546 C |
272 | |
273 | muted: queryParams.muted, | |
274 | loop: queryParams.loop, | |
4c72c1cd | 275 | subtitle: queryParams.subtitle, |
5efab546 C |
276 | |
277 | playerMode: queryParams.mode, | |
278 | peertubeLink: false | |
4c72c1cd C |
279 | } |
280 | ||
281 | this.onVideoFetched(video, captionsResult.data, urlOptions) | |
e2f01c47 C |
282 | .catch(err => this.handleError(err)) |
283 | }) | |
284 | } | |
285 | ||
286 | private loadPlaylist (playlistId: string) { | |
287 | // Playlist did not change | |
288 | if (this.playlist && this.playlist.uuid === playlistId) return | |
289 | ||
290 | this.playlistService.getVideoPlaylist(playlistId) | |
291 | .pipe( | |
292 | // If 401, the video is private or blacklisted so redirect to 404 | |
293 | catchError(err => this.restExtractor.redirectTo404IfNotFound(err, [ 400, 401, 403, 404 ])) | |
294 | ) | |
295 | .subscribe(playlist => { | |
296 | this.playlist = playlist | |
297 | ||
298 | const videoId = this.route.snapshot.queryParams['videoId'] | |
72675ebe | 299 | this.videoWatchPlaylist.loadPlaylistElements(playlist, !videoId) |
e2f01c47 C |
300 | }) |
301 | } | |
302 | ||
2de96f4d C |
303 | private updateVideoDescription (description: string) { |
304 | this.video.description = description | |
305 | this.setVideoDescriptionHTML() | |
4c72c1cd | 306 | .catch(err => console.error(err)) |
2de96f4d C |
307 | } |
308 | ||
41d71344 C |
309 | private async setVideoDescriptionHTML () { |
310 | this.videoHTMLDescription = await this.markdownService.textMarkdownToHTML(this.video.description) | |
2de96f4d C |
311 | } |
312 | ||
e9189001 | 313 | private setVideoLikesBarTooltipText () { |
2186386c C |
314 | this.likesBarTooltipText = this.i18n('{{likesNumber}} likes / {{dislikesNumber}} dislikes', { |
315 | likesNumber: this.video.likes, | |
316 | dislikesNumber: this.video.dislikes | |
317 | }) | |
e9189001 C |
318 | } |
319 | ||
0c31c33d C |
320 | private handleError (err: any) { |
321 | const errorMessage: string = typeof err === 'string' ? err : err.message | |
bf5685f0 C |
322 | if (!errorMessage) return |
323 | ||
6d88de72 | 324 | // Display a message in the video player instead of a notification |
0f7fedc3 | 325 | if (errorMessage.indexOf('from xs param') !== -1) { |
6d88de72 C |
326 | this.flushPlayer() |
327 | this.remoteServerDown = true | |
3b492bff C |
328 | this.changeDetector.detectChanges() |
329 | ||
6d88de72 | 330 | return |
0c31c33d C |
331 | } |
332 | ||
f8b2c1b4 | 333 | this.notifier.error(errorMessage) |
0c31c33d C |
334 | } |
335 | ||
df98563e | 336 | private checkUserRating () { |
d38b8281 | 337 | // Unlogged users do not have ratings |
df98563e | 338 | if (this.isUserLoggedIn() === false) return |
d38b8281 C |
339 | |
340 | this.videoService.getUserVideoRating(this.video.id) | |
2186386c C |
341 | .subscribe( |
342 | ratingObject => { | |
343 | if (ratingObject) { | |
344 | this.userRating = ratingObject.rating | |
345 | } | |
346 | }, | |
347 | ||
f8b2c1b4 | 348 | err => this.notifier.error(err.message) |
2186386c | 349 | ) |
d38b8281 C |
350 | } |
351 | ||
597a9266 C |
352 | private async onVideoFetched ( |
353 | video: VideoDetails, | |
354 | videoCaptions: VideoCaption[], | |
5efab546 | 355 | urlOptions: CustomizationOptions & { playerMode: PlayerMode } |
597a9266 | 356 | ) { |
df98563e | 357 | this.video = video |
2f4c784a | 358 | this.videoCaptions = videoCaptions |
92fb909c | 359 | |
c448d412 C |
360 | // Re init attributes |
361 | this.descriptionLoading = false | |
362 | this.completeDescriptionShown = false | |
6d88de72 | 363 | this.remoteServerDown = false |
f0a39880 | 364 | this.currentTime = undefined |
c448d412 | 365 | |
72675ebe | 366 | this.videoWatchPlaylist.updatePlaylistIndex(video) |
e2f01c47 | 367 | |
5efab546 | 368 | let startTime = timeToInt(urlOptions.startTime) || (this.video.userHistory ? this.video.userHistory.currentTime : 0) |
43483d12 | 369 | // If we are at the end of the video, reset the timer |
6e46de09 C |
370 | if (this.video.duration - startTime <= 1) startTime = 0 |
371 | ||
e2f01c47 | 372 | if (this.isVideoBlur(this.video)) { |
22b59e80 | 373 | const res = await this.confirmService.confirm( |
989e526a C |
374 | this.i18n('This video contains mature or explicit content. Are you sure you want to watch it?'), |
375 | this.i18n('Mature or explicit content') | |
d6e32a2e | 376 | ) |
901637bb | 377 | if (res === false) return this.redirectService.redirectToHomepage() |
92fb909c C |
378 | } |
379 | ||
09edde40 C |
380 | // Flush old player if needed |
381 | this.flushPlayer() | |
b891f9bc C |
382 | |
383 | // Build video element, because videojs remove it on dispose | |
e2f01c47 | 384 | const playerElementWrapper = this.elementRef.nativeElement.querySelector('#videojs-wrapper') |
b891f9bc C |
385 | this.playerElement = document.createElement('video') |
386 | this.playerElement.className = 'video-js vjs-peertube-skin' | |
e7eb5b39 | 387 | this.playerElement.setAttribute('playsinline', 'true') |
b891f9bc C |
388 | playerElementWrapper.appendChild(this.playerElement) |
389 | ||
16f7022b C |
390 | const playerCaptions = videoCaptions.map(c => ({ |
391 | label: c.language.label, | |
392 | language: c.language.id, | |
393 | src: environment.apiUrl + c.captionPath | |
394 | })) | |
395 | ||
6ec0b75b | 396 | const options: PeertubePlayerManagerOptions = { |
2adfc7ea C |
397 | common: { |
398 | autoplay: this.isAutoplay(), | |
6ec0b75b | 399 | |
2adfc7ea | 400 | playerElement: this.playerElement, |
6ec0b75b C |
401 | onPlayerElementChange: (element: HTMLVideoElement) => this.playerElement = element, |
402 | ||
2adfc7ea C |
403 | videoDuration: this.video.duration, |
404 | enableHotkeys: true, | |
405 | inactivityTimeout: 2500, | |
406 | poster: this.video.previewUrl, | |
5efab546 | 407 | |
2adfc7ea | 408 | startTime, |
f0a39880 | 409 | stopTime: urlOptions.stopTime, |
5efab546 C |
410 | controls: urlOptions.controls, |
411 | muted: urlOptions.muted, | |
412 | loop: urlOptions.loop, | |
413 | subtitle: urlOptions.subtitle, | |
414 | ||
415 | peertubeLink: urlOptions.peertubeLink, | |
2adfc7ea C |
416 | |
417 | theaterMode: true, | |
418 | captions: videoCaptions.length !== 0, | |
2adfc7ea | 419 | |
4c72c1cd C |
420 | videoViewUrl: this.video.privacy.id !== VideoPrivacy.PRIVATE |
421 | ? this.videoService.getVideoViewUrl(this.video.uuid) | |
422 | : null, | |
2adfc7ea C |
423 | embedUrl: this.video.embedUrl, |
424 | ||
425 | language: this.localeId, | |
426 | ||
2adfc7ea C |
427 | userWatching: this.user && this.user.videosHistoryEnabled === true ? { |
428 | url: this.videoService.getUserWatchingVideoUrl(this.video.uuid), | |
429 | authorizationHeader: this.authService.getRequestHeaderValue() | |
430 | } : undefined, | |
aa8b6df4 | 431 | |
2adfc7ea C |
432 | serverUrl: environment.apiUrl, |
433 | ||
434 | videoCaptions: playerCaptions | |
6ec0b75b C |
435 | }, |
436 | ||
437 | webtorrent: { | |
438 | videoFiles: this.video.files | |
09209296 | 439 | } |
e945b184 C |
440 | } |
441 | ||
65659166 C |
442 | let mode: PlayerMode |
443 | ||
444 | if (urlOptions.playerMode) { | |
445 | if (urlOptions.playerMode === 'p2p-media-loader') mode = 'p2p-media-loader' | |
446 | else mode = 'webtorrent' | |
447 | } else { | |
448 | if (this.video.hasHlsPlaylist()) mode = 'p2p-media-loader' | |
449 | else mode = 'webtorrent' | |
450 | } | |
597a9266 C |
451 | |
452 | if (mode === 'p2p-media-loader') { | |
453 | const hlsPlaylist = this.video.getHlsPlaylist() | |
e945b184 | 454 | |
09209296 C |
455 | const p2pMediaLoader = { |
456 | playlistUrl: hlsPlaylist.playlistUrl, | |
457 | segmentsSha256Url: hlsPlaylist.segmentsSha256Url, | |
458 | redundancyBaseUrls: hlsPlaylist.redundancies.map(r => r.baseUrl), | |
459 | trackerAnnounce: this.video.trackerUrls, | |
2adfc7ea | 460 | videoFiles: this.video.files |
09209296 C |
461 | } as P2PMediaLoaderOptions |
462 | ||
463 | Object.assign(options, { p2pMediaLoader }) | |
e945b184 C |
464 | } |
465 | ||
e945b184 | 466 | this.zone.runOutsideAngular(async () => { |
09209296 | 467 | this.player = await PeertubePlayerManager.initialize(mode, options) |
9a18a625 | 468 | |
2adfc7ea | 469 | this.player.on('customError', ({ err }: { err: any }) => this.handleError(err)) |
f0a39880 C |
470 | |
471 | this.player.on('timeupdate', () => { | |
472 | this.currentTime = Math.floor(this.player.currentTime()) | |
473 | }) | |
e2f01c47 C |
474 | |
475 | this.player.one('ended', () => { | |
476 | if (this.playlist) { | |
72675ebe | 477 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
478 | } |
479 | }) | |
480 | ||
481 | this.player.one('stopped', () => { | |
482 | if (this.playlist) { | |
72675ebe | 483 | this.zone.run(() => this.videoWatchPlaylist.navigateToNextPlaylistVideo()) |
e2f01c47 C |
484 | } |
485 | }) | |
9a18a625 C |
486 | |
487 | this.player.on('theaterChange', (_: any, enabled: boolean) => { | |
488 | this.zone.run(() => this.theaterEnabled = enabled) | |
489 | }) | |
b891f9bc | 490 | }) |
22b59e80 C |
491 | |
492 | this.setVideoDescriptionHTML() | |
493 | this.setVideoLikesBarTooltipText() | |
494 | ||
495 | this.setOpenGraphTags() | |
496 | this.checkUserRating() | |
93cae479 | 497 | |
c9e3eeed | 498 | this.hooks.runAction('action:video-watch.video.loaded', 'video-watch') |
92fb909c C |
499 | } |
500 | ||
5c6d985f | 501 | private setRating (nextRating: UserVideoRateType) { |
4c72c1cd C |
502 | const ratingMethods: { [id in UserVideoRateType]: (id: number) => Observable<any> } = { |
503 | like: this.videoService.setVideoLike, | |
504 | dislike: this.videoService.setVideoDislike, | |
505 | none: this.videoService.unsetVideoLike | |
57a49263 BB |
506 | } |
507 | ||
4c72c1cd | 508 | ratingMethods[nextRating].call(this.videoService, this.video.id) |
2186386c C |
509 | .subscribe( |
510 | () => { | |
511 | // Update the video like attribute | |
512 | this.updateVideoRating(this.userRating, nextRating) | |
513 | this.userRating = nextRating | |
514 | }, | |
515 | ||
f8b2c1b4 | 516 | (err: { message: string }) => this.notifier.error(err.message) |
2186386c | 517 | ) |
57a49263 BB |
518 | } |
519 | ||
5c6d985f | 520 | private updateVideoRating (oldRating: UserVideoRateType, newRating: UserVideoRateType) { |
df98563e C |
521 | let likesToIncrement = 0 |
522 | let dislikesToIncrement = 0 | |
d38b8281 C |
523 | |
524 | if (oldRating) { | |
df98563e C |
525 | if (oldRating === 'like') likesToIncrement-- |
526 | if (oldRating === 'dislike') dislikesToIncrement-- | |
d38b8281 C |
527 | } |
528 | ||
df98563e C |
529 | if (newRating === 'like') likesToIncrement++ |
530 | if (newRating === 'dislike') dislikesToIncrement++ | |
d38b8281 | 531 | |
df98563e C |
532 | this.video.likes += likesToIncrement |
533 | this.video.dislikes += dislikesToIncrement | |
20b40b19 | 534 | |
22b59e80 | 535 | this.video.buildLikeAndDislikePercents() |
20b40b19 | 536 | this.setVideoLikesBarTooltipText() |
d38b8281 C |
537 | } |
538 | ||
df98563e C |
539 | private setOpenGraphTags () { |
540 | this.metaService.setTitle(this.video.name) | |
758b996d | 541 | |
df98563e | 542 | this.metaService.setTag('og:type', 'video') |
3ec343a4 | 543 | |
df98563e C |
544 | this.metaService.setTag('og:title', this.video.name) |
545 | this.metaService.setTag('name', this.video.name) | |
3ec343a4 | 546 | |
df98563e C |
547 | this.metaService.setTag('og:description', this.video.description) |
548 | this.metaService.setTag('description', this.video.description) | |
3ec343a4 | 549 | |
d38309c3 | 550 | this.metaService.setTag('og:image', this.video.previewPath) |
3ec343a4 | 551 | |
df98563e | 552 | this.metaService.setTag('og:duration', this.video.duration.toString()) |
3ec343a4 | 553 | |
df98563e | 554 | this.metaService.setTag('og:site_name', 'PeerTube') |
3ec343a4 | 555 | |
df98563e C |
556 | this.metaService.setTag('og:url', window.location.href) |
557 | this.metaService.setTag('url', window.location.href) | |
3ec343a4 | 558 | } |
1f3e9fec | 559 | |
d4c6a3b9 | 560 | private isAutoplay () { |
bf079b7b C |
561 | // We'll jump to the thread id, so do not play the video |
562 | if (this.route.snapshot.params['threadId']) return false | |
563 | ||
564 | // Otherwise true by default | |
d4c6a3b9 C |
565 | if (!this.user) return true |
566 | ||
567 | // Be sure the autoPlay is set to false | |
568 | return this.user.autoPlayVideo !== false | |
569 | } | |
09edde40 C |
570 | |
571 | private flushPlayer () { | |
572 | // Remove player if it exists | |
573 | if (this.player) { | |
536598cf C |
574 | try { |
575 | this.player.dispose() | |
576 | this.player = undefined | |
577 | } catch (err) { | |
578 | console.error('Cannot dispose player.', err) | |
579 | } | |
09edde40 C |
580 | } |
581 | } | |
1c8ddbfa C |
582 | |
583 | private initHotkeys () { | |
584 | this.hotkeys = [ | |
4c72c1cd | 585 | new Hotkey('shift+l', () => { |
1c8ddbfa C |
586 | this.setLike() |
587 | return false | |
588 | }, undefined, this.i18n('Like the video')), | |
4c72c1cd C |
589 | |
590 | new Hotkey('shift+d', () => { | |
1c8ddbfa C |
591 | this.setDislike() |
592 | return false | |
593 | }, undefined, this.i18n('Dislike the video')), | |
4c72c1cd C |
594 | |
595 | new Hotkey('shift+s', () => { | |
596 | this.subscribeButton.subscribed ? this.subscribeButton.unsubscribe() : this.subscribeButton.subscribe() | |
1c8ddbfa C |
597 | return false |
598 | }, undefined, this.i18n('Subscribe to the account')) | |
599 | ] | |
600 | if (this.isUserLoggedIn()) this.hotkeysService.add(this.hotkeys) | |
601 | } | |
dc8bc31b | 602 | } |