<commit_before>/**
 * Minds::mobile
 * Chat controller
 *
 * @author <NAME>
 */

define(function() {
	'use strict';

	function ctrl($rootScope, $scope, $stateParams, $state, Client, storage, $ionicScrollDelegate, $timeout, $ionicLoading, push, $ionicPopup, $ionicModal, socket, $q) {

		if (window.cordova) {
			cordova.plugins.Keyboard.disableScroll(false);
			cordova.plugins.Keyboard.close();
		}

		$scope.guid = $stateParams.username;
		$scope.name = $stateParams.name;

		$scope.messages = [];
		$scope.next = "";
		$scope.previous = "";
		$scope.hasMoreData = true;
		$scope.publickeys = {};
		$scope.inProgress = false;
		var poll = true;
		var timeout;

		/**
		 * Load more posts
		 */
		$scope.loadMore = function() {
			$scope.inProgress = true;
			console.log('loading messages from:' + $scope.next);

			Client.get('api/v1/conversations/' + $stateParams.username, {
				limit: 6,
				offset: $scope.next,
				cachebreak: Date.now()
			}, function(data) {
				$rootScope.newChat = false;
				$scope.inProgress = false;
				//now update the public keys
				$scope.publickeys = data.publickeys;

				if (!$scope.publickeys[$scope.guid]) {
					$ionicPopup.alert({
						title: 'Sorry!',
						template: $scope.name + " has not yet configured their encrypted chat yet."
					});
					$state.go('tab.chat');
					return true;
				}

				if (!data.messages) {
					$scope.hasMoreData = false;
					return false;
				} else {
					$scope.hasMoreData = true;
				};

				var first;
				if ($scope.messages.length === 0) {
					first = true;
				} else {
					first = false;
				}

				$scope.messages = data.messages.concat($scope.messages);

				console.log("------ MESSAGES ARE LOADED ------");

				$scope.next = data['load-previous'];
				$scope.previous = data['load-next'];
				$scope.$broadcast('scroll.refreshComplete');

				if (first) {
					$timeout(function() {
						$ionicScrollDelegate.scrollBottom();
					}, 1000);
				}

				poll = true;

			}, function(error) {
				$scope.inProgress = false;
			});

		};
		$scope.loadMore();

		$scope.startCall = function() {
			$scope.callConfig = {
				initiator: true,
				guid: $scope.guid
			};
			$timeout(function() {
				$ionicModal.fromTemplateUrl('templates/gatherings/chat/call.html', {
					scope: $scope,
					animation: 'slide-in-up',
					backdropClickToClose: false,
					hardwareBackButtonClose: false
				}).then(function(modal) {
					$scope.modal = modal;
					$scope.modal.show();
					//need to send a signal to our new controller<commit_msg>if something goes wrong..<commit_after>/**
 * Minds::mobile
 * Chat controller
 *
 * @author <NAME>
 */

define(function() {
	'use strict';

	function ctrl($rootScope, $scope, $stateParams, $state, Client, storage, $ionicScrollDelegate, $timeout, $ionicLoading, push, $ionicPopup, $ionicModal, socket, $q) {

		if (window.cordova) {
			cordova.plugins.Keyboard.disableScroll(false);
			cordova.plugins.Keyboard.close();
		}

		$scope.guid = $stateParams.username;
		$scope.name = $stateParams.name;

		$scope.messages = [];
		$scope.next = "";
		$scope.previous = "";
		$scope.hasMoreData = true;
		$scope.publickeys = {};
		$scope.inProgress = false;
		var poll = true;
		var timeout;

		/**
		 * Load more posts
		 */
		$scope.loadMore = function() {
			$scope.inProgress = true;
			console.log('loading messages from:' + $scope.next);

			Client.get('api/v1/conversations/' + $stateParams.username, {
				limit: 6,
				offset: $scope.next,
				cachebreak: Date.now()
			}, function(data) {
				$rootScope.newChat = false;
				$scope.inProgress = false;
				//now update the public keys
				$scope.publickeys = data.publickeys;

				if (!$scope.publickeys[$scope.guid]) {
					$ionicPopup.alert({
						title: 'Sorry!',
						template: $scope.name + " has not yet configured their encrypted chat yet."
					});
					$state.go('tab.chat');
					return true;
				}

				if (!data.messages) {
					$scope.hasMoreData = false;
					return false;
				} else {
					$scope.hasMoreData = true;
				};

				var first;
				if ($scope.messages.length === 0) {
					first = true;
				} else {
					first = false;
				}

				$scope.messages = data.messages.concat($scope.messages);

				console.log("------ MESSAGES ARE LOADED ------");

				$scope.next = data['load-previous'];
				$scope.previous = data['load-next'];
				$scope.$broadcast('scroll.refreshComplete');

				if (first) {
					$timeout(function() {
						$ionicScrollDelegate.scrollBottom();
					}, 1000);
				}

				poll = true;

			}, function(error) {
				$scope.inProgress = false;
			});

		};
		$scope.loadMore();

		$scope.startCall = function() {
			if($rootScope.inCall){
				alert('you are already in a call..');
				return true;
			}
			$scope.callConfig = {
				initiator: true,
				guid: $scope.guid
			};
			$timeout(function() {
				$ionicModal.fromTemplateUrl('templates/gatherings/chat/call.html', {
					scope: $scope,
					animation: 'slide-in-up',
					backdropClickToClose: false,
					hardwareBackButtonClose: false
				}).then(function(modal) {
					$scope.modal = modal;
					$scope.modal.show();
					//need to send a signal to our new controller
{'fraction_non_alphanumeric': 0.1638198757763975, 'fraction_numerical': 0.002717391304347826, 'mean_word_length': 2.836932241250931, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4665432', 'n_tokens_mistral': 2163, 'n_tokens_neox': 1662, 'n_words': 341}