"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ensureSyncExists = exports.videoChannelSyncValidator = exports.ensureSyncIsEnabled = 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 video_channel_sync_1 = require("../../../models/video/video-channel-sync");
const shared_1 = require("../shared");
const video_channel_syncs_1 = require("../shared/video-channel-syncs");
const ensureSyncIsEnabled = (req, res, next) => {
    if (!config_1.CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.ENABLED) {
        return res.fail({
            status: 403,
            message: 'Synchronization is impossible as video channel synchronization is not enabled on the server'
        });
    }
    return next();
};
exports.ensureSyncIsEnabled = ensureSyncIsEnabled;
exports.videoChannelSyncValidator = [
    (0, express_validator_1.body)('externalChannelUrl')
        .custom(misc_1.isUrlValid),
    (0, express_validator_1.body)('videoChannelId')
        .isInt(),
    (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 (!(yield (0, shared_1.doesVideoChannelIdExist)(body.videoChannelId, res)))
            return;
        const count = yield video_channel_sync_1.VideoChannelSyncModel.countByAccount(res.locals.videoChannel.accountId);
        if (count >= config_1.CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER) {
            return res.fail({
                message: `You cannot create more than ${config_1.CONFIG.IMPORT.VIDEO_CHANNEL_SYNCHRONIZATION.MAX_PER_USER} channel synchronizations`
            });
        }
        return next();
    })
];
exports.ensureSyncExists = [
    (0, express_validator_1.param)('id').exists().isInt().withMessage('Should have an sync id'),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, video_channel_syncs_1.doesVideoChannelSyncIdExist)(+req.params.id, res)))
            return;
        if (!(yield (0, shared_1.doesVideoChannelIdExist)(res.locals.videoChannelSync.videoChannelId, res)))
            return;
        return next();
    })
];
//# sourceMappingURL=video-channel-sync.js.map