"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendDeleteVideoPlaylist = exports.sendDeleteVideoComment = exports.sendDeleteActor = exports.sendDeleteVideo = void 0;
const tslib_1 = require("tslib");
const application_1 = require("../../../models/application/application");
const logger_1 = require("../../../helpers/logger");
const video_comment_1 = require("../../../models/video/video-comment");
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 sendDeleteVideo(video, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to broadcast delete of video %s.', video.url);
        const byActor = video.VideoChannel.Account.Actor;
        const activityBuilder = (audience) => {
            const url = (0, url_1.getDeleteActivityPubUrl)(video.url);
            return buildDeleteActivity(url, video.url, byActor, audience);
        };
        return (0, send_utils_1.sendVideoRelatedActivity)(activityBuilder, { byActor, video, contextType: 'Delete', transaction });
    });
}
exports.sendDeleteVideo = sendDeleteVideo;
function sendDeleteActor(byActor, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to broadcast delete of actor %s.', byActor.url);
        const url = (0, url_1.getDeleteActivityPubUrl)(byActor.url);
        const activity = buildDeleteActivity(url, byActor.url, byActor);
        const actorsInvolved = yield video_share_1.VideoShareModel.loadActorsWhoSharedVideosOf(byActor.id, transaction);
        const serverActor = yield (0, application_1.getServerActor)();
        actorsInvolved.push(serverActor);
        actorsInvolved.push(byActor);
        return (0, send_utils_1.broadcastToFollowers)({
            data: activity,
            byActor,
            toFollowersOf: actorsInvolved,
            contextType: 'Delete',
            transaction
        });
    });
}
exports.sendDeleteActor = sendDeleteActor;
function sendDeleteVideoComment(videoComment, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to send delete of comment %s.', videoComment.url);
        const isVideoOrigin = videoComment.Video.isOwned();
        const url = (0, url_1.getDeleteActivityPubUrl)(videoComment.url);
        const byActor = videoComment.isOwned()
            ? videoComment.Account.Actor
            : videoComment.Video.VideoChannel.Account.Actor;
        const threadParentComments = yield video_comment_1.VideoCommentModel.listThreadParentComments(videoComment, transaction);
        const threadParentCommentsFiltered = threadParentComments.filter(c => !c.isDeleted());
        const actorsInvolvedInComment = yield (0, shared_1.getActorsInvolvedInVideo)(videoComment.Video, transaction);
        actorsInvolvedInComment.push(byActor);
        const audience = (0, shared_1.getVideoCommentAudience)(videoComment, threadParentCommentsFiltered, actorsInvolvedInComment, isVideoOrigin);
        const activity = buildDeleteActivity(url, videoComment.url, byActor, audience);
        const actorsException = [byActor];
        yield (0, send_utils_1.broadcastToActors)({
            data: activity,
            byActor,
            toActors: threadParentCommentsFiltered.map(c => c.Account.Actor),
            transaction,
            contextType: 'Delete',
            actorsException
        });
        yield (0, send_utils_1.broadcastToFollowers)({
            data: activity,
            byActor,
            toFollowersOf: [byActor],
            contextType: 'Delete',
            transaction
        });
        if (isVideoOrigin) {
            return (0, send_utils_1.broadcastToFollowers)({
                data: activity,
                byActor,
                toFollowersOf: actorsInvolvedInComment,
                transaction,
                contextType: 'Delete',
                actorsException
            });
        }
        return transaction.afterCommit(() => {
            return (0, send_utils_1.unicastTo)({
                data: activity,
                byActor,
                toActorUrl: videoComment.Video.VideoChannel.Account.Actor.getSharedInbox(),
                contextType: 'Delete'
            });
        });
    });
}
exports.sendDeleteVideoComment = sendDeleteVideoComment;
function sendDeleteVideoPlaylist(videoPlaylist, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to send delete of playlist %s.', videoPlaylist.url);
        const byActor = videoPlaylist.OwnerAccount.Actor;
        const url = (0, url_1.getDeleteActivityPubUrl)(videoPlaylist.url);
        const activity = buildDeleteActivity(url, videoPlaylist.url, byActor);
        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: activity,
            byActor,
            toFollowersOf,
            contextType: 'Delete',
            transaction
        });
    });
}
exports.sendDeleteVideoPlaylist = sendDeleteVideoPlaylist;
function buildDeleteActivity(url, object, byActor, audience) {
    const activity = {
        type: 'Delete',
        id: url,
        actor: byActor.url,
        object
    };
    if (audience)
        return (0, audience_1.audiencify)(activity, audience);
    return activity;
}
//# sourceMappingURL=send-delete.js.map