"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoRatingValidator = exports.getAccountVideoRateValidatorFactory = exports.videoUpdateRateValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const accounts_1 = require("../../../helpers/custom-validators/accounts");
const misc_1 = require("../../../helpers/custom-validators/misc");
const video_rates_1 = require("../../../helpers/custom-validators/video-rates");
const videos_1 = require("../../../helpers/custom-validators/videos");
const logger_1 = require("../../../helpers/logger");
const account_video_rate_1 = require("../../../models/account/account-video-rate");
const shared_1 = require("../shared");
const videoUpdateRateValidator = [
    (0, shared_1.isValidVideoIdParam)('id'),
    (0, express_validator_1.body)('rating').custom(videos_1.isVideoRatingTypeValid).withMessage('Should have a valid rate type'),
    (req, res, next) => (0, tslib_1.__awaiter)(void 0, void 0, void 0, function* () {
        logger_1.logger.debug('Checking videoRate parameters', { parameters: req.body });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        if (!(yield (0, shared_1.doesVideoExist)(req.params.id, res)))
            return;
        if (!(yield (0, shared_1.checkCanSeeVideoIfPrivate)(req, res, res.locals.videoAll))) {
            return res.fail({
                status: 403,
                message: 'Cannot access to this ressource'
            });
        }
        return next();
    })
];
exports.videoUpdateRateValidator = videoUpdateRateValidator;
const getAccountVideoRateValidatorFactory = function (rateType) {
    return [
        (0, express_validator_1.param)('name').custom(accounts_1.isAccountNameValid).withMessage('Should have a valid account name'),
        (0, express_validator_1.param)('videoId').custom(misc_1.isIdValid).not().isEmpty().withMessage('Should have a valid videoId'),
        (req, res, next) => (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
            logger_1.logger.debug('Checking videoCommentGetValidator parameters.', { parameters: req.params });
            if ((0, shared_1.areValidationErrors)(req, res))
                return;
            const rate = yield account_video_rate_1.AccountVideoRateModel.loadLocalAndPopulateVideo(rateType, req.params.name, +req.params.videoId);
            if (!rate) {
                return res.fail({
                    status: 404,
                    message: 'Video rate not found'
                });
            }
            res.locals.accountVideoRate = rate;
            return next();
        })
    ];
};
exports.getAccountVideoRateValidatorFactory = getAccountVideoRateValidatorFactory;
const videoRatingValidator = [
    (0, express_validator_1.query)('rating').optional().custom(video_rates_1.isRatingValid).withMessage('Value must be one of "like" or "dislike"'),
    (req, res, next) => {
        logger_1.logger.debug('Checking rating parameter', { parameters: req.params });
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        return next();
    }
];
exports.videoRatingValidator = videoRatingValidator;
//# sourceMappingURL=video-rates.js.map