<commit_before>// https://gist.github.com/mojodna/1251812

var EmailWorker = function () {};

var log = require('../utils/logger');
var path = require('path');
var EmailTemplate = require('email-templates').EmailTemplate;
var nodemailer = require('nodemailer');
var ejs = require('ejs');
var i18n = require('../utils/i18n');
var toJson = require('../utils/to_json');
var airbrake = require('../utils/airbrake');
var async = require('async');

var templatesDir = path.resolve(__dirname, '..', 'email_templates', 'notifications');

var i18nFilter = function(text) {
  return i18n.t(text);
};

var transport = nodemailer.createTransport({
  service: 'sendgrid',
  auth: {
    user: process.env.SENDGRID_USERNAME,
    pass: <PASSWORD>.SENDGRID_PASSWORD
  }
});

var translateSubject = function (subjectHash) {
  var subject = i18n.t(subjectHash.translateToken);
  if (subjectHash.contentName) {
    subject += ": "+subjectHash.contentName
  }
  return subject;
};

var linkTo = function (url) {
  return '<a href="'+url+'">'+url+'</a>';
};

EmailWorker.prototype.sendOne = function (emailLocals, callback) {
  log.info("EmailWorker Started 1", {});
  async.series([
    function (seriesCallback) {
      log.info("EmailWorker Started 2", {});
      var template = new EmailTemplate(path.join(templatesDir, emailLocals.template));

      emailLocals['t'] = i18nFilter;

      emailLocals['linkTo'] = linkTo;

      if (!emailLocals['community']) {
        emailLocals['community'] = { hostname: 'www' }
      }

      var fromEmail;

      if (emailLocals.domain && emailLocals.domain.domain_name) {
        if (emailLocals.domain.domain_name.indexOf('betrireykjavik.is') > -1) {
          fromEmail = '<EMAIL>';
        } else if (emailLocals.domain.domain_name.indexOf('betraisland.is') > -1) {
          fromEmail = '<EMAIL>';
        } else {
          fromEmail = "<EMAIL>";
        }

        var locale;

        if (emailLocals.user.default_locale && emailLocals.user.default_locale != "") {
          locale = emailLocals.user.default_locale;
        } else if (emailLocals.community && emailLocals.community.default_locale && emailLocals.community.default_locale != "") {
          locale = emailLocals.community.default_locale;
        } else if (emailLocals.domain && emailLocals.domain.default_locale && emailLocals.domain.default_locale != "") {
          locale = emailLocals.domain.default_locale;
        } else {
          locale = 'en';
        }

        emailLocals.headerImageUrl = "";

        log.info("EmailWorker Selected locale", { locale: locale });

        i18n.changeLanguage(locale, function (err, t) {
          var translatedSubject = translateSubject(emailLocals.subject);
          template.render(emailLocals, function (error, results) {
            if (error) {
              log.error('EmailWorker Error', { err: error, userID: emailLocals.user.id });
              seriesCallback(error);
            } else {
              if (process.env.SENDGRID_USERNAME) {
                transport.sendMail({
                  from: fromEmail, // emailLocals.community.admin_email,
                  to: emailLocals.user.email,
                  bcc: '<EMAIL>,<EMAIL>',
                  subject: translatedSubject,
                  html: results.html,
                  text: results.text
                }, function (error, responseStatus) {
                  if (error) {
                    log.error('EmailWorker', { err: error, user: emailLocals.user });
                    seriesCallback(error);
                  } else {
                    log.info('EmailWorker Completed', { responseStatusMessage: responseStatus.message, email: emailLocals.user.email, userId: emailLocals.user.id });
                    seriesCallback();
                  }
                })
              } else {
                log.warn('EmailWorker no SMTP server', { subject: translatedSubject, userId: emailLocals.user.id, resultsHtml: results.html , resultsText: results.text });
                seriesCallback();
              }<commit_msg>Changed<commit_after>// https://gist.github.com/mojodna/1251812

var EmailWorker = function () {};

var log = require('../utils/logger');
var path = require('path');
var EmailTemplate = require('email-templates').EmailTemplate;
var nodemailer = require('nodemailer');
var ejs = require('ejs');
var i18n = require('../utils/i18n');
var toJson = require('../utils/to_json');
var airbrake = require('../utils/airbrake');
var async = require('async');

var templatesDir = path.resolve(__dirname, '..', 'email_templates', 'notifications');

var i18nFilter = function(text) {
  return i18n.t(text);
};

var transport = nodemailer.createTransport({
  service: 'sendgrid',
  auth: {
    user: process.env.SENDGRID_USERNAME,
    pass: process.env.SENDGRID_PASSWORD
  }
});

var translateSubject = function (subjectHash) {
  var subject = i18n.t(subjectHash.translateToken);
  if (subjectHash.contentName) {
    subject += ": "+subjectHash.contentName
  }
  return subject;
};

var linkTo = function (url) {
  return '<a href="'+url+'">'+url+'</a>';
};

EmailWorker.prototype.sendOne = function (emailLocals, callback) {
  log.info("EmailWorker Started 1", {});
  async.series([
    function (seriesCallback) {
      log.info("EmailWorker Started 2", {});
      var template = new EmailTemplate(path.join(templatesDir, emailLocals.template));

      emailLocals['t'] = i18nFilter;

      emailLocals['linkTo'] = linkTo;

      if (!emailLocals['community']) {
        emailLocals['community'] = { hostname: 'www' }
      }

      var fromEmail;

      if (emailLocals.domain && emailLocals.domain.domain_name) {
        if (emailLocals.domain.domain_name.indexOf('betrireykjavik.is') > -1) {
          fromEmail = '<EMAIL>';
        } else if (emailLocals.domain.domain_name.indexOf('betraisland.is') > -1) {
          fromEmail = '<EMAIL>';
        } else {
          fromEmail = "<EMAIL>";
        }

        var locale;

        if (emailLocals.user.default_locale && emailLocals.user.default_locale != "") {
          locale = emailLocals.user.default_locale;
        } else if (emailLocals.community && emailLocals.community.default_locale && emailLocals.community.default_locale != "") {
          locale = emailLocals.community.default_locale;
        } else if (emailLocals.domain && emailLocals.domain.default_locale && emailLocals.domain.default_locale != "") {
          locale = emailLocals.domain.default_locale;
        } else {
          locale = 'en';
        }

        emailLocals.headerImageUrl = "";

        log.info("EmailWorker Selected locale", { locale: locale });

        i18n.changeLanguage(locale, function (err, t) {
          var translatedSubject = translateSubject(emailLocals.subject);
          template.render(emailLocals, function (error, results) {
            if (error) {
              log.error('EmailWorker Error', { err: error, userID: emailLocals.user.id });
              seriesCallback(error);
            } else {
              if (process.env.SENDGRID_USERNAME) {
                transport.sendMail({
                  from: fromEmail, // emailLocals.community.admin_email,
                  to: emailLocals.user.email,
                  bcc: '<EMAIL>',
                  subject: translatedSubject,
                  html: results.html,
                  text: results.text
                }, function (error, responseStatus) {
                  if (error) {
                    log.error('EmailWorker', { err: error, user: emailLocals.user });
                    seriesCallback(error);
                  } else {
                    log.info('EmailWorker Completed', { responseStatusMessage: responseStatus.message, email: emailLocals.user.email, userId: emailLocals.user.id });
                    seriesCallback();
                  }
                })
              } else {
                log.warn('EmailWorker no SMTP server', { subject: translatedSubject, userId: emailLocals.user.id, resultsHtml: results.html , resultsText: results.text });
                seriesCallback();
              }
{'fraction_non_alphanumeric': 0.13588634959851761, 'fraction_numerical': 0.006176652254478073, 'mean_word_length': 2.474678111587983, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1600194', 'n_tokens_mistral': 2363, 'n_tokens_neox': 2271, 'n_words': 440}