<commit_before>'use strict';

const Bluebird = require('bluebird');
const request = require('request-promise');

const DynamoService = require('./DynamoService');
const Utils = require('./Utils');

const MSG_DELAY = Math.floor(Math.random() * (process.env.DELAY_MULTIPLIER || 1000)) + (process.env.DELAY_FLOOR || 1000);

module.exports = {
  sendMessages: function (content, userId) {
    return request.post({
      json: true,
      url: `https://graph.facebook.com/v2.6/me/messages?access_token=${process.env.FB_PAGE_ACCESS_TOKEN}`,
      body: {
        recipient: { id: userId },
        message: { text: content }
      },
      headers: { 'Content-Type': 'application/json' }
    });
  },

  sendDynamic: function (type, userId) {
    return DynamoService.findDynamic(type)
      .then(dynamic => {
        if (!dynamic || !userId) return;

        const messages = Utils.isJson(dynamic.messages);

        if (!Array.isArray(messages) || messages.length === 0) {
          return;
        }

        return this.sendMessages(messages[Utils.rdmKey(messages)], userId);
      });
  },

  sendMsgArray: function (msgArr, userId) {
    if (!userId) {
      return Bluebird.reject(new Error('userId was not supplied'));
    }

    const messages = Utils.isJson(msgArr);

    if (Array.isArray(messages) && messages.length > 0) {
      return Bluebird.each(messages, msg => Bluebird.delay(MSG_DELAY)
        .then(() => this.sendMessages(msg, userId)));
    }

    return Bluebird.resolve();
  }
};<commit_msg>Add a delay after all messages in an array are sent<commit_after>'use strict';

const Bluebird = require('bluebird');
const request = require('request-promise');

const DynamoService = require('./DynamoService');
const Utils = require('./Utils');

const MSG_DELAY = Math.floor(Math.random() * (process.env.DELAY_MULTIPLIER || 1000)) + (process.env.DELAY_FLOOR || 1000);

module.exports = {
  sendMessages: function (content, userId) {
    return request.post({
      json: true,
      url: `https://graph.facebook.com/v2.6/me/messages?access_token=${process.env.FB_PAGE_ACCESS_TOKEN}`,
      body: {
        recipient: { id: userId },
        message: { text: content }
      },
      headers: { 'Content-Type': 'application/json' }
    });
  },

  sendDynamic: function (type, userId) {
    return DynamoService.findDynamic(type)
      .then(dynamic => {
        if (!dynamic || !userId) return;

        const messages = Utils.isJson(dynamic.messages);

        if (!Array.isArray(messages) || messages.length === 0) {
          return;
        }

        return this.sendMessages(messages[Utils.rdmKey(messages)], userId);
      });
  },

  sendMsgArray: function (msgArr, userId) {
    if (!userId) {
      return Bluebird.reject(new Error('userId was not supplied'));
    }

    const messages = Utils.isJson(msgArr);

    if (Array.isArray(messages) && messages.length > 0) {
      return Bluebird.each(messages, msg => Bluebird.delay(MSG_DELAY)
        .then(() => this.sendMessages(msg, userId)))
        .then(() => Bluebird.delay(MSG_DELAY));
    }

    return Bluebird.resolve();
  }
};
{'fraction_non_alphanumeric': 0.15503377291733678, 'fraction_numerical': 0.0077195239626889674, 'mean_word_length': 3.113756613756614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 12, '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': '2547694', 'n_tokens_mistral': 983, 'n_tokens_neox': 964, 'n_words': 196}