"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendUpdateVideoPlaylist = exports.sendUpdateCacheFile = exports.sendUpdateVideo = exports.sendUpdateActor = void 0;
const tslib_1 = require("tslib");
const application_1 = require("../../../models/application/application");
const logger_1 = require("../../../helpers/logger");
const account_1 = require("../../../models/account/account");
const video_1 = require("../../../models/video/video");
const video_share_1 = require("../../../models/video/video-share");
const audience_1 = require("../audience");
const url_1 = require("../url");
const shared_1 = require("./shared");
const send_utils_1 = require("./shared/send-utils");
function sendUpdateVideo(videoArg, transaction, overriddenByActor) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const video = videoArg;
        if (!video.hasPrivacyForFederation())
            return undefined;
        logger_1.logger.info('Creating job to update video %s.', video.url);
        const byActor = overriddenByActor || video.VideoChannel.Account.Actor;
        const url = (0, url_1.getUpdateActivityPubUrl)(video.url, video.updatedAt.toISOString());
        if (!video.VideoCaptions) {
            video.VideoCaptions = yield video.$get('VideoCaptions', { transaction });
        }
        const videoObject = video.toActivityPubObject();
        const audience = (0, audience_1.getAudience)(byActor, video.privacy === 1);
        const updateActivity = buildUpdateActivity(url, byActor, videoObject, audience);
        const actorsInvolved = yield (0, shared_1.getActorsInvolvedInVideo)(video, transaction);
        if (overriddenByActor)
            actorsInvolved.push(overriddenByActor);
        return (0, send_utils_1.broadcastToFollowers)({
            data: updateActivity,
            byActor,
            toFollowersOf: actorsInvolved,
            contextType: 'Video',
            transaction
        });
    });
}
exports.sendUpdateVideo = sendUpdateVideo;
function sendUpdateActor(accountOrChannel, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const byActor = accountOrChannel.Actor;
        logger_1.logger.info('Creating job to update actor %s.', byActor.url);
        const url = (0, url_1.getUpdateActivityPubUrl)(byActor.url, byActor.updatedAt.toISOString());
        const accountOrChannelObject = accountOrChannel.toActivityPubObject();
        const audience = (0, audience_1.getAudience)(byActor);
        const updateActivity = buildUpdateActivity(url, byActor, accountOrChannelObject, audience);
        let actorsInvolved;
        if (accountOrChannel instanceof account_1.AccountModel) {
            actorsInvolved = yield video_share_1.VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, transaction);
        }
        else {
            actorsInvolved = yield video_share_1.VideoShareModel.loadActorsByVideoChannel(accountOrChannel.id, transaction);
        }
        actorsInvolved.push(byActor);
        return (0, send_utils_1.broadcastToFollowers)({
            data: updateActivity,
            byActor,
            toFollowersOf: actorsInvolved,
            transaction,
            contextType: 'Actor'
        });
    });
}
exports.sendUpdateActor = sendUpdateActor;
function sendUpdateCacheFile(byActor, redundancyModel) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to update cache file %s.', redundancyModel.url);
        const associatedVideo = redundancyModel.getVideo();
        if (!associatedVideo) {
            logger_1.logger.warn('Cannot send update activity for redundancy %s: no video files associated.', redundancyModel.url);
            return;
        }
        const video = yield video_1.VideoModel.loadFull(associatedVideo.id);
        const activityBuilder = (audience) => {
            const redundancyObject = redundancyModel.toActivityPubObject();
            const url = (0, url_1.getUpdateActivityPubUrl)(redundancyModel.url, redundancyModel.updatedAt.toISOString());
            return buildUpdateActivity(url, byActor, redundancyObject, audience);
        };
        return (0, send_utils_1.sendVideoRelatedActivity)(activityBuilder, { byActor, video, contextType: 'CacheFile' });
    });
}
exports.sendUpdateCacheFile = sendUpdateCacheFile;
function sendUpdateVideoPlaylist(videoPlaylist, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (videoPlaylist.privacy === 3)
            return undefined;
        const byActor = videoPlaylist.OwnerAccount.Actor;
        logger_1.logger.info('Creating job to update video playlist %s.', videoPlaylist.url);
        const url = (0, url_1.getUpdateActivityPubUrl)(videoPlaylist.url, videoPlaylist.updatedAt.toISOString());
        const object = yield videoPlaylist.toActivityPubObject(null, transaction);
        const audience = (0, audience_1.getAudience)(byActor, videoPlaylist.privacy === 1);
        const updateActivity = buildUpdateActivity(url, byActor, object, audience);
        const serverActor = yield (0, application_1.getServerActor)();
        const toFollowersOf = [byActor, serverActor];
        if (videoPlaylist.VideoChannel)
            toFollowersOf.push(videoPlaylist.VideoChannel.Actor);
        return (0, send_utils_1.broadcastToFollowers)({
            data: updateActivity,
            byActor,
            toFollowersOf,
            transaction,
            contextType: 'Playlist'
        });
    });
}
exports.sendUpdateVideoPlaylist = sendUpdateVideoPlaylist;
function buildUpdateActivity(url, byActor, object, audience) {
    if (!audience)
        audience = (0, audience_1.getAudience)(byActor);
    return (0, audience_1.audiencify)({
        type: 'Update',
        id: url,
        actor: byActor.url,
        object: (0, audience_1.audiencify)(object, audience)
    }, audience);
}
//# sourceMappingURL=send-update.js.map