"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendUndoCacheFile = exports.sendUndoAnnounce = exports.sendUndoDislike = exports.sendUndoLike = exports.sendUndoFollow = void 0;
const tslib_1 = require("tslib");
const logger_1 = require("../../../helpers/logger");
const video_1 = require("../../../models/video/video");
const audience_1 = require("../audience");
const url_1 = require("../url");
const send_announce_1 = require("./send-announce");
const send_create_1 = require("./send-create");
const send_dislike_1 = require("./send-dislike");
const send_follow_1 = require("./send-follow");
const send_like_1 = require("./send-like");
const send_utils_1 = require("./shared/send-utils");
function sendUndoFollow(actorFollow, t) {
    const me = actorFollow.ActorFollower;
    const following = actorFollow.ActorFollowing;
    if (!following.serverId)
        return;
    logger_1.logger.info('Creating job to send an unfollow request to %s.', following.url);
    const undoUrl = (0, url_1.getUndoActivityPubUrl)(actorFollow.url);
    const followActivity = (0, send_follow_1.buildFollowActivity)(actorFollow.url, me, following);
    const undoActivity = undoActivityData(undoUrl, me, followActivity);
    t.afterCommit(() => {
        return (0, send_utils_1.unicastTo)({
            data: undoActivity,
            byActor: me,
            toActorUrl: following.inboxUrl,
            contextType: 'Follow'
        });
    });
}
exports.sendUndoFollow = sendUndoFollow;
function sendUndoAnnounce(byActor, videoShare, video, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to undo announce %s.', videoShare.url);
        const undoUrl = (0, url_1.getUndoActivityPubUrl)(videoShare.url);
        const { activity: announce, actorsInvolvedInVideo } = yield (0, send_announce_1.buildAnnounceWithVideoAudience)(byActor, videoShare, video, transaction);
        const undoActivity = undoActivityData(undoUrl, byActor, announce);
        return (0, send_utils_1.broadcastToFollowers)({
            data: undoActivity,
            byActor,
            toFollowersOf: actorsInvolvedInVideo,
            transaction,
            actorsException: [byActor],
            contextType: 'Announce'
        });
    });
}
exports.sendUndoAnnounce = sendUndoAnnounce;
function sendUndoCacheFile(byActor, redundancyModel, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to undo cache file %s.', redundancyModel.url);
        const associatedVideo = redundancyModel.getVideo();
        if (!associatedVideo) {
            logger_1.logger.warn('Cannot send undo activity for redundancy %s: no video files associated.', redundancyModel.url);
            return;
        }
        const video = yield video_1.VideoModel.loadFull(associatedVideo.id);
        const createActivity = (0, send_create_1.buildCreateActivity)(redundancyModel.url, byActor, redundancyModel.toActivityPubObject());
        return sendUndoVideoRelatedActivity({
            byActor,
            video,
            url: redundancyModel.url,
            activity: createActivity,
            contextType: 'CacheFile',
            transaction
        });
    });
}
exports.sendUndoCacheFile = sendUndoCacheFile;
function sendUndoLike(byActor, video, t) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to undo a like of video %s.', video.url);
        const likeUrl = (0, url_1.getVideoLikeActivityPubUrlByLocalActor)(byActor, video);
        const likeActivity = (0, send_like_1.buildLikeActivity)(likeUrl, byActor, video);
        return sendUndoVideoRateToOriginActivity({ byActor, video, url: likeUrl, activity: likeActivity, transaction: t });
    });
}
exports.sendUndoLike = sendUndoLike;
function sendUndoDislike(byActor, video, t) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to undo a dislike of video %s.', video.url);
        const dislikeUrl = (0, url_1.getVideoDislikeActivityPubUrlByLocalActor)(byActor, video);
        const dislikeActivity = (0, send_dislike_1.buildDislikeActivity)(dislikeUrl, byActor, video);
        return sendUndoVideoRateToOriginActivity({ byActor, video, url: dislikeUrl, activity: dislikeActivity, transaction: t });
    });
}
exports.sendUndoDislike = sendUndoDislike;
function undoActivityData(url, byActor, object, audience) {
    if (!audience)
        audience = (0, audience_1.getAudience)(byActor);
    return (0, audience_1.audiencify)({
        type: 'Undo',
        id: url,
        actor: byActor.url,
        object
    }, audience);
}
function sendUndoVideoRelatedActivity(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const activityBuilder = (audience) => {
            const undoUrl = (0, url_1.getUndoActivityPubUrl)(options.url);
            return undoActivityData(undoUrl, options.byActor, options.activity, audience);
        };
        return (0, send_utils_1.sendVideoRelatedActivity)(activityBuilder, options);
    });
}
function sendUndoVideoRateToOriginActivity(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const activityBuilder = (audience) => {
            const undoUrl = (0, url_1.getUndoActivityPubUrl)(options.url);
            return undoActivityData(undoUrl, options.byActor, options.activity, audience);
        };
        return (0, send_utils_1.sendVideoActivityToOrigin)(activityBuilder, Object.assign(Object.assign({}, options), { contextType: 'Rate' }));
    });
}
//# sourceMappingURL=send-undo.js.map