From a9bfa85d2cdf13670aaced740da5b493fbeddfce Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Wed, 15 Dec 2021 15:58:10 +0100 Subject: Add ability for admins to set default p2p policy --- server/controllers/api/users/index.ts | 2 + server/controllers/api/users/me.ts | 8 ++- server/helpers/custom-validators/users.ts | 4 +- server/initializers/config.ts | 3 + server/initializers/constants.ts | 2 +- server/initializers/installer.ts | 1 + server/initializers/migrations/0675-p2p-enabled.ts | 21 +++++++ server/lib/auth/oauth-model.ts | 1 + server/lib/server-config-manager.ts | 3 + server/middlewares/validators/users.ts | 4 ++ server/models/user/user.ts | 13 ++-- server/tests/api/server/config-defaults.ts | 69 ++++++++++++++++++---- server/tests/api/server/follows.ts | 2 +- server/tests/api/users/users.ts | 22 +++++++ 14 files changed, 133 insertions(+), 22 deletions(-) create mode 100644 server/initializers/migrations/0675-p2p-enabled.ts (limited to 'server') diff --git a/server/controllers/api/users/index.ts b/server/controllers/api/users/index.ts index 11d3525e4..f3b4508d9 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.ts index 6bacdbbb6..1125771d4 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)[] = [ '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.email diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index f52c60b60..badf171d2 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.ts index e3e8c426e..a6ea6d888 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts @@ -78,6 +78,9 @@ const CONFIG = { COMMENTS_ENABLED: config.get('defaults.publish.comments_enabled'), PRIVACY: config.get('defaults.publish.privacy'), LICENCE: config.get('defaults.publish.licence') + }, + P2P: { + ENABLED: config.get('defaults.p2p.enabled') } }, diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 026c715c2..258ccdb51 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.ts index 75daeb5d8..19adaf177 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.ts new file mode 100644 index 000000000..b4f53381e --- /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 { + 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.ts index f2ef0a78a..754bee36d 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.ts index 8aea4cd6d..d759f85e1 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.ts index 7a6b2ce57..bc6007c6d 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.ts index b56f37e55..88c3ff528 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>> { 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>> { 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.ts index 3dff7bfb7..340d4b44b 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.ts index 748f4cd35..c132d99ea 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.ts index 6c41e7d56..f00cbab5a 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, -- cgit v1.2.3