Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var email = require("mailer");
- var User = require('./models/user')
- , Notification = require('./models/notification');
- var twitter = require('ntwitter');
- var Notify = module.exports = function(options) {
- this._options = options;
- };
- Notify.prototype = {
- sendNotification: function (users, subject, content, callback) {
- send(users, subject, content, this._options, callback)
- },
- notify: function (question, callback) {
- //
- },
- notifyUser: function(question, notificationType, callback)
- {
- var subject ="No subject"
- , content = "No Content"
- , usersToNotify=[];
- usersToNotify.push(user);
- selectNotificationContent(type, function(notification){
- send(usersToNotify, notification, this._options, callback);
- });
- }
- };
- function selectNotificationContent(typeName, callback())
- {
- Notification
- .where('typeName', type)
- .findOne(function(err, notification) {
- this._notification = notification;
- callback();
- });
- }
- function sendEmail(user, notification, options, callbacl())
- {
- email.send(
- {
- host : options.host,
- port : options.port,
- domain : options.domain,
- to : user.getEmail(function(email){return email;}),//TODO get the email from facebook api
- from : options.sender ,
- authentication: options.auth,
- subject : notification.subject,
- body: notification.body,
- username : options.username,
- password : options.password
- },
- function(err, result){
- if(err){
- callback();
- }
- console.log("Mail sent: ", user.getEmail() , result)
- });
- }
- function tweet(user, message, options, callback) {
- var twit = new twitter({
- consumer_key: options.twitter_key,
- consumer_secret: options.twitter_secret,
- access_token_key: options.twitter_token_key,
- access_token_secret: options.twitter_token_secret
- });
- twit
- .verifyCredentials(function (err) {
- console.log(err);
- })
- .updateStatus("@" + user.get("twitter.screen_name") + " " + message,
- function (err) {
- console.log(err)
- console.log(user.get("twitter.screen_name"))
- done(i);
- }
- );
- user.tweet(socialMediaContent, function() {
- callback();
- });
- }
- function send(users, notification, options, callback) {
- var done = function(i) {
- if((i + 1) == users.length) {
- callback();
- }
- };
- users.forEach(function(user, i) {
- async.parallel(
- {
- sendFacebook: function(callback) {
- if (notification.hasChannel("facebook") && user.hasFacebook()) {
- //TODO facebook notification
- console.log("Missing facebook notification Code")
- callback();
- }
- }
- , sendEmail: function(callback) {
- if (notification.hasChannel("email"))
- sendEmail(user, notification, options, function() {
- callback();
- });
- }
- , sendTwitter: function(callback) {
- tweet(user, notification.body, options, function() {
- callback();
- });
- }
- }
- , function(err, results) {
- console.log(results)
- done(i);
- }
- );
- });
- };
Add Comment
Please, Sign In to add comment