"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processAnnounceActivity = void 0;
const tslib_1 = require("tslib");
const activity_1 = require("../activity");
const database_utils_1 = require("../../../helpers/database-utils");
const logger_1 = require("../../../helpers/logger");
const database_1 = require("../../../initializers/database");
const video_share_1 = require("../../../models/video/video-share");
const notifier_1 = require("../../notifier");
const send_utils_1 = require("../send/shared/send-utils");
const videos_1 = require("../videos");
function processAnnounceActivity(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { activity, byActor: actorAnnouncer } = options;
        const notify = options.fromFetch !== true;
        if (actorAnnouncer.type !== 'Application' && actorAnnouncer.type !== 'Group')
            return;
        return (0, database_utils_1.retryTransactionWrapper)(processVideoShare, actorAnnouncer, activity, notify);
    });
}
exports.processAnnounceActivity = processAnnounceActivity;
function processVideoShare(actorAnnouncer, activity, notify) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const objectUri = (0, activity_1.getAPId)(activity.object);
        let video;
        let videoCreated;
        try {
            const result = yield (0, videos_1.getOrCreateAPVideo)({ videoObject: objectUri });
            video = result.video;
            videoCreated = result.created;
        }
        catch (err) {
            logger_1.logger.debug('Cannot process share of %s. Maybe this is not a video object, so just skipping.', objectUri, { err });
            return;
        }
        yield database_1.sequelizeTypescript.transaction((t) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            const share = {
                actorId: actorAnnouncer.id,
                videoId: video.id,
                url: activity.id
            };
            const [, created] = yield video_share_1.VideoShareModel.findOrCreate({
                where: {
                    url: activity.id
                },
                defaults: share,
                transaction: t
            });
            if (video.isOwned() && created === true) {
                const exceptions = [actorAnnouncer];
                yield (0, send_utils_1.forwardVideoRelatedActivity)(activity, t, exceptions, video);
            }
            return undefined;
        }));
        if (videoCreated && notify)
            notifier_1.Notifier.Instance.notifyOnNewVideoIfNeeded(video);
    });
}
//# sourceMappingURL=process-announce.js.map