From a9bfa85d2cdf13670aaced740da5b493fbeddfce Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 15 Dec 2021 15:58:10 +0100 Subject: Add ability for admins to set default p2p policy --- .../information/privacy-concerns.component.ts | 26 ++++----- .../playlist/video-watch-playlist.component.ts | 39 ++++--------- .../recommended-videos.component.ts | 27 +++------ .../+videos/+video-watch/video-watch.component.ts | 64 +++++++++++++--------- 4 files changed, 68 insertions(+), 88 deletions(-) (limited to 'client/src/app/+videos/+video-watch') diff --git a/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts b/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts index bbc81d46d..24030df3e 100644 --- a/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts +++ b/client/src/app/+videos/+video-watch/shared/information/privacy-concerns.component.ts @@ -1,9 +1,8 @@ import { Component, Input, OnInit } from '@angular/core' -import { ServerService } from '@app/core' +import { ServerService, User, UserService } from '@app/core' import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage' import { HTMLServerConfig, Video } from '@shared/models' -import { getStoredP2PEnabled } from '../../../../../assets/player/peertube-player-local-storage' -import { isWebRTCDisabled } from '../../../../../assets/player/utils' +import { isP2PEnabled } from '../../../../../assets/player/utils' @Component({ selector: 'my-privacy-concerns', @@ -15,33 +14,32 @@ export class PrivacyConcernsComponent implements OnInit { @Input() video: Video - display = true + display = false private serverConfig: HTMLServerConfig constructor ( - private serverService: ServerService + private serverService: ServerService, + private userService: UserService ) { } ngOnInit () { this.serverConfig = this.serverService.getHTMLConfig() - if (isWebRTCDisabled() || this.isTrackerDisabled() || this.isP2PDisabled() || this.alreadyAccepted()) { - this.display = false - } + this.userService.getAnonymousOrLoggedUser() + .subscribe(user => this.updateDisplay(user)) } acceptedPrivacyConcern () { peertubeLocalStorage.setItem(PrivacyConcernsComponent.LOCAL_STORAGE_PRIVACY_CONCERN_KEY, 'true') - this.display = false - } - private isTrackerDisabled () { - return this.video.isLocal && this.serverConfig.tracker.enabled === false + this.display = false } - private isP2PDisabled () { - return getStoredP2PEnabled() === false + private updateDisplay (user: User) { + if (isP2PEnabled(this.video, this.serverConfig, user.p2pEnabled) && !this.alreadyAccepted()) { + this.display = true + } } private alreadyAccepted () { diff --git a/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts b/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts index b2863fed6..fbf9a3687 100644 --- a/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts +++ b/client/src/app/+videos/+video-watch/shared/playlist/video-watch-playlist.component.ts @@ -1,16 +1,9 @@ import { Component, EventEmitter, Input, Output } from '@angular/core' import { Router } from '@angular/router' -import { - AuthService, - ComponentPagination, - HooksService, - LocalStorageService, - Notifier, - SessionStorageService, - UserService -} from '@app/core' +import { AuthService, ComponentPagination, HooksService, Notifier, SessionStorageService, UserService } from '@app/core' import { VideoPlaylist, VideoPlaylistElement, VideoPlaylistService } from '@app/shared/shared-video-playlist' -import { peertubeLocalStorage, peertubeSessionStorage } from '@root-helpers/peertube-web-storage' +import { peertubeSessionStorage } from '@root-helpers/peertube-web-storage' +import { getBoolOrDefault } from '@root-helpers/local-storage-utils' import { VideoPlaylistPrivacy } from '@shared/models' @Component({ @@ -19,8 +12,7 @@ import { VideoPlaylistPrivacy } from '@shared/models' styleUrls: [ './video-watch-playlist.component.scss' ] }) export class VideoWatchPlaylistComponent { - static LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'auto_play_video_playlist' - static SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST = 'loop_playlist' + static SESSION_STORAGE_LOOP_PLAYLIST = 'loop_playlist' @Input() playlist: VideoPlaylist @@ -47,19 +39,15 @@ export class VideoWatchPlaylistComponent { private auth: AuthService, private notifier: Notifier, private videoPlaylist: VideoPlaylistService, - private localStorageService: LocalStorageService, private sessionStorage: SessionStorageService, private router: Router ) { - // defaults to true - this.autoPlayNextVideoPlaylist = this.auth.isLoggedIn() - ? this.auth.getUser().autoPlayNextVideoPlaylist - : this.localStorageService.getItem(VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) !== 'false' + this.userService.getAnonymousOrLoggedUser() + .subscribe(user => this.autoPlayNextVideoPlaylist = user.autoPlayNextVideoPlaylist) this.setAutoPlayNextVideoPlaylistSwitchText() - // defaults to false - this.loopPlaylist = this.sessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST) === 'true' + this.loopPlaylist = getBoolOrDefault(this.sessionStorage.getItem(VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST), false) this.setLoopPlaylistSwitchText() } @@ -201,16 +189,9 @@ export class VideoWatchPlaylistComponent { this.autoPlayNextVideoPlaylist = !this.autoPlayNextVideoPlaylist this.setAutoPlayNextVideoPlaylistSwitchText() - peertubeLocalStorage.setItem( - VideoWatchPlaylistComponent.LOCAL_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST, - this.autoPlayNextVideoPlaylist.toString() - ) + const details = { autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist } if (this.auth.isLoggedIn()) { - const details = { - autoPlayNextVideoPlaylist: this.autoPlayNextVideoPlaylist - } - this.userService.updateMyProfile(details) .subscribe({ next: () => { @@ -219,6 +200,8 @@ export class VideoWatchPlaylistComponent { error: err => this.notifier.error(err.message) }) + } else { + this.userService.updateMyAnonymousProfile(details) } } @@ -227,7 +210,7 @@ export class VideoWatchPlaylistComponent { this.setLoopPlaylistSwitchText() peertubeSessionStorage.setItem( - VideoWatchPlaylistComponent.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO_PLAYLIST, + VideoWatchPlaylistComponent.SESSION_STORAGE_LOOP_PLAYLIST, this.loopPlaylist.toString() ) } diff --git a/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts b/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts index dfc296d15..97f742499 100644 --- a/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts +++ b/client/src/app/+videos/+video-watch/shared/recommendations/recommended-videos.component.ts @@ -1,10 +1,9 @@ import { Observable } from 'rxjs' import { Component, EventEmitter, Input, OnChanges, OnInit, Output } from '@angular/core' -import { AuthService, Notifier, SessionStorageService, User, UserService } from '@app/core' +import { AuthService, Notifier, User, UserService } from '@app/core' import { Video } from '@app/shared/shared-main' import { MiniatureDisplayOptions } from '@app/shared/shared-video-miniature' import { VideoPlaylist } from '@app/shared/shared-video-playlist' -import { UserLocalStorageKeys } from '@root-helpers/users' import { RecommendationInfo } from './recommendation-info.model' import { RecommendedVideosStore } from './recommended-videos.store' @@ -39,24 +38,14 @@ export class RecommendedVideosComponent implements OnInit, OnChanges { private userService: UserService, private authService: AuthService, private notifier: Notifier, - private store: RecommendedVideosStore, - private sessionStorageService: SessionStorageService + private store: RecommendedVideosStore ) { this.videos$ = this.store.recommendations$ this.hasVideos$ = this.store.hasRecommendations$ this.videos$.subscribe(videos => this.gotRecommendations.emit(videos)) - if (this.authService.isLoggedIn()) { - this.autoPlayNextVideo = this.authService.getUser().autoPlayNextVideo - } else { - this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' - - this.sessionStorageService.watch([ UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO ]).subscribe( - () => { - this.autoPlayNextVideo = this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true' - } - ) - } + this.userService.getAnonymousOrLoggedUser() + .subscribe(user => this.autoPlayNextVideo = user.autoPlayNextVideo) this.autoPlayNextVideoTooltip = $localize`When active, the next video is automatically played after the current one.` } @@ -77,13 +66,9 @@ export class RecommendedVideosComponent implements OnInit, OnChanges { } switchAutoPlayNextVideo () { - this.sessionStorageService.setItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO, this.autoPlayNextVideo.toString()) + const details = { autoPlayNextVideo: this.autoPlayNextVideo } if (this.authService.isLoggedIn()) { - const details = { - autoPlayNextVideo: this.autoPlayNextVideo - } - this.userService.updateMyProfile(details) .subscribe({ next: () => { @@ -92,6 +77,8 @@ export class RecommendedVideosComponent implements OnInit, OnChanges { error: err => this.notifier.error(err.message) }) + } else { + this.userService.updateMyAnonymousProfile(details) } } } diff --git a/client/src/app/+videos/+video-watch/video-watch.component.ts b/client/src/app/+videos/+video-watch/video-watch.component.ts index fd61bcbf0..d542f243c 100644 --- a/client/src/app/+videos/+video-watch/video-watch.component.ts +++ b/client/src/app/+videos/+video-watch/video-watch.component.ts @@ -1,5 +1,6 @@ import { Hotkey, HotkeysService } from 'angular2-hotkeys' import { forkJoin, Subscription } from 'rxjs' +import { isP2PEnabled } from 'src/assets/player/utils' import { PlatformLocation } from '@angular/common' import { Component, ElementRef, Inject, LOCALE_ID, NgZone, OnDestroy, OnInit, ViewChild } from '@angular/core' import { ActivatedRoute, Router } from '@angular/router' @@ -14,6 +15,7 @@ import { RestExtractor, ScreenService, ServerService, + User, UserService } from '@app/core' import { HooksService } from '@app/core/plugins/hooks.service' @@ -237,31 +239,34 @@ export class VideoWatchComponent implements OnInit, OnDestroy { 'filter:api.video-watch.video.get.result' ) - forkJoin([ videoObs, this.videoCaptionService.listCaptions(videoId) ]) - .subscribe({ - next: ([ video, captionsResult ]) => { - const queryParams = this.route.snapshot.queryParams + forkJoin([ + videoObs, + this.videoCaptionService.listCaptions(videoId), + this.userService.getAnonymousOrLoggedUser() + ]).subscribe({ + next: ([ video, captionsResult, loggedInOrAnonymousUser ]) => { + const queryParams = this.route.snapshot.queryParams - const urlOptions = { - resume: queryParams.resume, + const urlOptions = { + resume: queryParams.resume, - startTime: queryParams.start, - stopTime: queryParams.stop, + startTime: queryParams.start, + stopTime: queryParams.stop, - muted: queryParams.muted, - loop: queryParams.loop, - subtitle: queryParams.subtitle, + muted: queryParams.muted, + loop: queryParams.loop, + subtitle: queryParams.subtitle, - playerMode: queryParams.mode, - peertubeLink: false - } + playerMode: queryParams.mode, + peertubeLink: false + } - this.onVideoFetched(video, captionsResult.data, urlOptions) - .catch(err => this.handleGlobalError(err)) - }, + this.onVideoFetched({ video, videoCaptions: captionsResult.data, loggedInOrAnonymousUser, urlOptions }) + .catch(err => this.handleGlobalError(err)) + }, - error: err => this.handleRequestError(err) - }) + error: err => this.handleRequestError(err) + }) } private loadPlaylist (playlistId: string) { @@ -323,11 +328,14 @@ export class VideoWatchComponent implements OnInit, OnDestroy { this.notifier.error(errorMessage) } - private async onVideoFetched ( - video: VideoDetails, - videoCaptions: VideoCaption[], + private async onVideoFetched (options: { + video: VideoDetails + videoCaptions: VideoCaption[] urlOptions: URLOptions - ) { + loggedInOrAnonymousUser: User + }) { + const { video, videoCaptions, urlOptions, loggedInOrAnonymousUser } = options + this.subscribeToLiveEventsIfNeeded(this.video, video) this.video = video @@ -346,7 +354,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { if (res === false) return this.location.back() } - this.buildPlayer(urlOptions) + this.buildPlayer(urlOptions, loggedInOrAnonymousUser) .catch(err => console.error('Cannot build the player', err)) this.setOpenGraphTags() @@ -359,7 +367,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { this.hooks.runAction('action:video-watch.video.loaded', 'video-watch', hookOptions) } - private async buildPlayer (urlOptions: URLOptions) { + private async buildPlayer (urlOptions: URLOptions, loggedInOrAnonymousUser: User) { // Flush old player if needed this.flushPlayer() @@ -380,6 +388,7 @@ export class VideoWatchComponent implements OnInit, OnDestroy { video: this.video, videoCaptions: this.videoCaptions, urlOptions, + loggedInOrAnonymousUser, user: this.user } const { playerMode, playerOptions } = await this.hooks.wrapFun( @@ -517,9 +526,10 @@ export class VideoWatchComponent implements OnInit, OnDestroy { video: VideoDetails videoCaptions: VideoCaption[] urlOptions: CustomizationOptions & { playerMode: PlayerMode } + loggedInOrAnonymousUser: User user?: AuthUser }) { - const { video, videoCaptions, urlOptions, user } = params + const { video, videoCaptions, urlOptions, loggedInOrAnonymousUser, user } = params const getStartTime = () => { const byUrl = urlOptions.startTime !== undefined @@ -547,6 +557,8 @@ export class VideoWatchComponent implements OnInit, OnDestroy { const options: PeertubePlayerManagerOptions = { common: { autoplay: this.isAutoplay(), + p2pEnabled: isP2PEnabled(video, this.serverConfig, loggedInOrAnonymousUser.p2pEnabled), + nextVideo: () => this.playNextVideoInAngularZone(), playerElement: this.playerElement, -- cgit v1.2.3