
/**
 * Module dependencies.
 * @api private
 */

var crypto = require('crypto');
// NOTE: should expose it in order to configure it
var client = require('redis').createClient();
var Emitter = require('component-emitter');

/**
 * Expose 'invite'
 */

module.exports = invite;

// invite is an emitter

Emitter(invite);

/**
 * Invite user with mail address.
 *
 * Invite doesn't check the validity of the user 
 * mail address.
 *
 * Examples:
 *
 *   var invite = require('invite')('bredele');
 *   invite('foo@bar.com', 'myproject');
 *   invite('beep@boop.com');
 *
 * @param {String} user
 * @return {Function}
 * @api public
 */

function invite(user) {
	return function(address, project) {
		var key = crypto.createHmac("md5", project || user);
		var hash = key.update(address).digest('base64');
    invite.emit('invite', user, project, address, hash);
    invite.emit('invite ' + user, project, address, hash);
		create(user, project, address, hash);
		return hash;
	};
}

/**
 * Create hfield in redis database with 
 * the following syntax `invite:$USER`
 *
 * When a project is specified, invite creates
 * a hfield `invite:$USER:$PROJECT`. 
 * 
 * @param  {String} user    
 * @param  {String} project 
 * @param  {String} address 
 * @param  {String} hash    
 * @api private
 */

function create(user, project, address, hash) {
	var field = 'invite:' + user;
	var db = project ? (field + ':' + project) : field;
	client.hset(db, hash, address, function(err, res) {
		if(err) return;
		invite.emit('created', user, project, address, hash);
		invite.emit('created ' + user, project, address, hash);
	});
}
@@ -653,17 +653,18 @@
 user) %7B%0A
-%09
+  
 return f
@@ -691,18 +691,20 @@
 ject) %7B%0A
-%09%09
+    
 var key 
@@ -748,18 +748,20 @@
  user);%0A
-%09%09
+    
 var hash
@@ -917,18 +917,20 @@
  hash);%0A
-%09%09
+    
 create(u
@@ -959,18 +959,20 @@
  hash);%0A
-%09%09
+    
 return h
@@ -976,17 +976,18 @@
 n hash;%0A
-%09
+  
 %7D;%0A%7D%0A%0A/*
@@ -1345,17 +1345,18 @@
 hash) %7B%0A
-%09
+  
 var fiel
@@ -1377,17 +1377,18 @@
 + user;%0A
-%09
+  
 var db =
@@ -1431,17 +1431,18 @@
  field;%0A
-%09
+  
 client.h
@@ -1485,18 +1485,20 @@
  res) %7B%0A
-%09%09
+    
 if(err) 
@@ -1505,18 +1505,20 @@
 return;%0A
-%09%09
+    
 invite.e
@@ -1567,10 +1567,12 @@
 h);%0A
-%09%09
+    
 invi
@@ -1627,9 +1627,10 @@
 h);%0A
-%09
+  
 %7D);%0A

fix typo