From 8f581725651c4b2c213d75fc028e306bbf239d3e Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Fri, 27 Aug 2021 10:15:55 +0200 Subject: Allow accounts to skip account setup modal --- server/controllers/api/users/me.ts | 1 + server/helpers/custom-validators/users.ts | 9 ++------ server/initializers/constants.ts | 2 +- .../migrations/0665-no-account-warning-modal.ts | 27 ++++++++++++++++++++++ server/middlewares/validators/users.ts | 12 ++++++---- server/models/user/user.ts | 19 +++++++++++---- server/tests/api/check-params/users.ts | 27 +++++++++++----------- server/tests/api/users/users.ts | 5 +++- 8 files changed, 71 insertions(+), 31 deletions(-) create mode 100644 server/initializers/migrations/0665-no-account-warning-modal.ts (limited to 'server') diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index ac6faca9c..a98d79218 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -203,6 +203,7 @@ async function updateMe (req: express.Request, res: express.Response) { 'videoLanguages', 'theme', 'noInstanceConfigWarningModal', + 'noAccountSetupWarningModal', 'noWelcomeModal' ] diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index 5b21c3529..f52c60b60 100644 --- a/server/helpers/custom-validators/users.ts +++ b/server/helpers/custom-validators/users.ts @@ -81,11 +81,7 @@ function isUserAutoPlayNextVideoPlaylistValid (value: any) { return isBooleanValid(value) } -function isNoInstanceConfigWarningModal (value: any) { - return isBooleanValid(value) -} - -function isNoWelcomeModal (value: any) { +function isUserNoModal (value: any) { return isBooleanValid(value) } @@ -119,6 +115,5 @@ export { isUserAutoPlayNextVideoPlaylistValid, isUserDisplayNameValid, isUserDescriptionValid, - isNoInstanceConfigWarningModal, - isNoWelcomeModal + isUserNoModal } diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 8a1526ae8..2d8087f46 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -24,7 +24,7 @@ import { CONFIG, registerConfigChangedHandler } from './config' // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 660 +const LAST_MIGRATION_VERSION = 665 // --------------------------------------------------------------------------- diff --git a/server/initializers/migrations/0665-no-account-warning-modal.ts b/server/initializers/migrations/0665-no-account-warning-modal.ts new file mode 100644 index 000000000..6718aed85 --- /dev/null +++ b/server/initializers/migrations/0665-no-account-warning-modal.ts @@ -0,0 +1,27 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction + queryInterface: Sequelize.QueryInterface + sequelize: Sequelize.Sequelize + db: any +}): Promise { + { + const data = { + type: Sequelize.BOOLEAN, + allowNull: false, + defaultValue: false + } + + await utils.queryInterface.addColumn('user', 'noAccountSetupWarningModal', data) + } +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} diff --git a/server/middlewares/validators/users.ts b/server/middlewares/validators/users.ts index bc8607523..c6977fcd9 100644 --- a/server/middlewares/validators/users.ts +++ b/server/middlewares/validators/users.ts @@ -9,14 +9,13 @@ import { UserRegister } from '../../../shared/models/users/user-register.model' import { toBooleanOrNull, toIntOrNull } from '../../helpers/custom-validators/misc' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' import { - isNoInstanceConfigWarningModal, - isNoWelcomeModal, isUserAdminFlagsValid, isUserAutoPlayNextVideoValid, isUserAutoPlayVideoValid, isUserBlockedReasonValid, isUserDescriptionValid, isUserDisplayNameValid, + isUserNoModal, isUserNSFWPolicyValid, isUserPasswordValid, isUserPasswordValidOrEmpty, @@ -251,12 +250,17 @@ const usersUpdateMeValidator = [ body('theme') .optional() .custom(v => isThemeNameValid(v) && isThemeRegistered(v)).withMessage('Should have a valid theme'), + body('noInstanceConfigWarningModal') .optional() - .custom(v => isNoInstanceConfigWarningModal(v)).withMessage('Should have a valid noInstanceConfigWarningModal boolean'), + .custom(v => isUserNoModal(v)).withMessage('Should have a valid noInstanceConfigWarningModal boolean'), body('noWelcomeModal') .optional() - .custom(v => isNoWelcomeModal(v)).withMessage('Should have a valid noWelcomeModal boolean'), + .custom(v => isUserNoModal(v)).withMessage('Should have a valid noWelcomeModal boolean'), + body('noAccountSetupWarningModal') + .optional() + .custom(v => isUserNoModal(v)).withMessage('Should have a valid noAccountSetupWarningModal boolean'), + body('autoPlayNextVideo') .optional() .custom(v => isUserAutoPlayNextVideoValid(v)).withMessage('Should have a valid autoPlayNextVideo boolean'), diff --git a/server/models/user/user.ts b/server/models/user/user.ts index 069d7266e..ddce455a1 100644 --- a/server/models/user/user.ts +++ b/server/models/user/user.ts @@ -39,8 +39,6 @@ import { UserAdminFlag } from '../../../shared/models/users/user-flag.model' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' import { isThemeNameValid } from '../../helpers/custom-validators/plugins' import { - isNoInstanceConfigWarningModal, - isNoWelcomeModal, isUserAdminFlagsValid, isUserAutoPlayNextVideoPlaylistValid, isUserAutoPlayNextVideoValid, @@ -48,6 +46,7 @@ import { isUserBlockedReasonValid, isUserBlockedValid, isUserEmailVerifiedValid, + isUserNoModal, isUserNSFWPolicyValid, isUserPasswordValid, isUserRoleValid, @@ -349,7 +348,7 @@ export class UserModel extends Model>> { @Default(false) @Is( 'UserNoInstanceConfigWarningModal', - value => throwIfNotValid(value, isNoInstanceConfigWarningModal, 'no instance config warning modal') + value => throwIfNotValid(value, isUserNoModal, 'no instance config warning modal') ) @Column noInstanceConfigWarningModal: boolean @@ -357,12 +356,21 @@ export class UserModel extends Model>> { @AllowNull(false) @Default(false) @Is( - 'UserNoInstanceConfigWarningModal', - value => throwIfNotValid(value, isNoWelcomeModal, 'no welcome modal') + 'UserNoWelcomeModal', + value => throwIfNotValid(value, isUserNoModal, 'no welcome modal') ) @Column noWelcomeModal: boolean + @AllowNull(false) + @Default(false) + @Is( + 'UserNoAccountSetupWarningModal', + value => throwIfNotValid(value, isUserNoModal, 'no account setup warning modal') + ) + @Column + noAccountSetupWarningModal: boolean + @AllowNull(true) @Default(null) @Column @@ -920,6 +928,7 @@ export class UserModel extends Model>> { noInstanceConfigWarningModal: this.noInstanceConfigWarningModal, noWelcomeModal: this.noWelcomeModal, + noAccountSetupWarningModal: this.noAccountSetupWarningModal, blocked: this.blocked, blockedReason: this.blockedReason, diff --git a/server/tests/api/check-params/users.ts b/server/tests/api/check-params/users.ts index 9d8f933db..58b360f92 100644 --- a/server/tests/api/check-params/users.ts +++ b/server/tests/api/check-params/users.ts @@ -491,20 +491,20 @@ describe('Test users API validators', function () { await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields }) }) - it('Should fail with an invalid noInstanceConfigWarningModal attribute', async function () { - const fields = { - noInstanceConfigWarningModal: -1 - } - - await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields }) - }) + it('Should fail with invalid no modal attributes', async function () { + const keys = [ + 'noInstanceConfigWarningModal', + 'noAccountSetupWarningModal', + 'noWelcomeModal' + ] + + for (const key of keys) { + const fields = { + [key]: -1 + } - it('Should fail with an invalid noWelcomeModal attribute', async function () { - const fields = { - noWelcomeModal: -1 + await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields }) } - - await makePutBodyRequest({ url: server.url, path: path + 'me', token: userToken, fields }) }) it('Should succeed to change password with the correct params', async function () { @@ -516,7 +516,8 @@ describe('Test users API validators', function () { email: 'super_email@example.com', theme: 'default', noInstanceConfigWarningModal: true, - noWelcomeModal: true + noWelcomeModal: true, + noAccountSetupWarningModal: true } await makePutBodyRequest({ diff --git a/server/tests/api/users/users.ts b/server/tests/api/users/users.ts index 318ff832a..085d9d870 100644 --- a/server/tests/api/users/users.ts +++ b/server/tests/api/users/users.ts @@ -597,6 +597,7 @@ describe('Test users', function () { expect(user.account.description).to.equal('my super description updated') expect(user.noWelcomeModal).to.be.false expect(user.noInstanceConfigWarningModal).to.be.false + expect(user.noAccountSetupWarningModal).to.be.false }) it('Should be able to update my theme', async function () { @@ -612,12 +613,14 @@ describe('Test users', function () { await server.users.updateMe({ token: userToken, noInstanceConfigWarningModal: true, - noWelcomeModal: true + noWelcomeModal: true, + noAccountSetupWarningModal: true }) const user = await server.users.getMyInfo({ token: userToken }) expect(user.noWelcomeModal).to.be.true expect(user.noInstanceConfigWarningModal).to.be.true + expect(user.noAccountSetupWarningModal).to.be.true }) }) -- cgit v1.2.3