<commit_before>'use strict';
/*global rtc: true */
/*global OEmbedProviders: true */
angular.module('mean.rooms').factory('ChatService',['$timeout','UIHandler','Rooms','Notification','$http',function($timeout,UIHandler,Rooms,Notification,$http){
	return function (){

		var uiHandler = UIHandler;
		this.welcomePublished = false;
		var self= this;
		var room = new Rooms({});

		this.oembedData = {};
		
		this.formatDate = function($scope,t,fm) {
		   var yyyy = t.getFullYear().toString();
		   var mm = (t.getMonth()+1).toString();
		   var dd  = t.getDate().toString();
		   var mi = t.getMinutes().toString();
		   var hh = t.getHours().toString();
		   var dm = $scope.resourceBundle.dateformat
		   			.replace('yyyy',yyyy)
		   			.replace('mm',(mm[1]?mm:'0'+mm[0]))
		   			.replace('dd',(dd[1]?dd:'0'+dd[0]));
		   var tm = $scope.resourceBundle.timeformat
		   			.replace('hh',(hh[1]?hh:'0'+hh[0]))
		   			.replace('mi',(mi[1]?mi:'0'+mi[0]));
		   return fm?dm+' '+tm:tm;
		};
		
	    this.timeAgo = function($scope,t) {
	        var t2 = (new Date()).getTime();
	        var t1 = t.getTime();
	        var secs =  parseInt((t2-t1)/(1000));
	        var mins = parseInt((t2-t1)/(1000*60));
	        if (secs>59) {
        		return (mins>59)?this.formatDate($scope,t,(mins>60*24)):$scope.resourceBundle._('timeprep',mins);
	        } else {
        		return (secs>30)?$scope.resourceBundle.lessthanaminute:$scope.resourceBundle.justnow;
	        }
	    };
	    
	    this.longTimeAgo = function(newTime,lastTime) {
	        var t2 = newTime.getTime();
	        var t1 = lastTime.getTime();
	        var secs =  parseInt((t2-t1)/(1000));
	        return secs>15;
	    };
	    
	    this.getOEmbedData = function(url,callb) {
	    	var retrieve = $http({
	    			method:'post',
	    			url:'/chat/oembed',
	    			data:{
	    				url:url
	    			},
	    			headers:{'x-csrf-token':window.csrf}
	    	});
	    	retrieve.success(function(response) {
	    		callb(response);
	        }).error(function() {
	        	callb(null);
	        });
	    };
	    
	    this.processOEmbedUrl = function($scope,url,list) {
			var realUniqueId = 'embed_'+(new Date()).getTime()+Math.floor(Math.random()*100);
			var miObj = {type:'oembed',idv:realUniqueId,loading:true,ready:false};
			var thumb = 'thumbnail_url';
	    	self.getOEmbedData(url,function(oembed){
	    		if (!oembed.error) {
					miObj.loading = false;
					miObj.ready = true;
					miObj.title = $scope.resourceBundle._('titleOpenVideoInWindow',oembed.title);
					var thumbUrl = oembed[thumb] || oembed.thumbnail;
					miObj.thumbnail = thumbUrl?thumbUrl:'';
					//miObj.videoid = video.id;
					setTimeout(function(){
						document.getElementById(realUniqueId).addEventListener ('click',function (event){
							$scope.openOEmbedFromService(oembed);
							event.preventDefault();
						});
					},500);
	    		} else {
					miObj.loading = false;
					miObj.ready = false;
	    		}
	    	});
			list.push(miObj);
	    };
	    
	    this.getOEmbedProvidersRegExp = function() {
	    	var domains = '';
	    	for (var d=0;d<OEmbedProviders.domains.length;d+=1) {
	    		domains += (domains===''?'':'|')+'(www\.)?'+OEmbedProviders.domains[d].replace('.','\\.');
	    	}
	    	return new RegExp('(^|^https?://)('+domains+')/.+','g');
	    };
	    
	    this.processOEmbedUrls = function($scope,txt,list) {
	    	var matches = self.getOEmbedProvidersRegExp().exec(txt);
	    	if (matches) {
	    		self.processOEmbedUrl($scope,txt,list);
	    	}
	    };
	    
	    this.processGoogleDoc = function($scope,txt,list) {
	    	var matches = /(?:https:\/\/)?docs\.google\.com\/presentation\/d\/([^\/\s]+)\/[^\s]+/g.exec(txt);
	    	if (matches) {
	    		var doc = matches[1];
		    	var docid = 'doc_'+doc+'_'+(new Date()).getTime()+Math.floor(Math.random()*100);
		    	setTimeout(function(){
		    		document.getElementById(docid).addEventListener('click',function(evt){
		    			$scope.openIFrameService('i'+docid,$scope.resourceBundle.googledoc,'//docs.google.com/presentation/d/'+doc+'/embed?start=false&loop=false&delayms=3000');
		    			evt.preventDefault();
		    		});
		    		
		    	},500);
		    	list.push({type:'edoc',fablack:true,title:$scope.resourceBundle.titleOpenGDocInWindow,id:docid,thumbnail:'/img/gslides.png'});
	    	}
	    };
	    
	    this.processTypeForm = function($scope,txt,list) {
	    	var matches = /(?:https:\/\/)?[^\.]+\.typeform\.com\/to\/([^\s]+)/g.exec(txt);
	    	if (matches) {
	    		var doc = matches[1];
	    		var docid = 'frm_'+doc+'_'+(new Date()).getTime()+Math.floor(Math.random()*100);
		    	setTimeout(function(){
		    		document.getElementById(docid).addEventListener('click',function(evt){
		    			$scope.openIFrameService('if'+docid,$scope.resourceBundle.typeform,'//showroom.typeform.com/to/'+doc);
		    			evt.preventDefault();
		    		});
		    		
		    	},500);
	    		list.push({type:'edoc',fawhite:true,title:$scope.resourceBundle.titleOpenTypeFormInWindow,id:docid,thumbnail:'/img/typeform.png'});
	    	}
	    };

	    this.processEtherpad = function($scope,txt,list) {
	    	var matches = /(?:https:\/\/)?(?:beta\.etherpad\.org|etherpad\.udl\.cat)\/p\/([^\s]+)/g.exec(txt);
	    	if (matches) {
	    		var doc = matches[1];
		    	var docid = 'doc_'+doc+'_'+(new Date()).getTime()+Math.floor(Math.random()*100);
		    	setTimeout(function(){
		    		document.getElementById(docid).addEventListener('click',function(evt){
		    			$scope.openIFrameService('i'+docid,$scope.resourceBundle.etherpad,'//beta.etherpad.org/p/'+doc+'?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false');
		    			evt.preventDefault();
		    		});
		    		
		    	},500);
		    	list.push({type:'edoc',fawhite:true,title:$scope.resourceBundle.titleOpenEtherpadInWindow,id:docid,thumbnail:'/img/etherpad.png'});
	    	}
	    };
	    
	    this.addObjects = function($scope,txt,list) {
	    	this.processTypeForm($scope,txt,list);
	    	this.processGoogleDoc($scope,txt,list);
	    	this.processEtherpad($scope,txt,list);
	    	this.processOEmbedUrls($scope,txt,list);
	    };
	    
	    this.getAllSelectors = function() { 
	        var ret = [];<commit_msg>Fix etherpad url issue. Fixes #184<commit_after>'use strict';
/*global rtc: true */
/*global OEmbedProviders: true */
angular.module('mean.rooms').factory('ChatService',['$timeout','UIHandler','Rooms','Notification','$http',function($timeout,UIHandler,Rooms,Notification,$http){
	return function (){

		var uiHandler = UIHandler;
		this.welcomePublished = false;
		var self= this;
		var room = new Rooms({});

		this.oembedData = {};
		
		this.formatDate = function($scope,t,fm) {
		   var yyyy = t.getFullYear().toString();
		   var mm = (t.getMonth()+1).toString();
		   var dd  = t.getDate().toString();
		   var mi = t.getMinutes().toString();
		   var hh = t.getHours().toString();
		   var dm = $scope.resourceBundle.dateformat
		   			.replace('yyyy',yyyy)
		   			.replace('mm',(mm[1]?mm:'0'+mm[0]))
		   			.replace('dd',(dd[1]?dd:'0'+dd[0]));
		   var tm = $scope.resourceBundle.timeformat
		   			.replace('hh',(hh[1]?hh:'0'+hh[0]))
		   			.replace('mi',(mi[1]?mi:'0'+mi[0]));
		   return fm?dm+' '+tm:tm;
		};
		
	    this.timeAgo = function($scope,t) {
	        var t2 = (new Date()).getTime();
	        var t1 = t.getTime();
	        var secs =  parseInt((t2-t1)/(1000));
	        var mins = parseInt((t2-t1)/(1000*60));
	        if (secs>59) {
        		return (mins>59)?this.formatDate($scope,t,(mins>60*24)):$scope.resourceBundle._('timeprep',mins);
	        } else {
        		return (secs>30)?$scope.resourceBundle.lessthanaminute:$scope.resourceBundle.justnow;
	        }
	    };
	    
	    this.longTimeAgo = function(newTime,lastTime) {
	        var t2 = newTime.getTime();
	        var t1 = lastTime.getTime();
	        var secs =  parseInt((t2-t1)/(1000));
	        return secs>15;
	    };
	    
	    this.getOEmbedData = function(url,callb) {
	    	var retrieve = $http({
	    			method:'post',
	    			url:'/chat/oembed',
	    			data:{
	    				url:url
	    			},
	    			headers:{'x-csrf-token':window.csrf}
	    	});
	    	retrieve.success(function(response) {
	    		callb(response);
	        }).error(function() {
	        	callb(null);
	        });
	    };
	    
	    this.processOEmbedUrl = function($scope,url,list) {
			var realUniqueId = 'embed_'+(new Date()).getTime()+Math.floor(Math.random()*100);
			var miObj = {type:'oembed',idv:realUniqueId,loading:true,ready:false};
			var thumb = 'thumbnail_url';
	    	self.getOEmbedData(url,function(oembed){
	    		if (!oembed.error) {
					miObj.loading = false;
					miObj.ready = true;
					miObj.title = $scope.resourceBundle._('titleOpenVideoInWindow',oembed.title);
					var thumbUrl = oembed[thumb] || oembed.thumbnail;
					miObj.thumbnail = thumbUrl?thumbUrl:'';
					//miObj.videoid = video.id;
					setTimeout(function(){
						document.getElementById(realUniqueId).addEventListener ('click',function (event){
							$scope.openOEmbedFromService(oembed);
							event.preventDefault();
						});
					},500);
	    		} else {
					miObj.loading = false;
					miObj.ready = false;
	    		}
	    	});
			list.push(miObj);
	    };
	    
	    this.getOEmbedProvidersRegExp = function() {
	    	var domains = '';
	    	for (var d=0;d<OEmbedProviders.domains.length;d+=1) {
	    		domains += (domains===''?'':'|')+'(www\.)?'+OEmbedProviders.domains[d].replace('.','\\.');
	    	}
	    	return new RegExp('(^|^https?://)('+domains+')/.+','g');
	    };
	    
	    this.processOEmbedUrls = function($scope,txt,list) {
	    	var matches = self.getOEmbedProvidersRegExp().exec(txt);
	    	if (matches) {
	    		self.processOEmbedUrl($scope,txt,list);
	    	}
	    };
	    
	    this.processGoogleDoc = function($scope,txt,list) {
	    	var matches = /(?:https:\/\/)?docs\.google\.com\/presentation\/d\/([^\/\s]+)\/[^\s]+/g.exec(txt);
	    	if (matches) {
	    		var doc = matches[1];
		    	var docid = 'doc_'+doc+'_'+(new Date()).getTime()+Math.floor(Math.random()*100);
		    	setTimeout(function(){
		    		document.getElementById(docid).addEventListener('click',function(evt){
		    			$scope.openIFrameService('i'+docid,$scope.resourceBundle.googledoc,'//docs.google.com/presentation/d/'+doc+'/embed?start=false&loop=false&delayms=3000');
		    			evt.preventDefault();
		    		});
		    		
		    	},500);
		    	list.push({type:'edoc',fablack:true,title:$scope.resourceBundle.titleOpenGDocInWindow,id:docid,thumbnail:'/img/gslides.png'});
	    	}
	    };
	    
	    this.processTypeForm = function($scope,txt,list) {
	    	var matches = /(?:https:\/\/)?[^\.]+\.typeform\.com\/to\/([^\s]+)/g.exec(txt);
	    	if (matches) {
	    		var doc = matches[1];
	    		var docid = 'frm_'+doc+'_'+(new Date()).getTime()+Math.floor(Math.random()*100);
		    	setTimeout(function(){
		    		document.getElementById(docid).addEventListener('click',function(evt){
		    			$scope.openIFrameService('if'+docid,$scope.resourceBundle.typeform,'//showroom.typeform.com/to/'+doc);
		    			evt.preventDefault();
		    		});
		    		
		    	},500);
	    		list.push({type:'edoc',fawhite:true,title:$scope.resourceBundle.titleOpenTypeFormInWindow,id:docid,thumbnail:'/img/typeform.png'});
	    	}
	    };

	    this.processEtherpad = function($scope,txt,list) {
	    	var matches = /(?:https?:\/\/)?(beta\.etherpad\.org|etherpad\.udl\.cat)\/p\/([^\s]+)/g.exec(txt);
	    	if (matches) {
	    		var host = matches[1];
	    		var doc = matches[2];
		    	var docid = 'doc_'+doc+'_'+(new Date()).getTime()+Math.floor(Math.random()*100);
		    	setTimeout(function(){
		    		document.getElementById(docid).addEventListener('click',function(evt){
		    			$scope.openIFrameService('i'+docid,$scope.resourceBundle.etherpad,'//'+host+'/p/'+doc+'?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false');
		    			evt.preventDefault();
		    		});
		    		
		    	},500);
		    	list.push({type:'edoc',fawhite:true,title:$scope.resourceBundle.titleOpenEtherpadInWindow,id:docid,thumbnail:'/img/etherpad.png'});
	    	}
	    };
	    
	    this.addObjects = function($scope,txt,list) {
	    	this.processTypeForm($scope,txt,list);
	    	this.processGoogleDoc($scope,txt,list);
	    	this.processEtherpad($scope,txt,list);
	    	this.processOEmbedUrls($scope,txt,list);
	    };
	    
	    this.getAllSelectors = function() { 
	        var ret = [];
{'fraction_non_alphanumeric': 0.19300507696027078, 'fraction_numerical': 0.013538560722056572, 'mean_word_length': 2.934686112872543, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4605126', 'n_tokens_mistral': 5005, 'n_tokens_neox': 4332, 'n_words': 480}