aboutsummaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2021-12-15 15:58:10 +0100
committerChocobozzz <me@florianbigard.com>2021-12-16 10:08:55 +0100
commita9bfa85d2cdf13670aaced740da5b493fbeddfce (patch)
tree3781c9218d4cc7786b6589365c0efbed2151703d /server
parentc77fdc605b3ccc1ab6890f889d8200fbe9372949 (diff)
downloadPeerTube-a9bfa85d2cdf13670aaced740da5b493fbeddfce.tar.gz
PeerTube-a9bfa85d2cdf13670aaced740da5b493fbeddfce.tar.zst
PeerTube-a9bfa85d2cdf13670aaced740da5b493fbeddfce.zip
Add ability for admins to set default p2p policy
Diffstat (limited to 'server')
-rw-r--r--server/controllers/api/users/index.ts2
-rw-r--r--server/controllers/api/users/me.ts8
-rw-r--r--server/helpers/custom-validators/users.ts4
-rw-r--r--server/initializers/config.ts3
-rw-r--r--server/initializers/constants.ts2
-rw-r--r--server/initializers/installer.ts1
-rw-r--r--server/initializers/migrations/0675-p2p-enabled.ts21
-rw-r--r--server/lib/auth/oauth-model.ts1
-rw-r--r--server/lib/server-config-manager.ts3
-rw-r--r--server/middlewares/validators/users.ts4
-rw-r--r--server/models/user/user.ts13
-rw-r--r--server/tests/api/server/config-defaults.ts69
-rw-r--r--server/tests/api/server/follows.ts2
-rw-r--r--server/tests/api/users/users.ts22
14 files changed, 133 insertions, 22 deletions
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) {
183 password: body.password, 183 password: body.password,
184 email: body.email, 184 email: body.email,
185 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, 185 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
186 p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,
186 autoPlayVideo: true, 187 autoPlayVideo: true,
187 role: body.role, 188 role: body.role,
188 videoQuota: body.videoQuota, 189 videoQuota: body.videoQuota,
@@ -232,6 +233,7 @@ async function registerUser (req: express.Request, res: express.Response) {
232 password: body.password, 233 password: body.password,
233 email: body.email, 234 email: body.email,
234 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, 235 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
236 p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,
235 autoPlayVideo: true, 237 autoPlayVideo: true,
236 role: UserRole.USER, 238 role: UserRole.USER,
237 videoQuota: CONFIG.USER.VIDEO_QUOTA, 239 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) {
197 const keysToUpdate: (keyof UserUpdateMe & keyof AttributesOnly<UserModel>)[] = [ 197 const keysToUpdate: (keyof UserUpdateMe & keyof AttributesOnly<UserModel>)[] = [
198 'password', 198 'password',
199 'nsfwPolicy', 199 'nsfwPolicy',
200 'webTorrentEnabled', 200 'p2pEnabled',
201 'autoPlayVideo', 201 'autoPlayVideo',
202 'autoPlayNextVideo', 202 'autoPlayNextVideo',
203 'autoPlayNextVideoPlaylist', 203 'autoPlayNextVideoPlaylist',
@@ -213,6 +213,12 @@ async function updateMe (req: express.Request, res: express.Response) {
213 if (body[key] !== undefined) user.set(key, body[key]) 213 if (body[key] !== undefined) user.set(key, body[key])
214 } 214 }
215 215
216 if (body.p2pEnabled !== undefined) {
217 user.set('p2pEnabled', body.p2pEnabled)
218 } else if (body.webTorrentEnabled !== undefined) { // FIXME: deprecated in 4.1
219 user.set('p2pEnabled', body.webTorrentEnabled)
220 }
221
216 if (body.email !== undefined) { 222 if (body.email !== undefined) {
217 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) { 223 if (CONFIG.SIGNUP.REQUIRES_EMAIL_VERIFICATION) {
218 user.pendingEmail = body.email 224 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) {
49 return exists(value) && nsfwPolicies.includes(value) 49 return exists(value) && nsfwPolicies.includes(value)
50} 50}
51 51
52function isUserWebTorrentEnabledValid (value: any) { 52function isUserP2PEnabledValid (value: any) {
53 return isBooleanValid(value) 53 return isBooleanValid(value)
54} 54}
55 55
@@ -109,7 +109,7 @@ export {
109 isUserAdminFlagsValid, 109 isUserAdminFlagsValid,
110 isUserEmailVerifiedValid, 110 isUserEmailVerifiedValid,
111 isUserNSFWPolicyValid, 111 isUserNSFWPolicyValid,
112 isUserWebTorrentEnabledValid, 112 isUserP2PEnabledValid,
113 isUserAutoPlayVideoValid, 113 isUserAutoPlayVideoValid,
114 isUserAutoPlayNextVideoValid, 114 isUserAutoPlayNextVideoValid,
115 isUserAutoPlayNextVideoPlaylistValid, 115 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 = {
78 COMMENTS_ENABLED: config.get<boolean>('defaults.publish.comments_enabled'), 78 COMMENTS_ENABLED: config.get<boolean>('defaults.publish.comments_enabled'),
79 PRIVACY: config.get<VideoPrivacy>('defaults.publish.privacy'), 79 PRIVACY: config.get<VideoPrivacy>('defaults.publish.privacy'),
80 LICENCE: config.get<number>('defaults.publish.licence') 80 LICENCE: config.get<number>('defaults.publish.licence')
81 },
82 P2P: {
83 ENABLED: config.get<boolean>('defaults.p2p.enabled')
81 } 84 }
82 }, 85 },
83 86
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'
25 25
26// --------------------------------------------------------------------------- 26// ---------------------------------------------------------------------------
27 27
28const LAST_MIGRATION_VERSION = 670 28const LAST_MIGRATION_VERSION = 675
29 29
30// --------------------------------------------------------------------------- 30// ---------------------------------------------------------------------------
31 31
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 () {
144 role, 144 role,
145 verified: true, 145 verified: true,
146 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, 146 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
147 p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,
147 videoQuota: -1, 148 videoQuota: -1,
148 videoQuotaDaily: -1 149 videoQuotaDaily: -1
149 } 150 }
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 @@
1import * as Sequelize from 'sequelize'
2
3async function up (utils: {
4 transaction: Sequelize.Transaction
5 queryInterface: Sequelize.QueryInterface
6 sequelize: Sequelize.Sequelize
7 db: any
8}): Promise<void> {
9 await utils.queryInterface.renameColumn('user', 'webTorrentEnabled', 'p2pEnabled')
10
11 await utils.sequelize.query('ALTER TABLE "user" ALTER COLUMN "p2pEnabled" DROP DEFAULT')
12}
13
14function down (options) {
15 throw new Error('Not implemented.')
16}
17
18export {
19 up,
20 down
21}
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: {
226 password: null, 226 password: null,
227 email: options.email, 227 email: options.email,
228 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, 228 nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY,
229 p2pEnabled: CONFIG.DEFAULTS.P2P.ENABLED,
229 autoPlayVideo: true, 230 autoPlayVideo: true,
230 role: options.role, 231 role: options.role,
231 videoQuota: CONFIG.USER.VIDEO_QUOTA, 232 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 {
61 commentsEnabled: CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED, 61 commentsEnabled: CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED,
62 privacy: CONFIG.DEFAULTS.PUBLISH.PRIVACY, 62 privacy: CONFIG.DEFAULTS.PUBLISH.PRIVACY,
63 licence: CONFIG.DEFAULTS.PUBLISH.LICENCE 63 licence: CONFIG.DEFAULTS.PUBLISH.LICENCE
64 },
65 p2p: {
66 enabled: CONFIG.DEFAULTS.P2P.ENABLED
64 } 67 }
65 }, 68 },
66 69
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 {
15 isUserDisplayNameValid, 15 isUserDisplayNameValid,
16 isUserNoModal, 16 isUserNoModal,
17 isUserNSFWPolicyValid, 17 isUserNSFWPolicyValid,
18 isUserP2PEnabledValid,
18 isUserPasswordValid, 19 isUserPasswordValid,
19 isUserPasswordValidOrEmpty, 20 isUserPasswordValidOrEmpty,
20 isUserRoleValid, 21 isUserRoleValid,
@@ -239,6 +240,9 @@ const usersUpdateMeValidator = [
239 body('autoPlayVideo') 240 body('autoPlayVideo')
240 .optional() 241 .optional()
241 .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'), 242 .custom(isUserAutoPlayVideoValid).withMessage('Should have a valid automatically plays video attribute'),
243 body('p2pEnabled')
244 .optional()
245 .custom(isUserP2PEnabledValid).withMessage('Should have a valid p2p enabled boolean'),
242 body('videoLanguages') 246 body('videoLanguages')
243 .optional() 247 .optional()
244 .custom(isUserVideoLanguages).withMessage('Should have a valid video languages attribute'), 248 .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 {
55 isUserVideoQuotaDailyValid, 55 isUserVideoQuotaDailyValid,
56 isUserVideoQuotaValid, 56 isUserVideoQuotaValid,
57 isUserVideosHistoryEnabledValid, 57 isUserVideosHistoryEnabledValid,
58 isUserWebTorrentEnabledValid 58 isUserP2PEnabledValid
59} from '../../helpers/custom-validators/users' 59} from '../../helpers/custom-validators/users'
60import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' 60import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto'
61import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants' 61import { DEFAULT_USER_THEME_NAME, NSFW_POLICY_TYPES } from '../../initializers/constants'
@@ -267,10 +267,9 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
267 nsfwPolicy: NSFWPolicyType 267 nsfwPolicy: NSFWPolicyType
268 268
269 @AllowNull(false) 269 @AllowNull(false)
270 @Default(true) 270 @Is('p2pEnabled', value => throwIfNotValid(value, isUserP2PEnabledValid, 'P2P enabled'))
271 @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled'))
272 @Column 271 @Column
273 webTorrentEnabled: boolean 272 p2pEnabled: boolean
274 273
275 @AllowNull(false) 274 @AllowNull(false)
276 @Default(true) 275 @Default(true)
@@ -892,7 +891,11 @@ export class UserModel extends Model<Partial<AttributesOnly<UserModel>>> {
892 emailVerified: this.emailVerified, 891 emailVerified: this.emailVerified,
893 892
894 nsfwPolicy: this.nsfwPolicy, 893 nsfwPolicy: this.nsfwPolicy,
895 webTorrentEnabled: this.webTorrentEnabled, 894
895 // FIXME: deprecated in 4.1
896 webTorrentEnabled: this.p2pEnabled,
897 p2pEnabled: this.p2pEnabled,
898
896 videosHistoryEnabled: this.videosHistoryEnabled, 899 videosHistoryEnabled: this.videosHistoryEnabled,
897 autoPlayVideo: this.autoPlayVideo, 900 autoPlayVideo: this.autoPlayVideo,
898 autoPlayNextVideo: this.autoPlayNextVideo, 901 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 () {
21 before(async function () { 21 before(async function () {
22 this.timeout(30000) 22 this.timeout(30000)
23 23
24 const overrideConfig = { 24 server = await createSingleServer(1)
25 defaults: {
26 publish: {
27 comments_enabled: false,
28 download_enabled: false,
29 privacy: VideoPrivacy.INTERNAL,
30 licence: 4
31 }
32 }
33 }
34
35 server = await createSingleServer(1, overrideConfig)
36 await setAccessTokensToServers([ server ]) 25 await setAccessTokensToServers([ server ])
37 await setDefaultVideoChannel([ server ]) 26 await setDefaultVideoChannel([ server ])
38 27
@@ -40,6 +29,23 @@ describe('Test config defaults', function () {
40 }) 29 })
41 30
42 describe('Default publish values', function () { 31 describe('Default publish values', function () {
32
33 before(async function () {
34 const overrideConfig = {
35 defaults: {
36 publish: {
37 comments_enabled: false,
38 download_enabled: false,
39 privacy: VideoPrivacy.INTERNAL,
40 licence: 4
41 }
42 }
43 }
44
45 await server.kill()
46 await server.run(overrideConfig)
47 })
48
43 const attributes = { 49 const attributes = {
44 name: 'video', 50 name: 'video',
45 downloadEnabled: undefined, 51 downloadEnabled: undefined,
@@ -117,6 +123,45 @@ describe('Test config defaults', function () {
117 }) 123 })
118 }) 124 })
119 125
126 describe('Default P2P values', function () {
127
128 before(async function () {
129 const overrideConfig = {
130 defaults: {
131 p2p: {
132 enabled: false
133 }
134 }
135 }
136
137 await server.kill()
138 await server.run(overrideConfig)
139 })
140
141 it('Should not have P2P enabled', async function () {
142 const config = await server.config.getConfig()
143
144 expect(config.defaults.p2p.enabled).to.be.false
145 })
146
147 it('Should create a user with this default setting', async function () {
148 await server.users.create({ username: 'user_p2p_1' })
149 const userToken = await server.login.getAccessToken('user_p2p_1')
150
151 const { p2pEnabled } = await server.users.getMyInfo({ token: userToken })
152 expect(p2pEnabled).to.be.false
153 })
154
155 it('Should register a user with this default setting', async function () {
156 await server.users.register({ username: 'user_p2p_2' })
157
158 const userToken = await server.login.getAccessToken('user_p2p_2')
159
160 const { p2pEnabled } = await server.users.getMyInfo({ token: userToken })
161 expect(p2pEnabled).to.be.false
162 })
163 })
164
120 after(async function () { 165 after(async function () {
121 await cleanupTests([ server ]) 166 await cleanupTests([ server ])
122 }) 167 })
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 () {
292 }) 292 })
293 293
294 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () { 294 it('Should upload a video on server 2 and 3 and propagate only the video of server 2', async function () {
295 this.timeout(60000) 295 this.timeout(120000)
296 296
297 await servers[1].videos.upload({ attributes: { name: 'server2' } }) 297 await servers[1].videos.upload({ attributes: { name: 'server2' } })
298 await servers[2].videos.upload({ attributes: { name: 'server3' } }) 298 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 () {
559 expect(user.autoPlayNextVideo).to.be.true 559 expect(user.autoPlayNextVideo).to.be.true
560 }) 560 })
561 561
562 it('Should be able to change the p2p attribute', async function () {
563 {
564 await server.users.updateMe({
565 token: userToken,
566 webTorrentEnabled: false
567 })
568
569 const user = await server.users.getMyInfo({ token: userToken })
570 expect(user.p2pEnabled).to.be.false
571 }
572
573 {
574 await server.users.updateMe({
575 token: userToken,
576 p2pEnabled: true
577 })
578
579 const user = await server.users.getMyInfo({ token: userToken })
580 expect(user.p2pEnabled).to.be.true
581 }
582 })
583
562 it('Should be able to change the email attribute', async function () { 584 it('Should be able to change the email attribute', async function () {
563 await server.users.updateMe({ 585 await server.users.updateMe({
564 token: userToken, 586 token: userToken,