<commit_before>'use strict';
/**
 * This object contains all the handlers to use for this provider
 */
var rarity = require('rarity');
var async = require('async');
var CancelError = require('anyfetch-provider').CancelError;
var Evernote = require('evernote').Evernote;

var config = require('../config/configuration.js');
var uploadNote = require('./helpers/upload.js');
var retrieveChanges = require('./helpers/retrieve.js');
var deleteNote = require('./helpers/delete.js');
var client = new Evernote.Client(config.clientConfig);


var redirectToService = function redirectToService(callbackUrl, cb) {
  // Redirect user to provider consentment page
  client.getRequestToken(callbackUrl, function(error, oauthToken, oauthTokenSecret) {
    var redirectUrl = client.getAuthorizeUrl(oauthToken);
    cb(null, redirectUrl, {
      redirectUrl: redirectUrl,
      callbackUrl: callbackUrl,
      oauthTokenSecret: oauthTokenSecret
    });
  });
};

var retrieveTokens = function retrieveTokens(reqParams, storedParams, cb) {
  if(!reqParams.oauth_verifier) {
    return cb(new CancelError());
  }

  async.waterfall([
    function getToken(cb) {
      client.getAccessToken(reqParams.oauth_token, storedParams.oauthTokenSecret, reqParams.oauth_verifier, cb);
    },
    function getUserInfo(token, tokenSecret, results, cb) {
      var client = new Evernote.Client({ token: token, sandbox: config.clientConfig.sandbox });
      var userStore = client.getUserStore();
      userStore.getUser(rarity.carry([token], cb));
    },
    function callFinalCb(token, data, cb) {
      cb(null, data.username, {token: token, callbackUrl: storedParams.callbackUrl});
    }
  ], cb);
};

var updateAccount = function updateAccount(serviceData, cursor, queues, cb) {
  // Retrieve all files since last call
  if(!serviceData.token) {
    return cb('token can\'t be ' + serviceData.token);
  }
  if(!cursor) {
    cursor = 0;
  }
  async.waterfall([
    function getChanges(cb) {
      retrieveChanges(cursor, serviceData.token, cb);
    },
    function applyChanges(newCursor, changes, cb) {
      if(!changes) {
        return cb(null, newCursor, serviceData);
      }
      changes.forEach(function(change) {
        if(change.deleted) {
          queues.deletion.push({
            title: change.title,
            id: change.guid
          });
        }
        else {
          queues.addition.push({
            title: change.title,
            url: config.evernoteRoot + '/Home.action?#st=p&n=' + change.guid,
            created: new Date(change.created),
            updated: new Date(change.updated),
            id: change.guid,
          });
        }
      });

      cb(null, newCursor, serviceData);
    }
  ], cb);
};

var additionQueueWorker = function additionQueueWorker(job, cb) {
  uploadNote(job.task, job.anyfetchClient, job.serviceData.token, cb);
};

var deletionQueueWorker = function deletionQueueWorker(job, cb) {
  deleteNote(job.task, job.anyfetchClient, cb);
};

module.exports = {
  connectFunctions: {
    redirectToService: redirectToService,
    retrieveTokens: retrieveTokens
  },
  updateAccount: updateAccount,
  workers: {
    addition: additionQueueWorker,
    deletion: deletionQueueWorker
  },

  config: config
};<commit_msg>Fix a bug<commit_after>'use strict';
/**
 * This object contains all the handlers to use for this provider
 */
var rarity = require('rarity');
var async = require('async');
var CancelError = require('anyfetch-provider').CancelError;
var Evernote = require('evernote').Evernote;

var config = require('../config/configuration.js');
var uploadNote = require('./helpers/upload.js');
var retrieveChanges = require('./helpers/retrieve.js');
var deleteNote = require('./helpers/delete.js');
var client = new Evernote.Client(config.clientConfig);


var redirectToService = function redirectToService(callbackUrl, cb) {
  // Redirect user to provider consentment page
  client.getRequestToken(callbackUrl, function(error, oauthToken, oauthTokenSecret) {
    var redirectUrl = client.getAuthorizeUrl(oauthToken);
    cb(null, redirectUrl, {
      redirectUrl: redirectUrl,
      callbackUrl: callbackUrl,
      oauthTokenSecret: oauthTokenSecret
    });
  });
};

var retrieveTokens = function retrieveTokens(reqParams, storedParams, cb) {
  if(!reqParams.oauth_verifier) {
    return cb(new CancelError());
  }

  async.waterfall([
    function getToken(cb) {
      client.getAccessToken(reqParams.oauth_token, storedParams.oauthTokenSecret, reqParams.oauth_verifier, cb);
    },
    function getUserInfo(token, tokenSecret, results, cb) {
      var client = new Evernote.Client({ token: token, sandbox: config.clientConfig.sandbox });
      var userStore = client.getUserStore();
      userStore.getUser(rarity.carry([token], cb));
    },
    function callFinalCb(token, data, cb) {
      cb(null, data.username, {token: token, callbackUrl: storedParams.callbackUrl});
    }
  ], cb);
};

var updateAccount = function updateAccount(serviceData, cursor, queues, cb) {
  // Retrieve all files since last call
  if(!serviceData.token) {
    return cb('token can\'t be ' + serviceData.token);
  }
  if(!cursor) {
    cursor = 0;
  }
  async.waterfall([
    function getChanges(cb) {
      retrieveChanges(cursor, serviceData.token, cb);
    },
    function applyChanges(newCursor, changes, cb) {
      if(!changes) {
        return cb(null, newCursor, serviceData);
      }
      changes.forEach(function(change) {
        if(change.deleted) {
          queues.deletion.push({
            title: change.title,
            id: change.guid
          });
        }
        else {
          queues.addition.push({
            title: change.title,
            url: config.evernoteRoot + '/Home.action?#st=p&n=' + change.guid,
            created: new Date(change.created),
            updated: new Date(change.updated),
            id: change.guid,
          });
        }
      });

      cb(null, newCursor, serviceData);
    }
  ], cb);
};

var additionQueueWorker = function additionQueueWorker(job, cb) {
  uploadNote(job.task, job.anyfetchClient, job.serviceData.token, cb);
};

var deletionQueueWorker = function deletionQueueWorker(job, cb) {
  deleteNote(job.task, job.anyfetchClient, function(err) {
    if(err && err.toString().match(/expected 204 "No Content", got 404 "Not Found"/i)) {
      err = null;
    }

    cb(err);
  });
};

module.exports = {
  connectFunctions: {
    redirectToService: redirectToService,
    retrieveTokens: retrieveTokens
  },
  updateAccount: updateAccount,
  workers: {
    addition: additionQueueWorker,
    deletion: deletionQueueWorker
  },

  config: config
};
{'fraction_non_alphanumeric': 0.12669274751730364, 'fraction_numerical': 0.0012037315678603672, 'mean_word_length': 3.3444444444444446, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1141535', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1956, 'n_words': 432}