"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.isAPVideoTrackerUrlObject = exports.isAPVideoFileUrlMetadataObject = exports.isRemoteVideoUrlValid = exports.sanitizeAndCheckVideoTorrentObject = exports.isRemoteStringIdentifierValid = exports.sanitizeAndCheckVideoTorrentUpdateActivity = void 0;
const tslib_1 = require("tslib");
const validator_1 = tslib_1.__importDefault(require("validator"));
const logger_1 = require("../../logger");
const constants_1 = require("../../../initializers/constants");
const core_utils_1 = require("../../core-utils");
const misc_1 = require("../misc");
const video_lives_1 = require("../video-lives");
const videos_1 = require("../videos");
const misc_2 = require("./misc");
function sanitizeAndCheckVideoTorrentUpdateActivity(activity) {
    return (0, misc_2.isBaseActivityValid)(activity, 'Update') &&
        sanitizeAndCheckVideoTorrentObject(activity.object);
}
exports.sanitizeAndCheckVideoTorrentUpdateActivity = sanitizeAndCheckVideoTorrentUpdateActivity;
function sanitizeAndCheckVideoTorrentObject(video) {
    if (!video || video.type !== 'Video')
        return false;
    if (!setValidRemoteTags(video)) {
        logger_1.logger.debug('Video has invalid tags', { video });
        return false;
    }
    if (!setValidRemoteVideoUrls(video)) {
        logger_1.logger.debug('Video has invalid urls', { video });
        return false;
    }
    if (!setRemoteVideoContent(video)) {
        logger_1.logger.debug('Video has invalid content', { video });
        return false;
    }
    if (!(0, misc_2.setValidAttributedTo)(video)) {
        logger_1.logger.debug('Video has invalid attributedTo', { video });
        return false;
    }
    if (!setValidRemoteCaptions(video)) {
        logger_1.logger.debug('Video has invalid captions', { video });
        return false;
    }
    if (!setValidRemoteIcon(video)) {
        logger_1.logger.debug('Video has invalid icons', { video });
        return false;
    }
    if (!(0, videos_1.isVideoStateValid)(video.state))
        video.state = 1;
    if (!(0, misc_1.isBooleanValid)(video.waitTranscoding))
        video.waitTranscoding = false;
    if (!(0, misc_1.isBooleanValid)(video.downloadEnabled))
        video.downloadEnabled = true;
    if (!(0, misc_1.isBooleanValid)(video.commentsEnabled))
        video.commentsEnabled = false;
    if (!(0, misc_1.isBooleanValid)(video.isLiveBroadcast))
        video.isLiveBroadcast = false;
    if (!(0, misc_1.isBooleanValid)(video.liveSaveReplay))
        video.liveSaveReplay = false;
    if (!(0, misc_1.isBooleanValid)(video.permanentLive))
        video.permanentLive = false;
    if (!(0, video_lives_1.isLiveLatencyModeValid)(video.latencyMode))
        video.latencyMode = 1;
    return (0, misc_2.isActivityPubUrlValid)(video.id) &&
        (0, videos_1.isVideoNameValid)(video.name) &&
        (0, misc_2.isActivityPubVideoDurationValid)(video.duration) &&
        (0, videos_1.isVideoDurationValid)(video.duration.replace(/[^0-9]+/g, '')) &&
        (0, misc_1.isUUIDValid)(video.uuid) &&
        (!video.category || isRemoteNumberIdentifierValid(video.category)) &&
        (!video.licence || isRemoteNumberIdentifierValid(video.licence)) &&
        (!video.language || isRemoteStringIdentifierValid(video.language)) &&
        (0, videos_1.isVideoViewsValid)(video.views) &&
        (0, misc_1.isBooleanValid)(video.sensitive) &&
        (0, misc_1.isDateValid)(video.published) &&
        (0, misc_1.isDateValid)(video.updated) &&
        (!video.originallyPublishedAt || (0, misc_1.isDateValid)(video.originallyPublishedAt)) &&
        (!video.content || isRemoteVideoContentValid(video.mediaType, video.content)) &&
        video.attributedTo.length !== 0;
}
exports.sanitizeAndCheckVideoTorrentObject = sanitizeAndCheckVideoTorrentObject;
function isRemoteVideoUrlValid(url) {
    return url.type === 'Link' &&
        (constants_1.ACTIVITY_PUB.URL_MIME_TYPES.VIDEO.includes(url.mediaType) &&
            (0, misc_2.isActivityPubUrlValid)(url.href) &&
            validator_1.default.isInt(url.height + '', { min: 0 }) &&
            validator_1.default.isInt(url.size + '', { min: 0 }) &&
            (!url.fps || validator_1.default.isInt(url.fps + '', { min: -1 }))) ||
        (constants_1.ACTIVITY_PUB.URL_MIME_TYPES.TORRENT.includes(url.mediaType) &&
            (0, misc_2.isActivityPubUrlValid)(url.href) &&
            validator_1.default.isInt(url.height + '', { min: 0 })) ||
        (constants_1.ACTIVITY_PUB.URL_MIME_TYPES.MAGNET.includes(url.mediaType) &&
            validator_1.default.isLength(url.href, { min: 5 }) &&
            validator_1.default.isInt(url.height + '', { min: 0 })) ||
        ((url.mediaType || url.mimeType) === 'application/x-mpegURL' &&
            (0, misc_2.isActivityPubUrlValid)(url.href) &&
            (0, misc_1.isArray)(url.tag)) ||
        isAPVideoTrackerUrlObject(url) ||
        isAPVideoFileUrlMetadataObject(url);
}
exports.isRemoteVideoUrlValid = isRemoteVideoUrlValid;
function isAPVideoFileUrlMetadataObject(url) {
    return url &&
        url.type === 'Link' &&
        url.mediaType === 'application/json' &&
        (0, misc_1.isArray)(url.rel) && url.rel.includes('metadata');
}
exports.isAPVideoFileUrlMetadataObject = isAPVideoFileUrlMetadataObject;
function isAPVideoTrackerUrlObject(url) {
    return (0, misc_1.isArray)(url.rel) &&
        url.rel.includes('tracker') &&
        (0, misc_2.isActivityPubUrlValid)(url.href);
}
exports.isAPVideoTrackerUrlObject = isAPVideoTrackerUrlObject;
function setValidRemoteTags(video) {
    if (Array.isArray(video.tag) === false)
        return false;
    video.tag = video.tag.filter(t => {
        return t.type === 'Hashtag' &&
            (0, videos_1.isVideoTagValid)(t.name);
    });
    return true;
}
function setValidRemoteCaptions(video) {
    if (!video.subtitleLanguage)
        video.subtitleLanguage = [];
    if (Array.isArray(video.subtitleLanguage) === false)
        return false;
    video.subtitleLanguage = video.subtitleLanguage.filter(caption => {
        if (!(0, misc_2.isActivityPubUrlValid)(caption.url))
            caption.url = null;
        return isRemoteStringIdentifierValid(caption);
    });
    return true;
}
function isRemoteNumberIdentifierValid(data) {
    return validator_1.default.isInt(data.identifier, { min: 0 });
}
function isRemoteStringIdentifierValid(data) {
    return typeof data.identifier === 'string';
}
exports.isRemoteStringIdentifierValid = isRemoteStringIdentifierValid;
function isRemoteVideoContentValid(mediaType, content) {
    return mediaType === 'text/markdown' && (0, videos_1.isVideoDescriptionValid)(content);
}
function setValidRemoteIcon(video) {
    if (video.icon && !(0, misc_1.isArray)(video.icon))
        video.icon = [video.icon];
    if (!video.icon)
        video.icon = [];
    video.icon = video.icon.filter(icon => {
        return icon.type === 'Image' &&
            (0, misc_2.isActivityPubUrlValid)(icon.url) &&
            icon.mediaType === 'image/jpeg' &&
            validator_1.default.isInt(icon.width + '', { min: 0 }) &&
            validator_1.default.isInt(icon.height + '', { min: 0 });
    });
    return video.icon.length !== 0;
}
function setValidRemoteVideoUrls(video) {
    if (Array.isArray(video.url) === false)
        return false;
    video.url = video.url.filter(u => isRemoteVideoUrlValid(u));
    return true;
}
function setRemoteVideoContent(video) {
    if (video.content) {
        video.content = (0, core_utils_1.peertubeTruncate)(video.content, { length: constants_1.CONSTRAINTS_FIELDS.VIDEOS.DESCRIPTION.max });
    }
    return true;
}
//# sourceMappingURL=videos.js.map