"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getExternalAuthValidator = exports.listPluginsValidator = exports.installOrUpdatePluginValidator = exports.existingPluginValidator = exports.listAvailablePluginsValidator = exports.uninstallPluginValidator = exports.updatePluginSettingsValidator = exports.getPluginValidator = exports.pluginStaticDirectoryValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const misc_1 = require("../../helpers/custom-validators/misc");
const plugins_1 = require("../../helpers/custom-validators/plugins");
const config_1 = require("../../initializers/config");
const plugin_manager_1 = require("../../lib/plugins/plugin-manager");
const plugin_1 = require("../../models/server/plugin");
const shared_1 = require("./shared");
const getPluginValidator = (pluginType, withVersion = true) => {
    const validators = [
        (0, express_validator_1.param)('pluginName')
            .custom(plugins_1.isPluginNameValid)
    ];
    if (withVersion) {
        validators.push((0, express_validator_1.param)('pluginVersion')
            .custom(plugins_1.isPluginStableOrUnstableVersionValid));
    }
    return validators.concat([
        (req, res, next) => {
            if ((0, shared_1.areValidationErrors)(req, res))
                return;
            const npmName = plugin_1.PluginModel.buildNpmName(req.params.pluginName, pluginType);
            const plugin = plugin_manager_1.PluginManager.Instance.getRegisteredPluginOrTheme(npmName);
            if (!plugin) {
                return res.fail({
                    status: 404,
                    message: 'No plugin found named ' + npmName
                });
            }
            if (withVersion && plugin.version !== req.params.pluginVersion) {
                return res.fail({
                    status: 404,
                    message: 'No plugin found named ' + npmName + ' with version ' + req.params.pluginVersion
                });
            }
            res.locals.registeredPlugin = plugin;
            return next();
        }
    ]);
};
exports.getPluginValidator = getPluginValidator;
const getExternalAuthValidator = [
    (0, express_validator_1.param)('authName')
        .custom(misc_1.exists),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const plugin = res.locals.registeredPlugin;
        if (!plugin.registerHelpers) {
            return res.fail({
                status: 404,
                message: 'No registered helpers were found for this plugin'
            });
        }
        const externalAuth = plugin.registerHelpers.getExternalAuths().find(a => a.authName === req.params.authName);
        if (!externalAuth) {
            return res.fail({
                status: 404,
                message: 'No external auths were found for this plugin'
            });
        }
        res.locals.externalAuth = externalAuth;
        return next();
    }
];
exports.getExternalAuthValidator = getExternalAuthValidator;
const pluginStaticDirectoryValidator = [
    (0, express_validator_1.param)('staticEndpoint')
        .custom(misc_1.isSafePath),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.pluginStaticDirectoryValidator = pluginStaticDirectoryValidator;
const listPluginsValidator = [
    (0, express_validator_1.query)('pluginType')
        .optional()
        .customSanitizer(misc_1.toIntOrNull)
        .custom(plugins_1.isPluginTypeValid),
    (0, express_validator_1.query)('uninstalled')
        .optional()
        .customSanitizer(misc_1.toBooleanOrNull)
        .custom(misc_1.isBooleanValid),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.listPluginsValidator = listPluginsValidator;
const installOrUpdatePluginValidator = [
    (0, express_validator_1.body)('npmName')
        .optional()
        .custom(plugins_1.isNpmPluginNameValid),
    (0, express_validator_1.body)('pluginVersion')
        .optional()
        .custom(plugins_1.isPluginStableOrUnstableVersionValid),
    (0, express_validator_1.body)('path')
        .optional()
        .custom(misc_1.isSafePath),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const body = req.body;
        if (!body.path && !body.npmName) {
            return res.fail({ message: 'Should have either a npmName or a path' });
        }
        if (body.pluginVersion && !body.npmName) {
            return res.fail({ message: 'Should have a npmName when specifying a pluginVersion' });
        }
        return next();
    }
];
exports.installOrUpdatePluginValidator = installOrUpdatePluginValidator;
const uninstallPluginValidator = [
    (0, express_validator_1.body)('npmName')
        .custom(plugins_1.isNpmPluginNameValid),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.uninstallPluginValidator = uninstallPluginValidator;
const existingPluginValidator = [
    (0, express_validator_1.param)('npmName')
        .custom(plugins_1.isNpmPluginNameValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const plugin = yield plugin_1.PluginModel.loadByNpmName(req.params.npmName);
        if (!plugin) {
            return res.fail({
                status: 404,
                message: 'Plugin not found'
            });
        }
        res.locals.plugin = plugin;
        return next();
    })
];
exports.existingPluginValidator = existingPluginValidator;
const updatePluginSettingsValidator = [
    (0, express_validator_1.body)('settings')
        .exists(),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.updatePluginSettingsValidator = updatePluginSettingsValidator;
const listAvailablePluginsValidator = [
    (0, express_validator_1.query)('search')
        .optional()
        .exists(),
    (0, express_validator_1.query)('pluginType')
        .optional()
        .customSanitizer(misc_1.toIntOrNull)
        .custom(plugins_1.isPluginTypeValid),
    (0, express_validator_1.query)('currentPeerTubeEngine')
        .optional()
        .custom(plugins_1.isPluginStableOrUnstableVersionValid),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (config_1.CONFIG.PLUGINS.INDEX.ENABLED === false) {
            return res.fail({ message: 'Plugin index is not enabled' });
        }
        return next();
    }
];
exports.listAvailablePluginsValidator = listAvailablePluginsValidator;
//# sourceMappingURL=plugins.js.map