"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.outboxRouter = void 0;
const tslib_1 = require("tslib");
const express_1 = tslib_1.__importDefault(require("express"));
const collection_1 = require("../../lib/activitypub/collection");
const context_1 = require("../../lib/activitypub/context");
const logger_1 = require("../../helpers/logger");
const audience_1 = require("../../lib/activitypub/audience");
const send_1 = require("../../lib/activitypub/send");
const middlewares_1 = require("../../middlewares");
const activitypub_1 = require("../../middlewares/validators/activitypub");
const video_1 = require("../../models/video/video");
const utils_1 = require("./utils");
const outboxRouter = express_1.default.Router();
exports.outboxRouter = outboxRouter;
outboxRouter.get('/accounts/:name/outbox', activitypub_1.apPaginationValidator, middlewares_1.localAccountValidator, (0, middlewares_1.asyncMiddleware)(outboxController));
outboxRouter.get('/video-channels/:nameWithHost/outbox', activitypub_1.apPaginationValidator, (0, middlewares_1.asyncMiddleware)(middlewares_1.videoChannelsNameWithHostValidator), middlewares_1.ensureIsLocalChannel, (0, middlewares_1.asyncMiddleware)(outboxController));
function outboxController(req, res) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const accountOrVideoChannel = res.locals.account || res.locals.videoChannel;
        const actor = accountOrVideoChannel.Actor;
        const actorOutboxUrl = actor.url + '/outbox';
        logger_1.logger.info('Receiving outbox request for %s.', actorOutboxUrl);
        const handler = (start, count) => buildActivities(actor, start, count);
        const json = yield (0, collection_1.activityPubCollectionPagination)(actorOutboxUrl, handler, req.query.page, req.query.size);
        return (0, utils_1.activityPubResponse)((0, context_1.activityPubContextify)(json, 'Collection'), res);
    });
}
function buildActivities(actor, start, count) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const data = yield video_1.VideoModel.listAllAndSharedByActorForOutbox(actor.id, start, count);
        const activities = [];
        for (const video of data.data) {
            const byActor = video.VideoChannel.Account.Actor;
            const createActivityAudience = (0, audience_1.buildAudience)([byActor.followersUrl], video.privacy === 1);
            if (video.VideoShares !== undefined && video.VideoShares.length !== 0) {
                const videoShare = video.VideoShares[0];
                const announceActivity = (0, send_1.buildAnnounceActivity)(videoShare.url, actor, video.url, createActivityAudience);
                activities.push(announceActivity);
            }
            else {
                const videoObject = video.toActivityPubObject();
                const createActivity = (0, send_1.buildCreateActivity)(video.url, byActor, videoObject, createActivityAudience);
                activities.push(createActivity);
            }
        }
        return {
            data: activities,
            total: data.total
        };
    });
}
//# sourceMappingURL=outbox.js.map