"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.UpdateVideosScheduler = void 0;
const tslib_1 = require("tslib");
const video_1 = require("../../models/video/video");
const logger_1 = require("../../helpers/logger");
const constants_1 = require("../../initializers/constants");
const database_1 = require("../../initializers/database");
const schedule_video_update_1 = require("../../models/video/schedule-video-update");
const notifier_1 = require("../notifier");
const video_2 = require("../video");
const video_path_manager_1 = require("../video-path-manager");
const video_privacy_1 = require("../video-privacy");
const abstract_scheduler_1 = require("./abstract-scheduler");
class UpdateVideosScheduler extends abstract_scheduler_1.AbstractScheduler {
    constructor() {
        super();
        this.schedulerIntervalMs = constants_1.SCHEDULER_INTERVALS_MS.UPDATE_VIDEOS;
    }
    internalExecute() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return this.updateVideos();
        });
    }
    updateVideos() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            if (!(yield schedule_video_update_1.ScheduleVideoUpdateModel.areVideosToUpdate()))
                return undefined;
            const schedules = yield schedule_video_update_1.ScheduleVideoUpdateModel.listVideosToUpdate();
            for (const schedule of schedules) {
                const videoOnly = yield video_1.VideoModel.load(schedule.videoId);
                const mutexReleaser = yield video_path_manager_1.VideoPathManager.Instance.lockFiles(videoOnly.uuid);
                try {
                    const { video, published } = yield this.updateAVideo(schedule);
                    if (published)
                        notifier_1.Notifier.Instance.notifyOnVideoPublishedAfterScheduledUpdate(video);
                }
                catch (err) {
                    logger_1.logger.error('Cannot update video', { err });
                }
                mutexReleaser();
            }
        });
    }
    updateAVideo(schedule) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            let oldPrivacy;
            let isNewVideo;
            let published = false;
            const video = yield database_1.sequelizeTypescript.transaction((t) => tslib_1.__awaiter(this, void 0, void 0, function* () {
                const video = yield video_1.VideoModel.loadFull(schedule.videoId, t);
                if (video.state === 2)
                    return null;
                logger_1.logger.info('Executing scheduled video update on %s.', video.uuid);
                if (schedule.privacy) {
                    isNewVideo = video.isNewVideo(schedule.privacy);
                    oldPrivacy = video.privacy;
                    (0, video_privacy_1.setVideoPrivacy)(video, schedule.privacy);
                    yield video.save({ transaction: t });
                    if (oldPrivacy === 3) {
                        published = true;
                    }
                }
                yield schedule.destroy({ transaction: t });
                return video;
            }));
            if (!video) {
                return { video, published: false };
            }
            yield (0, video_2.addVideoJobsAfterUpdate)({ video, oldPrivacy, isNewVideo, nameChanged: false });
            return { video, published };
        });
    }
    static get Instance() {
        return this.instance || (this.instance = new this());
    }
}
exports.UpdateVideosScheduler = UpdateVideosScheduler;
//# sourceMappingURL=update-videos-scheduler.js.map