"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoChannelImportVideosValidator = exports.videoChannelsListValidator = exports.videoChannelStatsValidator = exports.ensureChannelOwnerCanUpload = 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 misc_1 = require("../../../helpers/custom-validators/activitypub/misc");
const config_1 = require("../../../initializers/config");
const misc_2 = require("../../../helpers/custom-validators/misc");
const video_channels_1 = require("../../../helpers/custom-validators/video-channels");
const actor_1 = require("../../../models/actor/actor");
const video_channel_1 = require("../../../models/video/video-channel");
const shared_1 = require("../shared");
const video_channel_syncs_1 = require("../shared/video-channel-syncs");
exports.videoChannelsAddValidator = [
    (0, express_validator_1.body)('name')
        .custom(video_channels_1.isVideoChannelUsernameValid),
    (0, express_validator_1.body)('displayName')
        .custom(video_channels_1.isVideoChannelDisplayNameValid),
    (0, express_validator_1.body)('description')
        .optional()
        .custom(video_channels_1.isVideoChannelDescriptionValid),
    (0, express_validator_1.body)('support')
        .optional()
        .custom(video_channels_1.isVideoChannelSupportValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        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.videoChannelsUpdateValidator = [
    (0, express_validator_1.param)('nameWithHost')
        .exists(),
    (0, express_validator_1.body)('displayName')
        .optional()
        .custom(video_channels_1.isVideoChannelDisplayNameValid),
    (0, express_validator_1.body)('description')
        .optional()
        .custom(video_channels_1.isVideoChannelDescriptionValid),
    (0, express_validator_1.body)('support')
        .optional()
        .custom(video_channels_1.isVideoChannelSupportValid),
    (0, express_validator_1.body)('bulkVideosSupportUpdate')
        .optional()
        .custom(misc_2.isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoChannelsRemoveValidator = [
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if (!(yield checkVideoChannelIsNotTheLastOne(res.locals.videoChannel, res)))
            return;
        return next();
    })
];
exports.videoChannelsNameWithHostValidator = [
    (0, express_validator_1.param)('nameWithHost')
        .exists(),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesVideoChannelNameWithHostExist)(req.params.nameWithHost, res)))
            return;
        return next();
    })
];
exports.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.ensureChannelOwnerCanUpload = [
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        const channel = res.locals.videoChannel;
        const user = { id: channel.Account.userId };
        if (!(yield (0, shared_1.checkUserQuota)(user, 1, res)))
            return;
        next();
    })
];
exports.videoChannelStatsValidator = [
    (0, express_validator_1.query)('withStats')
        .optional()
        .customSanitizer(misc_2.toBooleanOrNull)
        .custom(misc_2.isBooleanValid).withMessage('Should have a valid stats flag boolean'),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoChannelsListValidator = [
    (0, express_validator_1.query)('search')
        .optional()
        .not().isEmpty(),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoChannelImportVideosValidator = [
    (0, express_validator_1.body)('externalChannelUrl')
        .custom(misc_1.isUrlValid),
    (0, express_validator_1.body)('videoChannelSyncId')
        .optional()
        .custom(misc_2.isIdValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const body = req.body;
        if (!config_1.CONFIG.IMPORT.VIDEOS.HTTP.ENABLED) {
            return res.fail({
                status: 403,
                message: 'Channel import is impossible as video upload via HTTP is not enabled on the server'
            });
        }
        if (body.videoChannelSyncId && !(yield (0, video_channel_syncs_1.doesVideoChannelSyncIdExist)(body.videoChannelSyncId, res)))
            return;
        if (res.locals.videoChannelSync && res.locals.videoChannelSync.videoChannelId !== res.locals.videoChannel.id) {
            return res.fail({
                status: 403,
                message: 'This channel sync is not owned by this channel'
            });
        }
        return next();
    })
];
function checkVideoChannelIsNotTheLastOne(videoChannel, res) {
    return 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