Skip to content

Commit cf76162

Browse files
author
Gerad Suyderhoud
committed
notify teams when judges click through
1 parent 782a9ba commit cf76162

File tree

9 files changed

+99
-5
lines changed

9 files changed

+99
-5
lines changed

config/app.js

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ var express = require('express')
66
, secrets = env.secrets
77
, EventEmitter = require('events').EventEmitter
88
, Stats = require('../models/stats')
9+
, Twitter = require('../models/twitter');
910
// , ratchetio = require('ratchetio');
1011
require('jadevu');
1112

@@ -47,6 +48,9 @@ app.stats.on('change', function(stats) {
4748
app.events.emit('updateStats', stats);
4849
});
4950

51+
// twitter
52+
app.twitter = new Twitter(secrets.twitterUser)
53+
5054

5155
// state (getting pretty gross)
5256
app.disable('registration'); // months beforehand

config/env.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@ if (env.development) {
3333
postageapp: process.env.POSTAGEAPP_SECRET,
3434
session: process.env.EXPRESS_SESSION_KEY,
3535
twitterUser: {
36-
accessToken: process.env.TWITTER_ACCESS_TOKEN,
37-
accessTokenSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET
36+
consumer_key: process.env.TWITTER_USER_CONSUMER_KEY,
37+
consumer_secret: process.env.TWITTER_USER_CONSUMER_SECRET,
38+
access_token_key: process.env.TWITTER_USER_ACCESS_TOKEN_KEY,
39+
access_token_secret: process.env.TWITTER_USER_ACCESS_TOKEN_SECRET
3840
}
3941
};
4042
env.irc = {

controllers/index.coffee

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
util = require 'util'
12
app = require '../config/app'
23
Team = app.db.model 'Team'
34
Person = app.db.model 'Person'
@@ -12,6 +13,7 @@ loadCurrentPersonWithTeam = (req, res, next) ->
1213
return next err if err
1314
req.team = team
1415
next()
16+
1517
loadCanRegister = (req, res, next) ->
1618
Team.canRegister (err, canRegister, left) ->
1719
return next err if err

controllers/notifications.coffee

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
util = require 'util'
2+
app = require '../config/app'
3+
Team = app.db.model 'Team'
4+
5+
message = ->
6+
messages = [
7+
'psst, a judge is checking out your app'
8+
'your page is being attacked, erm judged'
9+
'batten down the hatches, a judge is coming'
10+
'come quick! a judge is on the way'
11+
'the judge has entered the building, erm your app'
12+
'come and play, you\'re being judged'
13+
'judge dread? oh noes! your app is being judged'
14+
]
15+
messages[Math.floor(Math.random() * messages.length)]
16+
17+
# notify teams when a judge visits
18+
app.post '/notify', (req, res, next) ->
19+
# only notify when judges click through
20+
return res.send(200) unless req.user?.judge
21+
22+
# see if the url being clicked belongs to a team entry
23+
url = req.body.url
24+
Team.findOne 'entry.url': req.body.url, (err, team) ->
25+
return next(err) if err
26+
27+
# if the url doesn't belong to team, or the team is does not have alerts
28+
# enabled, just ignore it
29+
return res.send(200) unless team?.entry?.alert
30+
31+
# load the twitter handles for the team
32+
team.twitterScreenNames (err, handles) ->
33+
return next(err) if err
34+
35+
util.log("NOTIFY #{team} (#{handles.join(', ')})".magenta)
36+
37+
# DM the team with the message
38+
39+
app.twitter.dm handles, "#{message()} - join at #{url}", (err, result) ->
40+
return next(err) if err
41+
42+
console.dir(result)
43+
res.send(200)
44+

models/team.coffee

+7
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,13 @@ TeamSchema.method 'incrementStats', (stats, callback) ->
233233
# return the reloaded the team (after the increment has been applied)
234234
Team.findOne _id: @id, callback
235235

236+
TeamSchema.method 'twitterScreenNames', (callback) ->
237+
@people (err, people) ->
238+
return callback(err) if err
239+
twitterScreenNames = for p in people when p.twitterScreenName
240+
p.twitterScreenName
241+
callback null, twitterScreenNames
242+
236243
# associations
237244
TeamSchema.method 'people', (next) ->
238245
Person.find _id: { '$in': @peopleIds }, next

models/twitter.coffee

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
nTwitter = require('ntwitter')
2+
3+
class Twitter
4+
constructor: (secrets) ->
5+
@twitter = new nTwitter(secrets)
6+
7+
# send `text` in a dm to `users`
8+
dm: (users, text, callback) ->
9+
ret = {}
10+
i = users.length
11+
for user in users
12+
do (user) =>
13+
@post '/direct_messages/new.json', { screen_name: user, text: text }, (err, data) ->
14+
ret[user] = err ? data
15+
return callback(null, ret) if --i is 0
16+
17+
post: (url, data, callback) ->
18+
@twitter.post(url, data, callback)
19+
20+
module.exports = Twitter

public/javascripts/application.coffee

+6
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ $.ajaxPrefilter (options, originalOptions, xhr) ->
3434
# speed up default jQuery animations
3535
$.fx.speeds._default = 200
3636

37+
# notify team when judges click through
38+
$('a[href^=http]').live 'click', (e) ->
39+
$.post "/notify", { url: this.href }, (args...) ->
40+
console.log(args)
41+
false
42+
3743
load = ->
3844
$(':text:first').focus() # focus first input
3945

scripts/sanitize.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,16 @@ db.people.find({ email: { $nin: [
33
44
/@fortnightlabs\.com$/,
55
/\.nodeknockout.com$/] }}).forEach(function(doc) {
6+
7+
// ensure no emails
68
if (doc.email) {
7-
db.people.update({ _id: doc._id },
8-
{ $set: { email: doc.email + '.nodeknockout.com' }});
9+
db.people.update({ _id: doc._id }, {
10+
$set: { email: doc.email + '.nodeknockout.com' }});
11+
}
12+
13+
// ensure no DMs
14+
if (doc.twitterScreenName) {
15+
db.people.update({ _id: doc._id }, {
16+
$unset: { twitterScreenName: 1 }});
917
}
1018
});

server.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ require('coffee-script');
99
'votes',
1010
'websocket',
1111
'live',
12-
'redirect'
12+
'redirect',
13+
'notifications'
1314
].forEach(function(controller) {
1415
require('./controllers/' + controller);
1516
});

0 commit comments

Comments
 (0)