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 --- server/controllers/api/users/me.ts | 1 + server/helpers/custom-validators/users.ts | 8 ++++++- server/initializers/constants.ts | 10 ++++++++- .../migrations/0280-webtorrent-policy-user.ts | 26 ++++++++++++++++++++++ server/models/account/user.ts | 12 ++++++++-- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 server/initializers/migrations/0280-webtorrent-policy-user.ts (limited to 'server') diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index 591ec6b25..f78294f17 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -327,6 +327,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr if (body.password !== undefined) user.password = body.password if (body.email !== undefined) user.email = body.email if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy + if (body.webTorrentPolicy !== undefined) user.webTorrentPolicy = body.webTorrentPolicy if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo await sequelizeTypescript.transaction(async t => { diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index 90fc74a48..2024d4a22 100644 --- a/server/helpers/custom-validators/users.ts +++ b/server/helpers/custom-validators/users.ts @@ -1,7 +1,7 @@ import 'express-validator' import * as validator from 'validator' import { UserRole } from '../../../shared' -import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers' +import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES, WEBTORRENT_POLICY_TYPES } from '../../initializers' import { exists, isFileValid, isBooleanValid } from './misc' import { values } from 'lodash' @@ -42,6 +42,11 @@ function isUserNSFWPolicyValid (value: any) { return exists(value) && nsfwPolicies.indexOf(value) !== -1 } +const webTorrentPolicies = values(WEBTORRENT_POLICY_TYPES) +function isUserWebTorrentPolicyValid (value: any) { + return exists(value) && webTorrentPolicies.indexOf(value) !== -1 +} + function isUserAutoPlayVideoValid (value: any) { return isBooleanValid(value) } @@ -78,6 +83,7 @@ export { isUserUsernameValid, isUserEmailVerifiedValid, isUserNSFWPolicyValid, + isUserWebTorrentPolicyValid, isUserAutoPlayVideoValid, isUserDisplayNameValid, isUserDescriptionValid, diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index e08fd75cd..f56763a16 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -7,6 +7,7 @@ import { VideoAbuseState, VideoImportState, VideoPrivacy, VideoTranscodingFPS } // Do not use barrels, remain constants as independent as possible import { buildPath, isTestInstance, parseDuration, parseBytes, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' +import { WebTorrentPolicyType } from '../../shared/models/users/user-webtorrent-policy.type' import { invert } from 'lodash' import { CronRepeatOptions, EveryRepeatOptions } from 'bull' import * as bytes from 'bytes' @@ -16,7 +17,7 @@ let config: IConfig = require('config') // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 275 +const LAST_MIGRATION_VERSION = 280 // --------------------------------------------------------------------------- @@ -546,6 +547,12 @@ const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = { DISPLAY: 'display' } +const WEBTORRENT_POLICY_TYPES: { [ id: string]: WebTorrentPolicyType } = { + ENABLE: 'enable', + DISABLE: 'disable', + DISABLE_ON_MOBILE: 'disable_on_mobile' +} + // --------------------------------------------------------------------------- // Express static paths (router) @@ -698,6 +705,7 @@ export { FEEDS, JOB_TTL, NSFW_POLICY_TYPES, + WEBTORRENT_POLICY_TYPES, TORRENT_MIMETYPE_EXT, STATIC_MAX_AGE, STATIC_PATHS, diff --git a/server/initializers/migrations/0280-webtorrent-policy-user.ts b/server/initializers/migrations/0280-webtorrent-policy-user.ts new file mode 100644 index 000000000..d24f6709e --- /dev/null +++ b/server/initializers/migrations/0280-webtorrent-policy-user.ts @@ -0,0 +1,26 @@ +import * as Sequelize from 'sequelize' +import { values } from 'lodash' +import { WEBTORRENT_POLICY_TYPES } from '../constants' + +async function up (utils: { + transaction: Sequelize.Transaction + queryInterface: Sequelize.QueryInterface + sequelize: Sequelize.Sequelize +}): Promise { + { + const data = { + type: Sequelize.ENUM(values(WEBTORRENT_POLICY_TYPES)), + allowNull: false, + defaultValue: 'enable' + } + + await utils.queryInterface.addColumn('user', 'webTorrentPolicy', data) + } + +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { up, down } diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 39654cfcf..5fe7d7e7d 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -31,7 +31,8 @@ import { isUserRoleValid, isUserUsernameValid, isUserVideoQuotaDailyValid, - isUserVideoQuotaValid + isUserVideoQuotaValid, + isUserWebTorrentPolicyValid } from '../../helpers/custom-validators/users' import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { OAuthTokenModel } from '../oauth/oauth-token' @@ -39,8 +40,9 @@ import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { AccountModel } from './account' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' +import { WebTorrentPolicyType } from '../../../shared/models/users/user-webtorrent-policy.type' import { values } from 'lodash' -import { NSFW_POLICY_TYPES } from '../../initializers' +import { NSFW_POLICY_TYPES, WEBTORRENT_POLICY_TYPES } from '../../initializers' import { clearCacheByUserId } from '../../lib/oauth-model' enum ScopeNames { @@ -107,6 +109,11 @@ export class UserModel extends Model { @Column(DataType.ENUM(values(NSFW_POLICY_TYPES))) nsfwPolicy: NSFWPolicyType + @AllowNull(false) + @Is('UserWebTorrentPolicy', value => throwIfNotValid(value, isUserWebTorrentPolicyValid, 'WebTorrent policy')) + @Column(DataType.ENUM(values(WEBTORRENT_POLICY_TYPES))) + webTorrentPolicy: WebTorrentPolicyType + @AllowNull(false) @Default(true) @Is('UserAutoPlayVideo', value => throwIfNotValid(value, isUserAutoPlayVideoValid, 'auto play video boolean')) @@ -355,6 +362,7 @@ export class UserModel extends Model { email: this.email, emailVerified: this.emailVerified, nsfwPolicy: this.nsfwPolicy, + webTorrentPolicy: this.webTorrentPolicy, autoPlayVideo: this.autoPlayVideo, role: this.role, roleLabel: USER_ROLE_LABELS[ this.role ], -- 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 --- server/controllers/api/users/me.ts | 2 +- server/helpers/custom-validators/users.ts | 9 ++++----- server/initializers/constants.ts | 8 -------- .../migrations/0280-webtorrent-policy-user.ts | 16 +++++++++------- server/models/account/user.ts | 13 ++++++------- 5 files changed, 20 insertions(+), 28 deletions(-) (limited to 'server') diff --git a/server/controllers/api/users/me.ts b/server/controllers/api/users/me.ts index f78294f17..3c511dc70 100644 --- a/server/controllers/api/users/me.ts +++ b/server/controllers/api/users/me.ts @@ -327,7 +327,7 @@ async function updateMe (req: express.Request, res: express.Response, next: expr if (body.password !== undefined) user.password = body.password if (body.email !== undefined) user.email = body.email if (body.nsfwPolicy !== undefined) user.nsfwPolicy = body.nsfwPolicy - if (body.webTorrentPolicy !== undefined) user.webTorrentPolicy = body.webTorrentPolicy + if (body.webTorrentEnabled !== undefined) user.webTorrentEnabled = body.webTorrentEnabled if (body.autoPlayVideo !== undefined) user.autoPlayVideo = body.autoPlayVideo await sequelizeTypescript.transaction(async t => { diff --git a/server/helpers/custom-validators/users.ts b/server/helpers/custom-validators/users.ts index 2024d4a22..1cb5e5b0f 100644 --- a/server/helpers/custom-validators/users.ts +++ b/server/helpers/custom-validators/users.ts @@ -1,7 +1,7 @@ import 'express-validator' import * as validator from 'validator' import { UserRole } from '../../../shared' -import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES, WEBTORRENT_POLICY_TYPES } from '../../initializers' +import { CONSTRAINTS_FIELDS, NSFW_POLICY_TYPES } from '../../initializers' import { exists, isFileValid, isBooleanValid } from './misc' import { values } from 'lodash' @@ -42,9 +42,8 @@ function isUserNSFWPolicyValid (value: any) { return exists(value) && nsfwPolicies.indexOf(value) !== -1 } -const webTorrentPolicies = values(WEBTORRENT_POLICY_TYPES) -function isUserWebTorrentPolicyValid (value: any) { - return exists(value) && webTorrentPolicies.indexOf(value) !== -1 +function isUserWebTorrentEnabledValid (value: any) { + return isBooleanValid(value) } function isUserAutoPlayVideoValid (value: any) { @@ -83,7 +82,7 @@ export { isUserUsernameValid, isUserEmailVerifiedValid, isUserNSFWPolicyValid, - isUserWebTorrentPolicyValid, + isUserWebTorrentEnabledValid, isUserAutoPlayVideoValid, isUserDisplayNameValid, isUserDescriptionValid, diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index f56763a16..c3e4fcede 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -7,7 +7,6 @@ import { VideoAbuseState, VideoImportState, VideoPrivacy, VideoTranscodingFPS } // Do not use barrels, remain constants as independent as possible import { buildPath, isTestInstance, parseDuration, parseBytes, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' -import { WebTorrentPolicyType } from '../../shared/models/users/user-webtorrent-policy.type' import { invert } from 'lodash' import { CronRepeatOptions, EveryRepeatOptions } from 'bull' import * as bytes from 'bytes' @@ -547,12 +546,6 @@ const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = { DISPLAY: 'display' } -const WEBTORRENT_POLICY_TYPES: { [ id: string]: WebTorrentPolicyType } = { - ENABLE: 'enable', - DISABLE: 'disable', - DISABLE_ON_MOBILE: 'disable_on_mobile' -} - // --------------------------------------------------------------------------- // Express static paths (router) @@ -705,7 +698,6 @@ export { FEEDS, JOB_TTL, NSFW_POLICY_TYPES, - WEBTORRENT_POLICY_TYPES, TORRENT_MIMETYPE_EXT, STATIC_MAX_AGE, STATIC_PATHS, diff --git a/server/initializers/migrations/0280-webtorrent-policy-user.ts b/server/initializers/migrations/0280-webtorrent-policy-user.ts index d24f6709e..e6488356a 100644 --- a/server/initializers/migrations/0280-webtorrent-policy-user.ts +++ b/server/initializers/migrations/0280-webtorrent-policy-user.ts @@ -1,6 +1,4 @@ import * as Sequelize from 'sequelize' -import { values } from 'lodash' -import { WEBTORRENT_POLICY_TYPES } from '../constants' async function up (utils: { transaction: Sequelize.Transaction @@ -9,18 +7,22 @@ async function up (utils: { }): Promise { { const data = { - type: Sequelize.ENUM(values(WEBTORRENT_POLICY_TYPES)), + type: Sequelize.BOOLEAN, allowNull: false, - defaultValue: 'enable' + defaultValue: true } - await utils.queryInterface.addColumn('user', 'webTorrentPolicy', data) + await utils.queryInterface.addColumn('user', 'webTorrentEnabled', data) } } -function down (options) { - throw new Error('Not implemented.') +async function down (utils: { + transaction: Sequelize.Transaction + queryInterface: Sequelize.QueryInterface + sequelize: Sequelize.Sequelize +}): Promise { + await utils.queryInterface.removeColumn('user', 'webTorrentEnabled') } export { up, down } diff --git a/server/models/account/user.ts b/server/models/account/user.ts index 5fe7d7e7d..4b4a562fa 100644 --- a/server/models/account/user.ts +++ b/server/models/account/user.ts @@ -32,7 +32,7 @@ import { isUserUsernameValid, isUserVideoQuotaDailyValid, isUserVideoQuotaValid, - isUserWebTorrentPolicyValid + isUserWebTorrentEnabledValid } from '../../helpers/custom-validators/users' import { comparePassword, cryptPassword } from '../../helpers/peertube-crypto' import { OAuthTokenModel } from '../oauth/oauth-token' @@ -40,9 +40,8 @@ import { getSort, throwIfNotValid } from '../utils' import { VideoChannelModel } from '../video/video-channel' import { AccountModel } from './account' import { NSFWPolicyType } from '../../../shared/models/videos/nsfw-policy.type' -import { WebTorrentPolicyType } from '../../../shared/models/users/user-webtorrent-policy.type' import { values } from 'lodash' -import { NSFW_POLICY_TYPES, WEBTORRENT_POLICY_TYPES } from '../../initializers' +import { NSFW_POLICY_TYPES } from '../../initializers' import { clearCacheByUserId } from '../../lib/oauth-model' enum ScopeNames { @@ -110,9 +109,9 @@ export class UserModel extends Model { nsfwPolicy: NSFWPolicyType @AllowNull(false) - @Is('UserWebTorrentPolicy', value => throwIfNotValid(value, isUserWebTorrentPolicyValid, 'WebTorrent policy')) - @Column(DataType.ENUM(values(WEBTORRENT_POLICY_TYPES))) - webTorrentPolicy: WebTorrentPolicyType + @Is('UserWebTorrentEnabled', value => throwIfNotValid(value, isUserWebTorrentEnabledValid, 'WebTorrent enabled')) + @Column + webTorrentEnabled: boolean @AllowNull(false) @Default(true) @@ -362,7 +361,7 @@ export class UserModel extends Model { email: this.email, emailVerified: this.emailVerified, nsfwPolicy: this.nsfwPolicy, - webTorrentPolicy: this.webTorrentPolicy, + webTorrentEnabled: this.webTorrentEnabled, autoPlayVideo: this.autoPlayVideo, role: this.role, roleLabel: USER_ROLE_LABELS[ this.role ], -- cgit v1.2.3