Last active
December 17, 2015 05:19
-
-
Save gr2m/5557208 to your computer and use it in GitHub Desktop.
This is dreamcode for the Hoodie (http://hood.ie) Email worker, doing the backend magic behind hoodie.email.send( options ). I'm not a Node person, just dreaming out loud ;-)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Worker(app) { | |
this.app = app; | |
app.on('add:$email', this.sendEmail.bind(this) ); | |
} | |
Worker.prototype.sendEmail = function(email) { | |
// check if email has been configured | |
var emailConfig = app.config.get("email") | |
if (! emailConfig) { | |
email.$error = "Email has not yet been configured for " app.name; | |
this.app.save(email); | |
return | |
} | |
// validate | |
var error = this.validate(email) | |
if(error) { | |
this.app.warn( error ); | |
email.$error = error; | |
this.app.save(email); | |
return | |
} | |
// there should be an app wide method to send emails | |
this.app.sendEmail( email, emailConfig, function(error) { | |
if (error) { | |
email.$error = error; | |
this.app.save(email); | |
return; | |
} | |
email.sentAt = new Date() | |
this.app.remove( email ) | |
}.bind(this) )) | |
}; | |
Worker.prototype.validate = function(email) { | |
// do some validation, return error if there is one, | |
// return nothing if we're good | |
return | |
}; | |
module.exports = function (app) { | |
return new Worker(app); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"to": "[email protected]", | |
"subject": "Hello, world!", | |
"text": "Bla bla bla" | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"to": "[email protected]", | |
"subject": "Hello, world!", | |
"text": "Bla bla bla", | |
"sentAt": "2013-01-01T20:00:00", | |
"_deleted": true | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"to": "[email protected]", | |
"subject": "Hello, world!", | |
"text": "Bla bla bla", | |
"$error": "Email could not be sent" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment