diff --git a/packages/util-geo/geo.jsdoc b/packages/util-geo/geo.jsdoc index 0c33db7..9e77202 100644 --- a/packages/util-geo/geo.jsdoc +++ b/packages/util-geo/geo.jsdoc @@ -31,7 +31,7 @@ Geo = {} Geo.geocode = function(address, cb) { var url = "http://api.tiles.mapbox.com/v4/geocode/mapbox.places-v1/"+address+".json"; var options = {params: { format: "json", access_token: Settings['mapbox'].token }}; - + var response = function(res) { var data = res && res.content && JSON.parse(res.content); var feature = data && data.features && data.features[0]; @@ -39,7 +39,7 @@ Geo.geocode = function(address, cb) { if (center) return { lat: center[1], lng: center[0] }; } - + if (Meteor.isServer) return response(HTTP.get(url, options)); else @@ -73,13 +73,13 @@ Geo.getDistanceFromLatLonObj = function(latlong1, latlong2) { Geo.getDistanceFromLatLon = function(lat1,lon1,lat2,lon2) { var R = 6371; // Radius of the earth in km var dLat = Geo.deg2rad(lat2-lat1); // deg2rad below - var dLon = Geo.deg2rad(lon2-lon1); - var a = + var dLon = Geo.deg2rad(lon2-lon1); + var a = Math.sin(dLat/2) * Math.sin(dLat/2) + - Math.cos(Geo.deg2rad(lat1)) * Math.cos(Geo.deg2rad(lat2)) * + Math.cos(Geo.deg2rad(lat1)) * Math.cos(Geo.deg2rad(lat2)) * Math.sin(dLon/2) * Math.sin(dLon/2) - ; - var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); + ; + var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); var d = R * c; // Distance in km return d; // km } @@ -104,10 +104,13 @@ Geo.deg2rad = function(deg) { * ``` */ Geo.requestLocationForIp = function(ip) { - if (ip === "127.0.0.1") + if (ip === "127.0.0.1") ip = ""; // if on localhost, let telize determine my ip. try { - var data = HTTP.get("http://www.telize.com/geoip/" + ip, {timeout: 2000}).data; + var url = "http://www.telize.com/geoip/" + ip; // service discontinued since 15th november 2015 + url = "http://freegeoip.net/json/" + ip; + + var data = HTTP.get(url, {timeout: 2000}).data; return _.pick(data, 'longitude', 'latitude'); } catch(err) { console.log("request client-IP error:", err); diff --git a/server/Router.js b/server/Router.js index 366fb85..b8e895e 100644 --- a/server/Router.js +++ b/server/Router.js @@ -8,9 +8,9 @@ Router.route('/mandrill-webhook', function() { var events = EJSON.parse(Object.property(this.request.body, 'mandrill_events') || "[]"); var addEvent = function(event) { - // event format: http://help.mandrill.com/entries/24466132-Webhook-Format - - if (!event.event) + // event format: http://help.mandrill.com/entries/24466132-Webhook-Format + + if (!event.event) return; /* not a message event */ console.log('Email webhook event:', event.event); @@ -22,7 +22,7 @@ Router.route('/mandrill-webhook', function() { client: event.user_agent_parsed.ua_family, version: event.user_agent_parsed.ua_version, }; - + // link event to original message EmailsOutbound.update({'to.messageId': messageId}, { $push: { 'to.$.events': { @@ -49,26 +49,26 @@ Router.route('/mandrill-webhook', function() { Router.route('/:catchAll?', function () { - var url = getRequestUrl(this.request); + var url = getRequestUrl(this.request); var city = Url.city(url); var isLocalhost = Url.isLocalhost(url); - + // check if there is a valid city present in the url if (!city || !City.lookup(city)) { // this subdomain doesn't exist or isn't a valid city - + // we try to find the closest city var userIp = getClientIp(this.request); var location = Geo.requestLocationForIp(userIp); - var closestCity = Geo.findClosestCity(location); - + var closestCity = Geo.findClosestCity(location); + if (closestCity) { // if closest city is found we redirect the user to a new url var cityUrl = Url.replaceCity(closestCity, url); return redirect(cityUrl, this.response); - + } else if (city !== 'www') { // If closest city can't be found, we redirect to www. @@ -77,7 +77,7 @@ Router.route('/:catchAll?', function () { return redirect(cityUrl, this.response); } } - + // done! this.next();