"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.processCreateActivity = void 0;
const tslib_1 = require("tslib");
const blocklist_1 = require("../../blocklist");
const redundancy_1 = require("../../redundancy");
const video_1 = require("../../../models/video/video");
const database_utils_1 = require("../../../helpers/database-utils");
const logger_1 = require("../../../helpers/logger");
const database_1 = require("../../../initializers/database");
const notifier_1 = require("../../notifier");
const cache_file_1 = require("../cache-file");
const local_video_viewer_1 = require("../local-video-viewer");
const playlists_1 = require("../playlists");
const send_utils_1 = require("../send/shared/send-utils");
const video_comments_1 = require("../video-comments");
const videos_1 = require("../videos");
function processCreateActivity(options) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const { activity, byActor } = options;
        const notify = options.fromFetch !== true;
        const activityObject = activity.object;
        const activityType = activityObject.type;
        if (activityType === 'Video') {
            return processCreateVideo(activity, notify);
        }
        if (activityType === 'Note') {
            if (options.fromFetch)
                return;
            return (0, database_utils_1.retryTransactionWrapper)(processCreateVideoComment, activity, byActor, notify);
        }
        if (activityType === 'WatchAction') {
            return (0, database_utils_1.retryTransactionWrapper)(processCreateWatchAction, activity);
        }
        if (activityType === 'CacheFile') {
            return (0, database_utils_1.retryTransactionWrapper)(processCreateCacheFile, activity, byActor);
        }
        if (activityType === 'Playlist') {
            return (0, database_utils_1.retryTransactionWrapper)(processCreatePlaylist, activity, byActor);
        }
        logger_1.logger.warn('Unknown activity object type %s when creating activity.', activityType, { activity: activity.id });
        return Promise.resolve(undefined);
    });
}
exports.processCreateActivity = processCreateActivity;
function processCreateVideo(activity, notify) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const videoToCreateData = activity.object;
        const syncParam = { rates: false, shares: false, comments: false, thumbnail: true, refreshVideo: false };
        const { video, created } = yield (0, videos_1.getOrCreateAPVideo)({ videoObject: videoToCreateData, syncParam });
        if (created && notify)
            notifier_1.Notifier.Instance.notifyOnNewVideoIfNeeded(video);
        return video;
    });
}
function processCreateCacheFile(activity, byActor) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        if ((yield (0, redundancy_1.isRedundancyAccepted)(activity, byActor)) !== true)
            return;
        const cacheFile = activity.object;
        const { video } = yield (0, videos_1.getOrCreateAPVideo)({ videoObject: cacheFile.object });
        yield database_1.sequelizeTypescript.transaction((t) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            return (0, cache_file_1.createOrUpdateCacheFile)(cacheFile, video, byActor, t);
        }));
        if (video.isOwned()) {
            const exceptions = [byActor];
            yield (0, send_utils_1.forwardVideoRelatedActivity)(activity, undefined, exceptions, video);
        }
    });
}
function processCreateWatchAction(activity) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const watchAction = activity.object;
        if (watchAction.actionStatus !== 'CompletedActionStatus')
            return;
        const video = yield video_1.VideoModel.loadByUrl(watchAction.object);
        if (video.remote)
            return;
        yield database_1.sequelizeTypescript.transaction((t) => tslib_1.__awaiter(this, void 0, void 0, function* () {
            return (0, local_video_viewer_1.createOrUpdateLocalVideoViewer)(watchAction, video, t);
        }));
    });
}
function processCreateVideoComment(activity, byActor, notify) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const commentObject = activity.object;
        const byAccount = byActor.Account;
        if (!byAccount)
            throw new Error('Cannot create video comment with the non account actor ' + byActor.url);
        let video;
        let created;
        let comment;
        try {
            const resolveThreadResult = yield (0, video_comments_1.resolveThread)({ url: commentObject.id, isVideo: false });
            if (!resolveThreadResult)
                return;
            video = resolveThreadResult.video;
            created = resolveThreadResult.commentCreated;
            comment = resolveThreadResult.comment;
        }
        catch (err) {
            logger_1.logger.debug('Cannot process video comment because we could not resolve thread %s. Maybe it was not a video thread, so skip it.', commentObject.inReplyTo, { err });
            return;
        }
        if (video.isOwned()) {
            if (yield (0, blocklist_1.isBlockedByServerOrAccount)(comment.Account, video.VideoChannel.Account)) {
                logger_1.logger.info('Skip comment forward from blocked account or server %s.', comment.Account.Actor.url);
                return;
            }
            if (created === true) {
                const exceptions = [byActor];
                yield (0, send_utils_1.forwardVideoRelatedActivity)(activity, undefined, exceptions, video);
            }
        }
        if (created && notify)
            notifier_1.Notifier.Instance.notifyOnNewComment(comment);
    });
}
function processCreatePlaylist(activity, byActor) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const playlistObject = activity.object;
        const byAccount = byActor.Account;
        if (!byAccount)
            throw new Error('Cannot create video playlist with the non account actor ' + byActor.url);
        yield (0, playlists_1.createOrUpdateVideoPlaylist)(playlistObject, activity.to);
    });
}
//# sourceMappingURL=process-create.js.map