Guest User

Untitled

a guest
Sep 26th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. var email = require("mailer");
  2. var User = require('./models/user')
  3. , Notification = require('./models/notification');
  4. var twitter = require('ntwitter');
  5.  
  6.  
  7. var Notify = module.exports = function(options) {
  8. this._options = options;
  9.  
  10. };
  11.  
  12.  
  13. Notify.prototype = {
  14. sendNotification: function (users, subject, content, callback) {
  15. send(users, subject, content, this._options, callback)
  16. },
  17. notify: function (question, callback) {
  18. //
  19. },
  20. notifyUser: function(question, notificationType, callback)
  21. {
  22. var subject ="No subject"
  23. , content = "No Content"
  24. , usersToNotify=[];
  25.  
  26. usersToNotify.push(user);
  27.  
  28. selectNotificationContent(type, function(notification){
  29. send(usersToNotify, notification, this._options, callback);
  30. });
  31. }
  32. };
  33.  
  34. function selectNotificationContent(typeName, callback())
  35. {
  36. Notification
  37. .where('typeName', type)
  38. .findOne(function(err, notification) {
  39. this._notification = notification;
  40. callback();
  41. });
  42. }
  43.  
  44. function sendEmail(user, notification, options, callbacl())
  45. {
  46. email.send(
  47. {
  48. host : options.host,
  49. port : options.port,
  50. domain : options.domain,
  51. to : user.getEmail(function(email){return email;}),//TODO get the email from facebook api
  52. from : options.sender ,
  53. authentication: options.auth,
  54. subject : notification.subject,
  55. body: notification.body,
  56. username : options.username,
  57. password : options.password
  58. },
  59. function(err, result){
  60. if(err){
  61. callback();
  62. }
  63. console.log("Mail sent: ", user.getEmail() , result)
  64. });
  65. }
  66.  
  67.  
  68. function tweet(user, message, options, callback) {
  69. var twit = new twitter({
  70. consumer_key: options.twitter_key,
  71. consumer_secret: options.twitter_secret,
  72. access_token_key: options.twitter_token_key,
  73. access_token_secret: options.twitter_token_secret
  74. });
  75.  
  76. twit
  77. .verifyCredentials(function (err) {
  78. console.log(err);
  79. })
  80. .updateStatus("@" + user.get("twitter.screen_name") + " " + message,
  81. function (err) {
  82. console.log(err)
  83. console.log(user.get("twitter.screen_name"))
  84. done(i);
  85. }
  86. );
  87. user.tweet(socialMediaContent, function() {
  88. callback();
  89. });
  90. }
  91.  
  92.  
  93. function send(users, notification, options, callback) {
  94. var done = function(i) {
  95. if((i + 1) == users.length) {
  96. callback();
  97. }
  98. };
  99.  
  100. users.forEach(function(user, i) {
  101. async.parallel(
  102. {
  103. sendFacebook: function(callback) {
  104. if (notification.hasChannel("facebook") && user.hasFacebook()) {
  105. //TODO facebook notification
  106. console.log("Missing facebook notification Code")
  107. callback();
  108. }
  109.  
  110. }
  111. , sendEmail: function(callback) {
  112. if (notification.hasChannel("email"))
  113. sendEmail(user, notification, options, function() {
  114. callback();
  115. });
  116. }
  117. , sendTwitter: function(callback) {
  118. tweet(user, notification.body, options, function() {
  119. callback();
  120. });
  121.  
  122. }
  123. }
  124. , function(err, results) {
  125. console.log(results)
  126. done(i);
  127. }
  128. );
  129. });
  130. };
Add Comment
Please, Sign In to add comment