Skip to content

Commit

Permalink
Add Fitbit interface
Browse files Browse the repository at this point in the history
  • Loading branch information
bpred754 committed Dec 12, 2016
1 parent 67af859 commit f3e69bc
Show file tree
Hide file tree
Showing 65 changed files with 2,743 additions and 205 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
###Augeo
A web application that gives you the power to view your interests in a quantitative way. It's common sense... the more you do something the better you become. In this case, we categorize your online activity into different skills and award experience depending on the activity type. As your experience grows, you will gain levels and be ranked among other [Augeo](https://www.augeo.io) users. Although the current application only takes Twitter and Github activity into consideration, the [vision](https://github.com/bpred754/augeo/wiki/Vision) for [Augeo](https://www.augeo.io) is much larger. Our intention is to expand to a vast set of interfaces, from other sites and blogs to health related apps such as FitBit. The possibilities are endless. In a nutshell, Augeo is the gamification of life.
A web application that gives you the power to view your interests in a quantitative way. It's common sense... the more you do something the better you become. In this case, we categorize your online activity into different skills and award experience depending on the activity type. As your experience grows, you will gain levels and be ranked among other [Augeo](https://www.augeo.io) users. We have a large [vision](https://github.com/bpred754/augeo/wiki/Vision) for Augeo and our intention is to expand to a vast set of interfaces, from other sites and blogs to IOT devices. The possibilities are endless. In a nutshell, Augeo is the gamification of life.

###Who should join?
You! Even if you don't consider yourself a front-runner in your field of interest/work, it's a part of [Augeo](https://www.augeo.io/login)'s [vision](https://github.com/bpred754/augeo/wiki/vision) to incorporate a portal for people with similar passions to connect. Not only can you connect with like-minded people, the concept of gamification introduces some friendly competition. Now, you will be more motivated than ever to expand your knowledge and skills to out rank your friends.
Expand Down
4 changes: 3 additions & 1 deletion environment.env
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ ENV=local
TEST=false
DB_URL=

# Common Interface Variables
AUTH_STATE=

# Github
GITHUB_CLIENT_ID=
GITHUB_CLIENT_SECRET=
GITHUB_STATE=
GITHUB_SCREEN_NAME=
GITHUB_ACCESS_TOKEN=

Expand Down
3 changes: 3 additions & 0 deletions src/api/augeo-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@
// Route all admin requests to admin-api.js
app.use('/admin-api', require('./admin-api'));

// Route all fitbit-api requests to fitbit-api.js
app.use('/fitbit-api', require('./fitbit-api'));

// Router all github-api requests to github-api.js
app.use('/github-api', require('./github-api'));

Expand Down
187 changes: 187 additions & 0 deletions src/api/fitbit-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@

/***************************************************************************/
/* Augeo.io is a web application that uses Natural Language Processing to */
/* classify a user's internet activity into different 'skills'. */
/* Copyright (C) 2016 Brian Redd */
/* */
/* This program is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/***************************************************************************/

/***************************************************************************/
/* Description: Handles requests to Augeo's fitbit-api */
/***************************************************************************/

// Required libraries
var FitbitRouter = require('express').Router();

// Required local modules
var AugeoUtility = require('../utility/augeo-utility');
var Logger = require('../module/logger');
var AugeoValidator = require('../validator/augeo-validator');
var FitbitInterfaceService = require('../interface-service/fitbit-interface-service');
var FitbitQueueTask = require('../queue-task/fitbit/fitbit-event-task');
var FitbitService = require('../service/fitbit-service');
var QueueService = require('../service/queue-service');
var UserService = require('../service/user-service');

// Constants
var API = 'fitbit-api';
var CALLBACK = '/callback';
var GET_AUTHENTICATION_DATA = '/getAuthenticationData';
var GET_QUEUE_WAIT_TIMES = '/getQueueWaitTimes';
var INVALID_SESSION = 'Invalid session';

// Global variables
var log = new Logger();

/***************************************************************************/
/* Service starts */
/***************************************************************************/

FitbitRouter.get(CALLBACK, function(request, response) {
var username = AugeoValidator.isSessionValid(request) ? request.session.user.username : null;

var rollback = function(errorCode, message) {
log.functionError(API, CALLBACK, username, message);

// Remove invalid entry from FITBIT_USER
if(username && request.session.user._id) {
var logData = AugeoUtility.formatLogData(API+CALLBACK, username);
FitbitService.removeUser(request.session.user._id, logData, function(){}, function(){});
}
response.redirect(process.env.AUGEO_HOME + '/signup/error'); // Redirect to signup error page
};

if(username) {
log.functionCall(API, CALLBACK, null, username);
var logData = AugeoUtility.formatLogData(API+CALLBACK, username);

var userId = request.session.user._id;
var code = request.query.code;

FitbitInterfaceService.getAuthData(code, logData, function(accessToken, refreshToken, fitbitId) {
if(accessToken && refreshToken && fitbitId) {
FitbitInterfaceService.getUserData(accessToken, logData, function(userData) {
FitbitService.checkExistingFitbitId(fitbitId, logData, function(doesFitbitIdExist) {

if(!doesFitbitIdExist) {

userData.augeoUser = userId;
userData.fitbitId = fitbitId;
userData.refreshToken = refreshToken;

FitbitService.addUser(username, userData, logData, function(addedUser) {

var period = '1y';
FitbitInterfaceService.getSteps(userData, period, logData, function(stepHistory) {

if(stepHistory instanceof Array) {
FitbitService.addDailySteps(stepHistory, logData, function () {

FitbitService.getLastDateTime(userData.fitbitId, logData, function (lastDateTime) {

if (process.env.TEST != 'true') {
var task = new FitbitQueueTask(addedUser, JSON.parse(JSON.stringify(userData)), lastDateTime, logData);
QueueService.fitbitEventQueue.addTask(task, logData);
}

// Set user's session data
request.session.user = addedUser.toJSON();
delete userData.accessToken;
delete userData.refreshToken;
request.session.user.fitbit = userData;

// Set profile image if none is set
if (request.session.user.profileImg == 'image/avatar-medium.png') {
UserService.setProfileImage('Fitbit', request.session.user, logData, function (updatedUser) {
request.session.user = updatedUser;
response.redirect(process.env.AUGEO_HOME + '/interfaceHistory');
});
} else {
response.redirect(process.env.AUGEO_HOME + '/interfaceHistory');
}
});
});
} else {
rollback(400, 'Failed to retrieve users steps');
}
});
}, function(message) {
rollback(400, message);
});
} else {
rollback(400, 'Fitbit user already exists');
}
});
});
} else {
rollback(400, 'Invalid authorization response from Fitbit');
}
});
} else {
rollback(401, INVALID_SESSION);
}
});

FitbitRouter.get(GET_AUTHENTICATION_DATA, function(request, response) {
var username = AugeoValidator.isSessionValid(request) ? request.session.user.username : null;

var rollback = function(code, message) {
log.functionError(API, GET_AUTHENTICATION_DATA, username, message);
response.status(code).send('Fitbit authentication failed. Please try again.');
};

if(username) {
log.functionCall(API, GET_AUTHENTICATION_DATA, null, request.session.user.username);

var data = {
clientId: process.env.FITBIT_CLIENT_ID,
responseType: 'code',
scope: 'activity nutrition profile',
redirectUri: process.env.AUGEO_HOME + '/fitbit-api/callback',
state: process.env.AUTH_STATE
};

response.status(200).send(data);
} else {
rollback(401, INVALID_SESSION);
}
});

FitbitRouter.get(GET_QUEUE_WAIT_TIMES, function(request, response) {
var username = AugeoValidator.isSessionValid(request) ? request.session.user.username : null;

var rollback = function(message) {
log.functionError(API, GET_QUEUE_WAIT_TIMES, username, message);
response.sendStatus(401);
};

if(username) {
log.functionCall(API, GET_QUEUE_WAIT_TIMES, null, username);

var waitTimes = new Array();
if(request.session.user.fitbit) {
waitTimes.push(-1);
} else {
waitTimes.push(0);
}

response.status(200).json({waitTimes:waitTimes});
} else { // If the user doesn't exist in session respond with "Unauthorized" HTTP code
rollback(INVALID_SESSION);
}
});

module.exports = FitbitRouter;

6 changes: 3 additions & 3 deletions src/api/github-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
var logData = AugeoUtility.formatLogData(API+CALLBACK, username);
GithubService.removeUser(request.session.user._id, logData, function(){}, function(){});
}
response.redirect(errorCode, process.env.AUGEO_HOME + '/signup/error'); // Redirect to signup error page
response.redirect(process.env.AUGEO_HOME + '/signup/error'); // Redirect to signup error page
};

if(username) {
Expand All @@ -72,7 +72,7 @@
var code = request.query.code;
var state = request.query.state;

if(code && state == process.env.GITHUB_STATE) {
if(code && state == process.env.AUTH_STATE) {

GithubInterfaceService.getAccessToken(code, logData, function(accessToken) {

Expand Down Expand Up @@ -155,7 +155,7 @@
clientId: process.env.GITHUB_CLIENT_ID,
redirectUrl: process.env.AUGEO_HOME + '/github-api/callback',
scope: 'public_repo',
state: process.env.GITHUB_STATE
state: process.env.AUTH_STATE
};

response.status(200).send(data);
Expand Down
7 changes: 1 addition & 6 deletions src/api/twitter-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
var logData = AugeoUtility.formatLogData(API+CALLBACK, username);
TwitterService.removeUser(request.session.user._id, logData, function(){}, function(){});
}
response.redirect(301, process.env.AUGEO_HOME + '/signup/error'); // Redirect to signup error page
response.redirect(process.env.AUGEO_HOME + '/signup/error'); // Redirect to signup error page
};

if(username) {
Expand Down Expand Up @@ -107,11 +107,6 @@
QueueService.twitterConnectQueue.connectToTwitter(logData, function(){});
}

// Set user's Twitter session data
request.session.user.twitter = {
screenName: screenName
};

response.redirect(process.env.AUGEO_HOME + '/interfaceHistory');
}, rollback); // End updateTwitterInfo

Expand Down
11 changes: 9 additions & 2 deletions src/api/user-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,17 @@
log.functionCall(API, GET_SKILL_ACTIVITY, null, sessionUsername, {'username':username,'skill':skill,'timestamp':timestamp});
var logData = AugeoUtility.formatLogData(API+GET_SKILL_ACTIVITY, sessionUsername);
UserService.getSkillActivity(username, skill, timestamp, logData, function (newData) {
response.status(200).json(newData);
if(newData) {
UserService.getUser(username, logData, function (targetUser) {
newData.user = targetUser;
response.status(200).json(newData);
});
} else {
rollback(400, 'Invalid username');
}
}, rollback);
} else {
rollback(401)
rollback(401, INVALID_SESSION)
}
});

Expand Down

0 comments on commit f3e69bc

Please sign in to comment.