Fix Lint all the controllers

Fix many undefined variables
Fix many unhandled error calls
This commit is contained in:
Berkeley Martinez
2015-05-21 11:07:40 -07:00
parent c5d3aa4bff
commit b58b725f9d
9 changed files with 759 additions and 573 deletions

View File

@@ -1,4 +1,15 @@
var async = require('async'),
path = require('path'),
moment = require('moment'),
Twit = require('twit'),
debug = require('debug')('freecc:cntr:resources'),
cheerio = require('cheerio'),
request = require('request'),
R = require('ramda'),
_ = require('lodash'),
fs = require('fs'),
constantStrings = require('constantStrings.json'),
User = require('../models/User'),
Challenge = require('./../models/Challenge'),
Story = require('./../models/Story'),
@@ -8,16 +19,7 @@ var async = require('async'),
resources = require('./resources.json'),
secrets = require('./../config/secrets'),
nonprofits = require('../seed_data/nonprofits.json'),
fieldGuides = require('../seed_data/field-guides.json'),
moment = require('moment'),
Twit = require('twit'),
https = require('https'),
debug = require('debug')('freecc:cntr:resources'),
cheerio = require('cheerio'),
request = require('request'),
R = require('ramda'),
_ = require('lodash'),
fs = require('fs');
fieldGuides = require('../seed_data/field-guides.json');
/**
* Cached values
@@ -45,11 +47,14 @@ Array.zip = function(left, right, combinerFunction) {
(function() {
if (!challengeMap) {
var localChallengeMap = {};
var files = fs.readdirSync(__dirname + '/../seed_data/challenges');
var files = fs.readdirSync(
path.join(__dirname, '/../seed_data/challenges')
);
var keyCounter = 0;
files = files.map(function (file) {
return require(__dirname +
'/../seed_data/challenges/' + file);
return require(
path.join(__dirname, '/../seed_data/challenges/' + file)
);
});
files = files.sort(function (a, b) {
return a.order - b.order;
@@ -208,13 +213,16 @@ module.exports = {
res.redirect('http://freecode.slack.com');
} else {
res.render('resources/chat', {
title: "Watch us code live on Twitch.tv"
title: 'Watch us code live on Twitch.tv'
});
}
},
catPhotoSubmit: function catPhotoSubmit(req, res) {
res.send('Success! You have submitted your cat photo. Return to your website by typing any letter into your code editor.')
res.send(
'Success! You have submitted your cat photo. Return to your website ' +
'by typing any letter into your code editor.'
);
},
nonprofits: function nonprofits(req, res) {
@@ -247,8 +255,8 @@ module.exports = {
});
},
unsubscribe: function unsubscribe(req, res) {
User.findOne({email: req.params.email}, function(err, user) {
unsubscribe: function unsubscribe(req, res, next) {
User.findOne({ email: req.params.email }, function(err, user) {
if (user) {
if (err) {
return next(err);
@@ -268,52 +276,106 @@ module.exports = {
unsubscribed: function unsubscribed(req, res) {
res.render('resources/unsubscribed', {
title: "You have been unsubscribed"
title: 'You have been unsubscribed'
});
},
githubCalls: function(req, res) {
var githubHeaders = {headers: {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1521.3 Safari/537.36'}, port:80 };
request('https://api.github.com/repos/freecodecamp/freecodecamp/pulls?client_id=' + secrets.github.clientID + '&client_secret=' + secrets.github.clientSecret, githubHeaders, function(err, status1, pulls) {
pulls = pulls ? Object.keys(JSON.parse(pulls)).length : "Can't connect to github";
request('https://api.github.com/repos/freecodecamp/freecodecamp/issues?client_id=' + secrets.github.clientID + '&client_secret=' + secrets.github.clientSecret, githubHeaders, function (err, status2, issues) {
issues = ((pulls === parseInt(pulls)) && issues) ? Object.keys(JSON.parse(issues)).length - pulls : "Can't connect to GitHub";
res.send({"issues": issues, "pulls" : pulls});
});
});
githubCalls: function(req, res, next) {
var githubHeaders = {
headers: {
'User-Agent': constantStrings.gitHubUserAgent
},
port: 80
};
request(
[
'https://api.github.com/repos/freecodecamp/',
'freecodecamp/pulls?client_id=',
secrets.github.clientID,
'&client_secret=',
secrets.github.clientSecret
].join(''),
githubHeaders,
function(err, status1, pulls) {
if (err) { return next(err); }
pulls = pulls ?
Object.keys(JSON.parse(pulls)).length :
"Can't connect to github";
request(
[
'https://api.github.com/repos/freecodecamp/',
'freecodecamp/issues?client_id=',
secrets.github.clientID,
'&client_secret=',
secrets.github.clientSecret
].join(''),
githubHeaders,
function (err, status2, issues) {
if (err) { return next(err); }
issues = ((pulls === parseInt(pulls, 10)) && issues) ?
Object.keys(JSON.parse(issues)).length - pulls :
"Can't connect to GitHub";
res.send({
issues: issues,
pulls: pulls
});
}
);
}
);
},
trelloCalls: function(req, res, next) {
request('https://trello.com/1/boards/BA3xVpz9/cards?key=' + secrets.trello.key, function(err, status, trello) {
if (err) { return next(err); }
trello = (status && status.statusCode === 200) ? (JSON.parse(trello)) : "Can't connect to to Trello";
res.end(JSON.stringify(trello));
});
request(
'https://trello.com/1/boards/BA3xVpz9/cards?key=' +
secrets.trello.key,
function(err, status, trello) {
if (err) { return next(err); }
trello = (status && status.statusCode === 200) ?
(JSON.parse(trello)) :
"Can't connect to to Trello";
res.end(JSON.stringify(trello));
});
},
bloggerCalls: function(req, res, next) {
request('https://www.googleapis.com/blogger/v3/blogs/2421288658305323950/posts?key=' + secrets.blogger.key, function (err, status, blog) {
if (err) { return next(err); }
blog = (status && status.statusCode === 200) ? JSON.parse(blog) : "Can't connect to Blogger";
res.end(JSON.stringify(blog));
});
request(
'https://www.googleapis.com/blogger/v3/blogs/2421288658305323950/' +
'posts?key=' +
secrets.blogger.key,
function (err, status, blog) {
if (err) { return next(err); }
blog = (status && status.statusCode === 200) ?
JSON.parse(blog) :
"Can't connect to Blogger";
res.end(JSON.stringify(blog));
}
);
},
about: function(req, res, next) {
if (req.user) {
if (!req.user.profile.picture || req.user.profile.picture === "https://s3.amazonaws.com/freecodecamp/favicons/apple-touch-icon-180x180.png") {
req.user.profile.picture = "https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png";
if (
!req.user.profile.picture ||
req.user.profile.picture.indexOf('apple-touch-icon-180x180.png') !== -1
) {
req.user.profile.picture =
'https://s3.amazonaws.com/freecodecamp/camper-image-placeholder.png';
// TODO(berks): unhandled callback
req.user.save();
}
}
var date1 = new Date("10/15/2014");
var date1 = new Date('10/15/2014');
var date2 = new Date();
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var daysRunning = Math.ceil(timeDiff / (1000 * 3600 * 24));
var announcements = resources.announcements;
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
}
User.count({}, function (err, c3) {
if (err) {
@@ -331,28 +393,30 @@ module.exports = {
},
randomPhrase: function() {
return resources.phrases[Math.floor(
Math.random() * resources.phrases.length)];
return resources.phrases[
Math.floor(Math.random() * resources.phrases.length)
];
},
randomVerb: function() {
return resources.verbs[Math.floor(
Math.random() * resources.verbs.length)];
return resources.verbs[
Math.floor(Math.random() * resources.verbs.length)
];
},
randomCompliment: function() {
return resources.compliments[Math.floor(
Math.random() * resources.compliments.length)];
return resources.compliments[
Math.floor(Math.random() * resources.compliments.length)
];
},
allFieldGuideIds: function() {
if (allFieldGuideIds) {
return allFieldGuideIds;
} else {
allFieldGuideIds = fieldGuides.
map(function (elem) {
return elem._id;
});
allFieldGuideIds = fieldGuides.map(function (elem) {
return elem._id;
});
return allFieldGuideIds;
}
},
@@ -361,12 +425,9 @@ module.exports = {
if (allFieldGuideNames) {
return allFieldGuideNames;
} else {
allFieldGuideNames = fieldGuides.
map(function (elem) {
return {
name: elem.name
};
});
allFieldGuideNames = fieldGuides.map(function (elem) {
return { name: elem.name };
});
return allFieldGuideNames;
}
},
@@ -375,12 +436,9 @@ module.exports = {
if (allNonprofitNames) {
return allNonprofitNames;
} else {
allNonprofitNames = nonprofits.
map(function (elem) {
return {
name: elem.name
};
});
allNonprofitNames = nonprofits.map(function (elem) {
return { name: elem.name };
});
return allNonprofitNames;
}
},
@@ -396,16 +454,25 @@ module.exports = {
if (!error && response.statusCode === 200) {
var $ = cheerio.load(body);
var metaDescription = $("meta[name='description']");
var metaImage = $("meta[property='og:image']");
var urlImage = metaImage.attr('content') ? metaImage.attr('content') : '';
var metaImage = $("meta[property='og:image']");
var urlImage = metaImage.attr('content') ?
metaImage.attr('content') :
'';
var metaTitle = $('title');
var description = metaDescription.attr('content') ? metaDescription.attr('content') : '';
result.title = metaTitle.text().length < 90 ? metaTitle.text() : metaTitle.text().slice(0, 87) + "...";
var description = metaDescription.attr('content') ?
metaDescription.attr('content') :
'';
result.title = metaTitle.text().length < 90 ?
metaTitle.text() :
metaTitle.text().slice(0, 87) + '...';
result.image = urlImage;
result.description = description;
callback(null, result);
} else {
callback('failed');
callback(new Error('failed'));
}
});
})();
@@ -465,24 +532,33 @@ module.exports = {
}
},
codepenResources: {
twitter: function(req, res) {
twitter: function(req, res, next) {
// sends out random tweets about javascript
var T = new Twit({
consumer_key: secrets.twitter.consumerKey,
consumer_secret: secrets.twitter.consumerSecret,
access_token: secrets.twitter.token,
access_token_secret: secrets.twitter.tokenSecret
'consumer_key': secrets.twitter.consumerKey,
'consumer_secret': secrets.twitter.consumerSecret,
'access_token': secrets.twitter.token,
'access_token_secret': secrets.twitter.tokenSecret
});
var screenName;
if (req.params.screenName) {
screenName = req.params.screenName;
} else {
screenName = 'freecodecamp';
}
T.get('statuses/user_timeline', {screen_name: screenName, count:10}, function(err, data, response) {
return res.json(data);
});
T.get(
'statuses/user_timeline',
{
'screen_name': screenName,
count: 10
},
function(err, data) {
if (err) { return next(err); }
return res.json(data);
}
);
},
twitterFCCStream: function() {
// sends out a tweet stream from FCC's account