"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.videoLiveGetValidator = exports.videoLiveFindReplaySessionValidator = exports.videoLiveListSessionsValidator = exports.videoLiveUpdateValidator = exports.videoLiveAddValidator = void 0;
const tslib_1 = require("tslib");
const express_validator_1 = require("express-validator");
const video_lives_1 = require("../../../helpers/custom-validators/video-lives");
const constants_1 = require("../../../initializers/constants");
const moderation_1 = require("../../../lib/moderation");
const hooks_1 = require("../../../lib/plugins/hooks");
const video_1 = require("../../../models/video/video");
const video_live_1 = require("../../../models/video/video-live");
const video_live_session_1 = require("../../../models/video/video-live-session");
const misc_1 = require("../../../helpers/custom-validators/misc");
const videos_1 = require("../../../helpers/custom-validators/videos");
const express_utils_1 = require("../../../helpers/express-utils");
const logger_1 = require("../../../helpers/logger");
const config_1 = require("../../../initializers/config");
const shared_1 = require("../shared");
const videos_2 = require("./videos");
const videoLiveGetValidator = [
    (0, shared_1.isValidVideoIdParam)('videoId'),
    (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.doesVideoExist)(req.params.videoId, res, 'all')))
            return;
        const videoLive = yield video_live_1.VideoLiveModel.loadByVideoId(res.locals.videoAll.id);
        if (!videoLive) {
            return res.fail({
                status: 404,
                message: 'Live video not found'
            });
        }
        res.locals.videoLive = videoLive;
        return next();
    })
];
exports.videoLiveGetValidator = videoLiveGetValidator;
const videoLiveAddValidator = (0, videos_2.getCommonVideoEditAttributes)().concat([
    (0, express_validator_1.body)('channelId')
        .customSanitizer(misc_1.toIntOrNull)
        .custom(misc_1.isIdValid),
    (0, express_validator_1.body)('name')
        .custom(videos_1.isVideoNameValid).withMessage(`Should have a video name between ${constants_1.CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${constants_1.CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`),
    (0, express_validator_1.body)('saveReplay')
        .optional()
        .customSanitizer(misc_1.toBooleanOrNull)
        .custom(misc_1.isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
    (0, express_validator_1.body)('permanentLive')
        .optional()
        .customSanitizer(misc_1.toBooleanOrNull)
        .custom(misc_1.isBooleanValid).withMessage('Should have a valid permanentLive boolean'),
    (0, express_validator_1.body)('latencyMode')
        .optional()
        .customSanitizer(misc_1.toIntOrNull)
        .custom(video_lives_1.isLiveLatencyModeValid),
    (req, res, next) => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
        if ((0, shared_1.areValidationErrors)(req, res))
            return (0, express_utils_1.cleanUpReqFiles)(req);
        if (config_1.CONFIG.LIVE.ENABLED !== true) {
            (0, express_utils_1.cleanUpReqFiles)(req);
            return res.fail({
                status: 403,
                message: 'Live is not enabled on this instance',
                type: "live_not_enabled"
            });
        }
        const body = req.body;
        if (hasValidSaveReplay(body) !== true) {
            (0, express_utils_1.cleanUpReqFiles)(req);
            return res.fail({
                status: 403,
                message: 'Saving live replay is not enabled on this instance',
                type: "live_not_allowing_replay"
            });
        }
        if (hasValidLatencyMode(body) !== true) {
            (0, express_utils_1.cleanUpReqFiles)(req);
            return res.fail({
                status: 403,
                message: 'Custom latency mode is not allowed by this instance'
            });
        }
        const user = res.locals.oauth.token.User;
        if (!(yield (0, shared_1.doesVideoChannelOfAccountExist)(body.channelId, user, res)))
            return (0, express_utils_1.cleanUpReqFiles)(req);
        if (config_1.CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
            const totalInstanceLives = yield video_1.VideoModel.countLives({ remote: false, mode: 'not-ended' });
            if (totalInstanceLives >= config_1.CONFIG.LIVE.MAX_INSTANCE_LIVES) {
                (0, express_utils_1.cleanUpReqFiles)(req);
                return res.fail({
                    status: 403,
                    message: 'Cannot create this live because the max instance lives limit is reached.',
                    type: "max_instance_lives_limit_reached"
                });
            }
        }
        if (config_1.CONFIG.LIVE.MAX_USER_LIVES !== -1) {
            const totalUserLives = yield video_1.VideoModel.countLivesOfAccount(user.Account.id);
            if (totalUserLives >= config_1.CONFIG.LIVE.MAX_USER_LIVES) {
                (0, express_utils_1.cleanUpReqFiles)(req);
                return res.fail({
                    status: 403,
                    message: 'Cannot create this live because the max user lives limit is reached.',
                    type: "max_user_lives_limit_reached"
                });
            }
        }
        if (!(yield isLiveVideoAccepted(req, res)))
            return (0, express_utils_1.cleanUpReqFiles)(req);
        return next();
    })
]);
exports.videoLiveAddValidator = videoLiveAddValidator;
const videoLiveUpdateValidator = [
    (0, express_validator_1.body)('saveReplay')
        .optional()
        .customSanitizer(misc_1.toBooleanOrNull)
        .custom(misc_1.isBooleanValid).withMessage('Should have a valid saveReplay boolean'),
    (0, express_validator_1.body)('latencyMode')
        .optional()
        .customSanitizer(misc_1.toIntOrNull)
        .custom(video_lives_1.isLiveLatencyModeValid),
    (req, res, next) => {
        if ((0, shared_1.areValidationErrors)(req, res))
            return;
        const body = req.body;
        if (hasValidSaveReplay(body) !== true) {
            return res.fail({
                status: 403,
                message: 'Saving live replay is not allowed by this instance'
            });
        }
        if (hasValidLatencyMode(body) !== true) {
            return res.fail({
                status: 403,
                message: 'Custom latency mode is not allowed by this instance'
            });
        }
        if (res.locals.videoAll.state !== 4) {
            return res.fail({ message: 'Cannot update a live that has already started' });
        }
        const user = res.locals.oauth.token.User;
        if (!(0, shared_1.checkUserCanManageVideo)(user, res.locals.videoAll, 19, res))
            return;
        return next();
    }
];
exports.videoLiveUpdateValidator = videoLiveUpdateValidator;
const videoLiveListSessionsValidator = [
    (req, res, next) => {
        const user = res.locals.oauth.token.User;
        if (!(0, shared_1.checkUserCanManageVideo)(user, res.locals.videoAll, 19, res))
            return;
        return next();
    }
];
exports.videoLiveListSessionsValidator = videoLiveListSessionsValidator;
const videoLiveFindReplaySessionValidator = [
    (0, shared_1.isValidVideoIdParam)('videoId'),
    (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.doesVideoExist)(req.params.videoId, res, 'id')))
            return;
        const session = yield video_live_session_1.VideoLiveSessionModel.findSessionOfReplay(res.locals.videoId.id);
        if (!session) {
            return res.fail({
                status: 404,
                message: 'No live replay found'
            });
        }
        res.locals.videoLiveSession = session;
        return next();
    })
];
exports.videoLiveFindReplaySessionValidator = videoLiveFindReplaySessionValidator;
function isLiveVideoAccepted(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const acceptParameters = {
            liveVideoBody: req.body,
            user: res.locals.oauth.token.User
        };
        const acceptedResult = yield hooks_1.Hooks.wrapFun(moderation_1.isLocalLiveVideoAccepted, acceptParameters, 'filter:api.live-video.create.accept.result');
        if (!acceptedResult || acceptedResult.accepted !== true) {
            logger_1.logger.info('Refused local live video.', { acceptedResult, acceptParameters });
            res.fail({
                status: 403,
                message: acceptedResult.errorMessage || 'Refused local live video'
            });
            return false;
        }
        return true;
    });
}
function hasValidSaveReplay(body) {
    if (config_1.CONFIG.LIVE.ALLOW_REPLAY !== true && body.saveReplay === true)
        return false;
    return true;
}
function hasValidLatencyMode(body) {
    if (config_1.CONFIG.LIVE.LATENCY_SETTING.ENABLED !== true &&
        (0, misc_1.exists)(body.latencyMode) &&
        body.latencyMode !== 1)
        return false;
    return true;
}
//# sourceMappingURL=video-live.js.map