From 64cc5e8575fda47b281ae20abf0020e27fc8ce7c Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Fri, 5 Oct 2018 15:17:34 +0200 Subject: add webtorrent opt-out settings - add a key in localstorage to remember the opt-out - add a user setting --- .../my-account-video-settings.component.html | 12 ++++++++++++ .../my-account-video-settings.component.ts | 4 ++++ client/src/app/core/auth/auth-user.model.ts | 5 +++++ client/src/app/shared/users/user.model.ts | 4 ++++ client/src/assets/player/peertube-player-local-storage.ts | 10 ++++++++++ client/src/assets/player/peertube-videojs-plugin.ts | 6 +++++- 6 files changed, 40 insertions(+), 1 deletion(-) (limited to 'client/src') diff --git a/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html b/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html index 96629940f..adbb97f00 100644 --- a/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html +++ b/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html @@ -15,6 +15,18 @@ +
+ + +
+ +
+
+ { this.form.patchValue({ nsfwPolicy: this.user.nsfwPolicy, + webTorrentPolicy: this.user.webTorrentPolicy, autoPlayVideo: this.user.autoPlayVideo === true }) }) @@ -42,9 +44,11 @@ export class MyAccountVideoSettingsComponent extends FormReactive implements OnI updateDetails () { const nsfwPolicy = this.form.value['nsfwPolicy'] + const webTorrentPolicy = this.form.value['webTorrentPolicy'] const autoPlayVideo = this.form.value['autoPlayVideo'] const details: UserUpdateMe = { nsfwPolicy, + webTorrentPolicy, autoPlayVideo } diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 74ed1c580..97acf7bfe 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -4,6 +4,7 @@ import { UserRight } from '../../../../../shared/models/users/user-right.enum' import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' import { User, UserConstructorHash } from '../../shared/users/user.model' import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' +import { WebTorrentPolicyType } from '../../../../../shared/models/users/user-webtorrent-policy.type' export type TokenOptions = { accessToken: string @@ -72,6 +73,7 @@ export class AuthUser extends User { EMAIL: 'email', USERNAME: 'username', NSFW_POLICY: 'nsfw_policy', + WEBTORRENT_POLICY: 'peertube-videojs-' + 'webtorrent_policy', AUTO_PLAY_VIDEO: 'auto_play_video' } @@ -87,6 +89,7 @@ export class AuthUser extends User { email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, + webTorrentPolicy: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_POLICY) as WebTorrentPolicyType, autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true' }, Tokens.load() @@ -101,6 +104,7 @@ export class AuthUser extends User { peertubeLocalStorage.removeItem(this.KEYS.ID) peertubeLocalStorage.removeItem(this.KEYS.ROLE) peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY) + peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_POLICY) peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) peertubeLocalStorage.removeItem(this.KEYS.EMAIL) Tokens.flush() @@ -138,6 +142,7 @@ export class AuthUser extends User { peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) + peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_POLICY, this.webTorrentPolicy.toString()) peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) this.tokens.save() } diff --git a/client/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts index 877f1bf3a..240c7aacf 100644 --- a/client/src/app/shared/users/user.model.ts +++ b/client/src/app/shared/users/user.model.ts @@ -9,6 +9,7 @@ import { import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' import { Account } from '@app/shared/account/account.model' import { Avatar } from '../../../../../shared/models/avatars/avatar.model' +import { WebTorrentPolicyType } from '../../../../../shared/models/users/user-webtorrent-policy.type' export type UserConstructorHash = { id: number, @@ -18,6 +19,7 @@ export type UserConstructorHash = { videoQuota?: number, videoQuotaDaily?: number, nsfwPolicy?: NSFWPolicyType, + webTorrentPolicy?: WebTorrentPolicyType, autoPlayVideo?: boolean, createdAt?: Date, account?: AccountServerModel, @@ -32,6 +34,7 @@ export class User implements UserServerModel { email: string role: UserRole nsfwPolicy: NSFWPolicyType + webTorrentPolicy: WebTorrentPolicyType autoPlayVideo: boolean videoQuota: number videoQuotaDaily: number @@ -52,6 +55,7 @@ export class User implements UserServerModel { this.videoQuota = hash.videoQuota this.videoQuotaDaily = hash.videoQuotaDaily this.nsfwPolicy = hash.nsfwPolicy + this.webTorrentPolicy = hash.webTorrentPolicy this.autoPlayVideo = hash.autoPlayVideo this.createdAt = hash.createdAt this.blocked = hash.blocked diff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.ts index dac54c5a4..c3d8b71bc 100644 --- a/client/src/assets/player/peertube-player-local-storage.ts +++ b/client/src/assets/player/peertube-player-local-storage.ts @@ -10,6 +10,15 @@ function getStoredVolume () { return undefined } +function getStoredWebTorrentPolicy () { + const value = getLocalStorage('webtorrent_policy') + if (value !== null && value !== undefined) { + if (value.toString() === 'disable') return true + } + + return undefined +} + function getStoredMute () { const value = getLocalStorage('mute') if (value !== null && value !== undefined) return value === 'true' @@ -56,6 +65,7 @@ function getAverageBandwidthInStore () { export { getStoredVolume, + getStoredWebTorrentPolicy, getStoredMute, getStoredTheater, saveVolumeInStore, diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index 2330f476f..90ca8f9fa 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts @@ -8,6 +8,7 @@ import { isMobile, timeToInt, videoFileMaxByResolution, videoFileMinByResolution import * as CacheChunkStore from 'cache-chunk-store' import { PeertubeChunkStore } from './peertube-chunk-store' import { + getStoredWebTorrentPolicy, getAverageBandwidthInStore, getStoredMute, getStoredVolume, @@ -64,6 +65,7 @@ class PeerTubePlugin extends Plugin { private autoResolution = true private forbidAutoResolution = false private isAutoResolutionObservation = false + private playerRefusedP2P = false private videoViewInterval private torrentInfoInterval @@ -97,6 +99,7 @@ class PeerTubePlugin extends Plugin { if (volume !== undefined) this.player.volume(volume) const muted = getStoredMute() if (muted !== undefined) this.player.muted(muted) + this.playerRefusedP2P = getStoredWebTorrentPolicy() || false this.initializePlayer() this.runTorrentInfoScheduler() @@ -288,7 +291,8 @@ class PeerTubePlugin extends Plugin { renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => { this.renderer = renderer - if (err) return this.fallbackToHttp(done) + console.log('value this.playerRefusedP2P', this.playerRefusedP2P) + if (err || this.playerRefusedP2P) return this.fallbackToHttp(done) return this.tryToPlay(err => { if (err) return done(err) -- cgit v1.2.3 From ed638e5325096ef580da20f370ac61c59cd48cf7 Mon Sep 17 00:00:00 2001 From: Rigel Kent Date: Fri, 12 Oct 2018 18:12:39 +0200 Subject: move to boolean switch --- .../my-account-video-settings.component.html | 12 ++++++++---- .../my-account-video-settings.component.ts | 8 ++++---- client/src/app/core/auth/auth-user.model.ts | 9 ++++----- client/src/app/shared/users/user.model.ts | 7 +++---- client/src/assets/player/peertube-player-local-storage.ts | 12 +++++------- client/src/assets/player/peertube-videojs-plugin.ts | 5 ++--- 6 files changed, 26 insertions(+), 27 deletions(-) (limited to 'client/src') diff --git a/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html b/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html index adbb97f00..50f798c79 100644 --- a/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html +++ b/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html @@ -16,17 +16,21 @@
- +
- - - +
+ + { this.form.patchValue({ nsfwPolicy: this.user.nsfwPolicy, - webTorrentPolicy: this.user.webTorrentPolicy, + webTorrentEnabled: this.user.webTorrentEnabled, autoPlayVideo: this.user.autoPlayVideo === true }) }) @@ -44,11 +44,11 @@ export class MyAccountVideoSettingsComponent extends FormReactive implements OnI updateDetails () { const nsfwPolicy = this.form.value['nsfwPolicy'] - const webTorrentPolicy = this.form.value['webTorrentPolicy'] + const webTorrentEnabled = this.form.value['webTorrentEnabled'] const autoPlayVideo = this.form.value['autoPlayVideo'] const details: UserUpdateMe = { nsfwPolicy, - webTorrentPolicy, + webTorrentEnabled, autoPlayVideo } diff --git a/client/src/app/core/auth/auth-user.model.ts b/client/src/app/core/auth/auth-user.model.ts index 97acf7bfe..acd13d9c5 100644 --- a/client/src/app/core/auth/auth-user.model.ts +++ b/client/src/app/core/auth/auth-user.model.ts @@ -4,7 +4,6 @@ import { UserRight } from '../../../../../shared/models/users/user-right.enum' import { hasUserRight, UserRole } from '../../../../../shared/models/users/user-role' import { User, UserConstructorHash } from '../../shared/users/user.model' import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' -import { WebTorrentPolicyType } from '../../../../../shared/models/users/user-webtorrent-policy.type' export type TokenOptions = { accessToken: string @@ -73,7 +72,7 @@ export class AuthUser extends User { EMAIL: 'email', USERNAME: 'username', NSFW_POLICY: 'nsfw_policy', - WEBTORRENT_POLICY: 'peertube-videojs-' + 'webtorrent_policy', + WEBTORRENT_ENABLED: 'peertube-videojs-' + 'webtorrent_enabled', AUTO_PLAY_VIDEO: 'auto_play_video' } @@ -89,7 +88,7 @@ export class AuthUser extends User { email: peertubeLocalStorage.getItem(this.KEYS.EMAIL), role: parseInt(peertubeLocalStorage.getItem(this.KEYS.ROLE), 10) as UserRole, nsfwPolicy: peertubeLocalStorage.getItem(this.KEYS.NSFW_POLICY) as NSFWPolicyType, - webTorrentPolicy: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_POLICY) as WebTorrentPolicyType, + webTorrentEnabled: peertubeLocalStorage.getItem(this.KEYS.WEBTORRENT_ENABLED) === 'true', autoPlayVideo: peertubeLocalStorage.getItem(this.KEYS.AUTO_PLAY_VIDEO) === 'true' }, Tokens.load() @@ -104,7 +103,7 @@ export class AuthUser extends User { peertubeLocalStorage.removeItem(this.KEYS.ID) peertubeLocalStorage.removeItem(this.KEYS.ROLE) peertubeLocalStorage.removeItem(this.KEYS.NSFW_POLICY) - peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_POLICY) + peertubeLocalStorage.removeItem(this.KEYS.WEBTORRENT_ENABLED) peertubeLocalStorage.removeItem(this.KEYS.AUTO_PLAY_VIDEO) peertubeLocalStorage.removeItem(this.KEYS.EMAIL) Tokens.flush() @@ -142,7 +141,7 @@ export class AuthUser extends User { peertubeLocalStorage.setItem(AuthUser.KEYS.EMAIL, this.email) peertubeLocalStorage.setItem(AuthUser.KEYS.ROLE, this.role.toString()) peertubeLocalStorage.setItem(AuthUser.KEYS.NSFW_POLICY, this.nsfwPolicy.toString()) - peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_POLICY, this.webTorrentPolicy.toString()) + peertubeLocalStorage.setItem(AuthUser.KEYS.WEBTORRENT_ENABLED, JSON.stringify(this.webTorrentEnabled)) peertubeLocalStorage.setItem(AuthUser.KEYS.AUTO_PLAY_VIDEO, JSON.stringify(this.autoPlayVideo)) this.tokens.save() } diff --git a/client/src/app/shared/users/user.model.ts b/client/src/app/shared/users/user.model.ts index 240c7aacf..7c840ffa7 100644 --- a/client/src/app/shared/users/user.model.ts +++ b/client/src/app/shared/users/user.model.ts @@ -9,7 +9,6 @@ import { import { NSFWPolicyType } from '../../../../../shared/models/videos/nsfw-policy.type' import { Account } from '@app/shared/account/account.model' import { Avatar } from '../../../../../shared/models/avatars/avatar.model' -import { WebTorrentPolicyType } from '../../../../../shared/models/users/user-webtorrent-policy.type' export type UserConstructorHash = { id: number, @@ -19,7 +18,7 @@ export type UserConstructorHash = { videoQuota?: number, videoQuotaDaily?: number, nsfwPolicy?: NSFWPolicyType, - webTorrentPolicy?: WebTorrentPolicyType, + webTorrentEnabled?: boolean, autoPlayVideo?: boolean, createdAt?: Date, account?: AccountServerModel, @@ -34,7 +33,7 @@ export class User implements UserServerModel { email: string role: UserRole nsfwPolicy: NSFWPolicyType - webTorrentPolicy: WebTorrentPolicyType + webTorrentEnabled: boolean autoPlayVideo: boolean videoQuota: number videoQuotaDaily: number @@ -55,7 +54,7 @@ export class User implements UserServerModel { this.videoQuota = hash.videoQuota this.videoQuotaDaily = hash.videoQuotaDaily this.nsfwPolicy = hash.nsfwPolicy - this.webTorrentPolicy = hash.webTorrentPolicy + this.webTorrentEnabled = hash.webTorrentEnabled this.autoPlayVideo = hash.autoPlayVideo this.createdAt = hash.createdAt this.blocked = hash.blocked diff --git a/client/src/assets/player/peertube-player-local-storage.ts b/client/src/assets/player/peertube-player-local-storage.ts index c3d8b71bc..3ac5fe58a 100644 --- a/client/src/assets/player/peertube-player-local-storage.ts +++ b/client/src/assets/player/peertube-player-local-storage.ts @@ -10,13 +10,11 @@ function getStoredVolume () { return undefined } -function getStoredWebTorrentPolicy () { - const value = getLocalStorage('webtorrent_policy') - if (value !== null && value !== undefined) { - if (value.toString() === 'disable') return true - } +function getStoredWebTorrentEnabled (): boolean { + const value = getLocalStorage('webtorrent_enabled') + if (value !== null && value !== undefined) return value === 'true' - return undefined + return false } function getStoredMute () { @@ -65,7 +63,7 @@ function getAverageBandwidthInStore () { export { getStoredVolume, - getStoredWebTorrentPolicy, + getStoredWebTorrentEnabled, getStoredMute, getStoredTheater, saveVolumeInStore, diff --git a/client/src/assets/player/peertube-videojs-plugin.ts b/client/src/assets/player/peertube-videojs-plugin.ts index 90ca8f9fa..a53a2cc69 100644 --- a/client/src/assets/player/peertube-videojs-plugin.ts +++ b/client/src/assets/player/peertube-videojs-plugin.ts @@ -8,7 +8,7 @@ import { isMobile, timeToInt, videoFileMaxByResolution, videoFileMinByResolution import * as CacheChunkStore from 'cache-chunk-store' import { PeertubeChunkStore } from './peertube-chunk-store' import { - getStoredWebTorrentPolicy, + getStoredWebTorrentEnabled, getAverageBandwidthInStore, getStoredMute, getStoredVolume, @@ -82,6 +82,7 @@ class PeerTubePlugin extends Plugin { // Disable auto play on iOS this.autoplay = options.autoplay && this.isIOS() === false + this.playerRefusedP2P = !getStoredWebTorrentEnabled() this.startTime = timeToInt(options.startTime) this.videoFiles = options.videoFiles @@ -99,7 +100,6 @@ class PeerTubePlugin extends Plugin { if (volume !== undefined) this.player.volume(volume) const muted = getStoredMute() if (muted !== undefined) this.player.muted(muted) - this.playerRefusedP2P = getStoredWebTorrentPolicy() || false this.initializePlayer() this.runTorrentInfoScheduler() @@ -291,7 +291,6 @@ class PeerTubePlugin extends Plugin { renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => { this.renderer = renderer - console.log('value this.playerRefusedP2P', this.playerRefusedP2P) if (err || this.playerRefusedP2P) return this.fallbackToHttp(done) return this.tryToPlay(err => { -- cgit v1.2.3 From a73115f31ae891cb47759f075b1d2cead40817a4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 17 Oct 2018 10:47:01 +0200 Subject: Fix webtorrent disabling --- .../my-account-video-settings.component.html | 11 ----- .../src/assets/player/peertube-videojs-plugin.ts | 47 ++++++++++++++-------- 2 files changed, 30 insertions(+), 28 deletions(-) (limited to 'client/src') diff --git a/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html b/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html index 50f798c79..8be8a66cc 100644 --- a/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html +++ b/client/src/app/+my-account/my-account-settings/my-account-video-settings/my-account-video-settings.component.html @@ -15,17 +15,6 @@ -
- - -
- -
-
- { + this.player.playbackRate(oldPlaybackRate) + return done() + }) + } + this.addTorrent(this.currentVideoFile.magnetUri, previousVideoFile, options, () => { this.player.playbackRate(oldPlaybackRate) return done() @@ -251,11 +266,7 @@ class PeerTubePlugin extends Plugin { private addTorrent ( magnetOrTorrentUrl: string, previousVideoFile: VideoFile, - options: { - forcePlay?: boolean, - seek?: number, - delay?: number - }, + options: PlayOptions, done: Function ) { console.log('Adding ' + magnetOrTorrentUrl + '.') @@ -291,7 +302,7 @@ class PeerTubePlugin extends Plugin { renderVideo(torrent.files[ 0 ], this.playerElement, renderVideoOptions, (err, renderer) => { this.renderer = renderer - if (err || this.playerRefusedP2P) return this.fallbackToHttp(done) + if (err) return this.fallbackToHttp(options, done) return this.tryToPlay(err => { if (err) return done(err) @@ -299,7 +310,7 @@ class PeerTubePlugin extends Plugin { if (options.seek) this.seek(options.seek) if (options.forcePlay === false && paused === true) this.player.pause() - return done(err) + return done() }) }) }, options.delay || 0) @@ -435,12 +446,6 @@ class PeerTubePlugin extends Plugin { return this.updateVideoFile(undefined, { forcePlay: true, seek: this.startTime }) } - // Don't try on iOS that does not support MediaSource - if (this.isIOS()) { - this.currentVideoFile = this.pickAverageVideoFile() - return this.fallbackToHttp(undefined, false) - } - // Proxy first play const oldPlay = this.player.play.bind(this.player) this.player.play = () => { @@ -570,7 +575,9 @@ class PeerTubePlugin extends Plugin { return fetch(url, { method: 'PUT', body, headers }) } - private fallbackToHttp (done?: Function, play = true) { + private fallbackToHttp (options: PlayOptions, done?: Function) { + const paused = this.player.paused() + this.disableAutoResolution(true) this.flushVideoFile(this.currentVideoFile, true) @@ -582,9 +589,15 @@ class PeerTubePlugin extends Plugin { const httpUrl = this.currentVideoFile.fileUrl this.player.src = this.savePlayerSrcFunction this.player.src(httpUrl) - if (play) this.tryToPlay() - if (done) return done() + return this.tryToPlay(err => { + if (err && done) return done(err) + + if (options.seek) this.seek(options.seek) + if (options.forcePlay === false && paused === true) this.player.pause() + + if (done) return done() + }) } private handleError (err: Error | string) { -- cgit v1.2.3