<commit_before>'use strict';

define(['controllers', 'underscore', 'modules/socketConnector', 'bootstrap'], function (controllers, _, connector){
	controllers.controller('talkCtrl', [ '$scope', '$routeParams', '$location', function($scope, $routeParams, $location){
		$scope.serverStatus = connector.status;
		connector.addListener('statusChanged', serverStatusChanged);
		function serverStatusChanged (status){
			$scope.$apply(function(){
				$scope.serverStatus = status;
			});
		};

		connector.runWithConnection(function(){
			var connectionId = null;
			$scope.channelId = $routeParams.channelId;
			$scope.me = new TalkUser({
				id : $routeParams.userid, 
				name : $routeParams.username, 
				avatar : 'http://placehold.it/50/FA6F57/fff&text=ME'
			});
			$scope.others = [];
			$scope.onlineUsers = [];
			var talks = [];
			var lastTalkGroup = null;
			$scope.talkGroups = [];
			connector.request('getUserFromChannel', {
				channelId:$scope.channelId, 
				userId:$scope.me.id},
				function(res){
					if (res.result){
						$scope.me.id = res.result.id;
						$scope.me.name = res.result.name;
						joinChannel();
					}
					else{
						connector.request('addUserToChannel', {
							channelId:$scope.channelId, 
							userName:$scope.me.name,
							userId:$scope.me.id}, 
							function(res){
								if (res.err){
									alert(JSON.stringify(res.err));
									return;
								}

								if (res.result && res.result.id){
									refreshWithUserId(res.result.id);
								}
							}
						);
					}				
				}
			);

			$scope.$on('$destroy', function cleanup() {
				releaseConnectorEvents();

				if (connectionId){
					connector.request('leaveChannel', {connectionId:connectionId});
				}
			});

			
			$scope.getUser = function (userId) {
				if ($scope.me.id === userId){
					return $scope.me;
				}

				var selectedUsers = $scope.others.filter(function(item){ return item.id === userId;});
				if (selectedUsers && selectedUsers.length > 0)
				{
					return selectedUsers[0];
				}

				return null;
			};



			$scope.inputKeypress = function($event){
				if ($event.keyCode === 13) // Enter key pess
				{
					$scope.sendMessage();
				}
			};

			$scope.sendMessage = function(){
				if (!$scope.inputTalkMessage){
					return;
				}
				var data = {
					id:'temp' + new Date,
					userId:$scope.me.id, 
					channelId:$scope.channelId, 
					message:$scope.inputTalkMessage, 
					time:new Date()
				};

				var talk = addTalk(data);
				$scope.inputTalkMessage = '';
				connector.request('addNewTalk', data, function(res){
					if (res.err){
						alert(JSON.stringify(res.err));
						return;
					}
					if (talk){
						$scope.$apply(function(){
							talk.id = res.result.id;
							talk.time = res.result.time;
						});
					}
					
				});
			};


			// var isCopiedShareUrlTimer = null;
			// $scope.copyShareUrlComplete = function(){
			// 	$scope.isCopiedShareUrl = true;

			// 	if (isCopiedShareUrlTimer){
			// 		clearTimeout(isCopiedShareUrlTimer);	
			// 	}				

			// 	isCopiedShareUrlTimer = setTimeout(function(){
			// 		$scope.$apply(function(){
			// 			$scope.isCopiedShareUrl = false;
			// 		});
			// 	}, 2000);
			// };
			var isSetNewNameTimer = null;			
			$scope.setNewName = function(){
				if ($scope.newName && ($scope.newName !== $scope.me.name)){
					$scope.changingUsername = true;					
					connector.request('updateUser', 
						{
							channelId:$scope.channelId, 
							userId:$scope.me.id, 
							propertyName:'name', 
							data:$scope.newName
						}, function (res){
							if (!res.err){
								onUpdateUser(res.result);
							}
							$scope.$apply(function(){
								if (!res.err){
									$scope.isSettedNewName = true;
									if (isSetNewNameTimer) {clearTimeout(isSetNewNameTimer);}
									isSetNewNameTimer = setTimeout(function(){
										$scope.$apply(function(){$scope.isSettedNewName = false;})
									}, 2000);
								}

								$scope.changingUsername = false;
								$scope.newName = '';
							});
							
						});					
				}
			};

			$scope.openNewPopupWindow = function(){
				var url = encodeURI(window.location.protocol + '//' + window.location.host + '/p#/ch/' + $scope.channelId);
				window.open(url, "_blank", "directories=no, location=no, menubar=no, status=no, titlebar=no, toolbar=no, scrollbars=no, resizable=yes, width=300, height=485");
			};

			function onNewTalk(talk){
				$scope.$apply(function(){
					addTalk(talk);					
				});
			}

			function onAddUser(user){
				$scope.$apply(function(){
					$scope.addUser(new TalkUser(user));
				});
			}

			function onRemoveUser(userId){
				$scope.$apply(function(){
					$scope.removeUser(userId);
				});	
			}

			function onUpdateUser(data){
				if (data.userId && data.propertyName){<commit_msg>bug fix - clone by other user.<commit_after>'use strict';

define(['controllers', 'underscore', 'modules/socketConnector', 'bootstrap'], function (controllers, _, connector){
	controllers.controller('talkCtrl', [ '$scope', '$routeParams', '$location', function($scope, $routeParams, $location){
		$scope.serverStatus = connector.status;
		connector.addListener('statusChanged', serverStatusChanged);
		function serverStatusChanged (status){
			$scope.$apply(function(){
				$scope.serverStatus = status;
			});
		};

		connector.runWithConnection(function(){
			var connectionId = null;
			$scope.channelId = $routeParams.channelId;
			$scope.me = new TalkUser({
				id : $routeParams.userid, 
				name : $routeParams.username, 
				avatar : 'http://placehold.it/50/FA6F57/fff&text=ME'
			});
			$scope.others = [];
			$scope.onlineUsers = [];
			var talks = [];
			var lastTalkGroup = null;
			$scope.talkGroups = [];
			connector.request('getUserFromChannel', {
				channelId:$scope.channelId, 
				userId:$scope.me.id},
				function(res){
					if (res.result){
						$scope.me.id = res.result.id;
						$scope.me.name = res.result.name;
						joinChannel();
					}
					else{
						connector.request('addUserToChannel', {
							channelId:$scope.channelId, 
							userName:$scope.me.name,
							userId:$scope.me.id}, 
							function(res){
								if (res.err){
									alert(JSON.stringify(res.err));
									return;
								}

								if (res.result && res.result.id){
									refreshWithUserId(res.result.id);
								}
							}
						);
					}				
				}
			);

			$scope.$on('$destroy', function cleanup() {
				releaseConnectorEvents();

				if (connectionId){
					connector.request('leaveChannel', {connectionId:connectionId});
				}
			});

			
			$scope.getUser = function (userId) {
				if ($scope.me.id === userId){
					return $scope.me;
				}

				var selectedUsers = $scope.others.filter(function(item){ return item.id === userId;});
				if (selectedUsers && selectedUsers.length > 0)
				{
					return selectedUsers[0];
				}

				return null;
			};



			$scope.inputKeypress = function($event){
				if ($event.keyCode === 13) // Enter key pess
				{
					$scope.sendMessage();
				}
			};

			$scope.sendMessage = function(){
				if (!$scope.inputTalkMessage){
					return;
				}
				var data = {
					id:'temp' + new Date,
					userId:$scope.me.id, 
					channelId:$scope.channelId, 
					message:$scope.inputTalkMessage, 
					time:new Date()
				};

				var talk = addTalk(data);
				$scope.inputTalkMessage = '';
				connector.request('addNewTalk', data, function(res){
					if (res.err){
						alert(JSON.stringify(res.err));
						return;
					}
					if (talk){
						$scope.$apply(function(){
							talk.id = res.result.id;
							talk.time = res.result.time;
						});
					}
					
				});
			};


			// var isCopiedShareUrlTimer = null;
			// $scope.copyShareUrlComplete = function(){
			// 	$scope.isCopiedShareUrl = true;

			// 	if (isCopiedShareUrlTimer){
			// 		clearTimeout(isCopiedShareUrlTimer);	
			// 	}				

			// 	isCopiedShareUrlTimer = setTimeout(function(){
			// 		$scope.$apply(function(){
			// 			$scope.isCopiedShareUrl = false;
			// 		});
			// 	}, 2000);
			// };
			var isSetNewNameTimer = null;			
			$scope.setNewName = function(){
				if ($scope.newName && ($scope.newName !== $scope.me.name)){
					$scope.changingUsername = true;					
					connector.request('updateUser', 
						{
							channelId:$scope.channelId, 
							userId:$scope.me.id, 
							propertyName:'name', 
							data:$scope.newName
						}, function (res){
							if (!res.err){
								onUpdateUser(res.result);
							}
							$scope.$apply(function(){
								if (!res.err){
									$scope.isSettedNewName = true;
									if (isSetNewNameTimer) {clearTimeout(isSetNewNameTimer);}
									isSetNewNameTimer = setTimeout(function(){
										$scope.$apply(function(){$scope.isSettedNewName = false;})
									}, 2000);
								}

								$scope.changingUsername = false;
								$scope.newName = '';
							});
							
						});					
				}
			};

			$scope.openNewPopupWindow = function(){
				var url = encodeURI(window.location.protocol + '//' + window.location.host + '/p#/ch/' + $scope.channelId + '?userid=' + $scope.me.id);
				window.open(url, "_blank", "directories=no, location=no, menubar=no, status=no, titlebar=no, toolbar=no, scrollbars=no, resizable=yes, width=300, height=485");
			};

			function onNewTalk(talk){
				$scope.$apply(function(){
					addTalk(talk);					
				});
			}

			function onAddUser(user){
				$scope.$apply(function(){
					$scope.addUser(new TalkUser(user));
				});
			}

			function onRemoveUser(userId){
				$scope.$apply(function(){
					$scope.removeUser(userId);
				});	
			}

			function onUpdateUser(data){
				if (data.userId && data.propertyName){
{'fraction_non_alphanumeric': 0.1632928475033738, 'fraction_numerical': 0.004775251738814492, 'mean_word_length': 2.548434622467772, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2630263', 'n_tokens_mistral': 4418, 'n_tokens_neox': 3085, 'n_words': 480}