"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const lodash_1 = require("lodash");
const path_1 = require("path");
const image_utils_1 = require("../../server/helpers/image-utils");
const config_1 = require("../../server/initializers/config");
const constants_1 = require("../../server/initializers/constants");
const actors_1 = require("../../server/lib/activitypub/actors");
const send_1 = require("../../server/lib/activitypub/send");
const actor_image_1 = require("../../server/lib/actor-image");
const job_queue_1 = require("../../server/lib/job-queue");
const account_1 = require("../../server/models/account/account");
const actor_1 = require("../../server/models/actor/actor");
const video_channel_1 = require("../../server/models/video/video-channel");
const core_utils_1 = require("../../shared/core-utils");
const extra_utils_1 = require("../../shared/extra-utils");
const database_1 = require("../../server/initializers/database");
run()
    .then(() => process.exit(0))
    .catch(err => {
    console.error(err);
    process.exit(-1);
});
function run() {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        console.log('Generate avatar miniatures from existing avatars.');
        yield (0, database_1.initDatabaseModels)(true);
        job_queue_1.JobQueue.Instance.init();
        const accounts = yield account_1.AccountModel.findAll({
            include: [
                {
                    model: actor_1.ActorModel,
                    required: true,
                    where: {
                        serverId: null
                    }
                },
                {
                    model: video_channel_1.VideoChannelModel,
                    include: [
                        {
                            model: account_1.AccountModel
                        }
                    ]
                }
            ]
        });
        for (const account of accounts) {
            try {
                yield fillAvatarSizeIfNeeded(account);
                yield generateSmallerAvatarIfNeeded(account);
            }
            catch (err) {
                console.error(`Cannot process account avatar ${account.name}`, err);
            }
            for (const videoChannel of account.VideoChannels) {
                try {
                    yield fillAvatarSizeIfNeeded(videoChannel);
                    yield generateSmallerAvatarIfNeeded(videoChannel);
                }
                catch (err) {
                    console.error(`Cannot process channel avatar ${videoChannel.name}`, err);
                }
            }
        }
        console.log('Generation finished!');
    });
}
function fillAvatarSizeIfNeeded(accountOrChannel) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const avatars = accountOrChannel.Actor.Avatars;
        for (const avatar of avatars) {
            if (avatar.width && avatar.height)
                continue;
            console.log('Filling size of avatars of %s.', accountOrChannel.name);
            const { width, height } = yield (0, image_utils_1.getImageSize)((0, path_1.join)(config_1.CONFIG.STORAGE.ACTOR_IMAGES, avatar.filename));
            avatar.width = width;
            avatar.height = height;
            yield avatar.save();
        }
    });
}
function generateSmallerAvatarIfNeeded(accountOrChannel) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const avatars = accountOrChannel.Actor.Avatars;
        if (avatars.length !== 1) {
            return;
        }
        console.log(`Processing ${accountOrChannel.name}.`);
        yield generateSmallerAvatar(accountOrChannel.Actor);
        accountOrChannel.Actor = Object.assign(accountOrChannel.Actor, { Server: null });
        return (0, send_1.sendUpdateActor)(accountOrChannel, undefined);
    });
}
function generateSmallerAvatar(actor) {
    return tslib_1.__awaiter(this, void 0, void 0, function* () {
        const bigAvatar = (0, actor_image_1.getBiggestActorImage)(actor.Avatars);
        const imageSize = (0, lodash_1.minBy)(constants_1.ACTOR_IMAGES_SIZE[1], 'width');
        const sourceFilename = bigAvatar.filename;
        const newImageName = (0, extra_utils_1.buildUUID)() + (0, core_utils_1.getLowercaseExtension)(sourceFilename);
        const source = (0, path_1.join)(config_1.CONFIG.STORAGE.ACTOR_IMAGES, sourceFilename);
        const destination = (0, path_1.join)(config_1.CONFIG.STORAGE.ACTOR_IMAGES, newImageName);
        yield (0, image_utils_1.processImage)({ path: source, destination, newSize: imageSize, keepOriginal: true });
        const actorImageInfo = {
            name: newImageName,
            fileUrl: null,
            height: imageSize.height,
            width: imageSize.width,
            onDisk: true
        };
        yield (0, actors_1.updateActorImages)(actor, 1, [actorImageInfo], undefined);
    });
}
//# sourceMappingURL=peertube-4.2.js.map