From 3cf68b869decf07ff7435fe1436d4f3134df1bf4 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Tue, 14 Dec 2021 17:17:01 +0100 Subject: Ability for admins to set default upload values --- server/controllers/api/videos/import.ts | 6 +- server/initializers/checker-before-init.ts | 1 + server/initializers/config.ts | 11 ++- server/lib/server-config-manager.ts | 9 +++ server/lib/video.ts | 7 +- server/tests/api/server/config-defaults.ts | 116 +++++++++++++++++++++++++++++ server/tests/api/server/index.ts | 2 + 7 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 server/tests/api/server/config-defaults.ts (limited to 'server') diff --git a/server/controllers/api/videos/import.ts b/server/controllers/api/videos/import.ts index 52864bdfd..3c445efce 100644 --- a/server/controllers/api/videos/import.ts +++ b/server/controllers/api/videos/import.ts @@ -216,10 +216,10 @@ async function buildVideo (channelId: number, body: VideoImportCreate, importDat name: body.name || importData.name || 'Unknown name', remote: false, category: body.category || importData.category, - licence: body.licence || importData.licence, + licence: body.licence ?? importData.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE, language: body.language || importData.language, - commentsEnabled: body.commentsEnabled !== false, // If the value is not "false", the default is "true" - downloadEnabled: body.downloadEnabled !== false, + commentsEnabled: body.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED, + downloadEnabled: body.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED, waitTranscoding: body.waitTranscoding || false, state: VideoState.TO_IMPORT, nsfw: body.nsfw || importData.nsfw || false, diff --git a/server/initializers/checker-before-init.ts b/server/initializers/checker-before-init.ts index c85c389cd..2c24e20c8 100644 --- a/server/initializers/checker-before-init.ts +++ b/server/initializers/checker-before-init.ts @@ -34,6 +34,7 @@ function checkMissedConfig () { 'import.videos.http.enabled', 'import.videos.torrent.enabled', 'import.videos.concurrency', 'auto_blacklist.videos.of_users.enabled', 'trending.videos.interval_days', 'client.videos.miniature.prefer_author_display_name', 'client.menu.login.redirect_on_single_external_auth', + 'defaults.publish.download_enabled', 'defaults.publish.comments_enabled', 'defaults.publish.privacy', 'defaults.publish.licence', 'instance.name', 'instance.short_description', 'instance.description', 'instance.terms', 'instance.default_client_route', 'instance.is_nsfw', 'instance.default_nsfw_policy', 'instance.robots', 'instance.securitytxt', 'services.twitter.username', 'services.twitter.whitelisted', diff --git a/server/initializers/config.ts b/server/initializers/config.ts index eb848be6b..70179d25c 100644 --- a/server/initializers/config.ts +++ b/server/initializers/config.ts @@ -4,7 +4,7 @@ import { dirname, join } from 'path' import { decacheModule } from '@server/helpers/decache' import { VideoRedundancyConfigFilter } from '@shared/models/redundancy/video-redundancy-config-filter.type' import { BroadcastMessageLevel } from '@shared/models/server' -import { VideosRedundancyStrategy } from '../../shared/models' +import { VideoPrivacy, VideosRedundancyStrategy } from '../../shared/models' import { NSFWPolicyType } from '../../shared/models/videos/nsfw-policy.type' import { buildPath, parseBytes, parseDurationToMs, root } from '../helpers/core-utils' @@ -71,6 +71,15 @@ const CONFIG = { } }, + DEFAULTS: { + PUBLISH: { + DOWNLOAD_ENABLED: config.get('defaults.publish.download_enabled'), + COMMENTS_ENABLED: config.get('defaults.publish.comments_enabled'), + PRIVACY: config.get('defaults.publish.privacy'), + LICENCE: config.get('defaults.publish.licence') + } + }, + STORAGE: { TMP_DIR: buildPath(config.get('storage.tmp')), BIN_DIR: buildPath(config.get('storage.bin')), diff --git a/server/lib/server-config-manager.ts b/server/lib/server-config-manager.ts index 6aa459f82..8aea4cd6d 100644 --- a/server/lib/server-config-manager.ts +++ b/server/lib/server-config-manager.ts @@ -55,6 +55,15 @@ class ServerConfigManager { } }, + defaults: { + publish: { + downloadEnabled: CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED, + commentsEnabled: CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED, + privacy: CONFIG.DEFAULTS.PUBLISH.PRIVACY, + licence: CONFIG.DEFAULTS.PUBLISH.LICENCE + } + }, + webadmin: { configuration: { edition: { diff --git a/server/lib/video.ts b/server/lib/video.ts index 1cfe4f27c..e5af028ea 100644 --- a/server/lib/video.ts +++ b/server/lib/video.ts @@ -9,16 +9,17 @@ import { MThumbnail, MUserId, MVideoFile, MVideoTag, MVideoThumbnail, MVideoUUID import { ThumbnailType, VideoCreate, VideoPrivacy, VideoTranscodingPayload } from '@shared/models' import { CreateJobOptions, JobQueue } from './job-queue/job-queue' import { updateVideoMiniatureFromExisting } from './thumbnail' +import { CONFIG } from '@server/initializers/config' function buildLocalVideoFromReq (videoInfo: VideoCreate, channelId: number): FilteredModelAttributes { return { name: videoInfo.name, remote: false, category: videoInfo.category, - licence: videoInfo.licence, + licence: videoInfo.licence ?? CONFIG.DEFAULTS.PUBLISH.LICENCE, language: videoInfo.language, - commentsEnabled: videoInfo.commentsEnabled !== false, // If the value is not "false", the default is "true" - downloadEnabled: videoInfo.downloadEnabled !== false, + commentsEnabled: videoInfo.commentsEnabled ?? CONFIG.DEFAULTS.PUBLISH.COMMENTS_ENABLED, + downloadEnabled: videoInfo.downloadEnabled ?? CONFIG.DEFAULTS.PUBLISH.DOWNLOAD_ENABLED, waitTranscoding: videoInfo.waitTranscoding || false, nsfw: videoInfo.nsfw || false, description: videoInfo.description, diff --git a/server/tests/api/server/config-defaults.ts b/server/tests/api/server/config-defaults.ts new file mode 100644 index 000000000..2433d3119 --- /dev/null +++ b/server/tests/api/server/config-defaults.ts @@ -0,0 +1,116 @@ +/* eslint-disable @typescript-eslint/no-unused-expressions,@typescript-eslint/require-await */ + +import 'mocha' +import * as chai from 'chai' +import { cleanupTests, createSingleServer, FIXTURE_URLS, PeerTubeServer, setAccessTokensToServers, setDefaultVideoChannel } from '@shared/extra-utils' +import { VideoDetails, VideoPrivacy } from '@shared/models' + +const expect = chai.expect + +describe('Test config defaults', function () { + let server: PeerTubeServer + let channelId: number + + 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) + await setAccessTokensToServers([ server ]) + await setDefaultVideoChannel([ server ]) + + channelId = server.store.channel.id + }) + + describe('Default publish values', function () { + const attributes = { + name: 'video', + downloadEnabled: undefined, + commentsEnabled: undefined, + licence: undefined, + privacy: VideoPrivacy.PUBLIC // Privacy is mandatory for server + } + + function checkVideo (video: VideoDetails) { + expect(video.downloadEnabled).to.be.false + expect(video.commentsEnabled).to.be.false + expect(video.licence.id).to.equal(4) + } + + before(async function () { + await server.config.disableTranscoding() + await server.config.enableImports() + await server.config.enableLive({ allowReplay: false, transcoding: false }) + }) + + it('Should have the correct server configuration', async function () { + const config = await server.config.getConfig() + + expect(config.defaults.publish.commentsEnabled).to.be.false + expect(config.defaults.publish.downloadEnabled).to.be.false + expect(config.defaults.publish.licence).to.equal(4) + expect(config.defaults.publish.privacy).to.equal(VideoPrivacy.INTERNAL) + }) + + it('Should respect default values when uploading a video', async function () { + for (const mode of [ 'legacy' as 'legacy', 'resumable' as 'resumable' ]) { + const { id } = await server.videos.upload({ attributes, mode }) + + const video = await server.videos.get({ id }) + checkVideo(video) + } + }) + + it('Should respect default values when importing a video using URL', async function () { + const { video: { id } } = await server.imports.importVideo({ + attributes: { + ...attributes, + channelId, + targetUrl: FIXTURE_URLS.goodVideo + } + }) + + const video = await server.videos.get({ id }) + checkVideo(video) + }) + + it('Should respect default values when importing a video using magnet URI', async function () { + const { video: { id } } = await server.imports.importVideo({ + attributes: { + ...attributes, + channelId, + magnetUri: FIXTURE_URLS.magnet + } + }) + + const video = await server.videos.get({ id }) + checkVideo(video) + }) + + it('Should respect default values when creating a live', async function () { + const { id } = await server.live.create({ + fields: { + ...attributes, + channelId + } + }) + + const video = await server.videos.get({ id }) + checkVideo(video) + }) + }) + + after(async function () { + await cleanupTests([ server ]) + }) +}) diff --git a/server/tests/api/server/index.ts b/server/tests/api/server/index.ts index 8136fc3c6..45be107ce 100644 --- a/server/tests/api/server/index.ts +++ b/server/tests/api/server/index.ts @@ -1,4 +1,6 @@ import './auto-follows' +import './bulk' +import './config-defaults' import './config' import './contact-form' import './email' -- cgit v1.2.3