"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sendCreateWatchAction = exports.sendCreateCacheFile = exports.sendCreateVideoPlaylist = exports.sendCreateVideoComment = exports.buildCreateActivity = exports.sendCreateVideo = 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 audience_1 = require("../audience");
const shared_1 = require("./shared");
const lTags = (0, logger_1.loggerTagsFactory)('ap', 'create');
function sendCreateVideo(video, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (!video.hasPrivacyForFederation())
            return undefined;
        logger_1.logger.info('Creating job to send video creation of %s.', video.url, lTags(video.uuid));
        const byActor = video.VideoChannel.Account.Actor;
        const videoObject = video.toActivityPubObject();
        const audience = (0, audience_1.getAudience)(byActor, video.privacy === 1);
        const createActivity = buildCreateActivity(video.url, byActor, videoObject, audience);
        return (0, shared_1.broadcastToFollowers)({
            data: createActivity,
            byActor,
            toFollowersOf: [byActor],
            transaction,
            contextType: 'Video'
        });
    });
}
exports.sendCreateVideo = sendCreateVideo;
function sendCreateCacheFile(byActor, video, fileRedundancy) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to send file cache of %s.', fileRedundancy.url, lTags(video.uuid));
        return sendVideoRelatedCreateActivity({
            byActor,
            video,
            url: fileRedundancy.url,
            object: fileRedundancy.toActivityPubObject(),
            contextType: 'CacheFile'
        });
    });
}
exports.sendCreateCacheFile = sendCreateCacheFile;
function sendCreateWatchAction(stats, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to send create watch action %s.', stats.url, lTags(stats.uuid));
        const byActor = yield (0, application_1.getServerActor)();
        const activityBuilder = (audience) => {
            return buildCreateActivity(stats.url, byActor, stats.toActivityPubObject(), audience);
        };
        return (0, shared_1.sendVideoActivityToOrigin)(activityBuilder, { byActor, video: stats.Video, transaction, contextType: 'WatchAction' });
    });
}
exports.sendCreateWatchAction = sendCreateWatchAction;
function sendCreateVideoPlaylist(playlist, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if (playlist.privacy === 3)
            return undefined;
        logger_1.logger.info('Creating job to send create video playlist of %s.', playlist.url, lTags(playlist.uuid));
        const byActor = playlist.OwnerAccount.Actor;
        const audience = (0, audience_1.getAudience)(byActor, playlist.privacy === 1);
        const object = yield playlist.toActivityPubObject(null, transaction);
        const createActivity = buildCreateActivity(playlist.url, byActor, object, audience);
        const serverActor = yield (0, application_1.getServerActor)();
        const toFollowersOf = [byActor, serverActor];
        if (playlist.VideoChannel)
            toFollowersOf.push(playlist.VideoChannel.Actor);
        return (0, shared_1.broadcastToFollowers)({
            data: createActivity,
            byActor,
            toFollowersOf,
            transaction,
            contextType: 'Playlist'
        });
    });
}
exports.sendCreateVideoPlaylist = sendCreateVideoPlaylist;
function sendCreateVideoComment(comment, transaction) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        logger_1.logger.info('Creating job to send comment %s.', comment.url);
        const isOrigin = comment.Video.isOwned();
        const byActor = comment.Account.Actor;
        const threadParentComments = yield video_comment_1.VideoCommentModel.listThreadParentComments(comment, transaction);
        const commentObject = comment.toActivityPubObject(threadParentComments);
        const actorsInvolvedInComment = yield (0, shared_1.getActorsInvolvedInVideo)(comment.Video, transaction);
        actorsInvolvedInComment.push(byActor);
        const parentsCommentActors = threadParentComments.filter(c => !c.isDeleted())
            .map(c => c.Account.Actor);
        let audience;
        if (isOrigin) {
            audience = (0, shared_1.getVideoCommentAudience)(comment, threadParentComments, actorsInvolvedInComment, isOrigin);
        }
        else {
            audience = (0, shared_1.getAudienceFromFollowersOf)(actorsInvolvedInComment.concat(parentsCommentActors));
        }
        const createActivity = buildCreateActivity(comment.url, byActor, commentObject, audience);
        const actorsException = [byActor];
        yield (0, shared_1.broadcastToActors)({
            data: createActivity,
            byActor,
            toActors: parentsCommentActors,
            transaction,
            actorsException,
            contextType: 'Comment'
        });
        yield (0, shared_1.broadcastToFollowers)({
            data: createActivity,
            byActor,
            toFollowersOf: [byActor],
            transaction,
            contextType: 'Comment'
        });
        if (isOrigin) {
            return (0, shared_1.broadcastToFollowers)({
                data: createActivity,
                byActor,
                toFollowersOf: actorsInvolvedInComment,
                transaction,
                actorsException,
                contextType: 'Comment'
            });
        }
        return transaction.afterCommit(() => {
            return (0, shared_1.unicastTo)({
                data: createActivity,
                byActor,
                toActorUrl: comment.Video.VideoChannel.Account.Actor.getSharedInbox(),
                contextType: 'Comment'
            });
        });
    });
}
exports.sendCreateVideoComment = sendCreateVideoComment;
function buildCreateActivity(url, byActor, object, audience) {
    if (!audience)
        audience = (0, audience_1.getAudience)(byActor);
    return (0, audience_1.audiencify)({
        type: 'Create',
        id: url + '/activity',
        actor: byActor.url,
        object: (0, audience_1.audiencify)(object, audience)
    }, audience);
}
exports.buildCreateActivity = buildCreateActivity;
function sendVideoRelatedCreateActivity(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const activityBuilder = (audience) => {
            return buildCreateActivity(options.url, options.byActor, options.object, audience);
        };
        return (0, shared_1.sendVideoRelatedActivity)(activityBuilder, options);
    });
}
//# sourceMappingURL=send-create.js.map