<commit_before>var _  = require('underscore');
var moment = require('moment');
var FacebookStrategy = require('passport-facebook').Strategy;
var FacebookClient = require("facebook-client").FacebookClient;

module.exports = {
	id: "facebook",
	name: "Facebook",
	description: "Track your social activity and your relations from Facebook.",

	setup: function() {
		var that = this;
		_.defaults(this.config, {
			'interval': 60*60
		});

		// oAuth
		if (!this.setupOAuth(FacebookStrategy, {
			'scope': ['read_mailbox']
		})) {
			return;
		}
		
		// Cron to index
		this.addTask(function(user) {
			var userConfig = user.getTrackerSettings(this.id);
			if (userConfig == null || userConfig.accessToken == null) return;

			var client = new FacebookClient(that.config.clientId, that.config.clientSecret);
			client.getSessionByAccessToken(userConfig.accessToken)(function(session) {
				that.trackConversations(session, user, userConfig);
			});
		}, this.config.interval);
	},

	// Track user conversations
	trackConversations: function(session, user, userConfig) {
		var that = this;
		session.graphCall("/me/inbox", {
			"fields": "to,id"
		})(function(result) {
			if (result == null || result.data == null) {
				console.log("facebook: error getting inbox ", result)
			}
			_.each(result.data, function(conv) {
				that.trackConversation(session, user, userConfig, conv);
			});
		});
	},

	// Track an user conversation
	trackConversation: function(session, user, userConfig, conversation) {
		var that = this;

		// Get the other person
		var otherPerson = _.find(conversation.to.data, function(person) {
			return person.id != userConfig.userId;
		});
		if (otherPerson == null) return;

		var eventName = "conversation"+otherPerson.id;

		user.setModel({
			'eventNamespace': this.id,
			'eventName': eventName,

			'name': otherPerson.name+"/Conversation",
			'icon': 'http://graph.facebook.com/'+otherPerson.id+'/picture',
			'description': "Conversation with "+otherPerson.name
		});


		session.graphCall("/"+conversation.id+"/comments")(function(result) {
			if (result == null || result.data == null) {
				console.log("facebook: error getting comments ", result)
			}
			_.each(result.data, function(message) {
				var timestamp = moment(message.created_time).unix() * 1000;
				user.track({
					'eventId': timestamp,
					'namespace': that.id,
					'name': eventName,
					'properties': {
						'fromId': message.from.id,
						'fromName': message.from.name,
						'message': message.message
					},
					'timestamp': timestamp
				});
			});
		});
	},

	// Initialize user<commit_msg>Update facebook tracker to only track messages sent<commit_after>var _  = require('underscore');
var moment = require('moment');
var FacebookStrategy = require('passport-facebook').Strategy;
var FacebookClient = require("facebook-client").FacebookClient;

module.exports = {
	id: "facebook",
	name: "Facebook",
	description: "Track your social activity and your relations from Facebook.",

	setup: function() {
		var that = this;
		_.defaults(this.config, {
			'interval': 60*60
		});

		// oAuth
		if (!this.setupOAuth(FacebookStrategy, {
			'scope': ['read_mailbox']
		})) {
			return;
		}
		
		// Cron to index
		this.addTask(function(user) {
			var userConfig = user.getTrackerSettings(this.id);
			if (userConfig == null || userConfig.accessToken == null) return;

			var client = new FacebookClient(that.config.clientId, that.config.clientSecret);
			client.getSessionByAccessToken(userConfig.accessToken)(function(session) {
				that.trackConversations(session, user, userConfig);
			});
		}, this.config.interval);
	},

	// Track user conversations
	trackConversations: function(session, user, userConfig) {
		var that = this;

		user.setModel({
			'eventNamespace': this.id,
			'eventName': "message",

			'name': "Messages sent",
			'icon': '$facebook',
			'description': "Messages sent on Facebook"
		});

		session.graphCall("/me/inbox", {
			"fields": "to,id"
		})(function(result) {
			if (result == null || result.data == null) {
				console.log("facebook: error getting inbox ", result)
			}
			_.each(result.data, function(conv) {
				that.trackConversation(session, user, userConfig, conv);
			});
		});
	},

	// Track an user conversation
	trackConversation: function(session, user, userConfig, conversation) {
		var that = this;

		// Get the other person
		var otherPerson = _.find(conversation.to.data, function(person) {
			return person.id != userConfig.userId;
		});
		if (otherPerson == null) return;

		session.graphCall("/"+conversation.id+"/comments")(function(result) {
			if (result == null || result.data == null) {
				console.log("facebook: error getting comments ", result)
			}
			_.each(result.data, function(message) {
				var timestamp = moment(message.created_time).unix() * 1000;

				// Track only messages from yourself
				if (message.from.id != userConfig.userId) return;

				user.track({
					'eventId': timestamp,
					'namespace': that.id,
					'name': "message",
					'properties': {
						'toId': otherPerson.id,
						'toName': otherPerson.name,
						'message': message.message
					},
					'timestamp': timestamp
				});
			});
		});
	},

	// Initialize user
{'fraction_non_alphanumeric': 0.15417066155321188, 'fraction_numerical': 0.0030680728667305847, 'mean_word_length': 3.7678244972577697, 'pattern_counts': {'":': 2, '<': 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': 15, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2098585', 'n_tokens_mistral': 1897, 'n_tokens_neox': 1582, 'n_words': 356}