2017-03-15 02:01:55 -06:00
|
|
|
'use strict';
|
|
|
|
|
2017-03-15 02:08:12 -06:00
|
|
|
const router = require('express').Router(),
|
2016-05-31 21:54:21 -06:00
|
|
|
mw = require('../middleware.js'),
|
2017-03-13 23:52:54 -06:00
|
|
|
secrets = require('../secrets.js'),
|
2016-05-31 21:54:21 -06:00
|
|
|
User = require('../models/user.js');
|
|
|
|
|
|
|
|
// Show map
|
|
|
|
router.get('/:slug?', function(req,res,next){
|
|
|
|
var mapuser='', user='', cbc=0;
|
|
|
|
|
|
|
|
// Confirm sucessful queries
|
|
|
|
function checkQuery(err,found) {
|
|
|
|
if (err){ mw.throwErr(req,err); }
|
2016-09-08 05:26:12 -06:00
|
|
|
if (found){ return found; }
|
2016-05-31 21:54:21 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Call renderMap() on completion
|
|
|
|
function checkCBC() {
|
|
|
|
cbc+=1;
|
|
|
|
if (cbc>1){ renderMap(); }
|
|
|
|
}
|
|
|
|
|
|
|
|
// QUERIES
|
|
|
|
// Get logged in user -> user
|
|
|
|
if (req.isAuthenticated()) {
|
|
|
|
User.findById(req.session.passport.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
|
2016-09-08 05:26:12 -06:00
|
|
|
if (!mapuser && !user) {
|
2016-05-31 21:54:21 -06:00
|
|
|
res.redirect('/');
|
|
|
|
} else {
|
2016-09-09 21:55:53 -06:00
|
|
|
if (user && !mapuser) { mapuser = user; }
|
2016-05-31 21:54:21 -06:00
|
|
|
res.render('map.html', {
|
|
|
|
mapuser: mapuser,
|
2017-03-17 08:41:38 -06:00
|
|
|
mapApi: secrets.mapAPI,
|
2016-05-31 21:54:21 -06:00
|
|
|
user: 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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2017-03-17 08:41:38 -06:00
|
|
|
module.exports = router;
|