<commit_before>'use strict';

const _ = require('lodash');
const fs = require('fs');
const Web3 = require('web3');

const config = require('../config');
const notifier = require('../notifications');
const userHelpers = require('../users/helpers');

const ABI = fs.readFileSync(__dirname + "/ABI.txt", "utf8").trim();
const address = fs.readFileSync(__dirname + "/contractAddress.txt", "utf8").trim();

const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(config.getGethUrl()));
const contractInstance = web3.eth.contract(JSON.parse(ABI)).at(address);

const allEvents = contractInstance.allEvents();
allEvents.watch((err, result) => {
  if (err) {
    return logError("allEvents", err);
  }
  console.log("Result: ", JSON.stringify(result) + "\n");
  console.log("Result Args: ", JSON.stringify(result.args) + "\n");

  eventResultToData(result)
    .then(eventData => {
      console.log(`Notification: ${JSON.stringify(eventData.message)} \n`);
      notifyUser(eventData.eventName, eventData.message, eventData.receiverEthAccount);
    })
    .catch(err => logError('eventResultToData', err));
});

// helper functions
const eventResultToData = (eventResult) => {

  const eventName = eventResult.event;

  const argData = JSON.parse(eventResult.args.data);
  argData.dealId = eventResult.args.deal_id.toString();

  const receiverEthAccount = argData.to;
  const senderEthAccount = argData.from;

  argData.transactionHash = eventResult.transactionHash;
  argData.status = eventName;

  return userHelpers.findUser({ ethAccount: senderEthAccount })
    .then(user => {
      if (!user) {
        return Promise.reject(Error("Cannot find sender Ethereum user account: " + senderEthAccount));
      }

      argData.invoker = user._id;

      const data = {
        body: JSON.stringify(_.omit(argData, ['to']))
      };
<commit_msg>Revert "Send `user._id` in notification instead of `user.email`"<commit_after>'use strict';

const _ = require('lodash');
const fs = require('fs');
const Web3 = require('web3');

const config = require('../config');
const notifier = require('../notifications');
const userHelpers = require('../users/helpers');

const ABI = fs.readFileSync(__dirname + "/ABI.txt", "utf8").trim();
const address = fs.readFileSync(__dirname + "/contractAddress.txt", "utf8").trim();

const web3 = new Web3();
web3.setProvider(new web3.providers.HttpProvider(config.getGethUrl()));
const contractInstance = web3.eth.contract(JSON.parse(ABI)).at(address);

const allEvents = contractInstance.allEvents();
allEvents.watch((err, result) => {
  if (err) {
    return logError("allEvents", err);
  }
  console.log("Result: ", JSON.stringify(result) + "\n");
  console.log("Result Args: ", JSON.stringify(result.args) + "\n");

  eventResultToData(result)
    .then(eventData => {
      console.log(`Notification: ${JSON.stringify(eventData.message)} \n`);
      notifyUser(eventData.eventName, eventData.message, eventData.receiverEthAccount);
    })
    .catch(err => logError('eventResultToData', err));
});

// helper functions
const eventResultToData = (eventResult) => {

  const eventName = eventResult.event;

  const argData = JSON.parse(eventResult.args.data);
  argData.dealId = eventResult.args.deal_id.toString();

  const receiverEthAccount = argData.to;
  const senderEthAccount = argData.from;

  argData.transactionHash = eventResult.transactionHash;
  argData.status = eventName;

  return userHelpers.findUser({ ethAccount: senderEthAccount })
    .then(user => {
      if (!user) {
        return Promise.reject(Error("Cannot find sender Ethereum user account: " + senderEthAccount));
      }

      argData.invoker = user.email;

      const data = {
        body: JSON.stringify(_.omit(argData, ['to']))
      };

{'fraction_non_alphanumeric': 0.1593625498007968, 'fraction_numerical': 0.004780876494023904, 'mean_word_length': 4.395415472779369, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, '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': '1315279', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1143, 'n_words': 229}