"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkCanSeePrivateVideo = exports.checkCanSeeVideoIfPrivate = exports.checkUserCanManageVideo = exports.doesVideoFileOfVideoExist = exports.doesVideoExist = exports.doesVideoChannelOfAccountExist = void 0;
const tslib_1 = require("tslib");
const model_loaders_1 = require("../../../lib/model-loaders");
const auth_1 = require("../../auth");
const video_1 = require("../../../models/video/video");
const video_channel_1 = require("../../../models/video/video-channel");
const video_file_1 = require("../../../models/video/video-file");
function doesVideoExist(id, res, fetchType = 'all') {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const userId = res.locals.oauth ? res.locals.oauth.token.User.id : undefined;
        const video = yield (0, model_loaders_1.loadVideo)(id, fetchType, userId);
        if (video === null) {
            res.fail({
                status: 404,
                message: 'Video not found'
            });
            return false;
        }
        switch (fetchType) {
            case 'for-api':
                res.locals.videoAPI = video;
                break;
            case 'all':
                res.locals.videoAll = video;
                break;
            case 'only-immutable-attributes':
                res.locals.onlyImmutableVideo = video;
                break;
            case 'id':
                res.locals.videoId = video;
                break;
            case 'only-video':
                res.locals.onlyVideo = video;
                break;
        }
        return true;
    });
}
exports.doesVideoExist = doesVideoExist;
function doesVideoFileOfVideoExist(id, videoIdOrUUID, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        if (!(yield video_file_1.VideoFileModel.doesVideoExistForVideoFile(id, videoIdOrUUID))) {
            res.fail({
                status: 404,
                message: 'VideoFile matching Video not found'
            });
            return false;
        }
        return true;
    });
}
exports.doesVideoFileOfVideoExist = doesVideoFileOfVideoExist;
function doesVideoChannelOfAccountExist(channelId, user, res) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        const videoChannel = yield video_channel_1.VideoChannelModel.loadAndPopulateAccount(channelId);
        if (videoChannel === null) {
            res.fail({ message: 'Unknown video "video channel" for this instance.' });
            return false;
        }
        if (user.hasRight(17) === true) {
            res.locals.videoChannel = videoChannel;
            return true;
        }
        if (videoChannel.Account.id !== user.Account.id) {
            res.fail({
                message: 'Unknown video "video channel" for this account.'
            });
            return false;
        }
        res.locals.videoChannel = videoChannel;
        return true;
    });
}
exports.doesVideoChannelOfAccountExist = doesVideoChannelOfAccountExist;
function checkCanSeeVideoIfPrivate(req, res, video, authenticateInQuery = false) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        if (!video.requiresAuth())
            return true;
        const videoWithRights = yield video_1.VideoModel.loadAndPopulateAccountAndServerAndTags(video.id);
        return checkCanSeePrivateVideo(req, res, videoWithRights, authenticateInQuery);
    });
}
exports.checkCanSeeVideoIfPrivate = checkCanSeeVideoIfPrivate;
function checkCanSeePrivateVideo(req, res, video, authenticateInQuery = false) {
    return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () {
        yield (0, auth_1.authenticatePromiseIfNeeded)(req, res, authenticateInQuery);
        const user = res.locals.oauth ? res.locals.oauth.token.User : null;
        if (!user || !user.canGetVideo(video)) {
            return false;
        }
        return true;
    });
}
exports.checkCanSeePrivateVideo = checkCanSeePrivateVideo;
function checkUserCanManageVideo(user, video, right, res, onlyOwned = true) {
    if (onlyOwned && video.isOwned() === false) {
        res.fail({
            status: 403,
            message: 'Cannot manage a video of another server.'
        });
        return false;
    }
    const account = video.VideoChannel.Account;
    if (user.hasRight(right) === false && account.userId !== user.id) {
        res.fail({
            status: 403,
            message: 'Cannot manage a video of another user.'
        });
        return false;
    }
    return true;
}
exports.checkUserCanManageVideo = checkUserCanManageVideo;
//# sourceMappingURL=videos.js.map