Skip to content

Commit

Permalink
Add tracker foursquare
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Oct 16, 2013
1 parent fc1b072 commit 7d2fbe8
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 17 deletions.
1 change: 1 addition & 0 deletions bin/reportr.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var modulesSection = {

"./trackers",
"./tracker.ping",
"./tracker.foursquare",

"./model.user",
"./model.event",
Expand Down
5 changes: 5 additions & 0 deletions lib/config/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ function setup(options, imports, register) {
}*/
'tracker': {
'interval': 10*60
},
'foursquare': {
'interval': 6*60*60,
'clientId': process.env.FOURSQUARE_CLIENTID,
'clientSecret': process.env.FOURSQUARE_CLIENTSECRET
}
}
}
Expand Down
1 change: 0 additions & 1 deletion lib/model.user/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ function setup(options, imports, register) {
delete this.trackers[tId];
} else if (st && this.trackers[tId] == null) {
this.trackers[tId] = {};
tracker.setupUser(this);
afterUrl = tracker.authRedirect;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/queue/queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var MessagesQueue = function(logger){
}).on('error', function (error){
that.logger.exception(error);
}).on('close', function () {
that.logger.log('Queue closed');
that.logger.log('queue is empty');
});
};

Expand Down
2 changes: 1 addition & 1 deletion lib/tasks/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ function setup(options, imports, register) {
// -> run when user active the tracker
var addTrackerTask = function(tracker, task, interval) {
// Bind activation
queue.on("tracker."+tracker.id+".activation", function(data) {
queue.on("tracker."+tracker.id, function(data) {
User.findOne({
'token': data.token
}, function(err, user) {
Expand Down
95 changes: 95 additions & 0 deletions lib/tracker.foursquare/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
var _ = require("underscore");
var FoursquareStrategy = require('passport-foursquare').Strategy
var Foursquare = require('node-foursquare');

function setup(options, imports, register) {
var logger = imports.logger.namespace("tracker.foursquare");
var trackers = imports.trackers;
var tasks = imports.tasks;

var tracker = trackers.register({
'id': "foursquare",
'name': "Foursquare",
'description': "Track your foursquare activity into Reportr.",

'initConfig': function() {
_.defaults(this.config, {
'interval': 60*60
});
},

'initWeb': function() {
this.oauth(FoursquareStrategy);
},

'initWorker': function() {
var that = this;
this.foursquare = Foursquare({
'secrets' : {
'clientId': this.config.clientId,
'clientSecret': this.config.clientSecret,
'redirectUrl': "/auth/foursquare/callback"
}
});

tasks.addTrackerTask(this, function(user) {
var userConfig = user.getTrackerSettings(that.id);
if (userConfig == null || userConfig.accessToken == null) return;

that.trackCheckins(user, userConfig);
}, this.config.interval);
},

// Track user checkins
'trackCheckins': function(user, userConfig) {
logger.log("track checkin for ", user.token);
user.setModel({
'eventNamespace': 'foursquare',
'eventName': 'checkin',

'name': "Checkins",
'icon': '$foursquare',
'description': "Foursquare checkins."
});

this.foursquare.Users.getCheckins(undefined, {
'limit': 100
}, userConfig.accessToken, function(err, data) {
if (data == null
|| data.checkins == null
|| data.checkins.items == null) {
return;
}

_.each(data.checkins.items, function(checkin) {
if (!checkin.venue || !checkin.id || !checkin.createdAt) return;

var timestamp = checkin.createdAt * 1000;

// Track as an event
user.track({
'eventId': timestamp, // Unique checkin by timestamp
'namespace': 'foursquare',
'name': 'checkin',
'properties': {
'checkinId': checkin.id,
'placeId': checkin.venue.id,
'place': checkin.venue.name,
'@lat': checkin.venue.location.lat,
'@lng': checkin.venue.location.lng
},
'timestamp': timestamp
});
});
});
}
});


register(null, {
'tracker.foursquare': tracker
});
};

// Exports
module.exports = setup;
16 changes: 16 additions & 0 deletions lib/tracker.foursquare/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "tracker.foursquare",
"version": "0.0.1",

"main": "./main.js",
"private": true,

"plugin": {
"consumes": [
"logger", "trackers", "tasks"
],
"provides": [
"tracker.foursquare"
]
}
}
28 changes: 15 additions & 13 deletions lib/trackers/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ function setup(options, imports, register) {
};

// Setup an user
this.setupUser = function(user) {
queue.post("tracker."+this.id+".activation", {
this.runUserTasks = function(user) {
queue.post("tracker."+this.id, {
'token': user.token
});
};
Expand All @@ -67,19 +67,19 @@ function setup(options, imports, register) {

// Init config
_.extend(this, properties);
this.initConfig();
this.initConfig();

if (options.worker) {
this.initWorker();
}
if (options.worker) {
this.initWorker();
}

if (options.web) {
this.initWeb();
}
if (options.web) {
this.initWeb();
}
};

// Setup oauth
this.oauth = function(app, Strategy, options) {
this.oauth = function(Strategy, options) {
var that = this;
_.defaults(this.config, {
'interval': 60*60,
Expand All @@ -105,10 +105,10 @@ function setup(options, imports, register) {

// oAuth
passport.use(new Strategy(options.strategyOptions, function(req, accessToken, refreshToken, profile, done) {
console.log(that.id, ":new user ", accessToken, refreshToken, profile.id);
logger.log(that.id, ":new user ", accessToken, refreshToken, profile.id);

// Set tracker settings
req.user.setTrackerServiceSettings(that.id, {
req.user.setTrackerSettings(that.id, {
'userId': profile.id,
'accessToken': accessToken,
'refreshToken': refreshToken
Expand Down Expand Up @@ -143,7 +143,9 @@ function setup(options, imports, register) {
var registerTracker = function(module) {
logger.log("register tracker", module.id);
trackers[module.id] = new TrackerService(module);
return trackers[module.id];
return {
'id': trackers[module.id].id
};
};

// Load all trackers
Expand Down
12 changes: 12 additions & 0 deletions lib/web.main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ function setup(options, imports, register) {
var trackers = imports.trackers;
var User = imports.User.Model;

// Configure passport
passport.serializeUser(function(user, done) {
done(null, user.token);
});
passport.deserializeUser(function(obj, done) {
User.getByToken(obj).then(function(user) {
done(null, user);
}, function() {
done(new Error("Error authenticate the user by token"), null);
});
});

// Create server
var app = express();
var server = http.createServer(app);
Expand Down
2 changes: 1 addition & 1 deletion lib/worker.main/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"plugin": {
"consumes": [
"config", "logger", "database", "trackers", "tasks",
"tracker.ping"
"tracker.ping", "tracker.foursquare"
]
}
}

0 comments on commit 7d2fbe8

Please sign in to comment.