【发布时间】:2017-06-22 02:58:10
【问题描述】:
我有一个快速应用程序,当新用户注册到数据库时,管理员应该会收到即时通知。请注意,我使用 Passport 通过 Google 策略登录用户:
passport.use(new GoogleStrategy({
clientID : configAuth.googleAuth.clientID,
clientSecret: configAuth.googleAuth.clientSecret,
callbackURL : configAuth.googleAuth.callbackURL,
},
function(token, refreshToken, profile, done){
process.nextTick(function(){
// try to find a user based on their google id
User.findOne({ 'google.id' : profile.id }, function(err, user){
if(err)
return done(err)
if(user){
// if a user is found, log them in
return done(null, user)
} else {
// if the user isn't in our database, create a new user
var newUser = new User({
'google.id': profile.id,
'google.token': token,
'google.name': profile.displayName,
'google.email': profile.emails[0].value, //pull the first email
'google.active': 0,
'google.level': 0
})
// save the user
newUser.save(function(err){
if(err)
throw err
return done(null, newUser)
})
// Give the new user the role of 'inactive'
acl.addUserRoles(newUser.id, 'inactive', function(err){})
// Email a notice to the new user that they have to be accepted by an administrator.
...
// Send an email to all "admin-level" users to activate this user.
acl.roleUsers('admin', function(err, users){
// Loop through all admin users
users.forEach(function(value){
// Find each individual admin user by _id
User.findOne({ '_id' : value }, function(err, user){
if(err)
return done(err)
// Send this admin an internal mail (IMail)
var iMail = new IMail()
iMail.body = newUser.google.name + ' needs approval.',
// Send each administrator a notification that a new user needs approval
iMail.recipients.push(user.id)
iMail.save(function(err){
if(err)
return err
console.log("IMail was sent to notify admin " + user.google.name + " to approve " + newUser.google.name + " as a system user.")
})
})
})
})
}
})
})
}))
内
iMail.save(function(err){
...
})
块,我知道需要触发某种类型的事件,这会以某种方式提醒当前登录的每个管理员。不幸的是,这就是我的理解在这一点上崩溃的地方。我如何立即将此新通知通知管理员?我希望它像 Gmail,例如,当发送新电子邮件时收件箱自动显示 (1)。
作为旁注,我已经阅读了发布者/订阅者模式 (https://joshbedo.github.io/JS-Design-Patterns/) 并认为这可能会让我走上正轨,我只是不确定如何实现它。有人向正确的方向推动一点点将不胜感激。
【问题讨论】:
标签: javascript node.js design-patterns