ActivityPub-federated video streaming platform using P2P directly in your web browser
Add ability for admins to set default p2p policy
Chocobozzza9bfa85d2cdf
diff --git a/client/e2e/src/po/anonymous-settings.po.ts b/client/e2e/src/po/anonymous-settings.po.tsnew file mode 100644index 0000000000..180d371fa0--- /dev/null+++ b/client/e2e/src/po/anonymous-settings.po.ts@@ -0,0 +1,21 @@+import { getCheckbox } from '../utils'++export class AnonymousSettingsPage {++ async openSettings () {+ const link = await $$('.menu-link').filter(async i => {+ return await i.getText() === 'My settings'+ }).then(links => links[0])++ await link.click()++ await $('my-user-video-settings').waitForDisplayed()+ }++ async clickOnP2PCheckbox () {+ const p2p = getCheckbox('p2pEnabled')+ await p2p.waitForClickable()++ await p2p.click()+ }+}diff --git a/client/e2e/src/po/my-account.ts b/client/e2e/src/po/my-account.po.tssimilarity index 89%rename from client/e2e/src/po/my-account.tsrename to client/e2e/src/po/my-account.po.tsindex b51614fd99..13a764e878 100644--- a/client/e2e/src/po/my-account.ts+++ b/client/e2e/src/po/my-account.po.ts@@ -1,4 +1,4 @@-import { go } from '../utils'+import { getCheckbox, go } from '../utils'export class MyAccountPage {@@ -27,6 +27,21 @@ export class MyAccountPage {await nsfw.scrollIntoView(false) // Avoid issues with fixed header on firefoxawait nsfw.selectByAttribute('value', newValue)+ await this.submitVideoSettings()+ }++ async clickOnP2PCheckbox () {+ const p2p = getCheckbox('p2pEnabled')++ await p2p.waitForClickable()+ await p2p.scrollIntoView(false) // Avoid issues with fixed header on firefox++ await p2p.click()++ await this.submitVideoSettings()+ }++ private async submitVideoSettings () {const submit = $('my-user-video-settings input[type=submit]')await submit.scrollIntoView(false)await submit.click()diff --git a/client/e2e/src/po/video-upload.po.ts b/client/e2e/src/po/video-upload.po.tsindex dd437c3902..2206b56c3f 100644--- a/client/e2e/src/po/video-upload.po.ts+++ b/client/e2e/src/po/video-upload.po.ts@@ -1,5 +1,5 @@import { join } from 'path'-import { clickOnCheckbox } from '../utils'+import { getCheckbox, selectCustomSelect } from '../utils'export class VideoUploadPage {async navigateTo () {@@ -32,7 +32,7 @@ export class VideoUploadPage {}setAsNSFW () {- return clickOnCheckbox('nsfw')+ return getCheckbox('nsfw').click()}async validSecondUploadStep (videoName: string) {@@ -47,6 +47,10 @@ export class VideoUploadPage {})}+ setAsPublic () {+ return selectCustomSelect('privacy', 'Public')+ }+private getSecondStepSubmitButton () {return $('.submit-container my-button')}diff --git a/client/e2e/src/po/video-watch.po.ts b/client/e2e/src/po/video-watch.po.tsindex 1406c971a1..cecda3a8b9 100644--- a/client/e2e/src/po/video-watch.po.ts+++ b/client/e2e/src/po/video-watch.po.ts@@ -39,12 +39,23 @@ export class VideoWatchPage {return $('my-video-comment-add').isExisting()}+ isPrivacyWarningDisplayed () {+ return $('my-privacy-concerns').isDisplayed()+ }+async goOnAssociatedEmbed () {let url = await browser.getUrl()url = url.replace('/w/', '/videos/embed/')url = url.replace(':3333', ':9001')- return go(url)+ await go(url)+ await $('.vjs-big-play-button').waitForDisplayed()+ }++ async isEmbedWarningDisplayed () {+ const text = await $('.vjs-dock-description').getText()++ return !!text.trim()}goOnP2PMediaLoaderEmbed () {diff --git a/client/e2e/src/suites-all/videos.e2e-spec.ts b/client/e2e/src/suites-all/videos.e2e-spec.tsindex 3b8305a25f..b3a87c8e2f 100644--- a/client/e2e/src/suites-all/videos.e2e-spec.ts+++ b/client/e2e/src/suites-all/videos.e2e-spec.ts@@ -1,5 +1,5 @@import { LoginPage } from '../po/login.po'-import { MyAccountPage } from '../po/my-account'+import { MyAccountPage } from '../po/my-account.po'import { PlayerPage } from '../po/player.po'import { VideoListPage } from '../po/video-list.po'import { VideoUpdatePage } from '../po/video-update.po'diff --git a/client/e2e/src/suites-local/custom-server-defaults.e2e-spec.ts b/client/e2e/src/suites-local/custom-server-defaults.e2e-spec.tsindex c2c8edcc91..e060d382fa 100644--- a/client/e2e/src/suites-local/custom-server-defaults.e2e-spec.ts+++ b/client/e2e/src/suites-local/custom-server-defaults.e2e-spec.ts@@ -1,7 +1,7 @@import { LoginPage } from '../po/login.po'import { VideoUploadPage } from '../po/video-upload.po'import { VideoWatchPage } from '../po/video-watch.po'-import { isMobileDevice, isSafari, waitServerUp } from '../utils'+import { go, isMobileDevice, isSafari, waitServerUp } from '../utils'describe('Custom server defaults', () => {let videoUploadPage: VideoUploadPage@@ -10,9 +10,7 @@ describe('Custom server defaults', () => {before(async () => {await waitServerUp()- })- beforeEach(async () => {loginPage = new LoginPage()videoUploadPage = new VideoUploadPage()videoWatchPage = new VideoWatchPage(isMobileDevice(), isSafari())@@ -20,18 +18,69 @@ describe('Custom server defaults', () => {await browser.maximizeWindow()})- it('Should upload a video with custom default values', async function () {- await loginPage.loginAsRootUser()- await videoUploadPage.navigateTo()- await videoUploadPage.uploadVideo()- await videoUploadPage.validSecondUploadStep('video')+ describe('Publish default values', function () {+ before(async function () {+ await loginPage.loginAsRootUser()+ })++ it('Should upload a video with custom default values', async function () {+ await videoUploadPage.navigateTo()+ await videoUploadPage.uploadVideo()+ await videoUploadPage.validSecondUploadStep('video')- await videoWatchPage.waitWatchVideoName('video')+ await videoWatchPage.waitWatchVideoName('video')- expect(await videoWatchPage.getPrivacy()).toBe('Internal')- expect(await videoWatchPage.getLicence()).toBe('Attribution - Non Commercial')- expect(await videoWatchPage.isDownloadEnabled()).toBeFalsy()- expect(await videoWatchPage.areCommentsEnabled()).toBeFalsy()+ expect(await videoWatchPage.getPrivacy()).toBe('Internal')+ expect(await videoWatchPage.getLicence()).toBe('Attribution - Non Commercial')+ expect(await videoWatchPage.isDownloadEnabled()).toBeFalsy()+ expect(await videoWatchPage.areCommentsEnabled()).toBeFalsy()+ })++ after(async function () {+ await loginPage.logout()+ })})+ describe('P2P', function () {+ let videoUrl: string++ async function goOnVideoWatchPage () {+ await go(videoUrl)+ await videoWatchPage.waitWatchVideoName('video')+ }++ async function checkP2P (enabled: boolean) {+ await goOnVideoWatchPage()+ expect(await videoWatchPage.isPrivacyWarningDisplayed()).toEqual(enabled)++ await videoWatchPage.goOnAssociatedEmbed()+ expect(await videoWatchPage.isEmbedWarningDisplayed()).toEqual(enabled)+ }++ before(async () => {+ await loginPage.loginAsRootUser()+ await videoUploadPage.navigateTo()+ await videoUploadPage.uploadVideo()+ await videoUploadPage.setAsPublic()+ await videoUploadPage.validSecondUploadStep('video')++ await videoWatchPage.waitWatchVideoName('video')++ videoUrl = await browser.getUrl()+ })++ beforeEach(async function () {+ await goOnVideoWatchPage()+ })++ it('Should have P2P disabled for a logged in user', async function () {+ await checkP2P(false)+ })++ it('Should have P2P disabled for anonymous users', async function () {+ await loginPage.logout()++ await checkP2P(false)+ })+ })})diff --git a/client/e2e/src/suites-local/user-settings.e2e-spec.ts b/client/e2e/src/suites-local/user-settings.e2e-spec.tsnew file mode 100644index 0000000000..b87501cd1e--- /dev/null+++ b/client/e2e/src/suites-local/user-settings.e2e-spec.ts@@ -0,0 +1,82 @@+import { AnonymousSettingsPage } from '../po/anonymous-settings.po'+import { LoginPage } from '../po/login.po'+import { MyAccountPage } from '../po/my-account.po'+import { VideoUploadPage } from '../po/video-upload.po'+import { VideoWatchPage } from '../po/video-watch.po'+import { go, isMobileDevice, isSafari, waitServerUp } from '../utils'++describe('User settings', () => {+ let videoUploadPage: VideoUploadPage+ let loginPage: LoginPage+ let videoWatchPage: VideoWatchPage+ let myAccountPage: MyAccountPage+ let anonymousSettingsPage: AnonymousSettingsPage++ before(async () => {+ await waitServerUp()++ loginPage = new LoginPage()+ videoUploadPage = new VideoUploadPage()+ videoWatchPage = new VideoWatchPage(isMobileDevice(), isSafari())+ myAccountPage = new MyAccountPage()+ anonymousSettingsPage = new AnonymousSettingsPage()++ await browser.maximizeWindow()+ })++ describe('P2P', function () {+ let videoUrl: string++ async function goOnVideoWatchPage () {+ await go(videoUrl)+ await videoWatchPage.waitWatchVideoName('video')+ }++ async function checkP2P (enabled: boolean) {+ await goOnVideoWatchPage()+ expect(await videoWatchPage.isPrivacyWarningDisplayed()).toEqual(enabled)++ await videoWatchPage.goOnAssociatedEmbed()+ expect(await videoWatchPage.isEmbedWarningDisplayed()).toEqual(enabled)+ }++ before(async () => {+ await loginPage.loginAsRootUser()+ await videoUploadPage.navigateTo()+ await videoUploadPage.uploadVideo()+ await videoUploadPage.validSecondUploadStep('video')++ await videoWatchPage.waitWatchVideoName('video')++ videoUrl = await browser.getUrl()+ })++ beforeEach(async function () {+ await goOnVideoWatchPage()+ })++ it('Should have P2P enabled for a logged in user', async function () {+ await checkP2P(true)+ })++ it('Should disable P2P for a logged in user', async function () {+ await myAccountPage.navigateToMySettings()+ await myAccountPage.clickOnP2PCheckbox()++ await checkP2P(false)+ })++ it('Should have P2P enabled for anonymous users', async function () {+ await loginPage.logout()++ await checkP2P(true)+ })++ it('Should disable P2P for an anonymous user', async function () {+ await anonymousSettingsPage.openSettings()+ await anonymousSettingsPage.clickOnP2PCheckbox()++ await checkP2P(false)+ })+ })+})diff --git a/client/e2e/src/suites-local/videos-list.e2e-spec.ts b/client/e2e/src/suites-local/videos-list.e2e-spec.tsindex bca6018b94..ce57261b9e 100644--- a/client/e2e/src/suites-local/videos-list.e2e-spec.ts+++ b/client/e2e/src/suites-local/videos-list.e2e-spec.ts@@ -1,6 +1,6 @@import { AdminConfigPage } from '../po/admin-config.po'import { LoginPage } from '../po/login.po'-import { MyAccountPage } from '../po/my-account'+import { MyAccountPage } from '../po/my-account.po'import { VideoListPage } from '../po/video-list.po'import { VideoSearchPage } from '../po/video-search.po'import { VideoUploadPage } from '../po/video-upload.po'diff --git a/client/e2e/src/utils/elements.ts b/client/e2e/src/utils/elements.tsindex cadc46cce4..3157188799 100644--- a/client/e2e/src/utils/elements.ts+++ b/client/e2e/src/utils/elements.ts@@ -1,7 +1,22 @@-function clickOnCheckbox (name: string) {- return $(`my-peertube-checkbox[inputname=${name}] label`).click()+function getCheckbox (name: string) {+ return $(`my-peertube-checkbox[inputname=${name}] label`)+}++async function selectCustomSelect (id: string, valueLabel: string) {+ await $(`[formcontrolname=${id}] .ng-arrow-wrapper`).click()++ const option = await $$(`[formcontrolname=${id}] .ng-option`).filter(async o => {+ const text = await o.getText()++ return text.trimStart().startsWith(valueLabel)+ }).then(options => options[0])++ await option.waitForDisplayed()++ return option.click()}export {- clickOnCheckbox+ getCheckbox,+ selectCustomSelect}diff --git a/client/e2e/src/utils/hooks.ts b/client/e2e/src/utils/hooks.tsindex e42c6a5d8b..2f3d10fe39 100644--- a/client/e2e/src/utils/hooks.ts+++ b/client/e2e/src/utils/hooks.ts@@ -55,6 +55,9 @@ function buildConfig (suiteFile: string = undefined) {comments_enabled: false,privacy: 4,licence: 4+ },+ p2p: {+ enabled: false}}}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.tsindex bbc81d46d8..24030df3e4 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 = falseprivate serverConfig: HTMLServerConfigconstructor (- 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.tsindex b2863fed6e..fbf9a36878 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.autoPlayNextVideoPlaylistthis.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.tsindex dfc296d154..97f742499c 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.tsindex fd61bcbf04..d542f243ce 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 neededthis.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: VideoDetailsvideoCaptions: VideoCaption[]urlOptions: CustomizationOptions & { playerMode: PlayerMode }+ loggedInOrAnonymousUser: Useruser?: AuthUser}) {- const { video, videoCaptions, urlOptions, user } = params+ const { video, videoCaptions, urlOptions, loggedInOrAnonymousUser, user } = paramsconst 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,diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.tsindex 95af89b19d..a60138af9f 100644--- a/client/src/app/app.component.ts+++ b/client/src/app/app.component.ts@@ -14,7 +14,8 @@ import {ScrollService,ServerService,ThemeService,- User+ User,+ UserLocalStorageService} from '@app/core'import { HooksService } from '@app/core/plugins/hooks.service'import { PluginService } from '@app/core/plugins/plugin.service'@@ -70,6 +71,7 @@ export class AppComponent implements OnInit, AfterViewInit {private ngbConfig: NgbConfig,private loadingBar: LoadingBarService,private scrollService: ScrollService,+ private userLocalStorage: UserLocalStorageService,public menu: MenuService) {this.ngbConfig.animation = false@@ -86,6 +88,8 @@ export class AppComponent implements OnInit, AfterViewInit {ngOnInit () {document.getElementById('incompatible-browser').className += ' browser-ok'+ this.loadUser()+this.serverConfig = this.serverService.getHTMLConfig()this.hooks.runAction('action:application.init', 'common')@@ -300,4 +304,15 @@ export class AppComponent implements OnInit, AfterViewInit {}, undefined, $localize`Go to the videos upload page`)])}++ private loadUser () {+ const tokens = this.userLocalStorage.getTokens()+ if (!tokens) return++ const user = this.userLocalStorage.getLoggedInUser()+ if (!user) return++ // Initialize user+ this.authService.buildAuthUser(user, tokens)+ }}diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.tsindex f10b37e5ad..cd9665e378 100644--- a/client/src/app/core/auth/auth-user.model.ts+++ b/client/src/app/core/auth/auth-user.model.ts@@ -1,13 +1,7 @@import { Observable, of } from 'rxjs'import { map } from 'rxjs/operators'import { User } from '@app/core/users/user.model'-import {- flushUserInfoFromLocalStorage,- getUserInfoFromLocalStorage,- saveUserInfoIntoLocalStorage,- TokenOptions,- Tokens-} from '@root-helpers/users'+import { UserTokens } from '@root-helpers/users'import { hasUserRight } from '@shared/core-utils/users'import {MyUser as ServerMyUserModel,@@ -19,31 +13,15 @@ import {} from '@shared/models'export class AuthUser extends User implements ServerMyUserModel {- tokens: Tokens+ tokens: UserTokensspecialPlaylists: MyUserSpecialPlaylist[]canSeeVideosLink = true- static load () {- const tokens = Tokens.load()- if (!tokens) return null-- const userInfo = getUserInfoFromLocalStorage()- if (!userInfo) return null-- return new AuthUser(userInfo, tokens)- }-- static flush () {- flushUserInfoFromLocalStorage()-- Tokens.flush()- }-- constructor (userHash: Partial<ServerMyUserModel>, hashTokens: TokenOptions) {+ constructor (userHash: Partial<ServerMyUserModel>, hashTokens: Partial<UserTokens>) {super(userHash)- this.tokens = new Tokens(hashTokens)+ this.tokens = new UserTokens(hashTokens)this.specialPlaylists = userHash.specialPlaylists}@@ -77,20 +55,6 @@ export class AuthUser extends User implements ServerMyUserModel {return user.role === UserRole.USER}- save () {- saveUserInfoIntoLocalStorage({- id: this.id,- username: this.username,- email: this.email,- role: this.role,- nsfwPolicy: this.nsfwPolicy,- webTorrentEnabled: this.webTorrentEnabled,- autoPlayVideo: this.autoPlayVideo- })-- this.tokens.save()- }-computeCanSeeVideosLink (quotaObservable: Observable<UserVideoQuota>): Observable<boolean> {if (!this.isUploadDisabled()) {this.canSeeVideosLink = truediff --git a/client/src/app/core/auth/auth.service.ts b/client/src/app/core/auth/auth.service.tsindex 79239a17a2..2ac88c1852 100644--- a/client/src/app/core/auth/auth.service.ts+++ b/client/src/app/core/auth/auth.service.ts@@ -5,7 +5,7 @@ import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'import { Injectable } from '@angular/core'import { Router } from '@angular/router'import { Notifier } from '@app/core/notification/notifier.service'-import { objectToUrlEncoded, peertubeLocalStorage } from '@root-helpers/index'+import { objectToUrlEncoded, peertubeLocalStorage, UserTokens } from '@root-helpers/index'import { HttpStatusCode, MyUser as UserServerModel, OAuthClientLocal, User, UserLogin, UserRefreshToken } from '@shared/models'import { environment } from '../../../environments/environment'import { RestExtractor } from '../rest/rest-extractor.service'@@ -34,6 +34,7 @@ export class AuthService {loginChangedSource: Observable<AuthStatus>userInformationLoaded = new ReplaySubject<boolean>(1)+ tokensRefreshed = new ReplaySubject<void>(1)hotkeys: Hotkey[]private clientId: string = peertubeLocalStorage.getItem(AuthService.LOCAL_STORAGE_OAUTH_CLIENT_KEYS.CLIENT_ID)@@ -52,9 +53,6 @@ export class AuthService {this.loginChanged = new Subject<AuthStatus>()this.loginChangedSource = this.loginChanged.asObservable()- // Return null if there is nothing to load- this.user = AuthUser.load()-// Set HotKeysthis.hotkeys = [new Hotkey('m s', (event: KeyboardEvent): boolean => {@@ -76,6 +74,10 @@ export class AuthService {]}+ buildAuthUser (userInfo: Partial<User>, tokens: UserTokens) {+ this.user = new AuthUser(userInfo, tokens)+ }+loadClientCredentials () {// Fetch the client_id/client_secretthis.http.get<OAuthClientLocal>(AuthService.BASE_CLIENT_URL)@@ -180,8 +182,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particularthis.user = null- AuthUser.flush()-this.setStatus(AuthStatus.LoggedOut)this.hotkeysService.remove(this.hotkeys)@@ -239,7 +239,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular.subscribe({next: res => {this.user.patch(res)- this.user.save()this.userInformationLoaded.next(true)}@@ -262,7 +261,6 @@ Ensure you have correctly configured PeerTube (config/ directory), in particular}this.user = new AuthUser(obj, hashTokens)- this.user.save()this.setStatus(AuthStatus.LoggedIn)this.userInformationLoaded.next(true)@@ -272,7 +270,7 @@ Ensure you have correctly configured PeerTube (config/ directory), in particularprivate handleRefreshToken (obj: UserRefreshToken) {this.user.refreshTokens(obj.access_token, obj.refresh_token)- this.user.save()+ this.tokensRefreshed.next()}private setStatus (status: AuthStatus) {diff --git a/client/src/app/core/core.module.ts b/client/src/app/core/core.module.tsindex 04be0671c4..d80f95ed63 100644--- a/client/src/app/core/core.module.ts+++ b/client/src/app/core/core.module.ts@@ -30,7 +30,7 @@ import { ServerConfigResolver } from './routing/server-config-resolver.service'import { ScopedTokensService } from './scoped-tokens'import { ServerService } from './server'import { ThemeService } from './theme'-import { UserService } from './users'+import { UserLocalStorageService, UserService } from './users'import { LocalStorageService, ScreenService, SessionStorageService } from './wrappers'@NgModule({@@ -79,6 +79,7 @@ import { LocalStorageService, ScreenService, SessionStorageService } from './wraRestService,UserService,+ UserLocalStorageService,ScreenService,LocalStorageService,diff --git a/client/src/app/core/users/index.ts b/client/src/app/core/users/index.tsindex 7b5a67bc78..e235a875b7 100644--- a/client/src/app/core/users/index.ts+++ b/client/src/app/core/users/index.ts@@ -1,2 +1,3 @@+export * from './user-local-storage.service'export * from './user.model'export * from './user.service'diff --git a/client/src/app/core/users/user-local-storage.service.ts b/client/src/app/core/users/user-local-storage.service.tsnew file mode 100644index 0000000000..85da46e0da--- /dev/null+++ b/client/src/app/core/users/user-local-storage.service.ts@@ -0,0 +1,186 @@++import { filter, throttleTime } from 'rxjs'+import { Injectable } from '@angular/core'+import { AuthService, AuthStatus } from '@app/core/auth'+import { UserLocalStorageKeys, UserTokens } from '@root-helpers/users'+import { getBoolOrDefault } from '@root-helpers/local-storage-utils'+import { UserRole, UserUpdateMe } from '@shared/models'+import { NSFWPolicyType } from '@shared/models/videos'+import { ServerService } from '../server'+import { LocalStorageService } from '../wrappers/storage.service'++@Injectable()+export class UserLocalStorageService {++ constructor (+ private authService: AuthService,+ private server: ServerService,+ private localStorageService: LocalStorageService+ ) {+ this.authService.userInformationLoaded.subscribe({+ next: () => {+ const user = this.authService.getUser()++ this.setLoggedInUser(user)+ this.setUserInfo(user)+ this.setTokens(user.tokens)+ }+ })++ this.authService.loginChangedSource+ .pipe(filter(status => status === AuthStatus.LoggedOut))+ .subscribe({+ next: () => {+ this.flushLoggedInUser()+ this.flushUserInfo()+ this.flushTokens()+ }+ })++ this.authService.tokensRefreshed+ .subscribe({+ next: () => {+ const user = this.authService.getUser()++ this.setTokens(user.tokens)+ }+ })+ }++ // ---------------------------------------------------------------------------++ getLoggedInUser () {+ const usernameLocalStorage = this.localStorageService.getItem(UserLocalStorageKeys.USERNAME)++ if (!usernameLocalStorage) return undefined++ return {+ id: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ID), 10),+ username: this.localStorageService.getItem(UserLocalStorageKeys.USERNAME),+ email: this.localStorageService.getItem(UserLocalStorageKeys.EMAIL),+ role: parseInt(this.localStorageService.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,++ ...this.getUserInfo()+ }+ }++ setLoggedInUser (user: {+ id: number+ username: string+ email: string+ role: UserRole+ }) {+ this.localStorageService.setItem(UserLocalStorageKeys.ID, user.id.toString())+ this.localStorageService.setItem(UserLocalStorageKeys.USERNAME, user.username)+ this.localStorageService.setItem(UserLocalStorageKeys.EMAIL, user.email)+ this.localStorageService.setItem(UserLocalStorageKeys.ROLE, user.role.toString())+ }++ flushLoggedInUser () {+ this.localStorageService.removeItem(UserLocalStorageKeys.ID)+ this.localStorageService.removeItem(UserLocalStorageKeys.USERNAME)+ this.localStorageService.removeItem(UserLocalStorageKeys.EMAIL)+ this.localStorageService.removeItem(UserLocalStorageKeys.ROLE)+ }++ // ---------------------------------------------------------------------------++ getUserInfo () {+ let videoLanguages: string[]++ try {+ const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)+ videoLanguages = languagesString && languagesString !== 'undefined'+ ? JSON.parse(languagesString)+ : null+ } catch (err) {+ videoLanguages = null+ console.error('Cannot parse desired video languages from localStorage.', err)+ }++ const htmlConfig = this.server.getHTMLConfig()++ const defaultNSFWPolicy = htmlConfig.instance.defaultNSFWPolicy+ const defaultP2PEnabled = htmlConfig.defaults.p2p.enabled++ return {+ nsfwPolicy: this.localStorageService.getItem<NSFWPolicyType>(UserLocalStorageKeys.NSFW_POLICY) || defaultNSFWPolicy,+ p2pEnabled: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.P2P_ENABLED), defaultP2PEnabled),+ theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default',+ videoLanguages,++ autoPlayVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO), true),+ autoPlayNextVideo: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_NEXT_VIDEO), false),+ autoPlayNextVideoPlaylist: getBoolOrDefault(this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST), true)+ }+ }++ setUserInfo (profile: UserUpdateMe) {+ const localStorageKeys: { [ id in keyof UserUpdateMe ]: string } = {+ nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY,+ p2pEnabled: UserLocalStorageKeys.P2P_ENABLED,+ autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO,+ autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,+ theme: UserLocalStorageKeys.THEME,+ videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES+ }++ const obj = Object.keys(localStorageKeys)+ .filter(key => key in profile)+ .map(key => ([ localStorageKeys[key], profile[key] ]))++ for (const [ key, value ] of obj) {+ try {+ if (value === undefined) {+ this.localStorageService.removeItem(key)+ continue+ }++ const localStorageValue = typeof value === 'string'+ ? value+ : JSON.stringify(value)++ this.localStorageService.setItem(key, localStorageValue)+ } catch (err) {+ console.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err)+ }+ }+ }++ flushUserInfo () {+ this.localStorageService.removeItem(UserLocalStorageKeys.NSFW_POLICY)+ this.localStorageService.removeItem(UserLocalStorageKeys.P2P_ENABLED)+ this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)+ this.localStorageService.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST)+ this.localStorageService.removeItem(UserLocalStorageKeys.THEME)+ this.localStorageService.removeItem(UserLocalStorageKeys.VIDEO_LANGUAGES)+ }++ listenUserInfoChange () {+ return this.localStorageService.watch([+ UserLocalStorageKeys.NSFW_POLICY,+ UserLocalStorageKeys.P2P_ENABLED,+ UserLocalStorageKeys.AUTO_PLAY_VIDEO,+ UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,+ UserLocalStorageKeys.THEME,+ UserLocalStorageKeys.VIDEO_LANGUAGES+ ]).pipe(+ throttleTime(200),+ filter(() => this.authService.isLoggedIn() !== true)+ )+ }++ // ---------------------------------------------------------------------------++ getTokens () {+ return UserTokens.getUserTokens(this.localStorageService)+ }++ setTokens (tokens: UserTokens) {+ UserTokens.saveToLocalStorage(this.localStorageService, tokens)+ }++ flushTokens () {+ UserTokens.flushLocalStorage(this.localStorageService)+ }+}diff --git a/client/src/app/core/users/user.model.ts b/client/src/app/core/users/user.model.tsindex c0e5d31698..f211051cec 100644--- a/client/src/app/core/users/user.model.ts+++ b/client/src/app/core/users/user.model.ts@@ -26,7 +26,11 @@ export class User implements UserServerModel {autoPlayVideo: booleanautoPlayNextVideo: booleanautoPlayNextVideoPlaylist: boolean- webTorrentEnabled: boolean++ p2pEnabled: boolean+ // FIXME: deprecated in 4.1+ webTorrentEnabled: never+videosHistoryEnabled: booleanvideoLanguages: string[]@@ -84,7 +88,7 @@ export class User implements UserServerModel {this.videoCommentsCount = hash.videoCommentsCountthis.nsfwPolicy = hash.nsfwPolicy- this.webTorrentEnabled = hash.webTorrentEnabled+ this.p2pEnabled = hash.p2pEnabledthis.autoPlayVideo = hash.autoPlayVideothis.autoPlayNextVideo = hash.autoPlayNextVideothis.autoPlayNextVideoPlaylist = hash.autoPlayNextVideoPlaylistdiff --git a/client/src/app/core/users/user.service.ts b/client/src/app/core/users/user.service.tsindex 632361e9be..a6a0474aba 100644--- a/client/src/app/core/users/user.service.ts+++ b/client/src/app/core/users/user.service.ts@@ -1,11 +1,10 @@import { SortMeta } from 'primeng/api'import { from, Observable, of } from 'rxjs'-import { catchError, concatMap, filter, first, map, shareReplay, tap, throttleTime, toArray } from 'rxjs/operators'+import { catchError, concatMap, first, map, shareReplay, tap, toArray } from 'rxjs/operators'import { HttpClient, HttpParams } from '@angular/common/http'import { Injectable } from '@angular/core'import { AuthService } from '@app/core/auth'import { getBytes } from '@root-helpers/bytes'-import { UserLocalStorageKeys } from '@root-helpers/users'import {ActorImage,ResultList,@@ -17,10 +16,9 @@ import {UserUpdateMe,UserVideoQuota} from '@shared/models'-import { ServerService } from '../'import { environment } from '../../../environments/environment'import { RestExtractor, RestPagination, RestService } from '../rest'-import { LocalStorageService, SessionStorageService } from '../wrappers/storage.service'+import { UserLocalStorageService } from './'import { User } from './user.model'@Injectable()@@ -33,12 +31,10 @@ export class UserService {constructor (private authHttp: HttpClient,- private server: ServerService,private authService: AuthService,private restExtractor: RestExtractor,private restService: RestService,- private localStorageService: LocalStorageService,- private sessionStorageService: SessionStorageService+ private userLocalStorageService: UserLocalStorageService) { }hasSignupInThisSession () {@@ -73,6 +69,23 @@ export class UserService {)}+ // ---------------------------------------------------------------------------++ updateMyAnonymousProfile (profile: UserUpdateMe) {+ this.userLocalStorageService.setUserInfo(profile)+ }++ listenAnonymousUpdate () {+ return this.userLocalStorageService.listenUserInfoChange()+ .pipe(map(() => this.getAnonymousUser()))+ }++ getAnonymousUser () {+ return new User(this.userLocalStorageService.getUserInfo())+ }++ // ---------------------------------------------------------------------------+updateMyProfile (profile: UserUpdateMe) {const url = UserService.BASE_USERS_URL + 'me'@@ -83,53 +96,6 @@ export class UserService {)}- updateMyAnonymousProfile (profile: UserUpdateMe) {- const localStorageKeys: { [ id in keyof UserUpdateMe ]: string } = {- nsfwPolicy: UserLocalStorageKeys.NSFW_POLICY,- webTorrentEnabled: UserLocalStorageKeys.WEBTORRENT_ENABLED,- autoPlayNextVideo: UserLocalStorageKeys.AUTO_PLAY_VIDEO,- autoPlayNextVideoPlaylist: UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,- theme: UserLocalStorageKeys.THEME,- videoLanguages: UserLocalStorageKeys.VIDEO_LANGUAGES- }-- const obj = Object.keys(localStorageKeys)- .filter(key => key in profile)- .map(key => ([ localStorageKeys[key], profile[key] ]))-- for (const [ key, value ] of obj) {- try {- if (value === undefined) {- this.localStorageService.removeItem(key)- continue- }-- const localStorageValue = typeof value === 'string'- ? value- : JSON.stringify(value)-- this.localStorageService.setItem(key, localStorageValue)- } catch (err) {- console.error(`Cannot set ${key}->${value} in localStorage. Likely due to a value impossible to stringify.`, err)- }- }- }-- listenAnonymousUpdate () {- return this.localStorageService.watch([- UserLocalStorageKeys.NSFW_POLICY,- UserLocalStorageKeys.WEBTORRENT_ENABLED,- UserLocalStorageKeys.AUTO_PLAY_VIDEO,- UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST,- UserLocalStorageKeys.THEME,- UserLocalStorageKeys.VIDEO_LANGUAGES- ]).pipe(- throttleTime(200),- filter(() => this.authService.isLoggedIn() !== true),- map(() => this.getAnonymousUser())- )- }-deleteMe () {const url = UserService.BASE_USERS_URL + 'me'@@ -287,36 +253,6 @@ export class UserService {.pipe(catchError(err => this.restExtractor.handleError(err)))}- getAnonymousUser () {- let videoLanguages: string[]-- try {- const languagesString = this.localStorageService.getItem(UserLocalStorageKeys.VIDEO_LANGUAGES)- videoLanguages = languagesString && languagesString !== 'undefined'- ? JSON.parse(languagesString)- : null- } catch (err) {- videoLanguages = null- console.error('Cannot parse desired video languages from localStorage.', err)- }-- const defaultNSFWPolicy = this.server.getHTMLConfig().instance.defaultNSFWPolicy-- return new User({- // local storage keys- nsfwPolicy: this.localStorageService.getItem(UserLocalStorageKeys.NSFW_POLICY) || defaultNSFWPolicy,- webTorrentEnabled: this.localStorageService.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) !== 'false',- theme: this.localStorageService.getItem(UserLocalStorageKeys.THEME) || 'instance-default',- videoLanguages,-- autoPlayNextVideoPlaylist: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO_PLAYLIST) !== 'false',- autoPlayVideo: this.localStorageService.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',-- // session storage keys- autoPlayNextVideo: this.sessionStorageService.getItem(UserLocalStorageKeys.SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO) === 'true'- })- }-getUsers (parameters: {pagination: RestPaginationsort: SortMetadiff --git a/client/src/app/menu/menu.component.html b/client/src/app/menu/menu.component.htmlindex 9ea9910429..48b3fdc854 100644--- a/client/src/app/menu/menu.component.html+++ b/client/src/app/menu/menu.component.html@@ -60,7 +60,7 @@<my-global-icon iconName="p2p" aria-hidden="true"></my-global-icon><ng-container i18n>Help share videos</ng-container>- <my-input-switch class="ml-auto" [checked]="user.webTorrentEnabled"></my-input-switch>+ <my-input-switch class="ml-auto" [checked]="user.p2pEnabled"></my-input-switch></a><div class="dropdown-divider"></div>diff --git a/client/src/app/menu/menu.component.ts b/client/src/app/menu/menu.component.tsindex d5ddc29cbe..983f0a9383 100644--- a/client/src/app/menu/menu.component.ts+++ b/client/src/app/menu/menu.component.ts@@ -196,9 +196,9 @@ export class MenuComponent implements OnInit {toggleUseP2P () {if (!this.user) return- this.user.webTorrentEnabled = !this.user.webTorrentEnabled+ this.user.p2pEnabled = !this.user.p2pEnabled- this.userService.updateMyProfile({ webTorrentEnabled: this.user.webTorrentEnabled })+ this.userService.updateMyProfile({ p2pEnabled: this.user.p2pEnabled }).subscribe(() => this.authService.refreshUserInformation())}diff --git a/client/src/app/shared/shared-search/search.service.ts b/client/src/app/shared/shared-search/search.service.tsindex 61acfb466c..ad2de0f37f 100644--- a/client/src/app/shared/shared-search/search.service.ts+++ b/client/src/app/shared/shared-search/search.service.ts@@ -4,7 +4,6 @@ import { HttpClient, HttpParams } from '@angular/common/http'import { Injectable } from '@angular/core'import { ComponentPaginationLight, RestExtractor, RestPagination, RestService } from '@app/core'import { Video, VideoChannel, VideoChannelService, VideoService } from '@app/shared/shared-main'-import { peertubeLocalStorage } from '@root-helpers/peertube-web-storage'import {ResultList,Video as VideoServerModel,@@ -25,11 +24,7 @@ export class SearchService {private restService: RestService,private videoService: VideoService,private playlistService: VideoPlaylistService- ) {- // Add ability to override search endpoint if the user updated this local storage key- const searchUrl = peertubeLocalStorage.getItem('search-url')- if (searchUrl) SearchService.BASE_SEARCH_URL = searchUrl- }+ ) { }searchVideos (parameters: {search?: stringdiff --git a/client/src/app/shared/shared-user-settings/user-video-settings.component.html b/client/src/app/shared/shared-user-settings/user-video-settings.component.htmlindex bc9dd0f7ff..4843f65b99 100644--- a/client/src/app/shared/shared-user-settings/user-video-settings.component.html+++ b/client/src/app/shared/shared-user-settings/user-video-settings.component.html@@ -38,7 +38,7 @@<div class="form-group"><my-peertube-checkbox- inputName="webTorrentEnabled" formControlName="webTorrentEnabled" [recommended]="true"+ inputName="p2pEnabled" formControlName="p2pEnabled" [recommended]="true"i18n-labelText labelText="Help share videos being played"><ng-container ngProjectAs="description">diff --git a/client/src/app/shared/shared-user-settings/user-video-settings.component.ts b/client/src/app/shared/shared-user-settings/user-video-settings.component.tsindex 0cd889a8a3..7d6b69469c 100644--- a/client/src/app/shared/shared-user-settings/user-video-settings.component.ts+++ b/client/src/app/shared/shared-user-settings/user-video-settings.component.ts@@ -34,7 +34,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,ngOnInit () {this.buildForm({nsfwPolicy: null,- webTorrentEnabled: null,+ p2pEnabled: null,autoPlayVideo: null,autoPlayNextVideo: null,videoLanguages: null@@ -48,7 +48,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,this.form.patchValue({nsfwPolicy: this.user.nsfwPolicy || this.defaultNSFWPolicy,- webTorrentEnabled: this.user.webTorrentEnabled,+ p2pEnabled: this.user.p2pEnabled,autoPlayVideo: this.user.autoPlayVideo === true,autoPlayNextVideo: this.user.autoPlayNextVideo,videoLanguages: this.user.videoLanguages@@ -65,7 +65,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,updateDetails (onlyKeys?: string[]) {const nsfwPolicy = this.form.value['nsfwPolicy']- const webTorrentEnabled = this.form.value['webTorrentEnabled']+ const p2pEnabled = this.form.value['p2pEnabled']const autoPlayVideo = this.form.value['autoPlayVideo']const autoPlayNextVideo = this.form.value['autoPlayNextVideo']@@ -80,7 +80,7 @@ export class UserVideoSettingsComponent extends FormReactive implements OnInit,let details: UserUpdateMe = {nsfwPolicy,- webTorrentEnabled,+ p2pEnabled,autoPlayVideo,autoPlayNextVideo,videoLanguagesdiff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.tsindex d4cbda3a91..d9dacfba53 100644--- a/client/src/assets/player/peertube-player-local-storage.ts+++ b/client/src/assets/player/peertube-player-local-storage.ts@@ -10,14 +10,6 @@ function getStoredVolume () {return undefined}-function getStoredP2PEnabled (): boolean {- const value = getLocalStorage('webtorrent_enabled')- if (value !== null && value !== undefined) return value === 'true'-- // By default webtorrent is enabled- return true-}-function getStoredMute () {const value = getLocalStorage('mute')if (value !== null && value !== undefined) return value === 'true'@@ -123,7 +115,6 @@ function cleanupVideoWatch () {export {getStoredVolume,- getStoredP2PEnabled,getStoredMute,getStoredTheater,saveVolumeInStore,diff --git a/client/src/assets/player/peertube-player-manager.ts b/client/src/assets/player/peertube-player-manager.tsindex 3e6398f500..c27024beba 100644--- a/client/src/assets/player/peertube-player-manager.ts+++ b/client/src/assets/player/peertube-player-manager.ts@@ -31,7 +31,7 @@ import { copyToClipboard } from '../../root-helpers/utils'import { RedundancyUrlManager } from './p2p-media-loader/redundancy-url-manager'import { segmentUrlBuilderFactory } from './p2p-media-loader/segment-url-builder'import { segmentValidatorFactory } from './p2p-media-loader/segment-validator'-import { getAverageBandwidthInStore, getStoredP2PEnabled, saveAverageBandwidth } from './peertube-player-local-storage'+import { getAverageBandwidthInStore, saveAverageBandwidth } from './peertube-player-local-storage'import {NextPreviousVideoButtonOptions,P2PMediaLoaderPluginOptions,@@ -86,6 +86,7 @@ export interface CommonOptions extends CustomizationOptions {onPlayerElementChange: (element: HTMLVideoElement) => voidautoplay: boolean+ p2pEnabled: booleannextVideo?: () => voidhasNextVideo?: () => boolean@@ -374,7 +375,7 @@ export class PeertubePlayerManager {requiredSegmentsPriority: 1,simultaneousHttpDownloads: 1,segmentUrlBuilder: segmentUrlBuilderFactory(redundancyUrlManager, 1),- useP2P: getStoredP2PEnabled(),+ useP2P: commonOptions.p2pEnabled,consumeOnly},segments: {@@ -437,6 +438,7 @@ export class PeertubePlayerManager {const webtorrent = {autoplay,+ playerRefusedP2P: commonOptions.p2pEnabled === false,videoDuration: commonOptions.videoDuration,playerElement: commonOptions.playerElement,videoFiles: webtorrentOptions.videoFiles.length !== 0diff --git a/client/src/assets/player/peertube-videojs-typings.ts b/client/src/assets/player/peertube-videojs-typings.tsindex ea39ac44d4..824ea058b4 100644--- a/client/src/assets/player/peertube-videojs-typings.ts+++ b/client/src/assets/player/peertube-videojs-typings.ts@@ -137,6 +137,8 @@ type WebtorrentPluginOptions = {videoFiles: VideoFile[]startTime: number | string++ playerRefusedP2P: boolean}type P2PMediaLoaderPluginOptions = {diff --git a/client/src/assets/player/utils.ts b/client/src/assets/player/utils.tsindex 18380d9501..7e25e3067d 100644--- a/client/src/assets/player/utils.ts+++ b/client/src/assets/player/utils.ts@@ -1,4 +1,4 @@-import { VideoFile } from '@shared/models'+import { HTMLServerConfig, Video, VideoFile } from '@shared/models'function toTitleCase (str: string) {return str.charAt(0).toUpperCase() + str.slice(1)@@ -8,6 +8,13 @@ function isWebRTCDisabled () {return !!((window as any).RTCPeerConnection || (window as any).mozRTCPeerConnection || (window as any).webkitRTCPeerConnection) === false}+function isP2PEnabled (video: Video, config: HTMLServerConfig, userP2PEnabled: boolean) {+ if (video.isLocal && config.tracker.enabled === false) return false+ if (isWebRTCDisabled()) return false++ return userP2PEnabled+}+function isIOS () {if (/iPad|iPhone|iPod/.test(navigator.platform)) {return true@@ -97,6 +104,7 @@ export {getRtcConfig,toTitleCase,isWebRTCDisabled,+ isP2PEnabled,buildVideoOrPlaylistEmbed,videoFileMaxByResolution,diff --git a/client/src/assets/player/webtorrent/webtorrent-plugin.ts b/client/src/assets/player/webtorrent/webtorrent-plugin.tsindex 1a1cd7f1a6..2b93902065 100644--- a/client/src/assets/player/webtorrent/webtorrent-plugin.ts+++ b/client/src/assets/player/webtorrent/webtorrent-plugin.ts@@ -2,13 +2,7 @@ import videojs from 'video.js'import * as WebTorrent from 'webtorrent'import { timeToInt } from '@shared/core-utils'import { VideoFile } from '@shared/models'-import {- getAverageBandwidthInStore,- getStoredMute,- getStoredP2PEnabled,- getStoredVolume,- saveAverageBandwidth-} from '../peertube-player-local-storage'+import { getAverageBandwidthInStore, getStoredMute, getStoredVolume, saveAverageBandwidth } from '../peertube-player-local-storage'import { PeerTubeResolution, PlayerNetworkInfo, WebtorrentPluginOptions } from '../peertube-videojs-typings'import { getRtcConfig, isIOS, videoFileMaxByResolution, videoFileMinByResolution } from '../utils'import { PeertubeChunkStore } from './peertube-chunk-store'@@ -74,9 +68,10 @@ class WebTorrentPlugin extends Plugin {this.startTime = timeToInt(options.startTime)- // Disable auto play on iOS+ // Custom autoplay handled by webtorrent because we lazy play the videothis.autoplay = options.autoplay- this.playerRefusedP2P = !getStoredP2PEnabled()++ this.playerRefusedP2P = options.playerRefusedP2Pthis.videoFiles = options.videoFilesthis.videoDuration = options.videoDurationdiff --git a/client/src/root-helpers/index.ts b/client/src/root-helpers/index.tsindex 63d55d7ef0..aa3b442dde 100644--- a/client/src/root-helpers/index.ts+++ b/client/src/root-helpers/index.ts@@ -1,6 +1,7 @@export * from './users'export * from './bytes'export * from './images'+export * from './local-storage-utils'export * from './peertube-web-storage'export * from './utils'export * from './plugins-manager'diff --git a/client/src/root-helpers/local-storage-utils.ts b/client/src/root-helpers/local-storage-utils.tsnew file mode 100644index 0000000000..c2b3f90352--- /dev/null+++ b/client/src/root-helpers/local-storage-utils.ts@@ -0,0 +1,10 @@+function getBoolOrDefault (value: string, defaultValue: boolean) {+ if (value === 'true') return true+ if (value === 'false') return false++ return defaultValue+}++export {+ getBoolOrDefault+}diff --git a/client/src/root-helpers/users/index.ts b/client/src/root-helpers/users/index.tsindex 8fbaca9e3f..2b11d0b7ed 100644--- a/client/src/root-helpers/users/index.ts+++ b/client/src/root-helpers/users/index.ts@@ -1,3 +1,2 @@export * from './user-local-storage-keys'-export * from './user-local-storage-manager'export * from './user-tokens'diff --git a/client/src/root-helpers/users/user-local-storage-keys.ts b/client/src/root-helpers/users/user-local-storage-keys.tsindex 5f915899c6..c3934ae3cf 100644--- a/client/src/root-helpers/users/user-local-storage-keys.ts+++ b/client/src/root-helpers/users/user-local-storage-keys.ts@@ -1,15 +1,25 @@export const UserLocalStorageKeys = {ID: 'id',+ USERNAME: 'username',ROLE: 'role',EMAIL: 'email',+VIDEOS_HISTORY_ENABLED: 'videos-history-enabled',- USERNAME: 'username',NSFW_POLICY: 'nsfw_policy',- WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled',+ P2P_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled',+AUTO_PLAY_VIDEO: 'auto_play_video',- SESSION_STORAGE_AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video',+ AUTO_PLAY_NEXT_VIDEO: 'auto_play_next_video',AUTO_PLAY_VIDEO_PLAYLIST: 'auto_play_video_playlist',+THEME: 'theme',LAST_ACTIVE_THEME: 'last_active_theme',+VIDEO_LANGUAGES: 'video_languages'}++export const UserTokenLocalStorageKeys = {+ ACCESS_TOKEN: 'access_token',+ REFRESH_TOKEN: 'refresh_token',+ TOKEN_TYPE: 'token_type'+}diff --git a/client/src/root-helpers/users/user-local-storage-manager.ts b/client/src/root-helpers/users/user-local-storage-manager.tsdeleted file mode 100644index c75cea1270..0000000000--- a/client/src/root-helpers/users/user-local-storage-manager.ts+++ /dev/null@@ -1,55 +0,0 @@-import { NSFWPolicyType, UserRole } from '@shared/models'-import { peertubeLocalStorage } from '../peertube-web-storage'-import { UserLocalStorageKeys } from './user-local-storage-keys'--function getUserInfoFromLocalStorage () {- const usernameLocalStorage = peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME)-- if (!usernameLocalStorage) return undefined-- return {- id: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ID), 10),- username: peertubeLocalStorage.getItem(UserLocalStorageKeys.USERNAME),- email: peertubeLocalStorage.getItem(UserLocalStorageKeys.EMAIL),- role: parseInt(peertubeLocalStorage.getItem(UserLocalStorageKeys.ROLE), 10) as UserRole,- nsfwPolicy: peertubeLocalStorage.getItem(UserLocalStorageKeys.NSFW_POLICY) as NSFWPolicyType,- webTorrentEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.WEBTORRENT_ENABLED) === 'true',- autoPlayVideo: peertubeLocalStorage.getItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO) === 'true',- videosHistoryEnabled: peertubeLocalStorage.getItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED) === 'true'- }-}--function flushUserInfoFromLocalStorage () {- peertubeLocalStorage.removeItem(UserLocalStorageKeys.ID)- peertubeLocalStorage.removeItem(UserLocalStorageKeys.USERNAME)- peertubeLocalStorage.removeItem(UserLocalStorageKeys.EMAIL)- peertubeLocalStorage.removeItem(UserLocalStorageKeys.ROLE)- peertubeLocalStorage.removeItem(UserLocalStorageKeys.NSFW_POLICY)- peertubeLocalStorage.removeItem(UserLocalStorageKeys.WEBTORRENT_ENABLED)- peertubeLocalStorage.removeItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO)- peertubeLocalStorage.removeItem(UserLocalStorageKeys.VIDEOS_HISTORY_ENABLED)-}--function saveUserInfoIntoLocalStorage (info: {- id: number- username: string- email: string- role: UserRole- nsfwPolicy: NSFWPolicyType- webTorrentEnabled: boolean- autoPlayVideo: boolean-}) {- peertubeLocalStorage.setItem(UserLocalStorageKeys.ID, info.id.toString())- peertubeLocalStorage.setItem(UserLocalStorageKeys.USERNAME, info.username)- peertubeLocalStorage.setItem(UserLocalStorageKeys.EMAIL, info.email)- peertubeLocalStorage.setItem(UserLocalStorageKeys.ROLE, info.role.toString())- peertubeLocalStorage.setItem(UserLocalStorageKeys.NSFW_POLICY, info.nsfwPolicy.toString())- peertubeLocalStorage.setItem(UserLocalStorageKeys.WEBTORRENT_ENABLED, JSON.stringify(info.webTorrentEnabled))- peertubeLocalStorage.setItem(UserLocalStorageKeys.AUTO_PLAY_VIDEO, JSON.stringify(info.autoPlayVideo))-}--export {- getUserInfoFromLocalStorage,- saveUserInfoIntoLocalStorage,- flushUserInfoFromLocalStorage-}diff --git a/client/src/root-helpers/users/user-tokens.ts b/client/src/root-helpers/users/user-tokens.tsindex d42e1c8f32..a6d614cb7b 100644--- a/client/src/root-helpers/users/user-tokens.ts+++ b/client/src/root-helpers/users/user-tokens.ts@@ -1,46 +1,11 @@-import { peertubeLocalStorage } from '../peertube-web-storage'--export type TokenOptions = {- accessToken: string- refreshToken: string- tokenType: string-}--// Private class only used by User-export class Tokens {- private static KEYS = {- ACCESS_TOKEN: 'access_token',- REFRESH_TOKEN: 'refresh_token',- TOKEN_TYPE: 'token_type'- }+import { UserTokenLocalStorageKeys } from './user-local-storage-keys'+export class UserTokens {accessToken: stringrefreshToken: stringtokenType: string- static load () {- const accessTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.ACCESS_TOKEN)- const refreshTokenLocalStorage = peertubeLocalStorage.getItem(this.KEYS.REFRESH_TOKEN)- const tokenTypeLocalStorage = peertubeLocalStorage.getItem(this.KEYS.TOKEN_TYPE)-- if (accessTokenLocalStorage && refreshTokenLocalStorage && tokenTypeLocalStorage) {- return new Tokens({- accessToken: accessTokenLocalStorage,- refreshToken: refreshTokenLocalStorage,- tokenType: tokenTypeLocalStorage- })- }-- return null- }-- static flush () {- peertubeLocalStorage.removeItem(this.KEYS.ACCESS_TOKEN)- peertubeLocalStorage.removeItem(this.KEYS.REFRESH_TOKEN)- peertubeLocalStorage.removeItem(this.KEYS.TOKEN_TYPE)- }-- constructor (hash?: TokenOptions) {+ constructor (hash?: Partial<UserTokens>) {if (hash) {this.accessToken = hash.accessTokenthis.refreshToken = hash.refreshToken@@ -53,9 +18,29 @@ export class Tokens {}}- save () {- peertubeLocalStorage.setItem(Tokens.KEYS.ACCESS_TOKEN, this.accessToken)- peertubeLocalStorage.setItem(Tokens.KEYS.REFRESH_TOKEN, this.refreshToken)- peertubeLocalStorage.setItem(Tokens.KEYS.TOKEN_TYPE, this.tokenType)+ static getUserTokens (localStorage: Pick<Storage, 'getItem'>) {+ const accessTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.ACCESS_TOKEN)+ const refreshTokenLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.REFRESH_TOKEN)+ const tokenTypeLocalStorage = localStorage.getItem(UserTokenLocalStorageKeys.TOKEN_TYPE)++ if (!accessTokenLocalStorage || !refreshTokenLocalStorage || !tokenTypeLocalStorage) return null++ return new UserTokens({+ accessToken: accessTokenLocalStorage,+ refreshToken: refreshTokenLocalStorage,+ tokenType: tokenTypeLocalStorage+ })+ }++ static saveToLocalStorage (localStorage: Pick<Storage, 'setItem'>, tokens: UserTokens) {+ localStorage.setItem(UserTokenLocalStorageKeys.ACCESS_TOKEN, tokens.accessToken)+ localStorage.setItem(UserTokenLocalStorageKeys.REFRESH_TOKEN, tokens.refreshToken)+ localStorage.setItem(UserTokenLocalStorageKeys.TOKEN_TYPE, tokens.tokenType)+ }++ static flushLocalStorage (localStorage: Pick<Storage, 'removeItem'>) {+ localStorage.removeItem(UserTokenLocalStorageKeys.ACCESS_TOKEN)+ localStorage.removeItem(UserTokenLocalStorageKeys.REFRESH_TOKEN)+ localStorage.removeItem(UserTokenLocalStorageKeys.TOKEN_TYPE)}}diff --git a/client/src/standalone/videos/embed.ts b/client/src/standalone/videos/embed.tsindex 874be580db..94f1096b77 100644--- a/client/src/standalone/videos/embed.ts+++ b/client/src/standalone/videos/embed.ts@@ -7,6 +7,7 @@ import {OAuth2ErrorCode,ResultList,UserRefreshToken,+ Video,VideoCaption,VideoDetails,VideoPlaylist,@@ -16,9 +17,11 @@ import {import { P2PMediaLoaderOptions, PeertubePlayerManagerOptions, PlayerMode } from '../../assets/player/peertube-player-manager'import { VideoJSCaption } from '../../assets/player/peertube-videojs-typings'import { TranslationsManager } from '../../assets/player/translations-manager'+import { isP2PEnabled } from '../../assets/player/utils'+import { getBoolOrDefault } from '../../root-helpers/local-storage-utils'import { peertubeLocalStorage } from '../../root-helpers/peertube-web-storage'import { PluginsManager } from '../../root-helpers/plugins-manager'-import { Tokens } from '../../root-helpers/users'+import { UserLocalStorageKeys, UserTokens } from '../../root-helpers/users'import { objectToUrlEncoded } from '../../root-helpers/utils'import { RegisterClientHelpers } from '../../types/register-client-option.model'import { PeerTubeEmbedApi } from './embed-api'@@ -48,7 +51,7 @@ export class PeerTubeEmbed {mode: PlayerModescope = 'peertube'- userTokens: Tokens+ userTokens: UserTokensheaders = new Headers()LOCAL_STORAGE_OAUTH_CLIENT_KEYS = {CLIENT_ID: 'client_id',@@ -118,7 +121,7 @@ export class PeerTubeEmbed {return res.json()}).then((obj: UserRefreshToken & { code?: OAuth2ErrorCode }) => {if (!obj || obj.code === OAuth2ErrorCode.INVALID_GRANT) {- Tokens.flush()+ UserTokens.flushLocalStorage(peertubeLocalStorage)this.removeTokensFromHeaders()return resolve()@@ -126,7 +129,7 @@ export class PeerTubeEmbed {this.userTokens.accessToken = obj.access_tokenthis.userTokens.refreshToken = obj.refresh_token- this.userTokens.save()+ UserTokens.saveToLocalStorage(peertubeLocalStorage, this.userTokens)this.setHeadersFromTokens()@@ -138,7 +141,7 @@ export class PeerTubeEmbed {return refreshingTokenPromise.catch(() => {- Tokens.flush()+ UserTokens.flushLocalStorage(peertubeLocalStorage)this.removeTokensFromHeaders()}).then(() => fetch(url, {@@ -258,7 +261,7 @@ export class PeerTubeEmbed {}async init () {- this.userTokens = Tokens.load()+ this.userTokens = UserTokens.getUserTokens(peertubeLocalStorage)await this.initCore()}@@ -515,6 +518,8 @@ export class PeerTubeEmbed {muted: this.muted,loop: this.loop,+ p2pEnabled: this.isP2PEnabled(videoInfo),+captions: videoCaptions.length !== 0,subtitle: this.subtitle,@@ -669,7 +674,7 @@ export class PeerTubeEmbed {const title = this.title ? videoInfo.name : undefined- const description = this.warningTitle && (!videoInfo.isLocal || this.config.tracker.enabled)+ const description = this.warningTitle && this.isP2PEnabled(videoInfo)? '<span class="text">' + peertubeTranslate('Watching this video may reveal your IP address to others.') + '</span>': undefined@@ -784,6 +789,15 @@ export class PeerTubeEmbed {translate: (value: string) => Promise.resolve(peertubeTranslate(value, translations))}}++ private isP2PEnabled (video: Video) {+ const userP2PEnabled = getBoolOrDefault(+ peertubeLocalStorage.getItem(UserLocalStorageKeys.P2P_ENABLED),+ this.config.defaults.p2p.enabled+ )++ return isP2PEnabled(video, this.config, userP2PEnabled)+ }}PeerTubeEmbed.main()diff --git a/config/default.yaml b/config/default.yamlindex fbe0dbbfbf..421c195690 100644--- a/config/default.yaml+++ b/config/default.yaml@@ -92,6 +92,11 @@ defaults:# No licence by defaultlicence: null+ p2p:+ # Enable P2P by default+ # Can be enabled/disabled by anonymous users and logged in users+ enabled: true+# From the project root directorystorage:tmp: 'storage/tmp/' # Use to download data (imports etc), store uploaded files before and during processing...@@ -216,7 +221,7 @@ security:enabled: truetracker:- # If you disable the tracker, you disable the P2P aspect of PeerTube+ # If you disable the tracker, you disable the P2P on your PeerTube instanceenabled: true# Only handle requests on your videos# If you set this to false it means you have a public trackerdiff --git a/config/production.yaml.example b/config/production.yaml.exampleindex 6363a51794..13219fd5d8 100644--- a/config/production.yaml.example+++ b/config/production.yaml.example@@ -90,6 +90,11 @@ defaults:# No licence by defaultlicence: null+ p2p:+ # Enable P2P by default+ # Can be enabled/disabled by anonymous users and logged in users+ enabled: true+# From the project root directorystorage:tmp: '/var/www/peertube/storage/tmp/' # Use to download data (imports etc), store uploaded files before and during processing...diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.tsindex 11d3525e4a..f3b4508d93 100644--- a/server/controllers/api/users/index.ts+++ b/server/controllers/api/users/index.ts@@ -183,6 +183,7 @@ async function createUser (req: express.Request, res: express.Response) {password: body.password,email: body.email,nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,+ p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,autoPlayVideo: true,role: body.role,videoQuota: body.videoQuota,@@ -232,6 +233,7 @@ async function registerUser (req: express.Request, res: express.Response) {password: body.password,email: body.email,nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,+ p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,autoPlayVideo: true,role: UserRole.USER,videoQuota: CONFIG.USER.VIDEO_QUOTA,diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.tsindex 6bacdbbb63..1125771d44 100644--- a/server/controllers/api/users/me.ts+++ b/server/controllers/api/users/me.ts@@ -197,7 +197,7 @@ async function updateMe (req: express.Request, res: express.Response) {const keysToUpdate: (keyof UserUpdateMe & keyof AttributesOnly<UserModel>)[] = ['password','nsfwPolicy',- 'webTorrentEnabled',+ 'p2pEnabled','autoPlayVideo','autoPlayNextVideo','autoPlayNextVideoPlaylist',@@ -213,6 +213,12 @@ async function updateMe (req: express.Request, res: express.Response) {if (body[key] !== undefined) user.set(key, body[key])}+ if (body.p2pEnabled !== undefined) {+ user.set('p2pEnabled', body.p2pEnabled)+ } else if (body.webTorrentEnabled !== undefined) { // FIXME: deprecated in 4.1+ user.set('p2pEnabled', body.webTorrentEnabled)+ }+if (body.email !== undefined) {if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {user.pendingEmail = body.emaildiff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.tsindex f52c60b603..badf171d24 100644--- a/server/helpers/custom-validators/users.ts+++ b/server/helpers/custom-validators/users.ts@@ -49,7 +49,7 @@ function isUserNSFWPolicyValid (value: any) {return exists(value) && nsfwPolicies.includes(value)}-function isUserWebTorrentEnabledValid (value: any) {+function isUserP2PEnabledValid (value: any) {return isBooleanValid(value)}@@ -109,7 +109,7 @@ export {isUserAdminFlagsValid,isUserEmailVerifiedValid,isUserNSFWPolicyValid,- isUserWebTorrentEnabledValid,+ isUserP2PEnabledValid,isUserAutoPlayVideoValid,isUserAutoPlayNextVideoValid,isUserAutoPlayNextVideoPlaylistValid,diff --git a/server/initializers/config.ts b/server/initializers/config.tsindex e3e8c426e0..a6ea6d8882 100644--- a/server/initializers/config.ts+++ b/server/initializers/config.ts@@ -78,6 +78,9 @@ const CONFIG = {COMMENTS_ENABLED: config.get<boolean>('defaults.publish.comments_enabled'),PRIVACY: config.get<VideoPrivacy>('defaults.publish.privacy'),LICENCE: config.get<number>('defaults.publish.licence')+ },+ P2P: {+ ENABLED: config.get<boolean>('defaults.p2p.enabled')}},diff --git a/server/initializers/constants.ts b/server/initializers/constants.tsindex 026c715c2d..258ccdb51e 100644--- a/server/initializers/constants.ts+++ b/server/initializers/constants.ts@@ -25,7 +25,7 @@ import { CONFIG, registerConfigChangedHandler } from './config'// ----------------------------------------------------------------------------const LAST_MIGRATION_VERSION = 670+const LAST_MIGRATION_VERSION = 675// ---------------------------------------------------------------------------diff --git a/server/initializers/installer.ts b/server/initializers/installer.tsindex 75daeb5d89..19adaf1776 100644--- a/server/initializers/installer.ts+++ b/server/initializers/installer.ts@@ -144,6 +144,7 @@ async function createOAuthAdminIfNotExist () {role,verified: true,nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,+ p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,videoQuota: -1,videoQuotaDaily: -1}diff --git a/server/initializers/migrations/0675-p2p-enabled.ts b/server/initializers/migrations/0675-p2p-enabled.tsnew file mode 100644index 0000000000..b4f53381ea--- /dev/null+++ b/server/initializers/migrations/0675-p2p-enabled.ts@@ -0,0 +1,21 @@+import * as Sequelize from 'sequelize'++async function up (utils: {+ transaction: Sequelize.Transaction+ queryInterface: Sequelize.QueryInterface+ sequelize: Sequelize.Sequelize+ db: any+}): Promise<void> {+ await utils.queryInterface.renameColumn('user', 'webTorrentEnabled', 'p2pEnabled')++ await utils.sequelize.query('ALTER TABLE "user" ALTER COLUMN "p2pEnabled" DROP DEFAULT')+}++function down (options) {+ throw new Error('Not implemented.')+}++export {+ up,+ down+}diff --git a/server/lib/auth/oauth-model.ts b/server/lib/auth/oauth-model.tsindex f2ef0a78af..754bee36dd 100644--- a/server/lib/auth/oauth-model.ts+++ b/server/lib/auth/oauth-model.ts@@ -226,6 +226,7 @@ async function createUserFromExternal (pluginAuth: string, options: {password: null,email: options.email,nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,+ p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,autoPlayVideo: true,role: options.role,videoQuota: CONFIG.USER.VIDEO_QUOTA,diff --git a/server/lib/server-config-manager.ts b/server/lib/server-config-manager.tsindex 8aea4cd6d2..d759f85e17 100644--- a/server/lib/server-config-manager.ts+++ b/server/lib/server-config-manager.ts@@ -61,6 +61,9 @@ class ServerConfigManager {commentsEnabled: CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,privacy: CONFIG.DEFAULTS.PUBLISH.PRIVACY,licence: CONFIG.DEFAULTS.PUBLISH.LICENCE+ },+ p2p: {+ enabled: CONFIG.DEFAULTS.P2P.ENABLED}},diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.tsindex 7a6b2ce57f..bc6007c6d4 100644--- a/server/middlewares/validators/users.ts+++ b/server/middlewares/validators/users.ts@@ -15,6 +15,7 @@ import {isUserDisplayNameValid,isUserNoModal,isUserNSFWPolicyValid,+ isUserP2PEnabledValid,isUserPasswordValid,isUserPasswordValidOrEmpty,isUserRoleValid,@@ -239,6 +240,9 @@ const usersUpdateMeValidator = [body('autoPlayVideo').optional().custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),+ body('p2pEnabled')+ .optional()+ .custom(isUserP2PEnabledValid).withMessage('Should have a valid p2p enabled boolean'),body('videoLanguages').optional().custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'),diff --git a/server/models/user/user.ts b/server/models/user/user.tsindex b56f37e558..88c3ff5283 100644--- a/server/models/user/user.ts+++ b/server/models/user/user.ts@@ -55,7 +55,7 @@ import {isUserVideoQuotaDailyValid,isUserVideoQuotaValid,isUserVideosHistoryEnabledValid,- isUserWebTorrentEnabledValid+ isUserP2PEnabledValid} from '../../helpers/custom-validators/users'import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto'import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants'@@ -267,10 +267,9 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {nsfwPolicy: NSFWPolicyType@AllowNull(false)- @Default(true)- @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled'))+ @Is('p2pEnabled', value => throwIfNotValid(value, isUserP2PEnabledValid, 'P2P enabled'))@Column- webTorrentEnabled: boolean+ p2pEnabled: boolean@AllowNull(false)@Default(true)@@ -892,7 +891,11 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {emailVerified: this.emailVerified,nsfwPolicy: this.nsfwPolicy,- webTorrentEnabled: this.webTorrentEnabled,++ // FIXME: deprecated in 4.1+ webTorrentEnabled: this.p2pEnabled,+ p2pEnabled: this.p2pEnabled,+videosHistoryEnabled: this.videosHistoryEnabled,autoPlayVideo: this.autoPlayVideo,autoPlayNextVideo: this.autoPlayNextVideo,diff --git a/server/tests/api/server/config-defaults.ts b/server/tests/api/server/config-defaults.tsindex 3dff7bfb7a..340d4b44bb 100644--- a/server/tests/api/server/config-defaults.ts+++ b/server/tests/api/server/config-defaults.ts@@ -21,18 +21,7 @@ describe('Test config defaults', function () {before(async function () {this.timeout(30000)- const overrideConfig = {- defaults: {- publish: {- comments_enabled: false,- download_enabled: false,- privacy: VideoPrivacy.INTERNAL,- licence: 4- }- }- }-- server = await createSingleServer(1, overrideConfig)+ server = await createSingleServer(1)await setAccessTokensToServers([ server ])await setDefaultVideoChannel([ server ])@@ -40,6 +29,23 @@ describe('Test config defaults', function () {})describe('Default publish values', function () {++ before(async function () {+ const overrideConfig = {+ defaults: {+ publish: {+ comments_enabled: false,+ download_enabled: false,+ privacy: VideoPrivacy.INTERNAL,+ licence: 4+ }+ }+ }++ await server.kill()+ await server.run(overrideConfig)+ })+const attributes = {name: 'video',downloadEnabled: undefined,@@ -117,6 +123,45 @@ describe('Test config defaults', function () {})})+ describe('Default P2P values', function () {++ before(async function () {+ const overrideConfig = {+ defaults: {+ p2p: {+ enabled: false+ }+ }+ }++ await server.kill()+ await server.run(overrideConfig)+ })++ it('Should not have P2P enabled', async function () {+ const config = await server.config.getConfig()++ expect(config.defaults.p2p.enabled).to.be.false+ })++ it('Should create a user with this default setting', async function () {+ await server.users.create({ username: 'user_p2p_1' })+ const userToken = await server.login.getAccessToken('user_p2p_1')++ const { p2pEnabled } = await server.users.getMyInfo({ token: userToken })+ expect(p2pEnabled).to.be.false+ })++ it('Should register a user with this default setting', async function () {+ await server.users.register({ username: 'user_p2p_2' })++ const userToken = await server.login.getAccessToken('user_p2p_2')++ const { p2pEnabled } = await server.users.getMyInfo({ token: userToken })+ expect(p2pEnabled).to.be.false+ })+ })+after(async function () {await cleanupTests([ server ])})diff --git a/server/tests/api/server/follows.ts b/server/tests/api/server/follows.tsindex 748f4cd356..c132d99eab 100644--- a/server/tests/api/server/follows.ts+++ b/server/tests/api/server/follows.ts@@ -292,7 +292,7 @@ describe('Test follows', function () {})it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {- this.timeout(60000)+ this.timeout(120000)await servers[1].videos.upload({ attributes: { name: 'server2' } })await servers[2].videos.upload({ attributes: { name: 'server3' } })diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.tsindex 6c41e7d560..f00cbab5a7 100644--- a/server/tests/api/users/users.ts+++ b/server/tests/api/users/users.ts@@ -559,6 +559,28 @@ describe('Test users', function () {expect(user.autoPlayNextVideo).to.be.true})+ it('Should be able to change the p2p attribute', async function () {+ {+ await server.users.updateMe({+ token: userToken,+ webTorrentEnabled: false+ })++ const user = await server.users.getMyInfo({ token: userToken })+ expect(user.p2pEnabled).to.be.false+ }++ {+ await server.users.updateMe({+ token: userToken,+ p2pEnabled: true+ })++ const user = await server.users.getMyInfo({ token: userToken })+ expect(user.p2pEnabled).to.be.true+ }+ })+it('Should be able to change the email attribute', async function () {await server.users.updateMe({token: userToken,diff --git a/shared/models/server/server-config.model.ts b/shared/models/server/server-config.model.tsindex 9c3dcd6d3c..71540e6034 100644--- a/shared/models/server/server-config.model.ts+++ b/shared/models/server/server-config.model.ts@@ -55,6 +55,10 @@ export interface ServerConfig {privacy: VideoPrivacylicence: number}++ p2p: {+ enabled: boolean+ }}webadmin: {diff --git a/shared/models/users/user-update-me.model.ts b/shared/models/users/user-update-me.model.tsindex 6d7df38fbb..e664e44b5e 100644--- a/shared/models/users/user-update-me.model.ts+++ b/shared/models/users/user-update-me.model.ts@@ -5,7 +5,10 @@ export interface UserUpdateMe {description?: stringnsfwPolicy?: NSFWPolicyType+ // FIXME: deprecated in favour of p2pEnabled in 4.1webTorrentEnabled?: boolean+ p2pEnabled?: boolean+autoPlayVideo?: booleanautoPlayNextVideo?: booleanautoPlayNextVideoPlaylist?: booleandiff --git a/shared/models/users/user.model.ts b/shared/models/users/user.model.tsindex 78870c5564..63c5c8a92f 100644--- a/shared/models/users/user.model.ts+++ b/shared/models/users/user.model.ts@@ -20,7 +20,11 @@ export interface User {autoPlayVideo: booleanautoPlayNextVideo: booleanautoPlayNextVideoPlaylist: boolean++ // @deprecated in favour of p2pEnabledwebTorrentEnabled: boolean+ p2pEnabled: boolean+videosHistoryEnabled: booleanvideoLanguages: string[]diff --git a/support/doc/api/openapi.yaml b/support/doc/api/openapi.yamlindex 1d5581072a..7b6e8a1e4f 100644--- a/support/doc/api/openapi.yaml+++ b/support/doc/api/openapi.yaml@@ -6679,7 +6679,7 @@ components:type: integerdescription: The user daily video quota in bytesexample: -1- webtorrentEnabled:+ p2pEnabled:type: booleandescription: Enable P2P in the playerUserWithStats:@@ -6780,7 +6780,7 @@ components:- 'true'- 'false'- both- webTorrentEnabled:+ p2pEnabled:type: booleandescription: whether to enable P2P in the player or notautoPlayVideo: