"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureConfigIsEditable = exports.customConfigUpdateValidator = void 0;
const express_validator_1 = require("express-validator");
const misc_1 = require("../../helpers/custom-validators/misc");
const config_1 = require("../../initializers/config");
const plugins_1 = require("../../helpers/custom-validators/plugins");
const users_1 = require("../../helpers/custom-validators/users");
const theme_utils_1 = require("../../lib/plugins/theme-utils");
const shared_1 = require("./shared");
const customConfigUpdateValidator = [
    (0, express_validator_1.body)('instance.name').exists(),
    (0, express_validator_1.body)('instance.shortDescription').exists(),
    (0, express_validator_1.body)('instance.description').exists(),
    (0, express_validator_1.body)('instance.terms').exists(),
    (0, express_validator_1.body)('instance.defaultNSFWPolicy').custom(users_1.isUserNSFWPolicyValid),
    (0, express_validator_1.body)('instance.defaultClientRoute').exists(),
    (0, express_validator_1.body)('instance.customizations.css').exists(),
    (0, express_validator_1.body)('instance.customizations.javascript').exists(),
    (0, express_validator_1.body)('services.twitter.username').exists(),
    (0, express_validator_1.body)('services.twitter.whitelisted').isBoolean(),
    (0, express_validator_1.body)('cache.previews.size').isInt(),
    (0, express_validator_1.body)('cache.captions.size').isInt(),
    (0, express_validator_1.body)('cache.torrents.size').isInt(),
    (0, express_validator_1.body)('signup.enabled').isBoolean(),
    (0, express_validator_1.body)('signup.limit').isInt(),
    (0, express_validator_1.body)('signup.requiresEmailVerification').isBoolean(),
    (0, express_validator_1.body)('signup.minimumAge').isInt(),
    (0, express_validator_1.body)('admin.email').isEmail(),
    (0, express_validator_1.body)('contactForm.enabled').isBoolean(),
    (0, express_validator_1.body)('user.videoQuota').custom(users_1.isUserVideoQuotaValid),
    (0, express_validator_1.body)('user.videoQuotaDaily').custom(users_1.isUserVideoQuotaDailyValid),
    (0, express_validator_1.body)('videoChannels.maxPerUser').isInt(),
    (0, express_validator_1.body)('transcoding.enabled').isBoolean(),
    (0, express_validator_1.body)('transcoding.allowAdditionalExtensions').isBoolean(),
    (0, express_validator_1.body)('transcoding.threads').isInt(),
    (0, express_validator_1.body)('transcoding.concurrency').isInt({ min: 1 }),
    (0, express_validator_1.body)('transcoding.resolutions.0p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.144p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.240p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.360p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.480p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.720p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.1080p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.1440p').isBoolean(),
    (0, express_validator_1.body)('transcoding.resolutions.2160p').isBoolean(),
    (0, express_validator_1.body)('transcoding.alwaysTranscodeOriginalResolution').isBoolean(),
    (0, express_validator_1.body)('transcoding.webtorrent.enabled').isBoolean(),
    (0, express_validator_1.body)('transcoding.hls.enabled').isBoolean(),
    (0, express_validator_1.body)('videoStudio.enabled').isBoolean(),
    (0, express_validator_1.body)('import.videos.concurrency').isInt({ min: 0 }),
    (0, express_validator_1.body)('import.videos.http.enabled').isBoolean(),
    (0, express_validator_1.body)('import.videos.torrent.enabled').isBoolean(),
    (0, express_validator_1.body)('import.videoChannelSynchronization.enabled').isBoolean(),
    (0, express_validator_1.body)('trending.videos.algorithms.default').exists(),
    (0, express_validator_1.body)('trending.videos.algorithms.enabled').exists(),
    (0, express_validator_1.body)('followers.instance.enabled').isBoolean(),
    (0, express_validator_1.body)('followers.instance.manualApproval').isBoolean(),
    (0, express_validator_1.body)('theme.default').custom(v => (0, plugins_1.isThemeNameValid)(v) && (0, theme_utils_1.isThemeRegistered)(v)),
    (0, express_validator_1.body)('broadcastMessage.enabled').isBoolean(),
    (0, express_validator_1.body)('broadcastMessage.message').exists(),
    (0, express_validator_1.body)('broadcastMessage.level').exists(),
    (0, express_validator_1.body)('broadcastMessage.dismissable').isBoolean(),
    (0, express_validator_1.body)('live.enabled').isBoolean(),
    (0, express_validator_1.body)('live.allowReplay').isBoolean(),
    (0, express_validator_1.body)('live.maxDuration').isInt(),
    (0, express_validator_1.body)('live.maxInstanceLives').custom(misc_1.isIntOrNull),
    (0, express_validator_1.body)('live.maxUserLives').custom(misc_1.isIntOrNull),
    (0, express_validator_1.body)('live.transcoding.enabled').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.threads').isInt(),
    (0, express_validator_1.body)('live.transcoding.resolutions.144p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.resolutions.240p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.resolutions.360p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.resolutions.480p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.resolutions.720p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.resolutions.1080p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.resolutions.1440p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.resolutions.2160p').isBoolean(),
    (0, express_validator_1.body)('live.transcoding.alwaysTranscodeOriginalResolution').isBoolean(),
    (0, express_validator_1.body)('search.remoteUri.users').isBoolean(),
    (0, express_validator_1.body)('search.remoteUri.anonymous').isBoolean(),
    (0, express_validator_1.body)('search.searchIndex.enabled').isBoolean(),
    (0, express_validator_1.body)('search.searchIndex.url').exists(),
    (0, express_validator_1.body)('search.searchIndex.disableLocalSearch').isBoolean(),
    (0, express_validator_1.body)('search.searchIndex.isDefaultSearch').isBoolean(),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!checkInvalidConfigIfEmailDisabled(req.body, res))
            return;
        if (!checkInvalidTranscodingConfig(req.body, res))
            return;
        if (!checkInvalidSynchronizationConfig(req.body, res))
            return;
        if (!checkInvalidLiveConfig(req.body, res))
            return;
        if (!checkInvalidVideoStudioConfig(req.body, res))
            return;
        return next();
    }
];
exports.customConfigUpdateValidator = customConfigUpdateValidator;
function ensureConfigIsEditable(req, res, next) {
    if (!config_1.CONFIG.WEBADMIN.CONFIGURATION.EDITION.ALLOWED) {
        return res.fail({
            status: 405,
            message: 'Server configuration is static and cannot be edited'
        });
    }
    return next();
}
exports.ensureConfigIsEditable = ensureConfigIsEditable;
function checkInvalidConfigIfEmailDisabled(customConfig, res) {
    if ((0, config_1.isEmailEnabled)())
        return true;
    if (customConfig.signup.requiresEmailVerification === true) {
        res.fail({ message: 'Emailer is disabled but you require signup email verification.' });
        return false;
    }
    return true;
}
function checkInvalidTranscodingConfig(customConfig, res) {
    if (customConfig.transcoding.enabled === false)
        return true;
    if (customConfig.transcoding.webtorrent.enabled === false && customConfig.transcoding.hls.enabled === false) {
        res.fail({ message: 'You need to enable at least webtorrent transcoding or hls transcoding' });
        return false;
    }
    return true;
}
function checkInvalidSynchronizationConfig(customConfig, res) {
    if (customConfig.import.videoChannelSynchronization.enabled && !customConfig.import.videos.http.enabled) {
        res.fail({ message: 'You need to enable HTTP video import in order to enable channel synchronization' });
        return false;
    }
    return true;
}
function checkInvalidLiveConfig(customConfig, res) {
    if (customConfig.live.enabled === false)
        return true;
    if (customConfig.live.allowReplay === true && customConfig.transcoding.enabled === false) {
        res.fail({ message: 'You cannot allow live replay if transcoding is not enabled' });
        return false;
    }
    return true;
}
function checkInvalidVideoStudioConfig(customConfig, res) {
    if (customConfig.videoStudio.enabled === false)
        return true;
    if (customConfig.videoStudio.enabled === true && customConfig.transcoding.enabled === false) {
        res.fail({ message: 'You cannot enable video studio if transcoding is not enabled' });
        return false;
    }
    return true;
}
//# sourceMappingURL=config.js.map