"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkUserQuota = exports.checkCanSeeVideo = exports.checkUserCanManageVideo = exports.checkCanAccessVideoStaticFiles = exports.doesVideoFileOfVideoExist = exports.doesVideoExist = exports.doesVideoChannelOfAccountExist = void 0;
const tslib_1 = require("tslib");
const model_loaders_1 = require("../../../lib/model-loaders");
const user_1 = require("../../../lib/user");
const video_tokens_manager_1 = require("../../../lib/video-tokens-manager");
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 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) {
            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 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 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 checkCanSeeVideo(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { req, res, video, paramId } = options;
        if (video.requiresAuth({ urlParamId: paramId, checkBlacklist: true })) {
            return checkCanSeeAuthVideo(req, res, video);
        }
        if (video.privacy === 2 || video.privacy === 1) {
            return true;
        }
        throw new Error('Unknown video privacy when checking video right ' + video.url);
    });
}
exports.checkCanSeeVideo = checkCanSeeVideo;
function checkCanSeeAuthVideo(req, res, video) {
    var _a, _b, _c;
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const fail = () => {
            res.fail({
                status: 403,
                message: 'Cannot fetch information of private/internal/blocked video'
            });
            return false;
        };
        yield (0, auth_1.authenticatePromise)(req, res);
        const user = (_a = res.locals.oauth) === null || _a === void 0 ? void 0 : _a.token.User;
        if (!user)
            return fail();
        const videoWithRights = ((_c = (_b = video.VideoChannel) === null || _b === void 0 ? void 0 : _b.Account) === null || _c === void 0 ? void 0 : _c.userId)
            ? video
            : yield video_1.VideoModel.loadFull(video.id);
        const privacy = videoWithRights.privacy;
        if (privacy === 4) {
            return true;
        }
        const isOwnedByUser = videoWithRights.VideoChannel.Account.userId === user.id;
        if (videoWithRights.isBlacklisted()) {
            if (isOwnedByUser || user.hasRight(12))
                return true;
            return fail();
        }
        if (privacy === 3 || privacy === 2) {
            if (isOwnedByUser || user.hasRight(20))
                return true;
            return fail();
        }
        return fail();
    });
}
function checkCanAccessVideoStaticFiles(options) {
    var _a;
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { video, req, res } = options;
        if ((_a = res.locals.oauth) === null || _a === void 0 ? void 0 : _a.token.User) {
            return checkCanSeeVideo(options);
        }
        if (!video.hasPrivateStaticPath())
            return true;
        const videoFileToken = req.query.videoFileToken;
        if (!videoFileToken) {
            res.sendStatus(403);
            return false;
        }
        if (video_tokens_manager_1.VideoTokensManager.Instance.hasToken({ token: videoFileToken, videoUUID: video.uuid })) {
            return true;
        }
        res.sendStatus(403);
        return false;
    });
}
exports.checkCanAccessVideoStaticFiles = checkCanAccessVideoStaticFiles;
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;
function checkUserQuota(user, videoFileSize, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if ((yield (0, user_1.isAbleToUploadVideo)(user.id, videoFileSize)) === false) {
            res.fail({
                status: 413,
                message: 'The user video quota is exceeded with this video.',
                type: "quota_reached"
            });
            return false;
        }
        return true;
    });
}
exports.checkUserQuota = checkUserQuota;
//# sourceMappingURL=videos.js.map