From 0883b3245bf0deb9106c4041e9afbd3521b79280 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Thu, 19 Apr 2018 11:01:34 +0200 Subject: Add ability to choose what policy we have for NSFW videos There is a global instance setting and a per user setting --- server/initializers/checker.ts | 11 +++--- server/initializers/constants.ts | 11 +++++- server/initializers/installer.ts | 1 + .../migrations/0205-user-nsfw-policy.ts | 46 ++++++++++++++++++++++ 4 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 server/initializers/migrations/0205-user-nsfw-policy.ts (limited to 'server/initializers') diff --git a/server/initializers/checker.ts b/server/initializers/checker.ts index 71f303963..739f623c6 100644 --- a/server/initializers/checker.ts +++ b/server/initializers/checker.ts @@ -5,12 +5,12 @@ import { ApplicationModel } from '../models/application/application' import { OAuthClientModel } from '../models/oauth/oauth-client' // Some checks on configuration files +// Return an error message, or null if everything is okay function checkConfig () { - if (config.has('webserver.host')) { - let errorMessage = '`host` config key was renamed to `hostname` but it seems you still have a `host` key in your configuration files!' - errorMessage += ' Please ensure to rename your `host` configuration to `hostname`.' + const defaultNSFWPolicy = config.get('instance.default_nsfw_policy') - return errorMessage + if ([ 'do_not_list', 'blur', 'display' ].indexOf(defaultNSFWPolicy) === -1) { + return 'NSFW policy setting should be "do_not_list" or "blur" or "display" instead of ' + defaultNSFWPolicy } return null @@ -28,7 +28,8 @@ function checkMissedConfig () { 'log.level', 'user.video_quota', 'cache.previews.size', 'admin.email', 'signup.enabled', 'signup.limit', 'transcoding.enabled', 'transcoding.threads', - 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route' + 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', + 'instance.default_nsfw_policy' ] const miss: string[] = [] diff --git a/server/initializers/constants.ts b/server/initializers/constants.ts index 5ee13389d..d1915586a 100644 --- a/server/initializers/constants.ts +++ b/server/initializers/constants.ts @@ -6,13 +6,14 @@ import { FollowState } from '../../shared/models/actors' import { VideoPrivacy } from '../../shared/models/videos' // Do not use barrels, remain constants as independent as possible import { buildPath, isTestInstance, root, sanitizeHost, sanitizeUrl } from '../helpers/core-utils' +import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' // Use a variable to reload the configuration if we need let config: IConfig = require('config') // --------------------------------------------------------------------------- -const LAST_MIGRATION_VERSION = 200 +const LAST_MIGRATION_VERSION = 205 // --------------------------------------------------------------------------- @@ -167,6 +168,7 @@ const CONFIG = { get DESCRIPTION () { return config.get('instance.description') }, get TERMS () { return config.get('instance.terms') }, get DEFAULT_CLIENT_ROUTE () { return config.get('instance.default_client_route') }, + get DEFAULT_NSFW_POLICY () { return config.get('instance.default_nsfw_policy') }, CUSTOMIZATIONS: { get JAVASCRIPT () { return config.get('instance.customizations.javascript') }, get CSS () { return config.get('instance.customizations.css') } @@ -378,6 +380,12 @@ const BCRYPT_SALT_SIZE = 10 const USER_PASSWORD_RESET_LIFETIME = 60000 * 5 // 5 minutes +const NSFW_POLICY_TYPES: { [ id: string]: NSFWPolicyType } = { + DO_NOT_LIST: 'do_not_list', + BLUR: 'blur', + DISPLAY: 'display' +} + // --------------------------------------------------------------------------- // Express static paths (router) @@ -474,6 +482,7 @@ export { PRIVATE_RSA_KEY_SIZE, SORTABLE_COLUMNS, FEEDS, + NSFW_POLICY_TYPES, STATIC_MAX_AGE, STATIC_PATHS, ACTIVITY_PUB, diff --git a/server/initializers/installer.ts b/server/initializers/installer.ts index 09c6d5473..b0084b368 100644 --- a/server/initializers/installer.ts +++ b/server/initializers/installer.ts @@ -120,6 +120,7 @@ async function createOAuthAdminIfNotExist () { email, password, role, + nsfwPolicy: CONFIG.INSTANCE.DEFAULT_NSFW_POLICY, videoQuota: -1 } const user = new UserModel(userData) diff --git a/server/initializers/migrations/0205-user-nsfw-policy.ts b/server/initializers/migrations/0205-user-nsfw-policy.ts new file mode 100644 index 000000000..d0f6e8962 --- /dev/null +++ b/server/initializers/migrations/0205-user-nsfw-policy.ts @@ -0,0 +1,46 @@ +import * as Sequelize from 'sequelize' + +async function up (utils: { + transaction: Sequelize.Transaction, + queryInterface: Sequelize.QueryInterface, + sequelize: Sequelize.Sequelize +}): Promise { + + { + const data = { + type: Sequelize.ENUM('do_not_list', 'blur', 'display'), + allowNull: true, + defaultValue: null + } + await utils.queryInterface.addColumn('user', 'nsfwPolicy', data) + } + + { + const query = 'UPDATE "user" SET "nsfwPolicy" = \'do_not_list\'' + await utils.sequelize.query(query) + } + + { + const query = 'UPDATE "user" SET "nsfwPolicy" = \'display\' WHERE "displayNSFW" = true' + await utils.sequelize.query(query) + } + + { + const query = 'ALTER TABLE "user" ALTER COLUMN "nsfwPolicy" SET NOT NULL' + await utils.sequelize.query(query) + } + + { + await utils.queryInterface.removeColumn('user', 'displayNSFW') + } + +} + +function down (options) { + throw new Error('Not implemented.') +} + +export { + up, + down +} -- cgit v1.2.3