"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoChannelStatsValidator = exports.videoChannelsListValidator = exports.ensureIsLocalChannel = exports.videoChannelsNameWithHostValidator = exports.videoChannelsRemoveValidator = exports.videoChannelsUpdateValidator = exports.videoChannelsAddValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const config_1 = require("../../../initializers/config");
const misc_1 = require("../../../helpers/custom-validators/misc");
const video_channels_1 = require("../../../helpers/custom-validators/video-channels");
const logger_1 = require("../../../helpers/logger");
const actor_1 = require("../../../models/actor/actor");
const video_channel_1 = require("../../../models/video/video-channel");
const shared_1 = require("../shared");
const videoChannelsAddValidator = [
    (0, express_validator_1.body)('name').custom(video_channels_1.isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
    (0, express_validator_1.body)('displayName').custom(video_channels_1.isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
    (0, express_validator_1.body)('description').optional().custom(video_channels_1.isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
    (0, express_validator_1.body)('support').optional().custom(video_channels_1.isVideoChannelSupportValid).withMessage('Should have a valid support text'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const actor = yield actor_1.ActorModel.loadLocalByName(req.body.name);
        if (actor) {
            res.fail({
                status: 409,
                message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
            });
            return false;
        }
        const count = yield video_channel_1.VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id);
        if (count >= config_1.CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
            res.fail({ message: `You cannot create more than ${config_1.CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` });
            return false;
        }
        return next();
    })
];
exports.videoChannelsAddValidator = videoChannelsAddValidator;
const videoChannelsUpdateValidator = [
    (0, express_validator_1.param)('nameWithHost').exists().withMessage('Should have an video channel name with host'),
    (0, express_validator_1.body)('displayName')
        .optional()
        .custom(video_channels_1.isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
    (0, express_validator_1.body)('description')
        .optional()
        .custom(video_channels_1.isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
    (0, express_validator_1.body)('support')
        .optional()
        .custom(video_channels_1.isVideoChannelSupportValid).withMessage('Should have a valid support text'),
    (0, express_validator_1.body)('bulkVideosSupportUpdate')
        .optional()
        .custom(misc_1.isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
    (req, res, next) => {
        logger_1.logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoChannelsUpdateValidator = videoChannelsUpdateValidator;
const videoChannelsRemoveValidator = [
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params });
        if (!(yield checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)))
            return;
        return next();
    })
];
exports.videoChannelsRemoveValidator = videoChannelsRemoveValidator;
const videoChannelsNameWithHostValidator = [
    (0, express_validator_1.param)('nameWithHost').exists().withMessage('Should have an video channel name with host'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesVideoChannelNameWithHostExist)(req.params.nameWithHost, res)))
            return;
        return next();
    })
];
exports.videoChannelsNameWithHostValidator = videoChannelsNameWithHostValidator;
const ensureIsLocalChannel = [
    (req, res, next) => {
        if (res.locals.videoChannel.Actor.isOwned() === false) {
            return res.fail({
                status: 403,
                message: 'This channel is not owned.'
            });
        }
        return next();
    }
];
exports.ensureIsLocalChannel = ensureIsLocalChannel;
const videoChannelStatsValidator = [
    (0, express_validator_1.query)('withStats')
        .optional()
        .customSanitizer(misc_1.toBooleanOrNull)
        .custom(misc_1.isBooleanValid).withMessage('Should have a valid stats flag'),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoChannelStatsValidator = videoChannelStatsValidator;
const videoChannelsListValidator = [
    (0, express_validator_1.query)('search').optional().not().isEmpty().withMessage('Should have a valid search'),
    (req, res, next) => {
        logger_1.logger.debug('Checking video channels search query', { parameters: req.query });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoChannelsListValidator = videoChannelsListValidator;
function checkVideoChannelIsNotTheLastOne(videoChannel, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const count = yield video_channel_1.VideoChannelModel.countByAccount(videoChannel.Account.id);
        if (count <= 1) {
            res.fail({
                status: 409,
                message: 'Cannot remove the last channel of this user'
            });
            return false;
        }
        return true;
    });
}
//# sourceMappingURL=video-channels.js.map