<commit_before>var nodemailer = require('nodemailer');

RulesHelper = {
  executeRules(activity) {
    const matchingRules = this.findMatchingRules(activity);
    for (let i = 0; i < matchingRules.length; i++) {
      const action = matchingRules[i].getAction();
      if (action !== undefined) {
        this.performAction(activity, action);
      }
    }
  },
  findMatchingRules(activity) {
    const activityType = activity.activityType;
    if (TriggersDef[activityType] === undefined) {
      return [];
    }
    const matchingFields = TriggersDef[activityType].matchingFields;
    const matchingMap = this.buildMatchingFieldsMap(activity, matchingFields);
    const matchingTriggers = Triggers.find(matchingMap);
    const matchingRules = [];
    matchingTriggers.forEach(function(trigger) {
      const rule = trigger.getRule();
      // Check that for some unknown reason there are some leftover triggers
      // not connected to any rules
      if (rule !== undefined) {
        matchingRules.push(trigger.getRule());
      }
    });
    return matchingRules;
  },
  buildMatchingFieldsMap(activity, matchingFields) {
    const matchingMap = { activityType: activity.activityType };
    matchingFields.forEach(field => {
      // Creating a matching map with the actual field of the activity
      // and with the wildcard (for example: trigger when a card is added
      // in any [*] board
      let value = activity[field];
      if (field === 'oldListName') {
        const oldList = Lists.findOne({ _id: activity.oldListId });
        if (oldList) {
          value = oldList.title;
        }
      } else if (field === 'oldSwimlaneName') {
        const oldSwimlane = Swimlanes.findOne({ _id: activity.oldSwimlaneId });
        if (oldSwimlane) {
          value = oldSwimlane.title;
        }
      }
      matchingMap[field] = {
        $in: [value, '*'],
      };
    });
    return matchingMap;
  },
  performAction(activity, action) {
    const card = Cards.findOne({ _id: activity.cardId });
    const boardId = activity.boardId;
    if (
      action.actionType === 'moveCardToTop' ||
      action.actionType === 'moveCardToBottom'
    ) {
      let list;
      let listId;
      if (action.listName === '*') {
        list = card.list();
        if (boardId !== action.boardId) {
          list = Lists.findOne({ title: list.title, boardId: action.boardId });
        }
      } else {
        list = Lists.findOne({
          title: action.listName,
          boardId: action.boardId,
        });<commit_msg>Feature: allow word match for rules -> title filter; e.g. filter 'Foo' will now match both 'Foo' and 'Foo bar'<commit_after>var nodemailer = require('nodemailer');

RulesHelper = {
  executeRules(activity) {
    const matchingRules = this.findMatchingRules(activity);
    for (let i = 0; i < matchingRules.length; i++) {
      const action = matchingRules[i].getAction();
      if (action !== undefined) {
        this.performAction(activity, action);
      }
    }
  },
  findMatchingRules(activity) {
    const activityType = activity.activityType;
    if (TriggersDef[activityType] === undefined) {
      return [];
    }
    const matchingFields = TriggersDef[activityType].matchingFields;
    const matchingMap = this.buildMatchingFieldsMap(activity, matchingFields);
    const matchingTriggers = Triggers.find(matchingMap);
    const matchingRules = [];
    matchingTriggers.forEach(function(trigger) {
      const rule = trigger.getRule();
      // Check that for some unknown reason there are some leftover triggers
      // not connected to any rules
      if (rule !== undefined) {
        matchingRules.push(trigger.getRule());
      }
    });
    return matchingRules;
  },
  buildMatchingFieldsMap(activity, matchingFields) {
    const matchingMap = { activityType: activity.activityType };
    matchingFields.forEach(field => {
      // Creating a matching map with the actual field of the activity
      // and with the wildcard (for example: trigger when a card is added
      // in any [*] board
      let value = activity[field];
      if (field === 'oldListName') {
        const oldList = Lists.findOne({ _id: activity.oldListId });
        if (oldList) {
          value = oldList.title;
        }
      } else if (field === 'oldSwimlaneName') {
        const oldSwimlane = Swimlanes.findOne({ _id: activity.oldSwimlaneId });
        if (oldSwimlane) {
          value = oldSwimlane.title;
        }
      }
      let matchesList = [value, '*'];
      if (field === 'cardTitle') {
        matchesList = value.split(/\W/).concat(matchesList);
      }
      matchingMap[field] = {
        $in: matchesList,
      };
    });
    return matchingMap;
  },
  performAction(activity, action) {
    const card = Cards.findOne({ _id: activity.cardId });
    const boardId = activity.boardId;
    if (
      action.actionType === 'moveCardToTop' ||
      action.actionType === 'moveCardToBottom'
    ) {
      let list;
      let listId;
      if (action.listName === '*') {
        list = card.list();
        if (boardId !== action.boardId) {
          list = Lists.findOne({ title: list.title, boardId: action.boardId });
        }
      } else {
        list = Lists.findOne({
          title: action.listName,
          boardId: action.boardId,
        });
{'fraction_non_alphanumeric': 0.12287975876366378, 'fraction_numerical': 0.0003769317753486619, 'mean_word_length': 2.5593561368209254, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '7352689', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1517, 'n_words': 374}