Skip to content

Commit

Permalink
add twitter notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
btouellette committed Jun 10, 2014
1 parent 2989cba commit cf826fc
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 8 deletions.
33 changes: 28 additions & 5 deletions app/gameserver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
var cookie = require('cookie');
var connect = require('connect');
var nodemailer = require('nodemailer');
var twit = require('twit');
var auth = require('../config/auth');

// load up the gamestate model
var Tile = require('./models/tile');
Expand All @@ -23,6 +25,12 @@ var smtpTransport = nodemailer.createTransport("SMTP",{
pass: process.env.EMAIL_PASSWORD
}
});
var twitter = new twit({
consumer_key: auth.twitterAuth.consumerKey,
consumer_secret: auth.twitterAuth.consumerSecret,
access_token: auth.twitterAuth.accessToken,
access_token_secret: auth.twitterAuth.accessTokenSecret
});

module.exports = function(server, sessionStore) {

Expand Down Expand Up @@ -139,7 +147,7 @@ module.exports = function(server, sessionStore) {
} else {
gamestate.markModified('unusedTiles');
gamestate.populate('placedTiles.tile activeTile.tile unusedTiles players.user',
'cities.meepleOffset farms.meepleOffset roads.meepleOffset cloister imageURL email_notifications username local.email facebook.email google.email',
'cities.meepleOffset farms.meepleOffset roads.meepleOffset cloister imageURL email_notifications twitter_notifications username local.email facebook.email google.email twitter.username',
function(err, gamestate) {
if(err) {
console.log('send move populate err: ' + err);
Expand All @@ -152,11 +160,11 @@ module.exports = function(server, sessionStore) {
break;
}
}
// send e-mail notification to user if they have opted in and the user has changed
if(activeUser.email_notifications && activeUser.username !== previousUser.username) {
// send notification to user if the user has changed and they are not actively connected
if(!userToSocket[activeUser._id] && activeUser.username !== previousUser.username) {
var activeEmail = activeUser.local.email || activeUser.google.email || activeUser.facebook.email;
// send notification if we have a valid e-mail and the user doesn't have an active socket
if(activeEmail && !userToSocket[activeUser._id]) {
// send e-mail notification if we have a valid e-mail and the user has the option enabled
if(activeEmail && activeUser.email_notifications) {
smtpTransport.sendMail({
from: "Concarneau <[email protected]",
to: activeEmail,
Expand All @@ -168,6 +176,14 @@ module.exports = function(server, sessionStore) {
}
});
}
// send twitter notification if we have a valid twitter handle and the user has the option enabled
if(activeUser.twitter.username && activeUser.twitter_notifications) {
twitter.post('statuses/update', { status: '@' + activeUser.twitter.username + ' There is a Concarneau game where it is your turn: https://concarneau.herokuapp.com' }, function(err, data, response) {
if(err) {
console.log('twitter failed: ' + err);
}
});
}
}
// get distinct list of user IDs in the game
var distinctUserIDs = gamestate.players.map(function(player) { return player.user._id; }).filter(function(value, index, self) {
Expand Down Expand Up @@ -244,6 +260,13 @@ module.exports = function(server, sessionStore) {
}
});
});
socket.on('twitter notification', function(enabled) {
User.findByIdAndUpdate(currentUser._id, { $set: { twitter_notifications: enabled }} , function(err, user) {
if(!err && user) {
currentUser = user;
}
});
});
});
});
});
Expand Down
3 changes: 2 additions & 1 deletion app/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ var userSchema = mongoose.Schema({
activeGames: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Gamestate' }], // external reference to gamestate objects
friends: [mongoose.Schema.Types.ObjectId],
username: { type: String, lowercase: true, trim: true, unique: true, sparse: true },
email_notifications: Boolean
email_notifications: Boolean,
twitter_notifications: Boolean

});

Expand Down
2 changes: 2 additions & 0 deletions config/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ module.exports = {
'twitterAuth' : {
'consumerKey' : '0P6wn4IB9MQSSt5eaF3eDxj1v',
'consumerSecret' : process.env.TWITTER_SECRET,
'accessToken' : '2532814112-Ze8L0AZJ71j9roiGuBJ6KJP2jYx35mGuacpbe3P',
'accessTokenSecret' : process.env.TWITTER_ACCESS_SECRET,
'callbackURL' : process.env.TWITTER_CALLBACK || 'http:https://localhost:' + (process.env.PORT || 8080) + '/auth/twitter/callback'
},

Expand Down
2 changes: 2 additions & 0 deletions config/c9.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ var argv = require('yargs').argv;

process.env.FACEBOOK_SECRET = argv.FACEBOOK_SECRET;
process.env.TWITTER_SECRET = argv.TWITTER_SECRET;
process.env.TWITTER_ACCESS_SECRET = argv.TWITTER_ACCESS_SECRET;
process.env.GOOGLE_SECRET = argv.GOOGLE_SECRET;
process.env.EMAIL_PASSWORD = argv.EMAIL_PASSWORD;
process.env.TWITTER_PASSWORD = argv.TWITTER_PASSWORD;

process.env.FACEBOOK_CALLBACK = 'https://concarneau-c9-btouellette.c9.io/auth/facebook/callback';
process.env.TWITTER_CALLBACK = 'https://concarneau-c9-btouellette.c9.io/auth/twitter/callback';
Expand Down
2 changes: 1 addition & 1 deletion config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = function(passport) {

// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, 'username friends activeGames local facebook google twitter email_notifications', function(err, user) {
User.findById(id, 'username friends activeGames local facebook google twitter email_notifications twitter_notifications', function(err, user) {
user.populate('activeGames friends', 'players.user players.active started finished username unusedTiles', function(err, user) {
user.populate({ path: 'activeGames.players.user', model: 'User', select: 'username'}, function(err, user) {
done(err, user);
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"connect-mongo": "~0.4.0",
"nodetime": "~0.8.15",
"newrelic": "~1.5.2",
"nodemailer": "~0.6.5"
"nodemailer": "~0.6.5",
"twit": "~1.1.15"
}
}
9 changes: 9 additions & 0 deletions views/game.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@
</label>
<%- !(user.facebook.email || user.google.email || user.local.email) ? '(requires non-Twitter login linked in Profile)' : '' %>
</div>
<div class="checkbox">
<label>
<input type="checkbox"
onclick="socket.emit('twitter notification', this.checked)"
<%- user.twitter_notifications ? 'checked="true"' : '' %>
<%- !user.twitter.username ? 'disabled' : '' %>> Twitter notifications
</label>
<%- !user.twitter.username ? '(requires Twitter login linked in Profile)' : '' %>
</div>
<a href="/logout">Logout</a>
</div>
</div>
Expand Down

0 comments on commit cf826fc

Please sign in to comment.