<commit_before>const _ = require('lodash');
const debug = require('@tryghost/debug');
const logging = require('@tryghost/logging');
const mailgun = require('mailgun-js');

module.exports.BATCH_SIZE = 1000;

module.exports = class MailgunClient {
    #config;
    #settings;

    constructor({config, settings}) {
        this.#config = config;
        this.#settings = settings;
    }

    /**
     * Creates the data payload and sends to Mailgun
     *
     * @param {Object} message
     * @param {Object} recipientData
     * @param {Array<Object>} replacements
     *
     * recipientData format:
     * {
     *     '<EMAIL>': {
     *         name: '<NAME>',
     *         unique_id: '12345abcde',
     *         unsubscribe_url: 'https://example.com/unsub/me'
     *     }
     * }
     */
    send(message, recipientData, replacements) {
        if (recipientData.length > module.exports.BATCH_SIZE) {
            // err - too many recipients
        }

        const mailgunInstance = this.getInstance();
        if (!mailgunInstance) {
            logging.warn(`Mailgun is not configured`);
            return;
        }

        let messageData = {};

        try {
            const bulkEmailConfig = this.#config.get('bulkEmail');
            const messageContent = _.pick(message, 'subject', 'html', 'plaintext');

            // update content to use Mailgun variable syntax for replacements
            replacements.forEach((replacement) => {
                messageContent[replacement.format] = messageContent[replacement.format].replace(
                    replacement.match,
                    `%recipient.${replacement.id}%`
                );
            });

            messageData = {
                to: Object.keys(recipientData),
                from: message.from,
                'h:Reply-To': message.replyTo || message.reply_to,
                subject: messageContent.subject,
                html: messageContent.html,
                text: messageContent.plaintext,
                'recipient-variables': recipientData
            };<commit_msg>Fixed bug when checking email batch size<commit_after>const _ = require('lodash');
const debug = require('@tryghost/debug');
const logging = require('@tryghost/logging');
const mailgun = require('mailgun-js');

module.exports.BATCH_SIZE = 1000;

module.exports = class MailgunClient {
    #config;
    #settings;

    constructor({config, settings}) {
        this.#config = config;
        this.#settings = settings;
    }

    /**
     * Creates the data payload and sends to Mailgun
     *
     * @param {Object} message
     * @param {Object} recipientData
     * @param {Array<Object>} replacements
     *
     * recipientData format:
     * {
     *     '<EMAIL>': {
     *         name: '<NAME>',
     *         unique_id: '12345abcde',
     *         unsubscribe_url: 'https://example.com/unsub/me'
     *     }
     * }
     */
    send(message, recipientData, replacements) {
        const mailgunInstance = this.getInstance();
        if (!mailgunInstance) {
            logging.warn(`Mailgun is not configured`);
            return;
        }

        if (Object.keys(recipientData).length > module.exports.BATCH_SIZE) {
            // TODO: what to do here?
        }

        let messageData = {};

        try {
            const bulkEmailConfig = this.#config.get('bulkEmail');
            const messageContent = _.pick(message, 'subject', 'html', 'plaintext');

            // update content to use Mailgun variable syntax for replacements
            replacements.forEach((replacement) => {
                messageContent[replacement.format] = messageContent[replacement.format].replace(
                    replacement.match,
                    `%recipient.${replacement.id}%`
                );
            });

            messageData = {
                to: Object.keys(recipientData),
                from: message.from,
                'h:Reply-To': message.replyTo || message.reply_to,
                subject: messageContent.subject,
                html: messageContent.html,
                text: messageContent.plaintext,
                'recipient-variables': recipientData
            };
{'fraction_non_alphanumeric': 0.11854320399904784, 'fraction_numerical': 0.0042846941204475126, 'mean_word_length': 1.8761122518822724, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3213793', 'n_tokens_mistral': 1166, 'n_tokens_neox': 1096, 'n_words': 231}