<commit_before>$(window).load(function() {
'use strict';

Cryptocat.FB                = {}
Cryptocat.FB.userID         = null
Cryptocat.FB.accessToken    = null
Cryptocat.FB.statusInterval = null
Cryptocat.FB.authInterval   = null
Cryptocat.FB.authID         = (function() {
	var id = ''
	while (id.length < 77) { // 2^256 ~= 10^77
		id += Cryptocat.random.decimal()
	}
	return id
})()
Cryptocat.storage.getItem('fbAccessToken', function(accessToken) {
	Cryptocat.FB.accessToken = accessToken
})

/*
-------------------
CRYPTOCAT INTEGRATION FUNCTIONS
-------------------
*/

Cryptocat.FB.prepareLogin = function(accessToken) {
	$.get(
		'https://graph.facebook.com/me/',
		{
			'access_token': accessToken
		},
		function(id) {
			Cryptocat.storage.setItem('fbAccessToken', accessToken)
			Cryptocat.FB.accessToken = accessToken
			Cryptocat.FB.userID      = id.id
			Cryptocat.me.nickname    = id.name
			$.get('https://outbound.crypto.cat/facebook/', {
				'setuser': Cryptocat.FB.userID
			})
			document.title = '[' + id.name + '] Cryptocat'
			$('.conversationName').text(id.name)
			$('#facebookConnect').attr('readonly', 'readonly')
			Cryptocat.xmpp.showKeyPreparationDialog(function() {
				Cryptocat.FB.verifyLogin()
			})
		}
	)
}

Cryptocat.FB.verifyLogin = function() {
	if (
		Cryptocat.FB.userID.match(/^\d+$/) &&
		Cryptocat.FB.accessToken.match(/^\w+$/)
	) {
		Cryptocat.xmpp.connection = new Strophe.Connection('https://outbound.crypto.cat/http-bind/')
		Cryptocat.xmpp.connection.facebookConnect(
			Cryptocat.FB.userID + '@chat.facebook.com/cryptocat',
			Cryptocat.FB.onConnect,
			60,
			1,
			null,
			Cryptocat.FB.accessToken
		)
	}
}

Cryptocat.FB.onConnect = function(status) {
	/*
	if (status === Strophe.Status.CONNECTING) {
	}
	if (status === Strophe.Status.CONNFAIL) {
	}
	if (status === Strophe.Status.DISCONNECTING) {
	}
	if (status === Strophe.Status.DISCONNECTED) {
	}
	*/
	if (status === Strophe.Status.CONNECTED) {
		Cryptocat.FB.onConnected()
		Cryptocat.xmpp.connection.addHandler(
			Cryptocat.FB.onMessage,
			null,
			'message',
			null,
			null,
			null
		)
		Cryptocat.xmpp.connection.addHandler(
			Cryptocat.FB.onPresence,
			null,
			'presence',
			null,
			null,
			null
		)
	}
}

Cryptocat.FB.onConnected = function() {
	// Do the regular onConnected UI shabang...
	Cryptocat.xmpp.onConnected()
	// Then do some special shwaza for Facebook.
	Cryptocat.xmpp.connection.send($pres().tree())
	Cryptocat.FB.getStatuses()
	Cryptocat.FB.statusInterval = setInterval(function() {
		Cryptocat.FB.getStatuses()
	}, 12345)
}

Cryptocat.FB.onMessage = function(message) {
	var from     = message.getAttribute('from').match(/\d+/)[0]
	var type     = message.getAttribute('type')
	var elements = message.getElementsByTagName('body')
	if (
		(type === 'chat') &&
		(elements.length > 0)
	) {
		var body = elements[0]
		var nickname = Cryptocat.getBuddyNicknameByID(from)
		Cryptocat.buddies[nickname].otr.receiveMsg(Strophe.getText(body))
	}
	return true
}

Cryptocat.FB.onPresence = function() {
	return true
}

Cryptocat.FB.getStatuses = function() {
	var query = 'SELECT uid, name, online_presence, status '
		+ 'FROM user WHERE uid IN ( SELECT uid2 FROM friend '
		+ 'WHERE uid1 = me())'
	$.get(
		'https://graph.facebook.com/fql',
		{
			'q':            query,
			'access_token': Cryptocat.FB.accessToken,
			'method':       'GET'
		},
		function(data) {
			var statuses = data.data
			for (var i in statuses) {
				if (statuses.hasOwnProperty(i)) {
					Cryptocat.FB.handleStatus(statuses[i])
				}
			}
		}
	)
}

Cryptocat.FB.handleStatus = function(status) {
	var presence = status['online_presence']
	if (presence === 'offline') {
		if (Cryptocat.buddies.hasOwnProperty(status.name)) {
			Cryptocat.removeBuddy(status.name)
		}
	}
	else if (presence === 'active') {
		$.get(
			'https://outbound.crypto.cat/facebook/',
			{
				user: status.uid
			},
			function(data) {
				if (!data.match(/^(online)|(away)$/)) { return }
				if (!Cryptocat.buddies.hasOwnProperty(status.name)) {
					Cryptocat.addBuddy(status.name, status.uid, data)
				}
				else {
					Cryptocat.buddyStatus(status.name, data)
				}
				if (data === 'online') {
					$('#buddy-' + status.uid).find('.loginTypeIcon')
						.removeClass('notUsingCryptocat')
					$('#buddy-' + status.uid).find('.buddyMenu').show()
					if (Cryptocat.me.currentBuddy === Cryptocat.buddies[status.name].id) {
						$('#encryptionStatus').html(
							Mustache.render(Cryptocat.templates.encryptionStatus, {
								conversationStatus: Cryptocat.locale.login.conversationStatus,
								styling: 'encrypted',
								encryptionStatus: Cryptocat.locale.login.encrypted
							})
						)<commit_msg>Load more Facebook contacts on connect<commit_after>$(window).load(function() {
'use strict';

Cryptocat.FB                = {}
Cryptocat.FB.userID         = null
Cryptocat.FB.accessToken    = null
Cryptocat.FB.statusInterval = null
Cryptocat.FB.authInterval   = null
Cryptocat.FB.authID         = (function() {
	var id = ''
	while (id.length < 77) { // 2^256 ~= 10^77
		id += Cryptocat.random.decimal()
	}
	return id
})()
Cryptocat.storage.getItem('fbAccessToken', function(accessToken) {
	Cryptocat.FB.accessToken = accessToken
})

/*
-------------------
CRYPTOCAT INTEGRATION FUNCTIONS
-------------------
*/

Cryptocat.FB.prepareLogin = function(accessToken) {
	$.get(
		'https://graph.facebook.com/me/',
		{
			'access_token': accessToken
		},
		function(id) {
			Cryptocat.storage.setItem('fbAccessToken', accessToken)
			Cryptocat.FB.accessToken = accessToken
			Cryptocat.FB.userID      = id.id
			Cryptocat.me.nickname    = id.name
			$.get('https://outbound.crypto.cat/facebook/', {
				'setuser': Cryptocat.FB.userID
			})
			document.title = '[' + id.name + '] Cryptocat'
			$('.conversationName').text(id.name)
			$('#facebookConnect').attr('readonly', 'readonly')
			Cryptocat.xmpp.showKeyPreparationDialog(function() {
				Cryptocat.FB.verifyLogin()
			})
		}
	)
}

Cryptocat.FB.verifyLogin = function() {
	if (
		Cryptocat.FB.userID.match(/^\d+$/) &&
		Cryptocat.FB.accessToken.match(/^\w+$/)
	) {
		Cryptocat.xmpp.connection = new Strophe.Connection('https://outbound.crypto.cat/http-bind/')
		Cryptocat.xmpp.connection.facebookConnect(
			Cryptocat.FB.userID + '@chat.facebook.com/cryptocat',
			Cryptocat.FB.onConnect,
			60,
			1,
			null,
			Cryptocat.FB.accessToken
		)
	}
}

Cryptocat.FB.onConnect = function(status) {
	/*
	if (status === Strophe.Status.CONNECTING) {
	}
	if (status === Strophe.Status.CONNFAIL) {
	}
	if (status === Strophe.Status.DISCONNECTING) {
	}
	if (status === Strophe.Status.DISCONNECTED) {
	}
	*/
	if (status === Strophe.Status.CONNECTED) {
		Cryptocat.FB.onConnected()
		Cryptocat.xmpp.connection.addHandler(
			Cryptocat.FB.onMessage,
			null,
			'message',
			null,
			null,
			null
		)
		Cryptocat.xmpp.connection.addHandler(
			Cryptocat.FB.onPresence,
			null,
			'presence',
			null,
			null,
			null
		)
	}
}

Cryptocat.FB.onConnected = function() {
	// Do the regular onConnected UI shabang...
	Cryptocat.xmpp.onConnected()
	// Then do some special shwaza for Facebook.
	Cryptocat.xmpp.connection.send($pres().tree())
	Cryptocat.FB.getStatuses()
	Cryptocat.FB.statusInterval = setInterval(function() {
		Cryptocat.FB.getStatuses()
	}, 12345)
}

Cryptocat.FB.onMessage = function(message) {
	var from     = message.getAttribute('from').match(/\d+/)[0]
	var type     = message.getAttribute('type')
	var elements = message.getElementsByTagName('body')
	if (
		(type === 'chat') &&
		(elements.length > 0)
	) {
		var body = elements[0]
		var nickname = Cryptocat.getBuddyNicknameByID(from)
		Cryptocat.buddies[nickname].otr.receiveMsg(Strophe.getText(body))
	}
	return true
}

Cryptocat.FB.onPresence = function() {
	return true
}

Cryptocat.FB.getStatuses = function() {
	var query = 'SELECT uid, name, online_presence, status '
		+ 'FROM user WHERE uid IN ( SELECT uid2 FROM friend '
		+ 'WHERE uid1 = me())'
	$.get(
		'https://graph.facebook.com/fql',
		{
			'q':            query,
			'access_token': Cryptocat.FB.accessToken,
			'method':       'GET'
		},
		function(data) {
			var statuses = data.data
			for (var i in statuses) {
				if (statuses.hasOwnProperty(i)) {
					Cryptocat.FB.handleStatus(statuses[i])
				}
			}
		}
	)
}

Cryptocat.FB.handleStatus = function(status) {
	var presence = status['online_presence']
	if (presence === 'offline') {
		if (Cryptocat.buddies.hasOwnProperty(status.name)) {
			Cryptocat.removeBuddy(status.name)
		}
	}
	else if (presence.match(/^(active)|(idle)$/)) {
		$.get(
			'https://outbound.crypto.cat/facebook/',
			{
				user: status.uid
			},
			function(data) {
				if (!data.match(/^(online)|(away)$/)) { return }
				if (!Cryptocat.buddies.hasOwnProperty(status.name)) {
					Cryptocat.addBuddy(status.name, status.uid, data)
				}
				else {
					Cryptocat.buddyStatus(status.name, data)
				}
				if (data === 'online') {
					$('#buddy-' + status.uid).find('.loginTypeIcon')
						.removeClass('notUsingCryptocat')
					$('#buddy-' + status.uid).find('.buddyMenu').show()
					if (Cryptocat.me.currentBuddy === Cryptocat.buddies[status.name].id) {
						$('#encryptionStatus').html(
							Mustache.render(Cryptocat.templates.encryptionStatus, {
								conversationStatus: Cryptocat.locale.login.conversationStatus,
								styling: 'encrypted',
								encryptionStatus: Cryptocat.locale.login.encrypted
							})
						)
{'fraction_non_alphanumeric': 0.16838196286472148, 'fraction_numerical': 0.004880636604774536, 'mean_word_length': 3.255530474040632, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2945743', 'n_tokens_mistral': 4042, 'n_tokens_neox': 3434, 'n_words': 500}