"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.updateVideoRates = exports.syncVideoExternalAttributes = void 0;
const tslib_1 = require("tslib");
const database_utils_1 = require("../../../../helpers/database-utils");
const logger_1 = require("../../../../helpers/logger");
const requests_1 = require("../../../../helpers/requests");
const job_queue_1 = require("../../../job-queue");
const video_1 = require("../../../../models/video/video");
const video_comment_1 = require("../../../../models/video/video-comment");
const video_share_1 = require("../../../../models/video/video-share");
const crawl_1 = require("../../crawl");
const share_1 = require("../../share");
const video_comments_1 = require("../../video-comments");
const lTags = (0, logger_1.loggerTagsFactory)('ap', 'video');
function syncVideoExternalAttributes(video, fetchedVideo, syncParam) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid);
        const ratePromise = updateVideoRates(video, fetchedVideo);
        if (syncParam.rates)
            yield ratePromise;
        yield syncShares(video, fetchedVideo, syncParam.shares);
        yield syncComments(video, fetchedVideo, syncParam.comments);
    });
}
exports.syncVideoExternalAttributes = syncVideoExternalAttributes;
function updateVideoRates(video, fetchedVideo) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const [likes, dislikes] = yield Promise.all([
            getRatesCount('like', video, fetchedVideo),
            getRatesCount('dislike', video, fetchedVideo)
        ]);
        return (0, database_utils_1.runInReadCommittedTransaction)((t) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            yield video_1.VideoModel.updateRatesOf(video.id, 'like', likes, t);
            yield video_1.VideoModel.updateRatesOf(video.id, 'dislike', dislikes, t);
        }));
    });
}
exports.updateVideoRates = updateVideoRates;
function getRatesCount(type, video, fetchedVideo) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const uri = type === 'like'
            ? fetchedVideo.likes
            : fetchedVideo.dislikes;
        logger_1.logger.info('Sync %s of video %s', type, video.url);
        const options = { activityPub: true };
        const response = yield (0, requests_1.doJSONRequest)(uri, options);
        const totalItems = response.body.totalItems;
        if (isNaN(totalItems)) {
            logger_1.logger.error('Cannot sync %s of video %s, totalItems is not a number', type, video.url, { body: response.body });
            return;
        }
        return totalItems;
    });
}
function syncShares(video, fetchedVideo, isSync) {
    const uri = fetchedVideo.shares;
    if (!isSync) {
        return createJob({ uri, videoId: video.id, type: 'video-shares' });
    }
    const handler = items => (0, share_1.addVideoShares)(items, video);
    const cleaner = crawlStartDate => video_share_1.VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate);
    return (0, crawl_1.crawlCollectionPage)(uri, handler, cleaner)
        .catch(err => logger_1.logger.error('Cannot add shares of video %s.', video.uuid, Object.assign({ err, rootUrl: uri }, lTags(video.uuid, video.url))));
}
function syncComments(video, fetchedVideo, isSync) {
    const uri = fetchedVideo.comments;
    if (!isSync) {
        return createJob({ uri, videoId: video.id, type: 'video-comments' });
    }
    const handler = items => (0, video_comments_1.addVideoComments)(items);
    const cleaner = crawlStartDate => video_comment_1.VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate);
    return (0, crawl_1.crawlCollectionPage)(uri, handler, cleaner)
        .catch(err => logger_1.logger.error('Cannot add comments of video %s.', video.uuid, Object.assign({ err, rootUrl: uri }, lTags(video.uuid, video.url))));
}
function createJob(payload) {
    return job_queue_1.JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload });
}
//# sourceMappingURL=video-sync-attributes.js.map