Browse files

notify teams when judges click through

  • Loading branch information...
1 parent 782a9ba commit cf76162b19e861bdcbd43d29be70caaeea3ea5a7 @gerad gerad committed Nov 11, 2012
View
4 config/app.js
@@ -6,6 +6,7 @@ var express = require('express')
, secrets = env.secrets
, EventEmitter = require('events').EventEmitter
, Stats = require('../models/stats')
+ , Twitter = require('../models/twitter');
// , ratchetio = require('ratchetio');
require('jadevu');
@@ -47,6 +48,9 @@ app.stats.on('change', function(stats) {
app.events.emit('updateStats', stats);
});
+// twitter
+app.twitter = new Twitter(secrets.twitterUser)
+
// state (getting pretty gross)
app.disable('registration'); // months beforehand
View
6 config/env.js
@@ -33,8 +33,10 @@ if (env.development) {
postageapp: process.env.POSTAGEAPP_SECRET,
session: process.env.EXPRESS_SESSION_KEY,
twitterUser: {
- accessToken: process.env.TWITTER_ACCESS_TOKEN,
- accessTokenSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET
+ consumer_key: process.env.TWITTER_USER_CONSUMER_KEY,
+ consumer_secret: process.env.TWITTER_USER_CONSUMER_SECRET,
+ access_token_key: process.env.TWITTER_USER_ACCESS_TOKEN_KEY,
+ access_token_secret: process.env.TWITTER_USER_ACCESS_TOKEN_SECRET
}
};
env.irc = {
View
2 controllers/index.coffee
@@ -1,3 +1,4 @@
+util = require 'util'
app = require '../config/app'
Team = app.db.model 'Team'
Person = app.db.model 'Person'
@@ -12,6 +13,7 @@ loadCurrentPersonWithTeam = (req, res, next) ->
return next err if err
req.team = team
next()
+
loadCanRegister = (req, res, next) ->
Team.canRegister (err, canRegister, left) ->
return next err if err
View
44 controllers/notifications.coffee
@@ -0,0 +1,44 @@
+util = require 'util'
+app = require '../config/app'
+Team = app.db.model 'Team'
+
+message = ->
+ messages = [
+ 'psst, a judge is checking out your app'
+ 'your page is being attacked, erm judged'
+ 'batten down the hatches, a judge is coming'
+ 'come quick! a judge is on the way'
+ 'the judge has entered the building, erm your app'
+ 'come and play, you\'re being judged'
+ 'judge dread? oh noes! your app is being judged'
+ ]
+ messages[Math.floor(Math.random() * messages.length)]
+
+# notify teams when a judge visits
+app.post '/notify', (req, res, next) ->
+ # only notify when judges click through
+ return res.send(200) unless req.user?.judge
+
+ # see if the url being clicked belongs to a team entry
+ url = req.body.url
+ Team.findOne 'entry.url': req.body.url, (err, team) ->
+ return next(err) if err
+
+ # if the url doesn't belong to team, or the team is does not have alerts
+ # enabled, just ignore it
+ return res.send(200) unless team?.entry?.alert
+
+ # load the twitter handles for the team
+ team.twitterScreenNames (err, handles) ->
+ return next(err) if err
+
+ util.log("NOTIFY #{team} (#{handles.join(', ')})".magenta)
+
+ # DM the team with the message
+
+ app.twitter.dm handles, "#{message()} - join at #{url}", (err, result) ->
+ return next(err) if err
+
+ console.dir(result)
+ res.send(200)
+
View
7 models/team.coffee
@@ -233,6 +233,13 @@ TeamSchema.method 'incrementStats', (stats, callback) ->
# return the reloaded the team (after the increment has been applied)
Team.findOne _id: @id, callback
+TeamSchema.method 'twitterScreenNames', (callback) ->
+ @people (err, people) ->
+ return callback(err) if err
+ twitterScreenNames = for p in people when p.twitterScreenName
+ p.twitterScreenName
+ callback null, twitterScreenNames
+
# associations
TeamSchema.method 'people', (next) ->
Person.find _id: { '$in': @peopleIds }, next
View
20 models/twitter.coffee
@@ -0,0 +1,20 @@
+nTwitter = require('ntwitter')
+
+class Twitter
+ constructor: (secrets) ->
+ @twitter = new nTwitter(secrets)
+
+ # send `text` in a dm to `users`
+ dm: (users, text, callback) ->
+ ret = {}
+ i = users.length
+ for user in users
+ do (user) =>
+ @post '/direct_messages/new.json', { screen_name: user, text: text }, (err, data) ->
+ ret[user] = err ? data
+ return callback(null, ret) if --i is 0
+
+ post: (url, data, callback) ->
+ @twitter.post(url, data, callback)
+
+module.exports = Twitter
View
6 public/javascripts/application.coffee
@@ -34,6 +34,12 @@ $.ajaxPrefilter (options, originalOptions, xhr) ->
# speed up default jQuery animations
$.fx.speeds._default = 200
+# notify team when judges click through
+$('a[href^=http]').live 'click', (e) ->
+ $.post "/notify", { url: this.href }, (args...) ->
+ console.log(args)
+ false
+
load = ->
$(':text:first').focus() # focus first input
View
12 scripts/sanitize.js
@@ -3,8 +3,16 @@ db.people.find({ email: { $nin: [
'gerads@gmail.com',
/@fortnightlabs\.com$/,
/\.nodeknockout.com$/] }}).forEach(function(doc) {
+
+ // ensure no emails
if (doc.email) {
- db.people.update({ _id: doc._id },
- { $set: { email: doc.email + '.nodeknockout.com' }});
+ db.people.update({ _id: doc._id }, {
+ $set: { email: doc.email + '.nodeknockout.com' }});
+ }
+
+ // ensure no DMs
+ if (doc.twitterScreenName) {
+ db.people.update({ _id: doc._id }, {
+ $unset: { twitterScreenName: 1 }});
}
});
View
3 server.js
@@ -9,7 +9,8 @@ require('coffee-script');
'votes',
'websocket',
'live',
- 'redirect'
+ 'redirect',
+ 'notifications'
].forEach(function(controller) {
require('./controllers/' + controller);
});

1 comment on commit cf76162

@gerad
Node.js Knockout 3 member

this one could probably use another set of eyes so I don't dm spam everyone...

Please sign in to comment.