Last active
December 11, 2015 21:28
-
-
Save eventomer/4662627 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Parse.Cloud.define("NearByAds", nearByAds); | |
Parse.Cloud.define("myAds", myAds); | |
var locationUtil = require('cloud/latlon.js'); | |
/* | |
* limit is kInitialObjectsPerPageNum(100) | |
* oredered by descending adDate and createdAt | |
* kSpecialPropertyKeyApproved(approved) is YES | |
* kSpecialPropertyKeyDeleted(deleted) is NO | |
* not showing ads with price under 100 | |
* | |
* params: | |
* kSpecialPropertyKeyUserID(userID) | |
* location_lat, location_long for kSpecialPropertyKeyLocation(location) | |
*/ | |
function nearByAds(request, response) | |
{ | |
var distance = 3; | |
nearByAdsHelper(request,response,distance); | |
} | |
function nearByAdsHelper(request,response,distance) | |
{ | |
var query = baseQuery(request,distance); | |
query.find({ | |
success:function(results){ | |
//console.log('\ndistance:'+distance+' results:'+results.length); | |
//distance backoff in case of less then 50 ads with fallback if less than 4 km | |
if(results.length < 200 && distance < 4){ | |
nearByAdsHelper(request,response,Math.pow(distance,1.15)); | |
return; | |
} | |
//'great finds' are given preferred sorting, they go to 1/4 of the sort. | |
//'LIKE' go to 1/2 of the sort order | |
for(var i = 0; i<results.length; i++){ | |
if(results[i].get('ribbon')=='GREAT_FIND'){ | |
results.insert(i/4,results.remove(i).pop()); | |
}else if(results[i].get('ribbon')=='LIKE'){ | |
results.insert(i/2,results.remove(i).pop()); | |
} | |
} | |
response.success(results.slice(0,100)); | |
}, | |
error: function(error) { | |
response.error(error); | |
} | |
}); | |
} | |
function baseQuery(request,distance) | |
{ | |
var mainQuery = new Parse.Query("Ad"); | |
mainQuery.limit = 200; | |
mainQuery.descending("createdAt"); | |
mainQuery.equalTo("approved",true); | |
mainQuery.equalTo("deleted",false); | |
mainQuery.notEqualTo("visible_in_feed",false); | |
mainQuery.greaterThanOrEqualTo("price",80); | |
mainQuery.notEqualTo("userID",request.params.userID); | |
if(request.params.location_lat!='-1' || request.params.location_long != '-1'){//only if valid location | |
var location = new locationUtil.LatLon(request.params.location_lat,request.params.location_long); | |
//gets a relative geopoint by 225 degrees and 4.1km distance | |
var southwest = location.destinationPoint(225,distance); | |
var northeast = location.destinationPoint(45,distance);; | |
mainQuery.withinGeoBox("location", | |
new Parse.GeoPoint(southwest.lat(), southwest.lon()), | |
new Parse.GeoPoint(northeast.lat(), northeast.lon())); | |
} | |
return mainQuery; | |
} | |
/* | |
* limit is kInitialObjectsPerPageNum(100) | |
* oredered by descending adDate and createdAt | |
* | |
* params: | |
* kSpecialPropertyKeyUserID(userID) | |
*/ | |
function myAds(request, response) | |
{ | |
var mainQuery = new Parse.Query("Ad"); | |
mainQuery.limit = 100; | |
mainQuery.descending("createdAt"); | |
mainQuery.equalTo("deleted",false); | |
mainQuery.equalTo("userID",request.params.userID); | |
mainQuery.find({ | |
success:function(results){ | |
response.success(results); | |
}, | |
error: function(error) { | |
response.error(error); | |
} | |
}); | |
} | |
Array.prototype.insert = function (index, item) { | |
this.splice(index, 0, item); | |
}; | |
Array.prototype.remove = function (index) { | |
return this.splice(index, 1); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment