Skip to content

Commit

Permalink
merge
Browse files Browse the repository at this point in the history
  • Loading branch information
joelanman committed Feb 18, 2012
2 parents 6c8c3ef + 452a92c commit 2017547
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 24 deletions.
1 change: 1 addition & 0 deletions app.js
Expand Up @@ -36,6 +36,7 @@ app.get('/signedin', routes.signedin);
app.get('/alert/:username', routes.alert);
app.get('/status/', routes.status_listings);
app.get('/status/:username', routes.status);
app.get('/checkin/:username', routes.checkin);
app.get('/register', routes.register);
app.post('/newuser', routes.adduser);

Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -6,5 +6,6 @@
"express": "2.5.5"
, "stache": ">= 0.0.1"
, "mailer": ">= 0.0.1"
, "ntwitter": ">= 0.0.1"
}
}
81 changes: 74 additions & 7 deletions routes/index.js
@@ -1,12 +1,54 @@
var models = require('./models');
var users = new models.UserList();
var request = require('request');

var twitter = require('ntwitter');
var tSignals = new models.TwitterSignals();




var twit = new twitter({
consumer_key: '1NFZuC5z1E0bCjGvtiHFAw',
consumer_secret: 'uxYCzvY3ju0jN6v0prvnWZJzSKpkhcNBdMIXkTE',
access_token_key: '30838623-VVTcQRbHMfSoetEDb1bjfeiiohRMWkpmoCUIiuJ0',
access_token_secret: 'X04ban3VGmUKBklIyDu23LEoy9Tosl9y3r18Ys0n4'
});



function updateTwitterStream()
{

var allthetwitternames = [];

for( var twittername in tSignals.signals )
{
allthetwitternames.push(twittername);
}

console.log(allthetwitternames.toString());

twit.stream('statuses/filter', {'follow':allthetwitternames.toString()}, function(stream) {
stream.on('data', function (data) {

var chckin = new Checkin(new Date().getTime(), null, 'twitter', 'twitted: '+ data.text);
users.get_user(tSignals.get_user(data.user.screen_name)).checkin(chckin);
console.log(data.user.screen_name+": "+data.text);

});
});
}


function bootstrap() {
// bootstrap data
var dummy = new models.User('dummy', 'password', 'dummy123', 'I might be in trouble, please eat all the vegetables.', 'dummy@example.com');
users.add_user(dummy);
}

bootstrap();

exports.index = function(req, res){
res.render('index', {
locals: {
Expand Down Expand Up @@ -66,15 +108,36 @@ exports.status_listings = function(req, res) {
for (var username in users.users) {
user = users.get_user(username)
if (user != undefined) {
// TODO: .check_status() elsewhere
status = user.check_status();
statuses.push({'username': user.username,
'status': user.status});
'status': status});
}
}
res.render('status_listings', {
locals: {'statuses': statuses}
});
}

exports.checkin = function(req, res) {
var username = req.params.username;
var user = users.get_user(username);

if (user != undefined) {
user.checkin(models.sources.SYSTEM, 'online checkin')
console.log("Checked in for %s: %s", username, user.status);
res.render('status', {
locals: {'username': username,
'status': user.status}
});
} else {
console.log("Checkin for invalid username: %s", username);
res.render('404', {
locals: {'reason': 'User could not be found'}
});
}
};

exports.testmail = function(req, res){
var usr = new models.User('gerard', 'gerard123', 'QWERTY', 'I might be in trouble, please eat all the vegetables.', 'gerard@ideesabsurdes.net');
// users.add_user(usr);
Expand All @@ -91,13 +154,17 @@ exports.adduser = function(req, res){
var usr = new models.User(user_data.username, user_data.password, key, user_data.email, [user_data.contact01, user_data.contact02, user_data.contact03, user_data.contact04, user_data.contact05]);
users.add_user(usr);


request('https://api.twitter.com/1/users/lookup.json?screen_name='+user_data.twitter, function (error, response, body) {
if (!error && response.statusCode == 200) {
var tid = JSON.parse(body)[0].id;
console.log(tid);
tSignals.add_signal(user_data.username, tid);
updateTwitterStream();
}
})
res.redirect('/signedin');

/*
res.render('generic_message', {
locals:{'message': 'registered! This is your secret key to update your status from anywhere else:'+key+''}
});
*/

}


Expand Down
133 changes: 117 additions & 16 deletions routes/models.js
@@ -1,11 +1,31 @@
var email = require('mailer');

var sources = {};
sources.SYSTEM = 'system';
sources.TWITTER = 'twitter';

// Time thresholds for each source (in minutes).
// `active` - User is ok
// `unknown` - User has not checked in recently but is not considered detained
// `offline` - User has not checked in and is considered detained
var CONFIG_DEFAULTS = {};
CONFIG_DEFAULTS[sources.SYSTEM] = {
'active': 1,
'unknown': 10,
'offline': 30
};

CONFIG_DEFAULTS[sources.TWITTER] = {
'active': 5,
'unknown': 10,
'offline': 30
};

// UserList
//
//
//

var email = require('mailer');

function UserList(){
this.users = {};
}
Expand All @@ -24,8 +44,12 @@ function User(username, password, key, email, email_to) {
this.key = key;
this.email = email;
this.email_to = email_to;
this.status = 'ok';
this.checkins = [ new Checkin(new Date().getTime(), null, username, 'created account') ];
this.status = 'ok';
this.config = {};
// configure the user with the system settings
this.config[sources.SYSTEM] = CONFIG_DEFAULTS[sources.SYSTEM]
this.checkins = [];
this.checkin(sources.SYSTEM, 'created account');
}
User.prototype.get_username = function() { return this.username; };
User.prototype.get_password = function() { return this.password; };
Expand All @@ -35,15 +59,72 @@ User.prototype.get_emailto = function() { return this.email_to; };
User.prototype.get_status = function() { return this.status; };
User.prototype.get_checkins = function() { return this.checkins; };
User.prototype.get_last_checkin = function() { return this.checkins[checkins.length - 1]; };
User.prototype.get_config = function() {
config = {}
for (var source in this.config) {
if (this.config[source] != undefined) {
config[source] = this.config[source]
} else {
config[source] = CONFIG_DEFAULTS[source]
}
}
return config
}


User.prototype.checkin = function(checkin) { this.checkins.push(checkin); }
User.prototype.alert = function() {
var checkin = new Checkin(new Date().getTime(), null, this.username, 'panic alert')
User.prototype.checkin = function(source, message) {
var checkin = new Checkin(new Date().getTime(), null, source, message);
this.checkins.push(checkin);
};
User.prototype.alert = function() {
this.checkin(sources.SYSTEM, 'panic alert');
this.status = 'offline'
// send alerts & notifications
}
};
User.prototype.check_status = function() {
var now = new Date();
var status = 'offline';
var last_checkins = [];
var active_sources = [];
var config = this.get_config();

// for each configured source
for (var key in config) {
thresholds = config[key];
var last_checkin = null;
for (var i=this.checkins.length-1; checkin=this.checkins[i], i>=0; i--) {
if (checkin.source == key) {console.log(checkin); last_checkin = checkin; break}
}

if (last_checkin == null)
continue

if (status != 'active') {
console.log('last checkin for %s through %s was at %s', this.username, key, last_checkin.date_time);

var unknown = new Date();
unknown.setMinutes(unknown.getMinutes() - thresholds['unknown']);
var active = new Date();
active.setMinutes(active.getMinutes() - thresholds['active']);

if (last_checkin.date_time > active){
source_status = 'active';
status = source_status;
active_sources.push(key);
} else if (last_checkin.date_time > unknown) {
source_status = 'unknown';
if (status != 'active')
status = source_status;
}
last_checkins.push({'checkin': last_checkin, 'status': source_status});

}
}
this.status = status;
return {'status': this.status,
'active_sources': active_sources,
'checkins': last_checkins}
};

User.prototype.sendEmail = function(){

email.send({
Expand Down Expand Up @@ -79,12 +160,32 @@ function Checkin(date_time, location, source, action) {
this.source = source;
this.action = action;
}
User.prototype.get_date_time = function() { return this.date_time; };
User.prototype.get_location = function() { return this.location; };
User.prototype.get_source = function() { return this.source; };
User.prototype.get_action = function() { return this.action; };
Checkin.prototype.get_date_time = function() { return this.date_time; };
Checkin.prototype.get_location = function() { return this.location; };
Checkin.prototype.get_source = function() { return this.source; };
Checkin.prototype.get_action = function() { return this.action; };


// Signals
//
//
//

function TwitterSignals()
{
this.signals = {};
//this.local_username = user;
}

TwitterSignals.prototype.add_signal = function(username,twittername) { this.signals[twittername] = username; };
TwitterSignals.prototype.get_user = function(twitter) { return this.signals[twitter]; };





exports.User = User;
exports.Checkin = Checkin;
exports.UserList = UserList;
exports.User = User;
exports.Checkin = Checkin;
exports.UserList = UserList;
exports.sources = sources;
exports.TwitterSignals = TwitterSignals;
1 change: 1 addition & 0 deletions views/checkin.mustache
@@ -0,0 +1 @@
<h1>Checkin recieved for {{username}}</h1>
2 changes: 2 additions & 0 deletions views/registration.mustache
Expand Up @@ -3,8 +3,10 @@

<h1>Create an emergency alert</h1>


<form action="/newuser" method="post">


<div class="row clearfix">
<label for="username">Username</label>
<input id = 2type="text" name="user[username]">
Expand Down
5 changes: 4 additions & 1 deletion views/status.mustache
@@ -1,2 +1,5 @@
<h1>Current status of {{username}}</h1>
<p>{{status}}</p>
<p>{{status.status}} (Active on {{status.active_sources}})</p>
{{#status.last_checkins}}
<p>{{checkin.source}}: {{status}}</p>
{{/status.last_checkins}}

0 comments on commit 2017547

Please sign in to comment.