<commit_before>'use strict';

const request = require('request');
const qs = require('qs');
const _ = require('lodash');
const moment = require('moment');

const config = require('../config');
const fbReqGenerator = require('../config/fbFields');

exports.exchangeCodeForToken = (code, callback) => {
  const tokenRequestOptions = {
    url: 'https://graph.facebook.com/v2.5/oauth/access_token',
    qs: {
      code: code,
      client_id: config.fb.appID,
      client_secret: config.fb.appSecret,
      redirect_uri: config.webServerURL + '/facebook/authenticate'
    }
  };

  let now = moment();

  request.get(tokenRequestOptions, (err, res, body) => {
    if (err) return callback(err);

    let bodyJson;

    try {
      bodyJson = JSON.parse(body);
    } catch (e) {
      return callback(e);
    }

    let userPermissions = {
      accessToken: bodyJson.access_token,
      validUntil: now.add(bodyJson.expires_in, "s")
    };

    return callback(null, userPermissions);
  });
};

exports.post = (accessToken, message, callback) => {
  const reqOptions = {
    url: 'https://graph.facebook.com/v2.5/me/feed',
    qs: { access_token: accessToken },
    body: { message: message },
    json: true
  };

  request.post(reqOptions, (err, res, body) => {
    if (err) return callback(err);
    if (res.statusCode !== 200) return callback(body);

    return callback(null, body.id);
  });
};

exports.update = (accessToken, message, postId, callback) => {
  const reqOptions = {
    url: `https://graph.facebook.com/v2.5/${postId}`,
    qs: { access_token: accessToken },
    body: { message: message },
    json: true
  };

  request.post(reqOptions, (err, res, body) => {
    if (err) return callback(err);

    return callback(null, body);
  });
};

exports.delete = (accessToken, postId, callback) => {
  const reqOptions = {
    url: `https://graph.facebook.com/v2.5/${postId}`,
    qs: { access_token: accessToken },
    json: true
  };

  request.delete(reqOptions, (err, res, body) => {
    if (err) return callback (err);

    return callback(null, body);
  });
};

exports.getUserPermissions = (accessToken, callback) => {
  const reqOptions = {
    url: 'https://graph.facebook.com/v2.5/me/permissions',
    qs: { access_token: accessToken },
    json: true
  };

  request.get(reqOptions, (err, res, body) => {
    if (err) return callback(err);

    return callback(null, body.data)
  });
};

exports.getGraphNode = (node, accessToken, lastestUpdateTime, callback) => {
  let fbUrl;
  if (node === 'profile_picture') {
    fbUrl = fbReqGenerator.getProfilePictureUrl(accessToken);
  } else if (node === 'music_listens') {
    fbUrl = fbReqGenerator.getBaseUrl(accessToken, lastestUpdateTime);
  } else {
    fbUrl = fbReqGenerator.getRequestUrl(node, accessToken, lastestUpdateTime);
  }

  request.get({ url: fbUrl, json: true }, (err, res, body) => {
    if (err) return callback(err);

    const data = body.data ? body.data : body;
    const dataArray = Array.isArray(data) ? data : [data];

    if (dataArray.length < 1) {
      console.log('[FB] No data to process.')
      return callback(new Error('No data to process'));
    }

    return callback(null, dataArray);
  });
};

exports.revokeLogin = (accessToken, callback) => {
  const reqOptions = {
    url: 'https://graph.facebook.com/me/permissions',
    qs: { access_token: accessToken },
    json: true
  };

  request.delete(reqOptions, (err, res, body) => {
    if (err) return callback(err);
    if (body.success === true) {<commit_msg>Fixes bug to prevent timeouts during facebook deauthorization<commit_after>'use strict';

const request = require('request');
const qs = require('qs');
const _ = require('lodash');
const moment = require('moment');

const config = require('../config');
const fbReqGenerator = require('../config/fbFields');

exports.exchangeCodeForToken = (code, callback) => {
  const tokenRequestOptions = {
    url: 'https://graph.facebook.com/v2.5/oauth/access_token',
    qs: {
      code: code,
      client_id: config.fb.appID,
      client_secret: config.fb.appSecret,
      redirect_uri: config.webServerURL + '/facebook/authenticate'
    }
  };

  let now = moment();

  request.get(tokenRequestOptions, (err, res, body) => {
    if (err) return callback(err);

    let bodyJson;

    try {
      bodyJson = JSON.parse(body);
    } catch (e) {
      return callback(e);
    }

    let userPermissions = {
      accessToken: bodyJson.access_token,
      validUntil: now.add(bodyJson.expires_in, "s")
    };

    return callback(null, userPermissions);
  });
};

exports.post = (accessToken, message, callback) => {
  const reqOptions = {
    url: 'https://graph.facebook.com/v2.5/me/feed',
    qs: { access_token: accessToken },
    body: { message: message },
    json: true
  };

  request.post(reqOptions, (err, res, body) => {
    if (err) return callback(err);
    if (res.statusCode !== 200) return callback(body);

    return callback(null, body.id);
  });
};

exports.update = (accessToken, message, postId, callback) => {
  const reqOptions = {
    url: `https://graph.facebook.com/v2.5/${postId}`,
    qs: { access_token: accessToken },
    body: { message: message },
    json: true
  };

  request.post(reqOptions, (err, res, body) => {
    if (err) return callback(err);

    return callback(null, body);
  });
};

exports.delete = (accessToken, postId, callback) => {
  const reqOptions = {
    url: `https://graph.facebook.com/v2.5/${postId}`,
    qs: { access_token: accessToken },
    json: true
  };

  request.delete(reqOptions, (err, res, body) => {
    if (err) return callback (err);

    return callback(null, body);
  });
};

exports.getUserPermissions = (accessToken, callback) => {
  const reqOptions = {
    url: 'https://graph.facebook.com/v2.5/me/permissions',
    qs: { access_token: accessToken },
    json: true
  };

  request.get(reqOptions, (err, res, body) => {
    if (err) return callback(err);

    return callback(null, body.data)
  });
};

exports.getGraphNode = (node, accessToken, lastestUpdateTime, callback) => {
  let fbUrl;
  if (node === 'profile_picture') {
    fbUrl = fbReqGenerator.getProfilePictureUrl(accessToken);
  } else if (node === 'music_listens') {
    fbUrl = fbReqGenerator.getBaseUrl(accessToken, lastestUpdateTime);
  } else {
    fbUrl = fbReqGenerator.getRequestUrl(node, accessToken, lastestUpdateTime);
  }

  request.get({ url: fbUrl, json: true }, (err, res, body) => {
    if (err) return callback(err);

    const data = body.data ? body.data : body;
    const dataArray = Array.isArray(data) ? data : [data];

    if (dataArray.length < 1) {
      console.log('[FB] No data to process.')
      return callback(new Error('No data to process'));
    }

    return callback(null, dataArray);
  });
};

exports.revokeLogin = (accessToken, callback) => {
  const reqOptions = {
    url: 'https://graph.facebook.com/me/permissions',
    qs: { access_token: accessToken },
    json: true
  };

  request.delete(reqOptions, (err, res, body) => {
    if (err) {
      return callback(err);
    } else if (res.statusCode !== 200) {

      if (body.error && body.error.type === "OAuthException") {
        return callback(null);
      } else {
        return callback(body);
      }

    } else {
{'fraction_non_alphanumeric': 0.15892218861699203, 'fraction_numerical': 0.004261754193016222, 'mean_word_length': 3.2743830787309047, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 31, 'https://': 12, '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': '6057957', 'n_tokens_mistral': 2437, 'n_tokens_neox': 2356, 'n_words': 538}