diff options
| -rw-r--r-- | modules/changes/changes.js | 52 | 
1 files changed, 35 insertions, 17 deletions
diff --git a/modules/changes/changes.js b/modules/changes/changes.js index 67e4e43..4e9d87e 100644 --- a/modules/changes/changes.js +++ b/modules/changes/changes.js @@ -169,11 +169,16 @@ function refreshURLs(cb){  function collectLastHashes(){  	var lasthashes={}; -	for(url in urls){ +	var idx; +	for(var url in urls){  		if(urls[url].timeline.length==0){  			lasthashes[url]=null;  		} else { -			lasthashes[url]=urls[url].timeline[urls[url].timeline.length-1][1]; +			idx=urls[url].timeline.length-1; +			lasthashes[url]={ +				hash: urls[url].timeline[idx][1], +				date: urls[url].timeline[idx][0] +			};  		}  	}  	return lasthashes; @@ -181,7 +186,7 @@ function collectLastHashes(){  function sendMail(tomail,body,cb){  	var proc=child_process.spawn("sendmail",[tomail],{stdio:["pipe",process.stdout,process.stderr]}); -	proc.stdin.error(function(e){ +	proc.stdin.on("error",function(e){  		console.log(e);  		cb(e);  	}); @@ -195,14 +200,13 @@ function constructMailBody(tomail,diffs){  	var body="To: "+tomail+"\n"+  	         "From: changes <changes_module@tomsmeding.com>\n"+  	         "Subject: changes notification mail\n\n"+ -	         "The <a href=\"http://tomsmeding.com/changes\">changes</a> module recorded "+ -	           "some changes in watched webpages.\n"+ +	         "The changes module recorded some changes in watched webpages.\n"+  	         "The URLs and their old and new content hashes are shown below.\n\n";  	var i;  	for(i=0;i<diffs.length;i++){ -		body+="- <a href=\""+diffs[i].url+"\">"+diffs[i].url+"</a>\n"+ -		      "  was: "+diffs[i].oldhash+"\n"+ -		      "  now: "+diffs[i].newhash+"\n"; +		body+="- "+diffs[i].url+"\n"+ +		      "  was: "+diffs[i].oldhash+" ("+diffs[i].olddate+")\n"+ +		      "  now: "+diffs[i].newhash+" ("+diffs[i].newdate+")\n";  	}  	body+="\n\n"+  	      "- webserver\n"; @@ -213,23 +217,28 @@ function performNotify(settings,cb){  	var oldhashes=collectLastHashes();  	refreshURLs(function(){  		var newhashes=collectLastHashes(); -		var diffs=[]; // [{url, oldhash, newhash}] -		for(url in newhashes){ -			if(newhashes[url]==oldhashes[url])continue; +		var diffs=[]; // [{url, oldhash, olddate, newhash, newdate}] +		for(var url in newhashes){ +			if(newhashes[url].hash==oldhashes[url].hash)continue;  			diffs.push({  				url: url, -				oldhash: oldhashes[url], -				newhash: newhashes[url] +				oldhash: oldhashes[url].hash, +				olddate: oldhashes[url].date, +				newhash: newhashes[url].hash, +				newdate: newhashes[url].date  			});  		} -		sendMail(settings.to,constructMailBody(settings.to,diffs),cb); +		if(diffs.length!=0){ +			sendMail(settings.to,constructMailBody(settings.to,diffs),cb); +		}  	});  }  function performCleanup(){ -	//keep only the last hash and the last different hash intact -	//(if there is no different hash, delete everything but the last) -	for(url in urls){ +	for(var url in urls){ +		/* +		//keep only the last hash and the last different hash intact +		//(if there is no different hash, delete everything but the last)  		if(urls[url].timeline.length<=2)continue;  		var i,lasthash,prevhash=null,prevhashidx=-1,tllen;  		tllen=urls[url].timeline.length; @@ -247,6 +256,15 @@ function performCleanup(){  			urls[url].timeline.splice(prevhashidx+1,tllen-prevhashidx-2);  			urls[url].timeline.splice(0,prevhashidx);  		} +		*/ + +		//keep only the last record of a specific hash intact +		var i; +		for(i=urls[url].timeline.length-2;i>=0;i--){ +			if(urls[url].timeline[i][1]==urls[url].timeline[i+1][1]){ +				urls[url].timeline.splice(i,1); +			} +		}  	}  }  | 
