#50 Fixed server-side map logic

master
Keith Irwin 2017-04-14 20:19:06 -04:00
parent dc75a337fd
commit 81a9bba507
No known key found for this signature in database
GPG Key ID: 378933C743E2BBC0
1 changed files with 11 additions and 40 deletions

View File

@ -13,53 +13,24 @@ router.get('/', mw.ensureAuth, (req,res)=>{
// Show map
router.get('/:slug?', (req,res,next)=>{
var mapuser='', user='', cbc=0;
// Confirm sucessful queries
function checkQuery(err,found) {
if (err){ mw.throwErr(err,req); }
if (found){ return found; }
}
// Call renderMap() on completion
function checkCBC() {
cbc+=1;
if (cbc>1){ renderMap(); }
}
// QUERIES
// Get logged in user -> user
if (req.isAuthenticated()) {
User.findById(req.user, function(err, found) {
user = checkQuery(err,found);
checkCBC();
});
} else { checkCBC(); }
// Get tracked user -> mapuser
if (req.params.slug) {
User.findOne({slug:req.params.slug}, function(err, found) {
mapuser = checkQuery(err,found);
checkCBC();
});
} else { checkCBC(); }
// Show map
function renderMap() {
// GET /map shows logged-in user's map
if (!mapuser && !user) {
res.redirect('/');
User.findOne({slug:req.params.slug})
.then( (mapuser)=>{
if (mapuser===undefined){
res.sendStatus(404);
} else {
if (user && !mapuser) { mapuser = user; }
res.render('map', {
mapuser: mapuser,
mapApi: env.googleMapsAPI,
user: user,
user: req.user,
noFooter: '1',
noHeader: (req.query.noheader)?req.query.noheader.match(/\d/)[0]:'',
disp: (req.query.disp)?req.query.disp.match(/\d/)[0]:'' // 0=map, 1=streetview, 2=both
noHeader: (req.query.noheader)?req.query.noheader.match(/\d/)[0]:0,
disp: (req.query.disp)?req.query.disp.match(/\d/)[0]:2 // 0=map, 1=streetview, 2=both
});
}
}
}).catch( (err)=>{
mw.throwErr(err,req);
});
});