Add ability for admins to set default p2p policy

This commit is contained in:
Chocobozzz
2021-12-15 15:58:10 +01:00
parent c77fdc605b
commit a9bfa85d2c
59 changed files with 789 additions and 415 deletions

View File

@@ -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,

View File

@@ -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.email

View File

@@ -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,

View File

@@ -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')
}
},

View File

@@ -25,7 +25,7 @@ import { CONFIG, registerConfigChangedHandler } from './config'
// ---------------------------------------------------------------------------
const LAST_MIGRATION_VERSION = 670
const LAST_MIGRATION_VERSION = 675
// ---------------------------------------------------------------------------

View File

@@ -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
}

View File

@@ -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
}

View File

@@ -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,

View File

@@ -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
}
},

View File

@@ -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'),

View File

@@ -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,

View File

@@ -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 ])
})

View File

@@ -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' } })

View File

@@ -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,